<OpenGL>Lighting
I. Hidden-Surface Removal
Purpose: In order to increase performance, it is very important to draw objects that are closer to the viewing position and to eliminate objects obscured by others nearer to the eye.
Depth buffer: a value associating a depth, or a distance, from the view plane (usually the near clipping plane), with each pixel on the window. Initially, the depth values for all pixels are set to the largest possible distance (usually the far clipping plane) using the glClear() command with GL_DEPTH_BUFFER_BIT.
To use depth buffer, you need to enable depth-buffering. Before drawing, each time you draw the scene, you need to clear the depth buffer and then draw the objects in the scene in any order.
To perform hidden-face removal, using the following code:
1 glutInitDisplayMode(GLUT_DEPTH | ...); // enable depth-buffering 2 glEnable(GL_DEPTH_TEST); // enable hidden-surface removal 3 ... 4 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 5 /* drawings here */
II. OpenGL Lighting
i. Four components: Ambient, Diffuse, Specular, Emissive (All four component are computed independently and then added together)
1. Ambient illumination is light that's been scattered so much by the environment that it's direction is impossible to determine---it seems to come from all directions.(Environment lighting)
2. Diffuse component is the light that comes from one direction, so it's brighter if it comes squarely down on a surface than if it barely glances off the surface.(Chalk, carpet etc.)
3. Specular light comes from a particular direction, and it tends to bounce off the surface in a preferred direction.(Mirror, shiny metal etc.)
4. Emissive color, which materials may have, simulates light originating from an object. It does not introduce any additional light into the overall scene.
ii. Functions
1. glMaterial*(GLenum face, GLenum pname, TYPE param) // define materiall properties for the objects
face
Specifies which face or faces are being updated. Must be one of GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK.
pname
Specifies the single-valued material parameter of the face or faces that is being updated. Must be GL_SHININESS.
param
Specifies the value that parameter GL_SHININESS will be set to.
2. glLight*(GLenum light, GLenum pname, TYPE param); // create, position and enbale one or more light sources
light
Specifies a light. The number of lights depends on the implementation, but at least eight lights are supported. They are identified by symbolic names of the form GL_LIGHT i, where i ranges from 0 to the value of GL_MAX_LIGHTS - 1.
pname
Specifies a light source parameter for light. GL_AMBIENT, GL_DSE, GL_SPECULAR, GL_POSITION, GL_SPOT_CUTOFF, GL_SPOT_DIRECTION, GL_SPOT_EXPONENT, GL_CONSTANT_ATTENUATION,GL_LINEAR_ATTENUATION, and GL_QUADRATIC_ATTENUATION are accepted.
params
Specifies a pointer to the value or values that parameter pname of light source light will be set to.
ps. Remember using glEnable(GL_LIGHTi) to turn on the light source
Lighting example code:
1 void init(void) 2 { 3 GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0}; 4 GLfloat mat_shininess[] = {10.0}; 5 GLfloat light0_position[] = {1.73, 1.0, 1.0, 0.0}; 6 GLfloat light1_position[] = {-1.73, 1.0, 1.0, 0.0}; 7 GLfloat r_light[] = {1.0, 0.0, 0.0, 1.0}; 8 GLfloat g_light[] = {0.0, 1.0, 0.0, 1.0}; 9 10 glClearColor(0.0, 0.0, 0.0, 0.0); 11 12 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); 13 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); 14 glLightfv(GL_LIGHT0, GL_POSITION, light0_position); 15 glLightfv(GL_LIGHT0, GL_DIFFUSE, r_light); 16 glLightfv(GL_LIGHT0, GL_SPECULAR, r_light); 17 glLightfv(GL_LIGHT1, GL_POSITION, light1_position); 18 glLightfv(GL_LIGHT1, GL_DIFFUSE, g_light); 19 glLightfv(GL_LIGHT1, GL_SPECULAR, g_light); 20 21 glEnable(GL_DEPTH_TEST); 22 glEnable(GL_LIGHTING); 23 glEnable(GL_LIGHT0); 24 glEnable(GL_LIGHT1); 25 }
iii. Spotlights
You can have a positional light source act as a spotlight---that is, by restricting the shape of the light it emits to a cone.
Its properties can be modified by:
1 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, Default=(0.0, 0.0, -1.0)) // light's direction 2 glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, Default=0.0) // light's exponent 3 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, Default=180.0) // light's cutoff angle
ps. GL_SPOT_EXPONENT controls the light intensity concentration, given that light's intensity is highest in the center of the cone.
ps. the default cutoff angle is the radius but not diameter, the default 180.0 light covers 360.0 of total space.
iv. Light models
glLightModel* (GLenum pname, const GLfloat params);
glLightModel sets the lighting model parameter. pname names a parameter and params gives the new value. There are three lighting model parameters:
GL_LIGHT_MODEL_AMBIENT-
paramscontains four integer or floating-point values that specify the ambient RGBA intensity of the entire scene. Integer values are mapped linearly such that the most positive representable value maps to 1.0, and the most negative representable value maps to -1.0 . Floating-point values are mapped directly. Neither integer nor floating-point values are clamped. The initial ambient scene intensity is (0.2, 0.2, 0.2, 1.0). GL_LIGHT_MODEL_COLOR_CONTROL-
paramsmust be eitherGL_SEPARATE_SPECULAR_COLORorGL_SINGLE_COLOR.GL_SINGLE_COLORspecifies that a single color is generated from the lighting computation for a vertex.GL_SEPARATE_SPECULAR_COLORspecifies that the specular color computation of lighting be stored separately from the remainder of the lighting computation. The specular color is summed into the generated fragment's color after the application of texture mapping (if enabled). The initial value isGL_SINGLE_COLOR. GL_LIGHT_MODEL_LOCAL_VIEWER-
paramsis a single integer or floating-point value that specifies how specular reflection angles are computed. Ifparamsis 0 (or 0.0), specular reflection angles take the view direction to be parallel to and in the direction of the -z axis, regardless of the location of the vertex in eye coordinates. Otherwise, specular reflections are computed from the origin of the eye coordinate system. The initial value is 0. GL_LIGHT_MODEL_TWO_SIDE-
paramsis a single integer or floating-point value that specifies whether one- or two-sided lighting calculations are done for polygons. It has no effect on the lighting calculations for points, lines, or bitmaps. Ifparamsis 0 (or 0.0), one-sided lighting is specified, and only the front material parameters are used in the lighting equation. Otherwise, two-sided lighting is specified. In this case, vertices of back-facing polygons are lighted using the back material parameters and have their normals reversed before the lighting equation is evaluated. Vertices of front-facing polygons are always lighted using the front material parameters, with no change to their normals. The initial value is 0.
III. Normal
glNormal* (GLtype nx, GLtype ny, GLtype nz);
nx,ny,nz-
Specify the x, y, and z coordinates of the new current normal. The initial value of the current normal is the unit vector, (0, 0, 1).
The current normal is set to the given coordinates whenever glNormal is issued. Byte, short, or integer arguments are converted to floating-point format with a linear mapping that maps the most positive representable integer value to 1.0 and the most negative representable integer value to -1.0 .
Normals specified with glNormal need not have unit length. If GL_NORMALIZE is enabled, then normals of any length specified with glNormal are normalized after transformation. If GL_RESCALE_NORMAL is enabled, normals are scaled by a scaling factor derived from the modelview matrix. GL_RESCALE_NORMAL requires that the originally specified normals were of unit length, and that the modelview matrix contain only uniform scales for proper results. To enable and disable normalization, call glEnable and glDisable with either GL_NORMALIZE or GL_RESCALE_NORMAL. Normalization is initially disabled.
Notes: The normal of every object will be changed by glScalf() accordingly, thus it's important to use glEnable(GL_NORMALIZE) which automatically normalize every normal after scaling, by doing this, the lighting will perform properly.

浙公网安备 33010602011771号