<OpenGL>Lighting
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了<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.
以上是关于<OpenGL>Lighting的主要内容,如果未能解决你的问题,请参考以下文章