Min*_*wth 3 opengl aspect-ratio
我在全屏模式下遇到了一些问题。我可以将窗口设置为 800x600,但是当我全屏显示该分辨率时,它会拉伸。我认为这是因为纵横比的变化。我怎样才能解决这个问题?
编辑 #1
这是我所看到的情况的屏幕截图。
左:800x600
右:1366x768
编辑 #2
每次我重新调整窗口大小 (WM_SIZE) 时都会调用我的 initGraphics 函数。
void initGraphics(int width, int height) {
float aspect = (float)width / (float)height;
glViewport(0, 0, width, height);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND); //Enable alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, width, height * aspect, 0.0);
glMatrixMode(GL_MODELVIEW);
}
Run Code Online (Sandbox Code Playgroud)
解决方案: 真正的问题最终是您滥用了 gluOrtho2D 函数。而不是使用这个:
gluOrtho2D(0.0, width, height * aspect, 0.0);
Run Code Online (Sandbox Code Playgroud)
您需要将其切换为正确的形式:
gluOrtho2D(0.0, width, 0.0, height);
Run Code Online (Sandbox Code Playgroud)
后者创建一个 2D 正交投影,填充视口的整个宽度和高度,因此不会发生拉伸。
原始答案:
您需要修改投影以考虑新的纵横比。
确保您首先设置glViewport为新的窗口大小。设置视口后,您需要通过调用将矩阵模式切换到投影,glMatrixMode然后最终使用 计算新的纵横比width / height并将新的纵横比传递给gluPerspective。您也可以使用直接glFrustum代替gluPerspective您可以找到源gluPerspective来实现与glFrustum.
像这样的东西:
float aspectRatio = width / height;
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective(fov, aspectRatio, near, far);
Run Code Online (Sandbox Code Playgroud)