0

I have simple studyng project representing some space star's system. I have a star and several planets with moons rotating around the star. I'm using depth buffer, but planets are always in front of the star even if planet should be shadowed by the star. What can be the issue reason?

void drawPlanet(float orbitRadius, float planetRadius, float daysInYear, float hoursInDay){
    static float year = 0;
    static float days = 0;
    static float hours = 0;
    glPushMatrix();
    glRotatef(year, 0, 1, 0);
    glTranslatef(orbitRadius, 0 ,0);
    glRotatef(days, 1, 2, 0);
    glutWireSphere(planetRadius, 20, 16);
    drawLuna(0.3, 0.1, 30, 15);
    glPopMatrix();
    hours++;
    days = hours/hoursInDay;
    year=days/daysInYear;
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0,0,5, 0, 0, 0, 0 ,1, 0);

    glColor3f(1.0, 0.647059, 0.00);
    glutSolidSphere(0.5, 20, 16);

    glColor3b(197, 96, 63);
    drawPlanet(2, 0.2, 50, 12);
    drawPlanet(1, 0.15, 30, 15);

    glFlush();
}

int main(int argc, char** argv)
{
    ....
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glEnable(GL_DEPTH_TEST);
    ....

}

1 Answer 1

3

Your call to glEnable(GL_DEPTH_TEST) has no effect if there's no OpenGL context. GLUT creates the OpenGL context only after glutCreateWindow has been called.

As a general rule you should not call glEnable outside of the display function. You will also have to clear the depth buffer, so make this

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.