80 likes | 92 Views
This tutorial covers the specification of colors and the setting of shading models using the Open.GL library. It also explains how to define and enable lights, and provides an example of lighting in a Visual C++ project.
E N D
Open GL: Colors and Lighting Geb Thomas
Specifying Colors • void glColor3{b s i f d ub us ui} (TYPEr, TYPEg, TYPEb);void glColor4{b s i f d ub us ui} (TYPEr, TYPEg, TYPEb, TYPEa);void glColor3{b s i f d ub us ui}v (const TYPE*v);void glColor4{b s i f d ub us ui}v (const TYPE*v); • Sets the current red, green, blue, and alpha values. This command can have up to three suffixes, which differentiate variations of the parameters accepted. The first suffix is either 3 or 4, to indicate whether you supply an alpha value in addition to the red, green, and blue values. If you don't supply an alpha value, it's automatically set to 1.0. The second suffix indicates the data type for parameters: byte, short, integer, float, double, unsigned byte, unsigned short, or unsigned integer. The third suffix is an optional v, which indicates that the argument is a pointer to an array of values of the given data type.
Shading Models • void glShadeModel (GLenum mode); • Sets the shading model. The mode parameter can be either GL_SMOOTH (the default) or GL_FLAT. • Here is a Visual C++ Project example
Lighting • Define normals • Create, position and enable lights • Select a lighting model • Define the material properties of objects in the scene
Defining Lights • void glLight{if}(GLenum light, GLenum pname, TYPEparam);void glLight{if}v(GLenum light, GLenum pname, TYPE *param); • Creates the light specified by light, which can be GL_LIGHT0, GL_LIGHT1, ... , or GL_LIGHT7. The characteristic of the light being set is defined by pname, which specifies a named parameter, param indicates the values to which the pname characteristic is set; it's a pointer to a group of values if the vector version is used, or the value itself if the nonvector version is used. The nonvector version can be used to set only single-valued light characteristics.
Multiple Lights GLfloat light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light1_position[] = { -2.0, 2.0, 1.0, 1.0 }; GLfloat spot_direction[] = { -1.0, -1.0, 0.0 }; glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular); glLightfv(GL_LIGHT1, GL_POSITION, light1_position); glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5); glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5); glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2); glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0); glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction); glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0); glEnable(GL_LIGHT1);