Вы находитесь на странице: 1из 27

Hidden Surface & Lighting

Presentation modified from a


presentation at OpenGL.org
Hidden Surface
• Hidden Surface Removal is very important in
3D scenes
• Only surfaces closest to the eye should be
seen and objects that are hidden by others
should be eliminated.
• The use of a depth buffer facilitates hidden
surface removal.

2
Hidden Surface Removal
• To use depth-buffering, you need to enable it:
• glutInitDisplayMode (GLUT_DEPTH
| ..);
• glEnable (GL_DEPTH_TEST);

3
Hidden Surface Removal
• Initialize the depth buffer and color buffer by
using:

• glClear(GL_DEPTH_BUFFER_BIT |
• GL_COLOR_BUFFER_BIT);

4
Hidden Surface Removal
• glutInitDisplayMode (GLUT_DEPTH | ..);
• glEnable (GL_DEPTH_TEST);
• ...
• //in display function
• glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
• draw3DObjectA();
• draw3DObjectB();

5
Lighting Principles
• Lighting simulates how objects reflect light
– Lighting properties
– Light’s color and position
– Material composition of object
– Global lighting parameters
• ambient light
• two sided lighting
– available in RGBA mode

6
Lighting Properties:
• Light comes from light sources that can be
turned on/off
• Ambient lighting comes from light that is so
scattered there is no way to tell its original
location
• Example: Backlighting in a room

7
Lighting Properties:
• The diffuse component of lighting is light that
comes from one direction.
• Once it hits a surface, it is scattered equally in
all directions

8
Lighting Properties:
• Specular light comes from a particular
direction.
• It bounces off the surface in a particular
direction

9
RGB Values for Lights
• A light source is characterized by the amount
of red, green, & blue light it emits.
• Examples: If R=G=B=1.0, the light is the
brightest possible white.
• If R=G=B=.5, the color is still white, but only at
half intensity, so it appears gray.
• If R=G=1.0 and B=0.0, the light appears yellow.

10
Setting Lighting Properties
• glLightfv( light, property, value );
– light specifies which light
• multiple lights (at least 8), starting with GL_LIGHT0

– properties
• Colors for ambient, diffuse, & specular components
• position and type
• attenuation

11
Types of Lights
• OpenGL supports two types of Lights
– Local (Point) light sources
– Infinite (Directional) light sources
• Type of light controlled by w coordinate

w  0 Infinite Light directed along  x y z


w0 Local Light positioned at xw y
w
z
w 

12
Light Sources (cont.)
• Light color properties
–– GL_AMBIENT
GL_AMBIENT
–– GL_DIFFUSE
GL_DIFFUSE
–– GL_SPECULAR
GL_SPECULAR

13
Turning on the Lights
• Flip each light’s switch
glEnable( GL_LIGHTn );
• Turn on the power
glEnable( GL_LIGHTING );

14
Controlling a Light’s Position
• Modelview matrix affects a light’s position
– Different effects based on when position is
specified
• eye coordinates
• world coordinates
• model coordinates
– Push and pop matrices to uniquely control a light’s
position

15
Advanced Lighting Features
• Spotlights
– localize lighting affects
• GL_SPOT_DIRECTION
• GL_SPOT_CUTOFF
• GL_SPOT_EXPONENT

16
Advanced Lighting Features
• Light attenuation
– decrease light intensity with distance
• GL_CONSTANT_ATTENUATION
• GL_LINEAR_ATTENUATION
• GL_QUADRATIC_ATTENUATION

1
fi 
k c  kl d  k q d 2
17
Light Model Properties
• glLightModelfv( property, value );
• Enabling two sided lighting
GL_LIGHT_MODEL_TWO_SIDE
• Global ambient color
GL_LIGHT_MODEL_AMBIENT
• Local viewer mode
GL_LIGHT_MODEL_LOCAL_VIEWER
• Separate specular color
GL_LIGHT_MODEL_COLOR_CONTROL

18
How OpenGL Simulates Lights
• Gourad lighting model
– Computed at vertices
• Lighting contributors
– Light properties
– Lighting model properties
– Surface material properties

19
Material Properties
• Material’s color depends on % of incoming R,G,B light it reflects.
• Material Properties:
– Ambient is color of the object from all the undirected light in a scene.
– Diffuse is the base color of the object under current lighting. There
must be a light shining on the object to get a diffuse contribution.
– Specular is the contribution of the shiny highlights on the object.
– Emission is the contribution added in if the object emits light (i.e.
glows)

20
Material Properties
• Color Components for lights mean something
different than for materials.
• Example: if R = 1, G = 0.5, and B = 0 for a
material, that material reflects all the
incoming red light, half the incoming green
light, and none of the incoming blue light.

21
Material Properties
• Define the surface properties of a primitive
• glMaterialfv( face, property,
value );
GL_DIFFUSE Base color

GL_SPECULAR Highlight Color

GL_AMBIENT Low-light Color

GL_EMISSION Glow Color

GL_SHININESS Surface Smoothness

– separate materials for front and back

22
Light Material Tutorial

23
Light Position Tutorial

24
Surface
Normals
• Normals define how a surface reflects light
• glNormal3f( x, y, z )
– Current normal is used to compute vertex’s color
Poly.
Poly.
Per
Per
Vertex
Vertex

– Use unit normals for proper lighting


CPU
CPU
DL
DL
Texture
Texture
Raster
Raster
Frag
Frag
FB
FB

Pixel
• scaling affects a normal’s length
Pixel

glEnable( GL_NORMALIZE )
or
glEnable( GL_RESCALE_NORMAL )

25
Steps for adding lighting
• 1. Define normal vectors for each vertex of every
object. These normals determine the orientation of
the object relative to the light source.
2. Create, select, and position one or more light
sources.
3. Create and select a lighting model, which defines the
level of global ambient light and the effective
location of the viewpoint.
4. Define material properties for the objects in the
scene.

26
Tips for Better Lighting
• Recall lighting computed only at vertices
– model tessellation heavily affects lighting results
• better results but more geometry to process
• Use a single infinite light for fastest lighting
– minimal computation per vertex

27

Вам также может понравиться