将3D模型适应窗口大小

5

我想展示不同大小的模型适合于一个视图中,使整个模型在屏幕内可见。
最好的方法是什么? 我尝试使用这个公式来缩放(使用glScale)模型

scaleFactor = ( screenSize / (maxModelSize * constant) )

当size是高度或宽度时,取决于哪个更大。
常数是1 /(在OpenGL单位中的一个屏幕像素长度)
这里有两个问题:
1. 做一些变换后,我想能够使用Identity返回到这个初始比例(模型被缩放以适合窗口)。目前调用identity将使该模型恢复到其原始尺寸(在“修复”比例之前)。
2. “常数”是我通过试错得到的,我觉得这是错误的方法。 我还怀疑它根本不是一个常数,而是取决于屏幕分辨率和神知道什么。


经过一些变换后,我希望能够使用Identity返回到初始比例(模型被缩放以适应窗口),但目前调用identity会将模型带回到“修复”比例之前的原始尺寸。这对我来说意味着您正在缩放ModelView矩阵。也许您应该尝试缩放投影矩阵?换句话说,制作一个更大的相机,而不是一个更小的模型。 - Zecc
1个回答

9

Section 8.070:

The following is from a posting by Dave Shreiner on setting up a basic viewing system:

First, compute a bounding sphere for all objects in your scene. This should provide you with two bits of information: the center of the sphere (let ( c.x, c.y, c.z ) be that point) and its diameter (call it "diam").

Next, choose a value for the zNear clipping plane. General guidelines are to choose something larger than, but close to 1.0. So, let's say you set

zNear = 1.0; zFar = zNear + diam; 

Structure your matrix calls in this order (for an Orthographic projection):

GLdouble left = c.x - diam; 
GLdouble right = c.x + diam;
GLdouble bottom c.y - diam; 
GLdouble top = c.y + diam; 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
glOrtho(left, right, bottom, top, zNear, zFar); 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

This approach should center your objects in the middle of the window and stretch them to fit (i.e., its assuming that you're using a window with aspect ratio = 1.0). If your window isn't square, compute left, right, bottom, and top, as above, and put in the following logic before the call to glOrtho():

GLdouble aspect = (GLdouble) windowWidth / windowHeight; 
if ( aspect < 1.0 ) { 
    // window taller than wide 
    bottom /= aspect; 
    top /= aspect; 
} else { 
    left *= aspect; 
    right *= aspect;
} 

The above code should position the objects in your scene appropriately. If you intend to manipulate (i.e. rotate, etc.), you need to add a viewing transform to it.

A typical viewing transform will go on the ModelView matrix and might look like this:

GluLookAt (0., 0., 2.*diam, c.x, c.y, c.z, 0.0, 1.0, 0.0);

投影是透视的情况怎么办? - elect

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接