11. OpenGL Lighting
This opengl tutorial will show you how to use opengl light within your opengl application. Lighting is also very easily achieved in OpenGL.
You enable the depth, the lighting, and which light you are using.
Lighting is something that you must have to achieve the appearance of a
3d object.
Luckily OpenGL comes with its own lights.
To get our lighting up and running, we need to enable our depth support.
I have done it with this:
The first line clears the depth buffer
glClearDepth(1);
The second line enables depth testing
glEnable(GL_DEPTH_TEST);
Next we need to enable the OpenGL lighting support.
To do this simply call the line:
glEnable(GL_LIGHTING);
Now that we have depth and lighting, we need lights. OpenGL has direct
support for about 8 lights.
To enable a light, just call:
glEnable(GL_LIGHT0);
There is also:
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);
glEnable(GL_LIGHT4);
… etc.
Now that we have some, depth and lighting, run the program and you will
see that our cube is now lit. But it is only white and grey.
This is because we have not set the colours of our lights or the object
in which we are lighting. And also, even though we have set colours for the
objects in the past with:
glColor3f(1,0,0);
That won’t work with lights unless you enable:
glEnable(GL_COLOR_MATERIAL);
Now if you have any problems, feel free to email me at swiftless@gmail.com
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. |
#include <GL/gl.h> #include <GL/glut.h> GLfloat angle = 0.0; void cube (void) { void init (void) { void display (void) { void reshape (int w, int h) { int main (int argc, char **argv) {
|
Please add some images to the tutorials, cause it’s more didactic.
Thank you.
Hey it’s Dario again!
I’m trying to catch up on a lot of comments since I don’t get much time these days to play with the site.
If you’re offering to provide some images, that’d be great thanks!
Look forward to your next negative comment.
Thanks,
Swiftless
Great Work! thanks for the tutorials. But I see this tutorial has different format than others. There is also an ad in between the code. Other than this everything is fine. 🙂
Hi i have a problem with this part it says that its undeclared
glutSolidCube(2);
I added some code to make my cube transparent and colored (red), but I noticed that all my sides are transparent except one, which is completely opaque. Why is that?
P.S. I added some other colored shapes in there too, it’s cool to see the blending work with multiple objects/shapes of different colors.
Hi, the lighting works fine if I use primitive 3D objects such as glutSolidCube. But it doesn’t work if I create the cube using GL_QUADS. I wonder why is it like that :S And if you can tell me what is the solution to this matter, I’d be really grateful. Thank you 🙂
Hi Fiona,
That is because glutSolidCube also includes the normals associated with the faces. In order to get lighting to work on standard GL_QUADS, GL_TRIANGLES, etc, you need to assign a normal to either each vertex or each face, so that the lighting algorithm has something to compare the light to the surface.
Cheers,
Swiftless
I think previous tutorials (those at the beginning) were more explanatory,I wish you kept these more important tutorials longer.
Hi, You should try to define glClearColor and glClearDepth in your init() method. From then on, calling glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); will automatically clear those bits to the defined colors in init().
Hi KylBlz,
I’m going to disagree with you there and suggest setting the clear depth and clear color during your rendering loop for the sake of getting into the habit. For basic OpenGL applications you will often only need the one setting, but once you get into multiple rendering passes and start trying more advanced OpneGL, you will often have to vary these values at different times
Cheers,
Swiftless
Hi Lonelobo,
Unless you are going to call the lighting init after the gluLookAt, there is no reason to call it in the display function.
Although if you add a camera of some sort, then it is best to place the light position after the camera.
This is because the camera uses the current model view matrix to transform it’s position by.
Cheers,
Swiftless
This piece of code doesn’t seem to work in my project. Must whe not call void init (void) in the display fuction or so?