2017-03-02 71 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

相關問題