13. OpenGL Lighting Types
Previously we spoke about setting up some basic lights.
Here we are going to take that to the next level and set the type
of shading, the colour of the light, the colour of our object
and the type of light in which we are using.
Now lets take into account that OpenGL has different types of lights.
They are:
Specular
Diffuse
Ambient
Emissive
A specular light is a light that sets highlights and often the shininess of
an object. So if you light a sphere with a diffuse light and a specular light
the specular light will add the highlights.
A diffuse light, is just a coloured light, and handles the diffuse term for lighting. It basically sets the colour.
An ambient light is a light that lights up the scene from every direction.
So even though the sun is a diffuse light, it acts like an ambient light
to us who are on the surface of the earth and during the day, see light
everywhere.
Now an emissive light is a light which radiates from an object. Like a glow, only without the nice, blurred halo type effect.
Now first off we need variables to hold our light colours and positions.
I am setting these up in our Display function as they are requiring variables
which I am setting up at runtime.
set DiffuseLight[] to the specified values
GLfloat DiffuseLight[] = {dlr, dlg, dlb};
set AmbientLight[] to the specified values
GLfloat AmbientLight[] = {alr, alg, alb};
set the LightPosition to the specified values
GLfloat LightPosition[] = {lx, ly, lz, lw};
Now I am going to use 2 lights here, so I enable them with:
glEnable (GL_LIGHT0);
glEnable (GL_LIGHT1);
Next I am setting the light variables:
diffuse light color variables
GLfloat dlr = 1.0;
GLfloat dlg = 1.0;
GLfloat dlb = 1.0;
ambient light color variables
GLfloat alr = 1.0;
GLfloat alg = 1.0;
GLfloat alb = 1.0;
light position variables
GLfloat lx = 0.0;
GLfloat ly = 0.0;
GLfloat lz = 1.0;
GLfloat lw = 0.0;
As you can see, I have made the diffuse light white by setting it to:
R = 1, G = 1, B = 1
I have then done the same with the ambient light.
Next I have set the position so that it is placed at:
X = 0, Y = 0, Z = 1, W = 0
Now as for the fourth variable, that sets whether or not we want a point light. If we want a point light, we will set it to 1.0, if we want a directional light, we will use 0.0. In this case, I am using a light that points in the direction of the z axis.
Now to get this to take effect in our application I set up the lights with:
Set our diffuse light to our diffuse light color
glLightfv (GL_LIGHT0, GL_DIFFUSE, DiffuseLight);
Set our ambient light to our ambient light color
glLightfv (GL_LIGHT1, GL_AMBIENT, AmbientLight);
Move the light to the position we have set
glLightfv (GL_LIGHT0, GL_POSITION, LightPosition);
So light0 is our diffuse light and light1 is our ambient light.
You could technically set all these values to one light, but I have
done it in 2.
Now as for the shade model, you can set this to either:
GL_SMOOTH
or
GL_FLAT
Smooth allows for a nice transition from the dark colour to the light
colour, while flat makes the side, 1 colour, instead of a nice gradient.
If you have any queries over this tutorial, 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. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. |
#include <GL/gl.h> #include <GL/glut.h> //angle of rotation //diffuse light color variables //ambient light color variables //light position variables //draw the cube void init (void) { void display (void) { void reshape (int w, int h) { void keyboard (unsigned char key, int x, int y) { int main (int argc, char **argv) {
|
nice tutorials for the beginners….
nice tutor atlest hlping my work here
Great help with my work. Dont know where id be without this
Shall be visiting these tutorials often =D
Just add glutDisplayFunc(display) to the main function.
good tutos, but the code dont run. thanks anyway.
Hey Ferchoff,
It does run, if you are having problems, then it’s with your setup.
Cheers,
Swiftless
What doesn’t run? some of them come up with errors but are easy to fix so i don’t see why you will have any trouble?
Awesome tutorials so far. They’re great for total OpenGL beginners.