调整视口大小,裁剪场景

发布于 2024-11-05 02:03:14 字数 624 浏览 6 评论 0原文

我有一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡绿茶香 2024-11-12 02:03:14

调整视场参数。从技术上讲,如果使用 glFrustum 而不是 gluPerspective 来完成,你想要做的事情会更容易。

// Protect against a divide by zero
if ( height == 0 )
    height = 1;

// Setup our viewport.
glViewport( 0, 0, ( GLint )width, ( GLint )height );

// change to the projection matrix and set our viewing volume.
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

// supply some sensefull value for this; ideally let the user adjust it somehow
exterm float Zoom;

// near far should tightly wrap the actually visible set of objects. Hardcoded values
// like 0.1 ... 1000.f are problematic. Also your choosen value range slices your viewport
// into 10000 depth slices. Say you get only a 16 bit depth buffer already in the lineary
// slicing ortho projection a OpenGL length units in depth would recieve only about 6 
// slices. In perspective mode the slice density follows a 1/depth law. So already at depth
// 10 you'll run into depth resolution problems.
glFrustum(-Zoom * width, Zoom * width, -Zoom * height, Zoom * height, near, far);

// Switch back to the modelview
glMatrixMode( GL_MODELVIEW );

请注意,此代码属于显示函数。任何在窗口重塑处理程序中设置视口和投影的教程都是非常糟糕的风格;不要遵循它。

Adjust the fov parameters. Technically what you want to do is easier if done using glFrustum instead of gluPerspective.

// Protect against a divide by zero
if ( height == 0 )
    height = 1;

// Setup our viewport.
glViewport( 0, 0, ( GLint )width, ( GLint )height );

// change to the projection matrix and set our viewing volume.
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

// supply some sensefull value for this; ideally let the user adjust it somehow
exterm float Zoom;

// near far should tightly wrap the actually visible set of objects. Hardcoded values
// like 0.1 ... 1000.f are problematic. Also your choosen value range slices your viewport
// into 10000 depth slices. Say you get only a 16 bit depth buffer already in the lineary
// slicing ortho projection a OpenGL length units in depth would recieve only about 6 
// slices. In perspective mode the slice density follows a 1/depth law. So already at depth
// 10 you'll run into depth resolution problems.
glFrustum(-Zoom * width, Zoom * width, -Zoom * height, Zoom * height, near, far);

// Switch back to the modelview
glMatrixMode( GL_MODELVIEW );

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.

清风夜微凉 2024-11-12 02:03:14

理想情况下,由于您已将视口设置为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文