snakile
snakile

Reputation: 54521

How to rotate an object and but leaving the lighting fixed? (OpenGL)

I have a cube which I want to rotate. I also have a light source GL_LIGHT0. I want to rotate the cube and leave the light source fixed in its location. But the light source is rotating together with my cube. I use OpenGL ES 1.1 Here's a snippet of my code to make my question more clear.

GLfloat glfarr[] = {...} //cube points
GLubyte glubFaces[] = {...}
Vertex3D normals[] = {...} //normals to surfaces

const GLfloat light0Position[] = {0.0, 0.0, 3.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
glEnable(GL_LIGHT0);

for(i = 0; i < 8000; ++i)
{
        if (g_bDemoDone) break;
        glLoadIdentity();

        glTranslatef(0.0,0.0, -12); 
        glRotatef(rot, 0.0, 1.0,1.0);
        rot += 0.8;
        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);

        glNormalPointer(GL_FLOAT, 0, normals);

        glVertexPointer(3, GL_FLOAT, 0, glfarr);
        glDrawElements(GL_TRIANGLES, 3*12, GL_UNSIGNED_BYTE, glubFaces);

        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
        eglSwapBuffers(eglDisplay, eglSurface);
}

Thanks.

Upvotes: 2

Views: 3849

Answers (2)

ssube
ssube

Reputation: 48267

The problem seems to be that you're rotating the modelview matrix, not the cube itself. Essentially, you're moving the camera.

In order to rotate just the cube, you'll need to rotate the vertices that make up the cube. Generally that's done using a library (GLUT or some such) or simple trig. You'll be operating on the vertex data stored in the array, before the glDrawElements call. You may/may not have to or want to modify the normals or texture coordinates, it depends on your effects and how it ends up looking.

Upvotes: 0

Andreas Brinck
Andreas Brinck

Reputation: 52519

Fixed in relation to what? The light position is transformed by the current MODELVIEW matrix when you do glLightfv(GL_LIGHT0, GL_POSITION, light0Position);

If you want it to move with with the cube you'll have to move glLightfv(GL_LIGHT0, GL_POSITION, light0Position); to after the translation and rotation calls.

Upvotes: 2

Related Questions