2
votes

Been integrating this camera tutorial http://www.swiftless.com/tutorials/opengl/camera2.html and having a bit of trouble centering the camera in the skybox.

Using this code below makes my camera inside the box:

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= h)
      glOrtho(-1.0, 1.0, -1.0*(GLfloat)h/(GLfloat)w, 
              1.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
   else
      glOrtho(-1.0*(GLfloat)w/(GLfloat)h, 
              1.0*(GLfloat)w/(GLfloat)h, -1.0, 1.0, -10.0, 10.0);
   glMatrixMode(GL_MODELVIEW);
} 

To draw the skybox, I followed this tutorial: http://sidvind.com/wiki/Skybox_tutorial I've been trying to translate objects closer to the camera, but didn't work as I expected. Now I'm not sure what I need to do.

Appreciate any help.

1

1 Answers

2
votes

First: Don'y apply the projection in the reshape handler. Otherwise simple things appear impossible (like doing a skybox). Second: For a skybox to work you must use the very same projection like for the rendering of the rest of the scene. What you should change is the translation of the modelview to 0, yet keeping the camera orientation.

You can do this by setting the last column of the modelview matrix to (0,0,0,1).

So this makes your rendering code like this:

void render_skybox()
{

    push_modelview();
    set_modelview_column(3, 0, 0, 1);
    draw_skybox();
    pop_modelview();

}

void render()
{
    set_viewport();
    set_projection();

    apply_camera_transform();

    render_skybox();
    render_scene();
}