我正在一个方形窗口中绘制一个多边形.当我调整窗口大小时,例如通过全屏显示,纵横比会受到干扰.从参考文献中我发现了一种保留纵横比的方法.这是代码:
void reshape (int width, int height) {
float cx, halfWidth = width*0.5f;
float aspect = (float)width/(float)height;
glViewport (0, 0, (GLsizei) width, (GLsizei) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(cx-halfWidth*aspect, cx+halfWidth*aspect, bottom, top, zNear, zFar);
glMatrixMode (GL_MODELVIEW);
}
Run Code Online (Sandbox Code Playgroud)
在这里,cx是X中zNear平面的眼睛空间中心.我要求有人可以解释我怎么能计算出来.我相信这应该是glFrustum()的前两个参数的平均值.我对吗?任何帮助将不胜感激.
看起来你想要做的是在宽高比改变时保持视野或视角.请参阅标题为9.085的部分如何调用glFrustum()以匹配我对gluPerspective()的调用?有关如何操作的详细信息,请参阅OpenGL常见问题解答.这是简短的版本:
fov*0.5 = arctan ((top-bottom)*0.5 / near)
top = tan(fov*0.5) * near
bottom = -top
left = aspect * bottom
right = aspect * top
Run Code Online (Sandbox Code Playgroud)
请参阅链接了解详细信息.