2016-05-12 110 views
0

我有一個問題,我能夠在透視投影中看到所需的對象,但即使攝像機位於相同的座標中,也不能看到正交投影中的對象,並且看到相同的座標座標。Glut Ortho2D投影不顯示

我知道對象呈現正確,因爲它在像這樣的立體視圖顯示正確:

Perspective projection

的平面位於與10高度的起源,寬度50,沒有深度。它的位置在(0,-10,0)

我希望能夠在正交投影中查看它。 我把它的方式是這樣的,我CameraManager類:

void CameraManager::UpdateCamera() { 

    // exit in erroneous situations 
    if (m_screenWidth == 0 && m_screenHeight == 0) 
     return; 

    switch (m_projectionType) 
    { 
    case TWO_DIMENSIONAL: 
     SetupOrthographicCamera(); 

     break; 
    case THREE_DIMENSIONAL: 
     SetupPerspectiveCamera(); 

     break; 
    default: 
     break; 
    } 

    SetupModelView(); 

} 
我SetupOrthographicCamera

然後()我這樣做:

void CameraManager::SetupOrthographicCamera() { 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    //float aspectRatio = m_screenWidth/(float)m_screenHeight; 
    gluOrtho2D(-50, 50, -100, 100); 

    /* 
    m_cameraPosition = btVector3(0, 0, -1); 

    glMatrixMode(GL_MODELVIEW); 
    gluLookAt(m_cameraPosition[0], m_cameraPosition[1], m_cameraPosition[2], m_cameraTarget[0], m_cameraTarget[1], m_cameraTarget[2], m_upVector.getX(), m_upVector.getY(), m_upVector.getZ()); 
    */ 
} 

這是我的透視相機:

void CameraManager::SetupPerspectiveCamera() { 

    // select the projection matrix 
    glMatrixMode(GL_PROJECTION); 
    // set it to the matrix-equivalent of 1 
    glLoadIdentity(); 
    // determine the aspect ratio of the screen 
    float aspectRatio = m_screenWidth/(float)m_screenHeight; 
    // create a viewing frustum based on the aspect ratio and the 
    // boundaries of the camera 
    glFrustum(-aspectRatio * m_nearPlane, aspectRatio * m_nearPlane, -m_nearPlane, m_nearPlane, m_nearPlane, m_farPlane); 
    // the projection matrix is now set 
} 

這是我的SetupModelView():

void CameraManager::SetupModelView() { 

    // select the view matrix 
    glMatrixMode(GL_MODELVIEW); 
    // set it to '1' 
    glLoadIdentity(); 

    // our values represent the angles in degrees, but 3D 
    // math typically demands angular values are in radians. 
    float pitch = m_cameraPitch * RADIANS_PER_DEGREE; 
    float yaw = m_cameraYaw * RADIANS_PER_DEGREE; 

    // create a quaternion defining the angular rotation 
    // around the up vector 
    btQuaternion rotation(m_upVector, yaw); 

    // set the camera's position to 0,0,0, then move the 'z' 
    // position to the current value of m_cameraDistance. 
    btVector3 cameraPosition(0, 0, 0); 
    cameraPosition[2] = -m_cameraDistance; 

    // create a Bullet Vector3 to represent the camera 
    // position and scale it up if its value is too small. 
    btVector3 forward(cameraPosition[0], cameraPosition[1], cameraPosition[2]); 
    if (forward.length2() < SIMD_EPSILON) { 
     forward.setValue(1.f, 0.f, 0.f); 
    } 

    // figure out the 'right' vector by using the cross 
    // product on the 'forward' and 'up' vectors 
    btVector3 right = m_upVector.cross(forward); 

    // create a quaternion that represents the camera's roll 
    btQuaternion roll(right, -pitch); 

    // turn the rotation (around the Y-axis) and roll (around 
    // the forward axis) into transformation matrices and 
    // apply them to the camera position. This gives us the 
    // final position 
    cameraPosition = btMatrix3x3(rotation) * btMatrix3x3(roll) * cameraPosition; 

    // save our new position in the member variable, and 
    // shift it relative to the target position (so that we 
    // orbit it) 
    m_cameraPosition[0] = cameraPosition.getX(); 
    m_cameraPosition[1] = cameraPosition.getY(); 
    m_cameraPosition[2] = cameraPosition.getZ(); 
    m_cameraPosition += m_cameraTarget; 

    // create a view matrix based on the camera's position and where it's 
    // looking 
    //printf("Camera Position = %f, %f, %f\n", cameraPosition[0], cameraPosition[1], cameraPosition[2]); 
    // the view matrix is now set 
    gluLookAt(m_cameraPosition[0], m_cameraPosition[1], m_cameraPosition[2], m_cameraTarget[0], m_cameraTarget[1], m_cameraTarget[2], m_upVector.getX(), m_upVector.getY(), m_upVector.getZ()); 

} 

我不知道我錯過了什麼。

+0

請注意,對象在透視和拼寫效果圖中可能有非常不同的尺寸,除非您非常仔細地選擇邊界。它可能只是因爲物體離開了屏幕(因爲一切都很大),或者很小,所以你看不到它(因爲一切都很小)。我注意到你的相機不是直接指向物體,因爲屏幕的中心(在透視圖中)是藍色的而不是綠色的。 – immibis

回答

0

如果物體在正視圖中不呈現垂直,您可能看不到任何物體(因爲相機位於不同的z軸平面中)。另外,你是否從背面看背面剔除?那麼你永遠不會看到它。我會嘗試explicitly disabling this。我可以想到更多的理由...... Z軸是在OpenGL中反轉的(負值是從典型的歐幾里德視角w /尊重其他軸的屏幕,你可能太接近W /剪輯範圍提供,等等。對不起因爲含糊不清,只是你可能忽視的一些東西可能會對你有所幫助。)

+0

嗨,我相信這個對象是垂直於正視圖呈現的。我的相機看起來像是在物體後面,因爲我減去了距離並查看了原點。我也像這樣禁用了剔除:'glDisable(GL_CULL_FACE);'但我仍然看不到它。 = [ – terminix00

+0

您是否嘗試過顛倒相機z軸上的標誌以查看它是否突然出現?我已經很多次了,而且在我自己的座標系中,我已經變得「迷失方向」了。通常單個標誌翻轉可以解決問題。當然,不要隨意翻動標誌---你應該思考。我不禁注意到你的值非常小(例如'if語句中的'x + 1.0f')。嘗試繪製一個大的矩形並退出一個很大的距離。 – EntangledLoops

+0

好吧,現在我有一種情況,如果我在0,0,10處創建盒子平面當我將相機縮放到位置0,0,10時,盒子平面會佔據整個屏幕。當我在0,0,5或0,0,15兩個方向上再次邁出時,方塊平面消失。你知道那裏可能是什麼問題嗎? – terminix00