<opengl>lighting

Source: Internet
Author: User

I. Hidden-surface Removal

Purpose:in order to increase performance, it's very important to draw objects that was closer to the viewing position an D 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) Pixel on the window. Initially, the depth values for all pixels is 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 /* */

II. OpenGL Lighting

I. Four components:ambient, diffuse, specular, emissive (all four component is computed independently and then added tog Ether)

1. Ambient illumination is light that's been scattered so much by the environment that it's direction is impossible to Det Ermine---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 t Han if it barely glances off the surface. (Chalk, carpet etc.)

3. Specular light comes from a particular direction, and it tends to bounce off of surface in a preferred direction. (Mirror, shiny metal etc.)

4. emissive color, which materials may has, 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 is being updated. Must be one GL_FRONT of, GL_BACK , or GL_FRONT_AND_BACK .

pname

Specifies the single-valued material parameter of the face or faces that's being updated. Must be GL_SHININESS .

param

Specifies the value that parameter'll is GL_SHININESS 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 is supported. They is identified by symbolic names GL_LIGHT of the form I, where I ranges from 0 to the value of GL_MAX_LIGHTS -1.

pname

Specifies a light source parameter light for. GL_AMBIENT , GL_DSE GL_SPECULAR GL_POSITION GL_SPOT_CUTOFF GL_SPOT_DIRECTION GL_SPOT_EXPONENT GL_CONSTANT_ATTENUATION GL_LINEAR_ATTENUATION ,,,,,,,,, and is GL_QUADRATIC_ATTENUATION accepted.

params

Specifies a pointer to the value or values, parameter pname of light source light would be set To.

PS. Remember using glenable (Gl_lighti) to turn on the light source

Lighting Example code:

1 voidInitvoid)2 {3Glfloat mat_specular[] = {1.0,1.0,1.0,1.0};4Glfloat mat_shininess[] = {10.0};5Glfloat light0_position[] = {1.73,1.0,1.0,0.0};6Glfloat light1_position[] = {-1.73,1.0,1.0,0.0};7Glfloat r_light[] = {1.0,0.0,0.0,1.0};8Glfloat g_light[] = {0.0,1.0,0.0,1.0};9 TenGlclearcolor (0.0,0.0,0.0,0.0); One  A GLMATERIALFV (Gl_front, Gl_specular, mat_specular); - GLMATERIALFV (Gl_front, gl_shininess, mat_shininess); - GLLIGHTFV (gl_light0, gl_position, light0_position); the GLLIGHTFV (gl_light0, Gl_diffuse, r_light); - GLLIGHTFV (gl_light0, Gl_specular, r_light); - GLLIGHTFV (gl_light1, gl_position, light1_position); - GLLIGHTFV (gl_light1, Gl_diffuse, g_light); + GLLIGHTFV (gl_light1, Gl_specular, g_light); -  +Glenable (gl_depth_test); A glenable (gl_lighting); at glenable (gl_light0); - glenable (gl_light1); -}

III. Spotlights

You can has a positional light source act as a spotlight---that's, by restricting the shape of the light it emits to a C One.

Its properties can is modified by:

1 gllightfv (gl_light0, Gl_spot_direction, default= (0.00.0,-1.0))    // Light ' s direction2 GLLIGHTF (gl_light0, gl_spot_exponent, default=0.0)    // Light ' s exponent3 GLLIGHTF (gl_light0, Gl_spot_cutoff, default=180.0)    // Light ' s cutoff angle

PS. gl_spot_exponent controls the light intensity concentration, given. s intensity are highest in the center of The cone.

PS. The default cutoff angle is the radius and not diameter, the default 180.0 light covers 360.0 of total space.

<opengl>lighting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.