converted to 1.6 markup
|Deletions are marked like this.||Additions are marked like this.|
|Line 1:||Line 1:|
|Ultimately, OpenGL is a mechanism for placing [http://www.handprint.com/HP/WCL/color1.html colors] on a computer screen (or printout, etc.). But this is a rich and huge topic and OpenGL's choices help in some respects and can cause problems in other respects.||Ultimately, OpenGL is a mechanism for placing [[http://www.handprint.com/HP/WCL/color1.html|colors]] on a computer screen (or printout, etc.). But this is a rich and huge topic and OpenGL's choices help in some respects and can cause problems in other respects.|
Ultimately, OpenGL is a mechanism for placing colors on a computer screen (or printout, etc.). But this is a rich and huge topic and OpenGL's choices help in some respects and can cause problems in other respects.
For example: no matter how precise the geometry, if every pixel representing an object on a screen is the same color that object will appear flat and featureless -- you will only be able to make out its profile. A variation on this approach chooses random (garish) colors for different faces of a polyhedral solid -- this works, but still does not convey much of a sense of the geometry of the figure.
For example: opengl represents colors as a vector of three numbers in the range 0..1 (red, green, blue intensity). Furthermore, it simulates the interaction of light with a surface by multiplying colors. This means that colored light can only dimly illuminate an object (since the product of two positive numbers less than 1 must always be less than either of the originals).
To partially compensate for this lack, opengl provides several illumination mechanisms and the actual displayed colors are the result of the sum of these mechanisms.
Briefly, these four illumination mechanisms are:
ambient: flat light from all directions (object color times ambient color). This represents light from all directions and usually should be kept fairly dark if you want your lighting to emphasize the three dimensional character of your image.
diffuse: soft light from one direction (object color times diffuse color times cosine of angle between surface normal and diffuse direction). This represents direct lighting and usually should be fairly bright.
spectral: soft light from one direction (object color times spectral color times (cosine of angle between surface normal and spectral direction) raised to some power). This represents shininess usually you want your lights to have a fairly white spectral color so that shiny materials are shiny.
emissive: flat light from the object itself (object color). Usually you leave this alone, since most objects do not generate internal light. Note that if you are using this you might also want to think about positioning a light source "within the object" so that other objects are illuminated by it.
Note that the OpenGL lighting does not generate shadows (though they can be simulated using various techniques and shaders can be used to make some of these techniques perform reasonably).
To make any of these colors work, lighting must be enabled:
- glEnable GL_LIGHTING
Without lighting enabled, you get flat, featureless lighting. Flat lighting is simple to work with, but gives crude results. You can control flat lighting with the glColor commands.
With lighting enabled, you must supply additional information:
Each surface needs a normal defined (vector perpendicular to the surface, (+/&.:*: normal) should be 1).
- You need one or more lights. OpenGL supports at least eight lights. Each light has an ambient, diffuse and spectral color, and must also have a position and a direction.
Until you have provided some lights, if you have enabled lighting, your image will be completely black. (Without lighting, objects illuminate themselves.)
If you want to use all the lights, you would need to do something like:
glEnable GL_LIGHT_0 glEnable GL_LIGHT_1 glEnable GL_LIGHT_2 glEnable GL_LIGHT_3 glEnable GL_LIGHT_4 glEnable GL_LIGHT_5 glEnable GL_LIGHT_6 glEnable GL_LIGHT_7
- You need to define the surface normals -- typically these are defined at the vertices, and opengl uses linear interpolation to find the normals at each pixel. With shaders, you can also define normals with a "texture" (roughly speaking: a unique normal for each pixel).