OpenGL - Rotating the moon around the sun without spinning?

I am working on a graphic model of the moon orbiting the earth. Right now, the moon is spinning around its y axis, spinning around the earth. How can I stop the moon from spinning, but still allow it to go into orbit? Here is the code ..

Edit: Added animated video to demonstrate the problem:

http://www.youtube.com/watch?v=ltGV4pXD5Cs

void DrawInhabitants(GLint nShadow)
{
    static GLfloat yRot = 0.0f;         // Rotation angle for animation 

    if(nShadow == 0)
    {
        yRot += 0.2f; 
    } 

    // Draw the randomly located spheres
    glBindTexture(GL_TEXTURE_2D, textureObjects[MOON_TEXTURE]); 

    glPushMatrix();
        glTranslatef(0.0f, 0.1f, -2.5f);

        glPushMatrix();
            glRotatef(-yRot * 2.0f, 0.0f, 1.0f, 0.0f);
            glTranslatef(1.0f, 0.0f, 0.0f);
            gltDrawSphere(0.1f,21, 11);
        glPopMatrix();

        if(nShadow == 0)
        {
            // Torus alone will be specular
            glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight);
        }

        glRotatef(-yRot, 0.0f, 1.0f, 0.0f);
        glBindTexture(GL_TEXTURE_2D, textureObjects[EARTH_TEXTURE]);
        gltDrawSphere(0.3f, 21, 11);
        glMaterialfv(GL_FRONT, GL_SPECULAR, fNoLight);
    glPopMatrix();
}
+3
source share
1 answer

The problem is that you rotate the coordinate system to put the moon in the desired relative position. This rotation is global, so it affects the orientation of the moon. You need to cancel the rotation after the translation, so you have a "translator sandwich"

rotate a
translate
rotate -a
+3
source

All Articles