Search code examples
c++visual-studioopenglperspectiveaxes

OpenGL Axis Appearing Different


When I run my program, just viewing xyz axis, occasionally the perspective seems to change.
More often than not it looks like this. But sometimes it looks like this.
In the first image, anything in the 'foreground' is far smaller. Whereas in the second image everything appears about the same size. I much prefer the second image, but I don't know what is happening!
Here is my code:

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)     // Resize And Initialize The GL Window
{
  if (height == 0)                                      // Prevent A Divide By Zero By
  {
    height = 1;                                     // Making Height Equal One
  } 

  glViewport(0, 0, width, height);                      // Reset The Current Viewport

  aspectRatio = (GLfloat)width / (GLfloat)height;
  screenHeight = (GLfloat)height;
  screenWidth = (GLfloat)width;
}

int InitGL(GLvoid)                                      // All Setup For OpenGL Goes Here
{
  glShadeModel(GL_SMOOTH);                          // Enable Smooth Shading
  glClearColor(1.0f, 1.0f, 1.0f, 0.5f);             // Black Background
  glClearDepth(1.0f);                                   // Depth Buffer Setup
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Really Nice Perspective Calculations
  return TRUE;                                      // Initialization Went OK
}

int DrawGLScene(GLvoid)                                 // Here's Where We Do All The Drawing
{
  //3D setup
  glDepthMask(GL_TRUE);
  glEnable(GL_DEPTH_TEST);                          // Enables Depth Testing
  glDepthFunc(GL_LEQUAL);                               // The Type Of Depth Testing To Do
  glMatrixMode(GL_PROJECTION);                      // Select The Projection Matrix
  glLoadIdentity();                                 // Reset The Projection Matrix

  gluPerspective(45.0f, aspectRatio, 0.1f, 500.0f);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Clear Screen And Depth Buffer

  //3D stuff
  glTranslatef(-8.0f*aspectRatio, 0.0f, -zoomInOut);                // Move and zoom axes
  glRotatef(rotateLeftRight, 0.0f, 1.0f, 0.0f);                 // Rotations
  glRotatef(rotateUpDown, 1.0f, 0.0f, 0.0f);
  //axes
  glLineWidth(1.0);
  glColor3f(0.0, 0.0, 1.0);
  glBegin(GL_LINES);//x axis
  glVertex3f(-20.0, 0.0, 0.0);
  glVertex3f(20.0, 0, 0);
  glEnd();
  glBegin(GL_LINES);//y axis
  glVertex3f(0.0, 20.0, 0.0);
  glVertex3f(0.0, -20.0, 0);
  glEnd();
  glBegin(GL_LINES);//z axis
  glVertex3f(0.0, 0.0, 20.0);
  glVertex3f(0.0, 0.0, -20.0);
  glEnd();

  //2D setup
  glDepthMask(GL_FALSE);
  glDisable(GL_DEPTH_TEST);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity(); 

  gluOrtho2D(0, screenWidth, 0, screenHeight); //left,right,bottom,top
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  //2D overlay goes here

  return TRUE;                                      // Keep Going
}

What have I done that is changing how the axes look?


Solution

  • I can't see anything wrong in your code.

    Your translation code is a bit weird glTranslatef(-8.0f*aspectRatio, 0.0f, -zoomInOut);

    You are on the X=-8, Y=0 line and you name your Z coordinate zoomInOut. Which is not a zoom but a position. It's the same as if you where in an elevator with outside view and you look at the fountain in the middle of the square at the bottom of the building, you don't zoom, you just look closer (like that).

    To do a real zoom you have to change the field of view angle (first parameter of gluPerspective).

    You draw 3 line from -20 to 20 over X, Y and Z axis.

    On your screen-shots we almost see them from one end to the other. There is no weird clamp (your Z limits from gluPerspective are good [0.1f, 500.0f]).

    The fact that your brain sometime doesn't see the perspective has it should is not a bug, it's just because you are looking at a flat image with only two color (no shadow, fog...).

    "3D" images (from OpenGl & co.) are just a king of optical illusion, and the program you wrote just draw a poor illusion.

    A poor one, but a correct one! It's a good start.