Git Product home page Git Product logo

Comments (6)

nf avatar nf commented on August 22, 2024

After some digging the fix is kind of obvious:

diff --git a/gl21-cube/cube.go b/gl21-cube/cube.go
index 5a84ebc..b26b8f2 100644
--- a/gl21-cube/cube.go
+++ b/gl21-cube/cube.go
@@ -29,6 +29,8 @@ func init() {
        runtime.LockOSThread()
 }
 
+const width, height = 1280, 720
+
 func main() {
        if err := glfw.Init(); err != nil {
                log.Fatalln("failed to initialize glfw:", err)
@@ -38,7 +40,8 @@ func main() {
        glfw.WindowHint(glfw.Resizable, glfw.False)
        glfw.WindowHint(glfw.ContextVersionMajor, 2)
        glfw.WindowHint(glfw.ContextVersionMinor, 1)
-       window, err := glfw.CreateWindow(800, 600, "Cube", nil, nil)
+       monitor := glfw.GetPrimaryMonitor()
+       window, err := glfw.CreateWindow(width, height, "Cube", monitor, nil)
        if err != nil {
                panic(err)
        }
@@ -115,7 +118,9 @@ func setupScene() {
 
        gl.MatrixMode(gl.PROJECTION)
        gl.LoadIdentity()
-       gl.Frustum(-1, 1, -1, 1, 1.0, 10.0)
+       // Correct the projection for the monitor's aspect ratio.
+       f := ((float64(width) / height) - 1) / 2
+       gl.Frustum(-1-f, 1+f, -1, 1, 1.0, 10.0)
        gl.MatrixMode(gl.MODELVIEW)
        gl.LoadIdentity()
 }

I'm not sure that it's worth including this in the example program.

from example.

errcw avatar errcw commented on August 22, 2024

I'd be happy to accept a PR with a fix.

from example.

dmitshur avatar dmitshur commented on August 22, 2024

Thanks for reporting this @nf.

The window is not resizable, right? In that case, the aspect ratio can be hardcoded, no need to get the monitor (which might have a different aspect ratio). For a 800x600 window, it should be 4:3.

from example.

nf avatar nf commented on August 22, 2024

@errcw I'll give it a go.

@shurcooL I get the monitor to make it full screen; that's not a critical part of this fix.

from example.

dmitshur avatar dmitshur commented on August 22, 2024

Alternatively, to keep the example simpler, we can just change the window size to be square, since that's easier to compute a perspective projection matrix for (with the OpenGL 2.1 API).

Note that the gl41core-cube example doesn't have this problem, it calculates a perspective projection matrix accurately:

projection := mgl32.Perspective(mgl32.DegToRad(45.0), float32(windowWidth)/windowHeight, 0.1, 10.0)

from example.

dmitshur avatar dmitshur commented on August 22, 2024

For reference, the way I create a perspective projection matrix in OpenGL 2.1 projects is usually something like this, but it needs github.com/go-gl/mathgl/mgl64 package to be imported:

gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
var projectionMatrix [16]float64
perspMatrix := mgl64.Perspective(mgl64.DegToRad(45), float64(windowSize[0])/float64(windowSize[1]), 1.0, 1500.0)
for i := 0; i < 16; i++ {
	projectionMatrix[i] = float64(perspMatrix[i])
}
gl.MultMatrixd(&projectionMatrix[0])

(The code is from an older project, it can be cleaned up to improve style.)

from example.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.