调整视口大小,裁剪场景 [英] Resize viewport, crop scene

查看:107
本文介绍了调整视口大小,裁剪场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由OpenGL绘制到可调整大小窗口的3d场景.现在,当调整窗口大小时,我不想缩放视口,而是希望将场景保持在固定大小并显示更大的一部分(或裁剪场景图像).这是我当前的代码:

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?

推荐答案

调整fov参数.从技术上讲,如果使用 glFrustum 而不是 gluPerspective ,则您想做的事情会更容易.

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.

这篇关于调整视口大小,裁剪场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆