Comments (6)
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.
I'd be happy to accept a PR with a fix.
from example.
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.
@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.
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:
Line 65 in ee0644b
from example.
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)
- Improve unfriendly requirement that examples must be run in their package directory. HOT 2
- Go 1.6 -msan option error (memory sanitizer) HOT 6
- build error on OSX, go1.6 HOT 2
- [Question] Unable to run examples, only background windows showing in GL window HOT 4
- multiple contexts HOT 6
- Proposal: Rename repository to "example". HOT 6
- Specify permissive license. HOT 5
- Examples are broken HOT 1
- More information in examples HOT 2
- Odd choppiness to animation? HOT 8
- frame rate drops proportionally to number of process instances HOT 5
- Handle leak on Windows at SetSize? HOT 4
- fatal error: checkptr: unsafe pointer arithmetic on Go 1.14rc1 HOT 1
- Re: github.com/go-gl/example/gl41core-cube/cube.go HOT 1
- Can I render a webpage at the window HOT 1
- c:\go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1 HOT 1
- gl.GenTextures
- Error when binding in loop HOT 1
- Cel Shader Example HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from example.