diffuse lighting woes.

Started by
6 comments, last by Lonefox1 18 years, 7 months ago
Hay guys just trying to add some simple diffuse positional lighting to my 3d world. Instead of it going out in all directions it seems to be going a certain way which is not what i want, or do positional lights need a direction? , heres how im setting it up anyway and a screen shot

//global vars
const GLfloat greenLightColor[] = { 0.6f, 0.6f, 0.0f, 1.0f };
GLfloat greenLightPos[] = { 0.0, 0.0, 0.0, 1.0 };

//in initilisation function
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT2, GL_AMBIENT, greenLightColor);
glLightfv(GL_LIGHT2, GL_DIFFUSE, greenLightColor);

//in rendering function:
   glPushMatrix();
   
    glTranslatef(Lx,Ly,Lz); //moves to the position where i want the light    
    glLightfv(GL_LIGHT2, GL_POSITION,greenLightPos);
    glEnable(GL_LIGHT2);

   glPopMatrix();


heres a screenshot. the light is positioned just infront of the camera as you can see it seems to be directed in the far distance as its much darker just behind it: im pretty certain it isnt a position problem as all the co-ords seem to be correct, although im not entirely certain. what you guys think? cheers Si
Advertisement
Do your polygons have correctly defined normals?

Enigma
looks like none of the walls/floor have normals
thats what i get for not answering straight away, someones beaten me
are normals not calculated automatically depending on the way you set your vertices? (clockwise etc) or do you have to set them manually?
Normals are not calculated automatically. You have to do them yourself.

You don't have to normalize the normals, however it's faster if you it yourself instead of letting OGL do it for you.
well im getting there, ive normalised the faces of my cubes like so:
          //Front face            glNormal3f(0.0,0.0,1.0);                     glTexCoord2f(0.0f,1.0f); glVertex3f(0, 0, 0); //top left vertex            glTexCoord2f(0.0f,0.0f); glVertex3f(0, 0-h, 0); //bottom left            glTexCoord2f(1.0f,0.0f); glVertex3f(0+w, 0-h, 0); //bottom right                     glTexCoord2f(1.0f,1.0f); glVertex3f(0+w, 0, 0); //top right                      //back face          glNormal3f(0.0,0.0,-1.0);           glTexCoord2f(1.0f,1.0f); glVertex3f(0,0,0-d);//top right           glTexCoord2f(0.0f,1.0f); glVertex3f(0+w,0,0-d);//top left           glTexCoord2f(0.0f,0.0f); glVertex3f(0+w,0-h,0-d);//bottom left           glTexCoord2f(1.0f,0.0f); glVertex3f(0,0-h,0-d);//bottom right                     //left face          glNormal3f(-1.0,0.0,0.0);            glTexCoord2f(0.0f,1.0f); glVertex3f(0, 0, 0-d);//top left          glTexCoord2f(0.0f,0.0f); glVertex3f(0, 0-h, 0-d);//bott;om left          glTexCoord2f(1.0f,0.0f); glVertex3f(0, 0-h, 0);//bottom right          glTexCoord2f(1.0f,1.0f); glVertex3f(0, 0, 0);//top right                   //right face          glNormal3f(1.0,0.0,0.0);           glTexCoord2f(0.0f,1.0f); glVertex3f(0+w, 0,0);//top left           glTexCoord2f(0.0f,0.0f); glVertex3f(0+w, 0-h, 0);//bottom left           glTexCoord2f(1.0f,0.0f); glVertex3f(0+w, 0-h, 0-d);//bottom right           glTexCoord2f(1.0f,1.0f); glVertex3f(0+w, 0, 0-d);//top right                   //top face          glNormal3f(0.0,1.0,0.0);           glTexCoord2f(0.0f,1.0f); glVertex3f(0,0,0-d);//top left           glTexCoord2f(0.0f,0.0f); glVertex3f(0,0,0);//bottom left           glTexCoord2f(1.0f,0.0f); glVertex3f(0+w,0,0);//bottom right           glTexCoord2f(1.0f,1.0f); glVertex3f(0+w, 0, 0-d);//top right           //bottom face          glNormal3f(0.0,-1.0,0.0);          glTexCoord2f(0.0f,1.0f); glVertex3f(0, 0-h, 0);//top left          glTexCoord2f(0.0f,0.0f); glVertex3f(0, 0-h, 0-d);//bottom left          glTexCoord2f(1.0f,0.0f); glVertex3f(0+w, 0-h, 0-d);//bottom right          glTexCoord2f(1.0f,1.0f); glVertex3f(0+w, 0-h, 0);//top right


this results in:


the roof and floor seem to be working correctly but i cant figure out why the walls arnt, the far wall in the background is fully lit. the normals set up seems to be correct(?), any ideas?

cheers
si
Actually that is lighting correctly according to the lighting model you specified. The problem is that you didn't specify any attenuation for the light, which means only the angle of the normal to the direction of the light affects the lighting contribution, not the distance from the light. Try throwing in some quadratic attenuation (glLightfv(GL_QUADRATIC_ATTENUATION, somevalue)) to see the light attenuate with distance.

Enigma
cheers seems to work :D what are the normal values for that tho , to get anything out of it i had to use 0.0008, even then it seems a bit dim and if i go much higher it just looks like it did before :S

cheers
si

This topic is closed to new replies.

Advertisement