views:

46

answers:

2

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.

A: 

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.

peachykeen
Thanks. But: We can't use GLUT because we're using OpenGL ES (embedded). Isn't there another simpler way to rotate only the cube (not the view matrix)?
snakile
1. Rotating vertices directly is a bad advice. 2. OpenGL doesn't have **view** matrix. It has **modelview** matrix. Which is world*view.
SigTerm
@SigTerm OK, so how to rotate the cube without rotating vertices directly?
snakile
@snakile: With matrices, of course - glRotate,glTranslate,glScale,glMultMatrix. To position your light relative to cube, try setting light's coordinates after you set up all matrices, or try changing light to point (instead of directional).
SigTerm
Rotating vertices directly, for a problem like this, isn't really a bad idea. For a million high-poly objects, it would be. However, moving the light is probably a better solution. And the view/modelview was just a typo, fixed.
peachykeen
+1  A: 

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.

Andreas Brinck