gpt4 book ai didi

opengl - 在窗口内安装 3d 模型

转载 作者:行者123 更新时间:2023-12-04 17:40:53 26 4
gpt4 key购买 nike

我想显示适合 View 的不同尺寸的模型,以便整个模型在屏幕内可见。
最好的方法是什么?
我尝试使用此公式缩放(使用 glScale)模型

scaleFactor = ( screenSize / (maxModelSize * constant) )

其中 size 是高度或宽度,取决于哪个更大。
常数是 1 / (length of one screen pixel in OpenGL units)这有两个问题:
1. 做了一些转换之后,我希望能够使用 Identity 返回到这个初始比例(模型被缩放以适应窗口)。当前调用 identity 将使模型恢复到其原始尺寸(在“固定”比例之前)。
2.“常数”是我通过反复试验得到的东西,我觉得方法不对。我也怀疑它根本不是常数,取决于屏幕分辨率,天知道还有什么。

最佳答案

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);

关于opengl - 在窗口内安装 3d 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4180289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com