适合窗口内的3d模型 [英] Fit 3d model inside a window

查看:109
本文介绍了适合窗口内的3d模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在视图中显示不同尺寸的模型,以便整个模型在屏幕内可见。

最好的方法是什么?
我试着用这个公式缩放(使用glScale)模型

$ $ $ $ $ $ $ $ $ $> scaleFactor =(screenSize /(maxModelSize * constant) )

其中尺寸是高度或宽度,取决于哪个更大。

常量是 1 /(OpenGL单元中一个屏幕像素的长度)

有两个问题:

1。完成一些转换后,我希望能够使用Identity返回到初始缩放比例(模型缩放到适合窗口)。目前的呼叫身份会将模型带到它的原始尺寸(在固定比例之前)。

2。常数是我通过试错得到的东西,我感觉错误的方法给我。我也怀疑它根本不是一个常量,而是取决于屏幕分辨率,上帝知道还有什么。 解决方案

href =http://www.opengl.org/resources/faq/technical/viewing.htm =noreferrer>第8.070节


以下内容来自于
发布的Dave Shreiner设置基本
观看系统:

场景中的所有对象计算一个边界球。这应该
为您提供两位
信息:球体的中心
(let(cx,cy,cz)是该点)
及其直径(称之为diam)。

接下来,为zNear
剪切平面选择一个值。一般准则是
选择大于,但
接近1.0。所以,假设你设置了

  zNear = 1.0; zFar = zNear + diam; 

在这个
的顺序中构造您的矩阵调用(对于正交
投影) :

  GLdouble left = cx  -  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()之前放入以下逻辑: :


  GLdouble aspect =(GLdouble)windowWidth / windowHeight; 
if(aspect< 1.0){
//窗口宽于
bottom / = aspect;
top / = aspect;
} else {
left * = aspect;
对* =纵横;
}

上面的代码应该适当地将
对象放置在场景中。
如果你打算操作(即
旋转等),你需要添加一个
查看转换。



一个典型的视图变换将继续在
ModelView矩阵上,并可能看起来像这样:

$ b

  GluLookAt( 0.,0.2,* diam,cx,cy,cz,0.0,1.0,0.0); 



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

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.

解决方案

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

这篇关于适合窗口内的3d模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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