您的位置:首页 > 运维架构

关于OPenGL中gluLookAt()与glOrthO()的坐标位置关系

2017-01-16 17:29 495 查看
用glOrthO()定义一个正交投影变换,gluLookAt()的最后两个参数是指眼睛离视景体的最小最大位置。要使gluLookAt()定义的观察点能够看到图形,就要把它与glOrtho()的near与far联系起来。

这是我以前发的一个帖,答得比较详细,我将它帖出来,供大家学习参考

void COpenGLDemoView::GLReSize(int cx, int cy)

{

double nRange=400.0;

if(cy==0)

cy=1;

glViewport(0,0,cx,cy);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

//define 视景体(left,right,top,near,far)

if (cx<=cy)

{

   glOrtho(-nRange,nRange,-nRange*cy/cx,nRange*cy/cx,-nRange,nRange);

}

else

{

   glOrtho(-nRange*cx/cy,nRange*cx/cy,-nRange,nRange,-nRange,nRange);

}

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

//Set the 视点和方向

double eye[]={0,0,3*nRange};//观察点位置,以Z方向代表物体的远近

double ref[]={0,0,0};//指定参考点的位置

double up_dir[]={0,1,0};//代表物体的朝向的一个矢量。OpenGL是通过z方向的值来表明物体的远近的,

//一般来说,物体的朝向就是y方向了。 

gluLookAt(eye[0],eye[1],eye[2],ref[0],ref[1],ref[2],up_dir[0],up_dir[1],up_dir[2]);

}

物体大小(-80<X<80,-325<Y<-70,180<Z<370)

物体的中心大概是(0,-200,275)

如果把double ref[]={0,0,0};//指定参考点的位置

改为double ref[]={0,-200,275};还是看不到图形,

我不是用了gluLookAt定义在Z=3X400=1200的地方看一个XYZ坐标范围都在(-400,400)的一个视景体中所有的图形吗,图行又在这个视景体中,怎么看不到呢?这个又是正投影变换,在那里看都不改变物体的大小的啊!



//Set the 视点和方向

double eye[]={0,0,3*nRange};//观察点位置,以Z方向代表物体的远近

double ref[]={0,0,0};//指定参考点的位置

double up_dir[]={0,1,0};//代表物体的朝向的一个矢量。OpenGL是通过z方向的值来表明物体的远近的,

//一般来说,物体的朝向就是y方向了。 

gluLookAt(eye[0],eye[1],eye[2],ref[0],ref[1],ref[2],up_dir[0],up_dir[1],up_dir[2]);

注释掉就可以看到图形,这倒底是怎么回事啊?

如果把gluLookAt()注释掉后,我上面定义的 glOrtho(-nRange,nRange,-nRange*cy/cx,nRange*cy/cx,-nRange,nRange);视点是在原点还是在z=400(nRange)处啊?因为我没有改变坐标系啊,书上说没有变换就在原点。

这是关于glOrtho的说明

The command glOrtho() creates an orthographic parallel viewing volume. As with glFrustum(), you specify the corners of the near clipping plane and the distance to the far clipping plane. void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, 

GLdouble top, GLdouble near, GLdouble far); 

Creates a matrix for an orthographic parallel viewing volume and multiplies the current matrix by it. The near clipping plane is a rectangle with the lower left corner at (left, bottom, -near) and the upper right corner at (right, top, -near). The far clipping plane is a rectangle with corners at (left, bottom, -far) and (right, top, -far). Both near and far can be positive or negative. 



glOrtho(left,right,bottom,top,zNear,zFar)的最后两项是指和你眼睛的距离,你现在既然眼睛在(0,0,1200)处,zNear = -nRange, zFar = nRange,nRange=400,意思就是说你的视景体现在大概是(-400,400,-400,400,1600,800)这样你的物体是绘制在Z方向上<800的地方,所以可能看不到

现在最佳的near与far应该为

glOrtho(-nRange*cx/cy,nRange*cx/cy,-nRange,nRange,800,1300);

这样Z的范围就在(1200-1300)=-100<Z<(1200-800)=400。(我还画了三个长为100坐标轴,要多留一点旋转的空间)这样也相对的确定了视景体在Z方向的范围。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: