【问题标题】:Glut Ortho2D Projection not showingGlut Ortho2D 投影未显示
【发布时间】:2016-09-08 23:52:17
【问题描述】:

我有一个问题,我能够在透视投影中看到所需的对象,而在正交投影中看不到对象,即使相机位于相同的坐标并查看相同的坐标。

我知道该对象已正确渲染,因为它在透视图中正确显示,如下所示:


平面位于原点,高度为 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());

}

我不确定我错过了什么。

【问题讨论】:

  • 请注意,在透视和正交渲染中,对象的大小可能非常不同,除非您非常仔细地选择了边界。可能只是对象不在屏幕上(因为一切都非常大)或者太小以至于看不到它(因为一切都非常小)。我注意到您的相机没有直接指向对象,因为屏幕中心(在透视渲染中)是蓝色而不是绿色。

标签: c++ opengl glut freeglut


【解决方案1】:

如果对象未在正交视图中垂直渲染,您可能什么也看不到(因为相机位于不同的 z 轴平面中)。另外,你是在背面剔除的情况下从后面看它吗?那么你将永远看不到它。我会尝试explicitly disabling this。我能想到更多的原因......z轴在OpenGL中是倒置的(负是进入从典型的欧几里得视角w/相对于其他轴的屏幕,你可能太靠近w/剪裁范围提供等。抱歉含糊不清,只是您可能忽略了一些可能对您有帮助的事情。)

【讨论】:

  • 嗨,所以我相信对象是垂直于正交视图渲染的。我的相机看起来像是在物体后面,因为我减去了距离和它在看原点。我也像这样禁用了剔除:glDisable(GL_CULL_FACE); 但我仍然看不到它。 =[
  • 您是否尝试过反转相机 z 轴上的标志,看看它是否突然进入视野?我曾多次站在你的立场上,几乎总是我在自己的坐标系中变得“迷失方向”;通常一个标志翻转可以纠正这个问题。当然,不要随意翻转标志——你应该考虑清楚。我不禁注意到您的值非常小(例如if 语句中的x + 1.0f)。尝试画一个大矩形并后退一段距离。
  • 好的,现在我有一种情况,如果我在 0,0,10 创建盒子平面,当我将相机缩放到位置 0,0,10 时,盒子平面会占据整个屏幕。当我在 0,0,5 或 0,0,15 方向再迈出一步时,盒子平面消失了。你知道那里可能是什么问题吗?
  • 如果我的回答让您摆脱了原来的问题,请考虑接受它。如果您愿意,我们可以继续进一步讨论 in the OpenGL chatroom,因为 StackOverflow 对扩展评论讨论不满意。
猜你喜欢
  • 2023-02-19
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 2016-06-15
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多