2017-03-02 10 views
1

解決したい課題は簡単です:テクスチャリングされたOBJファイルをロードし、最大限のイメージスペースを占めるように表示します。カメラが負のz軸を見下ろし、y軸が上のベクトルである限り、メッシュを回転させる必要はありません。私はそれを行うためにpygletを使用しています。gluLookAtを使用して表示されると思われるビューがレンダリングされたイメージに表示されないのはなぜですか?

カメラを希望する位置に設定するには、以下のコードを実行します(コード参照)。メッシュの中心で定義されるメッシュの境界球と、最も離れた点までの半径センター。次に、例えば、説明したような錐台を計算する。 hereとし、それに応じて正射影を設定します。次に、私はgluLookAtを使ってmodelviewmatrixを次のように更新します:カメラは正のz軸上にあり、メッシュの中心に向かって見え、y軸は上のベクトルです。

問題は、私のレンダリングされた画像が、私がそうであると思うように全く見えないことです。次の図のように、メッシュの中心が画像の中心にはありません。画像には、境界ボックスと、メッシュの計算中心から始まる座標軸(赤:x軸、緑:y軸、青:z軸)が表示されます。

enter image description here

カメラを設定するためのコードは次のとおりです。

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 

を印刷し

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

は、あなたは私が私が間違っているの何識別するのに役立つことはできますか?

答えて

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であるためです。

関連する問題