PDA

View Full Version : Lighting problem on hierachical model



JammyB
04-30-2005, 01:07 PM
Hi, having a problem with getting the lighting to work correctly on a hierachical model. The model is made up of a static base, with different parts above being rotated.

The problem is that whilst the base is being lit correctly, the rest of the model seems to be being treated as if it's a single object and shading applied to the whole thing rather than to individual faces. The result of this is that the moving part fades in and out as a whole as it rotates.

I'm assuming it's something to do with how I've set up the matrix transformations, I'm a complete newbie so I may be doing something pretty stupid.

The executable is here if you want to see it in action: http://www.barwell.plus.com/misc/gl.exe

Here's the code that I reckon is relevent, I can post more if needed:


GLfloat light_0_dir[]={1.0,0.5,0.5,0.0};

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_0_dir);


glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glPushMatrix();
drawcube0(); //draw base

glRotatef(theta,0.0,1.0,0.0);
drawcube1(); //draw pole

glTranslatef(0,horsebob,0);
drawcube2(); //draw horses1+2

glRotatef(headspin,-0.1,0.0,0.0);
drawcube4(); //draw head1
glPopMatrix();

glPushMatrix();
glRotatef(theta,0.0,1.0,0.0);
glTranslatef(0,horsebob,0);
glRotatef(headspin,0.1,0.0,0.0);
drawcube5(); //draw head2
glPopMatrix();


glFlush();
glutSwapBuffers();
}Thanks very much for any help.

Edit: I have tried setting up the lights properly within the modelview matrix as well (after glPushMatrix()) and the same problem occurs so I don't believe it's that.

bansheeogl
04-30-2005, 03:45 PM
Nope, the problem is that you got no normals! opengl will set a normal for each vertex with the same value if you dont set your normals per vertex, this means that they will all share the same normal.

you should calculate your normals. this is the hardest thing when youre getting started like me!.

bansheeogl
04-30-2005, 04:02 PM
maybe this helps? its a cube with normals

glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();

JammyB
05-01-2005, 05:50 AM
Cheers, seems to be working now. I feel like a bit of an idiot for forgetting the normals! Thanks for the help.