调整视口大小,裁剪场景
我有一个由 OpenGL 绘制到可调整大小的窗口的 3D 场景。现在,当调整窗口大小时,我不想缩放视口,而是想将场景保持在固定大小并显示其中的较大部分(或裁剪场景图像)。这是我当前的代码:
GLfloat ratio;
// Protect against a divide by zero
if ( height == 0 )
height = 1;
ratio = ( GLfloat )width / ( GLfloat )height;
// Setup our viewport.
glViewport( 0, 0, ( GLint )width, ( GLint )height );
// change to the projection matrix and set our viewing volume.
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective( 60.0f, ratio, 0.1f, 1000.0f );
// Switch back to the modelview
glMatrixMode( GL_MODELVIEW );
如果我保持比例固定,那么场景图像只会缩放,但我想将其保持在固定大小并简单地显示更宽的视图。对此有什么想法吗?
I have a 3d scene drawn by OpenGL to a resizeable window. Now, when the window gets resized, I do not want to scale the viewport, rather I want to keep the scene at a fixed size and show a larger portion of it (or crop the scene image). This is my current code:
GLfloat ratio;
// Protect against a divide by zero
if ( height == 0 )
height = 1;
ratio = ( GLfloat )width / ( GLfloat )height;
// Setup our viewport.
glViewport( 0, 0, ( GLint )width, ( GLint )height );
// change to the projection matrix and set our viewing volume.
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective( 60.0f, ratio, 0.1f, 1000.0f );
// Switch back to the modelview
glMatrixMode( GL_MODELVIEW );
If I keep the ratio fixed, then the scene image simply gets scaled, but I want to keep it at fixed size and simply show a wider view. Any ideas on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调整视场参数。从技术上讲,如果使用 glFrustum 而不是 gluPerspective 来完成,你想要做的事情会更容易。
请注意,此代码属于显示函数。任何在窗口重塑处理程序中设置视口和投影的教程都是非常糟糕的风格;不要遵循它。
Adjust the fov parameters. Technically what you want to do is easier if done using glFrustum instead of gluPerspective.
Take note that this code belongs into the display function. Any tutorial that sets viewport and projection in a window reshape handler is very bad style; don't follow it.
理想情况下,由于您已将视口设置为 0,0,宽度,高度,因此图像应保持相同的大小。您能检查一下您发送的图像坐标吗?它是保持不变还是随着宽度/高度缩放?您可以发布添加顶点的代码吗?
Ideally, since you have set the viewport to 0,0,width,height, the image should remain in the same size. Can you check the coordinates you send for the image. Does that remain constant or does it scale along with width/ height? Can you post the code for adding vertices.