Search code examples
c++openglglut

OpenGL Issue with Camera(?)


I'm learning OpenGL and I have a problem with my program where I'm supposed to make the solar system.

First of all here's the code I use to setup my ModelView Matrix:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(20, 1, 0, 0);
glTranslatef(0, -20, -60);

And then I draw the orbits using line loops and the sun is a gluSphere:

glPushMatrix();
glColor3f(1, 0.4f, 0);
glTranslatef(0, -2, 0);
gluSphere(gluNewQuadric(), 4, 30, 30);
glPopMatrix();

And here's the result:

SolarSystem1

But then, when I "zoom in" using this code:

if (key=='w')
{
    glTranslatef(0, 1, 2.4);
}
else if (key=='s')
{
    glTranslatef(0, -1, -2.4);
}

this happens:

enter image description here

the lines stay in front of the sphere. I know it's probably something dumb I'm doing but I'm just starting to learn and this is really slowing me down.. Thanks!


Solution

  • You probably don't have the depth test turned on.

    glEnable(GL_DEPTH_TEST);
    

    You may also need to fiddle with the depth test parameters, though usually the default setting is sufficient.

    glDepthfunc(GL_LESS);
    

    I'd also like to take this time to strongly recommend that you stop using OpenGL's Immediate Mode and OpenGL's Fixed Function Pipeline, and learn Modern OpenGL.