2017-03-02 24 views
1

我想要解决的任务很简单:加载一个纹理化的OBJ文件并显示它,使其占用最大量的图像空间。只要照相机向下看z轴的负方向而y轴是向上向量,就不需要旋转网格。我正在使用pyglet来做到这一点。为了将相机设置到所需的位置,我正在做以下操作(代码见下面):计算网格的边界球体,网格的边界球体由网格中心定义,半径为离最远的点中心。然后我按照例如的说明来计算平截头体here,并据此设置正交投影。然后,我使用gluLookAt来更新modelviewmatrix,如下所示:摄像机位于正z轴上,朝向网格中心,y轴是向上矢量。为什么我的渲染图像没有显示我期望看到使用gluLookAt的视图?

问题是我的渲染图像看起来并不像我期望的那样,例如,网格的中心不在图像的中心,如下图所示。图像显示边界框和源自计算的网格中心的坐标轴(红色:x轴,绿色:y轴,蓝色:z轴)。

enter image description here

来设置相机的代码是:

# get the bounding sphere 
    center, radius = self._mesh.compute_bounding_sphere() 
    diam = radius*2.0 

    print 'center:' 
    print center 
    print 'radius: %f' % radius 

    # set up near and far clipping plane 
    z_near = 1.0 
    z_far = z_near + diam 

    # construct params for orthographic projection matrix 
    left = center[0] - radius 
    right = center[0] + radius 
    bottom = center[1] - radius 
    top = center[1] + radius 

    # if we are not rendering a square image, must correct for aspect ratio 
    aspect_ratio = float(self.width)/float(self.height) 
    if aspect_ratio < 1.0: 
     bottom /= aspect_ratio 
     top /= aspect_ratio 
    else: 
     left *= aspect_ratio 
     right *= aspect_ratio 

    print 'znear %f, zfar %f' % (z_near, z_far) 
    print 'left %f, right %f' % (left, right) 
    print 'bottom %f, top %f' % (bottom, top) 

    # specify a parallel projection with clipping planes as computed above 
    glMatrixMode(GL_PROJECTION) 
    glLoadIdentity() 
    glOrtho(left, right, bottom, top, z_near, z_far) 
    # gluPerspective(50.0, aspect_ratio, z_near, z_far) 

    # construct a viewing transform as follows: we look at the center of the mesh, the eye is located on the 
    # positive z-axis, and the 3D y-axis is the up-vector, i.e. it will be mapped to the y-axis in image space. 
    eye = center[2] + radius 
    print 'eye:' 
    print eye 
    glMatrixMode(GL_MODELVIEW) 
    glLoadIdentity() 
    gluLookAt(0.0, 0.0, eye, center[0], center[1], center[2], 0.0, 1.0, 0.0) 

它打印

center: 
[ 8.51203675 -1.95199815 0.35396978] 
radius: 10.462382 
znear 1.000000, zfar 21.924764 
left -1.950345, right 18.974419 
bottom -12.414380, top 8.510384 
eye: 
10.8163515441 

你能不能帮我鉴定一下我做错了吗?

回答

1

只要照相机向下看z轴的负方向而y轴是向上向量,就不需要旋转网格。

您的相机不是看不起的z轴:

gluLookAt(0.0, 0.0, eye, center[0], center[1], center[2], 0.0, 1.0, 0.0) 

这将引入一个旋转,从而在世界空间中的摄像机方向将是(center[0], center[1], center[2] - eye) = (center[0], center[1], -radius)

由于您已经根据您的对象移动了视图体积,因此lookAt点将是而不是是将出现在屏幕中心的点。你真正需要做的沿-z只是看什么,你只需要在摄像头一起+z翻译:

gluLookAt(0.0, 0.0, eye, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) 

需要注意的是概念,你不需要一个视图矩阵,在所有你的使用情况。你可以简化包围你的对象的坐标轴边界框,并可以直接使用它作为你的视图体的参数(就像你使用xy,但出于某些原因,而不是z)。唯一的技巧就是你需要取消nearfar的值,因为设计的方式是glOrtho

相关问题