在窗口内安装 3d 模型
我想在一个视图中显示不同尺寸的模型,以便整个模型在屏幕内可见.
最好的方法是什么?我尝试使用此公式缩放(使用 glScale)模型
I want to display models of different sizes fitted into a view, so that the whole model is visible inside the screen.
What is the best way to do it?
I tried scaling (using glScale) the model using this formula
scaleFactor = ( screenSize / (maxModelSize * constant) )
size 是高度还是宽度,取决于哪个更大.
常量是 1/(OpenGL 单位中一个屏幕像素的长度)
这有两个问题:
1. 进行一些转换后,我希望能够使用 Identity 返回到这个初始比例(模型被缩放以适合窗口).当前调用 identity 将使模型恢复其原始尺寸(在固定"比例之前).
2. 常数"是我通过反复试验得到的,我觉得方法不对.我也怀疑它根本不是一个常数,取决于屏幕分辨率,天知道还有什么.
Where size is height or width, depending on what is bigger.
Constant is 1 / (length of one screen pixel in OpenGL units)
There are two problems with this:
1. After doing some transformations, I want to be able to return to this initial scale (model is scaled to fit window) using Identity. Currently calling identity will bring the model to its original dimensions (before the "fixing" scale).
2. The "constant" is something I got by trial and error, I feels wrong method to me. I also suspect that it is not a constant at all, and depends on screen resolution and god knows what else.
推荐答案
第 8.070 节:
以下内容来自于Dave Shreiner 关于建立一个基本的查看系统:
The following is from a posting by Dave Shreiner on setting up a basic viewing system:
首先,计算一个边界球场景中的所有对象.这应该为您提供两点信息:球心(让 ( c.x, c.y, c.z ) 成为那个点)及其直径(称为直径").
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").
接下来,为 zNear 选择一个值剪裁平面.一般准则是选择大于,但是接近 1.0.所以,假设你设置了
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();
这种方法应该以您的窗口中间的对象并拉伸它们以适应(即,它的假设您正在使用窗口纵横比 = 1.0).如果你的窗口不是方形的,计算左边,右侧、底部和顶部,如上,以及把下面的逻辑放在前面调用 glOrtho():
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.
将进行典型的观看转换ModelView 矩阵,可能看起来像这样:
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);
相关文章