Jump to content
  • Advertisement
Sign in to follow this  
beebs1

Lighting Issue

This topic is 2566 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

Hiya,

I'm having some trouble with fixed-function lighting in OpenGL.

I have a 3D mesh, and a light source set to a position a little way from it. What I'm hoping for is that the polygons facing the light source will be lit, and the ones facing away will be darker.

The mesh has normals, and which I'm re-normalising when I read it in.

I've enabled the light source and positioned it, and set it's ambient and diffuse colours to white. I've also set a material for the mesh.


glEnable(GL_LIGHT0);

/* Set the light position. */
GLfloat position[4] = { position.x, position.y, position.z, 1.0f };
glLightfv(light, GL_POSITION, position);

/* Set the light colour. */
GLfloat diffuseColour[4] = { m_diffuseColour.r, m_diffuseColour.g, m_diffuseColour.b, 1.0f };
GLfloat ambientColour[4] = { m_ambientColour.r, m_ambientColour.g, m_ambientColour.b, 1.0f };
glLightfv(light, GL_DIFFUSE, diffuseColour);
glLightfv(light, GL_AMBIENT, ambientColour);

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);


For some reason, the mesh is very dark and not lit from any one side. I'm pretty new to OpenGL - am I doing something wrong?

Many thanks for any help.

Cheers!

Share this post


Link to post
Share on other sites
Advertisement
Thanks for your replies!

I've got the diffuse light to work, but it's still not 'directional'. It seems to apply to all faces equally.

Here are some screenshots:

Screenshot 1

Screenshot 2

Screenshot 3 (lighting OFF)

The light is positioned at the origin.
The ambient colour is set to (0, 0, 0, 0) (no colour).
The diffuse colour is set to (1, 0, 0, 0) (red).

I don't think it's a problem with the specular colour - I'm not setting one. GL_LIGHTING is enabled for the mesh objects.

Thanks again :)

[Edit]

It's also possible the light is following the camera :/

Share this post


Link to post
Share on other sites
Fixed!

In case anyone has the same problem - I needed to position the light *after* applying the camera transformation... The light was always following the camera. Or the camera and objects were always being moved to the light, depending on how you prefer it :P

One point - is there a way to set light attenutation for a point light?

The OpenGL documentation seems to hint it's possible, but it's not clear how.

I'd like a point light to illuminate faces up to a certain distance.

Thanks.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!