New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gl21-cube: Cube in demo appears squashed. #65

Open
nf opened this Issue Oct 11, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@nf

nf commented Oct 11, 2017

The cube in the demo is squashed. It looks like an aspect ratio thing. I can't see anything in the source that would cause this. Any ideas?

@nf

This comment has been minimized.

Show comment
Hide comment
@nf

nf Oct 11, 2017

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.

nf commented Oct 11, 2017

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.

@errcw

This comment has been minimized.

Show comment
Hide comment
@errcw

errcw Oct 11, 2017

Member

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

Member

errcw commented Oct 11, 2017

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

@dmitshur

This comment has been minimized.

Show comment
Hide comment
@dmitshur

dmitshur Oct 11, 2017

Member

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.

Member

dmitshur commented Oct 11, 2017

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.

@nf

This comment has been minimized.

Show comment
Hide comment
@nf

nf Oct 11, 2017

@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.

nf commented Oct 11, 2017

@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.

@dmitshur

This comment has been minimized.

Show comment
Hide comment
@dmitshur

dmitshur Oct 11, 2017

Member

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)

Member

dmitshur commented Oct 11, 2017

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)

@dmitshur dmitshur changed the title from Cube in demo appears squashed to gl21-cube: Cube in demo appears squashed. Oct 11, 2017

@dmitshur

This comment has been minimized.

Show comment
Hide comment
@dmitshur

dmitshur Oct 11, 2017

Member

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.)

Member

dmitshur commented Oct 11, 2017

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment