0

I have a problem where I am able to see the desired object in perspective projection while not being able to see the object in orthogonal projection even when the camera is located in the same coordinates and looking at the same coordinate.

I know that the object is rendered correctly since it displays correctly in the perspective view like so:

Perspective projection

The Plane is located at the origin with height of 10, width of 50, and no depth. It's positioned at (0, -10, 0)

I would like to be able to view this in orthographic projection. The way I set it up is like this in my CameraManager class:

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();

}

Then in my SetupOrthographicCamera() I do this:

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());
    */
}

This is my Perspective camera:

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
}

And this is my 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());

}

I'm not sure what I am missing.

genpfault
  • 51,148
  • 11
  • 85
  • 139
terminix00
  • 327
  • 2
  • 13
  • Be aware that the object is likely a very different size in the perspective and orthographic renderings, unless you chose the bounds very carefully. It may simply be that the object is off the screen (because everything's really big) or so tiny you can't see it (because everything's really small). I notice that your camera is ***not*** pointing directly at the object, because the centre of the screen (in the perspective rendering) is blue and not green. – user253751 May 13 '16 at 00:54

1 Answers1

0

If the object isn't rendered perpendicular in an ortho view, you may see nothing (as the camera is in a different z-axis plane). Also, are you looking at it from behind with backface culling on? Then you will never see it. I would try explicitly disabling this. I can think of more reasons...z-axis is inverted in OpenGL (negative is into the screen from the typical Euclidean perspective w/respect to other axes, you may be too close w/clipping ranges provided, etc. Sorry for vagueness, just some things you may have overlooked that may help you.)

EntangledLoops
  • 1,951
  • 1
  • 23
  • 36
  • Hi so I believe the object is rendered perpendicularly to the ortho view. My camera looks like it is behind the object since I subtract the distance and its looking at the origin. I also disabled the culling like so: `glDisable(GL_CULL_FACE);` But I still can't see it. =[ – terminix00 May 12 '16 at 23:59
  • Have you tried inverting the sign on the camera's z-axis to see if it suddenly comes into view? I have been in your shoes many times, and it's almost always the case that I have become "disoriented" in my own coordinate system; often a single sign flip corrects the issue. Of course, don't be flipping signs at random---you should think it through. I can't help but notice your values are very small (e.g. `x + 1.0f` in the `if` statement). Try drawing a large rectangle and backing off a large distance. – EntangledLoops May 13 '16 at 16:23
  • Okay, now I have a situation where if I create the box plane at 0,0,10 When I zoom my camera to position 0,0,10 the box plane takes up the entire screen. When I take another step out in either direction 0,0,5 or 0,0,15 the box plane dissapears. Do you know what could be the issue there? – terminix00 May 13 '16 at 17:36
  • If my answers lead you to move past your original problem, please consider accepting it. We can continue further discussion [in the OpenGL chatroom](https://chat.stackoverflow.com/rooms/111886/opengl) if you like, as StackOverflow frowns upon extended comment-discussions. – EntangledLoops May 13 '16 at 17:40