Jump to content
  • Advertisement
Sign in to follow this  
Peter Poulsen

OpenGL Light goes nuts when I move the LIGHT0 [SOLVED]

This topic is 3728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is a chunk of code that sets up the opengl and draws two planes. The lines marked 'important lines' moves the LIGTH0 (or sets it to its default position I think). If they are there the result looks like this: http://www.pgpoulsen.dk/light1.jpg. If I remove those lines it looks like this: http://www.pgpoulsen.dk/light2.jpg. The image without the LIGHT0 moved seems most 'right', but it is really annoying not being able to move the light source. Can anyone explain to me what is going wrong? Am I initialize in the wrong order or what? /peter --- /* Enable stuff */ glEnable (GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_LIGHT0); /**** The important lines */ float l_pos[] = { 0.0f, 0.0f, 0.0f }; glLightfv(GL_LIGHT0, GL_POSITION, l_pos); /**** End of important lines */ /* General setup stuff */ glViewport(0, 0, width,height); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (60, width / height, 1.0, 100.0); glMatrixMode (GL_MODELVIEW); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt (0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); /* Preparing to draw */ glColor3f(0.5f, 0.5f, 0.5f); glRotatef(340.0f, 0.0f, 1.0f, 0.0f); glRotatef(340.0f, 0.0f, 0.0f, 1.0f); /* Do the drawing */ glNormal3f(-0.5f, 0.0f, 0.5f); glBegin(GL_QUADS); glVertex3f(-0.5, -0.5, -0.5f); glVertex3f(-0.5, 0.5, -0.5f); glVertex3f(0.0f, 0.5, 0.0f); glVertex3f(0.0f, -0.5, 0.0f); glEnd(); glNormal3f(0.5f, 0.0f, 0.5f); glBegin(GL_QUADS); glVertex3f(0.0f, 0.5, 0.0f); glVertex3f(0.0f, -0.5, 0.0f); glVertex3f(0.5, -0.5, -0.5f); glVertex3f(0.5, 0.5, -0.5f); glEnd(); /* done */ glFlush(); --- [Edited by - Peter Poulsen on May 5, 2008 11:58:23 PM]

Share this post


Link to post
Share on other sites
Advertisement
You probably want to call glLightfv(GL_LIGHT0, GL_POSITION, l_pos) after setting the modelview matrix, that is after calling gluLookAt.

Think of the light's position as a vertex in world space. You want to transform it to view space.

Hope this helps.

Share this post


Link to post
Share on other sites
Thanks, it clearly does something. I'm not sure it the right thing though. Now it looks like this: http://www.pgpoulsen.dk/light3.jpg

/peter

Share this post


Link to post
Share on other sites
Post your images on imageshack.us or similiar as your links don't work (for me, at least), please.

Share this post


Link to post
Share on other sites
You need to change
float l_pos[] = { 0.0f, 0.0f, 0.0f };
glLightfv(GL_LIGHT0, GL_POSITION, l_pos);

float l_pos[] = { 0.0f, 0.0f, 0.0f, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, l_pos);

Share this post


Link to post
Share on other sites
Awesome! It seems to be working now. Mental note: light positions are points, not vectors, and they are affected by the transformations in the MODELVIEW matrix. Check!

Thanks a lot guys.

/peter

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!