PDA

View Full Version : Orbiting spheres, sometimes in front, sometimes behind



TalysShade
10-06-2012, 03:46 AM
I'm starting to feel stupid for not being able to solve this problem, but I can't even find any guide or tutorial on google from someone trying to do the same thing, even though it seems very simple. You guys helped me so good last time , though.. I hope you can help me just one more time.

I have two spheres. One is stable (big yellow sun) and the other a small blue one (planet) that is supposed to orbit the sun. I have a rotate function and I translate the blue one away from the sun center so that it will orbit the sun. I enabled DEPTH_TEST and depending on whether I choose the function DEPTH_FUNC(LEQUAL) or DEPTH_FUNC(LESS), the blue one will constantly appear in front of or behind the yellow one. No matter how I translate or change the rotation it will not do what I want it to. Which is a proper rotation where, if viewed from the front: if for example going from left to right it's behind the yellow one, and from right to left in front of it, so that it's actually orbiting.

Relevant code in initialisation:


glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);


Display function:


/* Draw background. */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



/* Instructions. */
glColor3f(1.0, 1.0, 1.0);
printText(-200, 340, "Click left to start motion, click right to stop motion");
glScalef(1, 1, 0);

glPushMatrix(); /* save start matrix */

glPushMatrix(); /* save start matrix again */

glColor3f(1.0, 1.0, 0.0);
glRotatef (angleSun, 0.0, 1.0, -1.0); /* rotate sun */

awSphere(0, 0, 50); /* draw sun with self made sphere function (which works, as long as i don't mess with perspective) */
glPopMatrix(); /* get start matrix, forget about sun */

glRotatef (angleSun*2, 0.0, 1.0, 0.0); /* circle planet around sun*/
glTranslatef (80,0,0); /* translate from sun center (Tried a lot of values for x, y and z here)*/

glPushMatrix(); /* save position of planet */

glRotatef (angleSun, -0.3, 1.0, 0.0); /* rotate planet itself */
glColor3f(0.0, 0.0, 1.0);
awSphere(0, 0, 25); /* draw planet */
glPopMatrix(); /* get position of planet */

glPopMatrix(); /* get start matrix, forget previous */




/* Swap output buffer. */
glutSwapBuffers ();

rotate(); /* A function to update the angleSun value */



If any other code is necessary, please let me know.

Just for example, these two pictures should alternate.
879880