280 likes | 463 Views
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.
E N D
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.
Hidden Surface Removal • To use depth-buffering, you need to enable it: • glutInitDisplayMode (GLUT_DEPTH | ..); • glEnable (GL_DEPTH_TEST);
Hidden Surface Removal • Initialize the depth buffer and color buffer by using: • glClear(GL_DEPTH_BUFFER_BIT | • GL_COLOR_BUFFER_BIT);
Hidden Surface Removal • glutInitDisplayMode (GLUT_DEPTH | ..); • glEnable (GL_DEPTH_TEST); • ... • //in display function • glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); • draw3DObjectA(); • draw3DObjectB();
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
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
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
Lighting Properties: • Specular light comes from a particular direction. • It bounces off the surface in a particular direction
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.
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
Types of Lights • OpenGL supports two types of Lights • Local (Point) light sources • Infinite (Directional) light sources • Type of light controlled by w coordinate
Light Sources (cont.) • Light color properties • GL_AMBIENT • GL_DIFFUSE • GL_SPECULAR
Turning on the Lights • Flip each light’s switch glEnable( GL_LIGHTn ); • Turn on the power glEnable( GL_LIGHTING );
Controlling a Light’s Position • Modelview matrix affects a light’s position • Different effects based on whenposition is specified • eye coordinates • world coordinates • model coordinates • Push and pop matrices to uniquely control a light’s position
Advanced Lighting Features • Spotlights • localize lighting affects • GL_SPOT_DIRECTION • GL_SPOT_CUTOFF • GL_SPOT_EXPONENT
Advanced Lighting Features • Light attenuation • decrease light intensity with distance • GL_CONSTANT_ATTENUATION • GL_LINEAR_ATTENUATION • GL_QUADRATIC_ATTENUATION
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
How OpenGL Simulates Lights • Gourad lighting model • Computed at vertices • Lighting contributors • Light properties • Lighting model properties • Surface material properties
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)
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.
GL_DIFFUSE Base color GL_SPECULAR Highlight Color GL_AMBIENT Low-light Color GL_EMISSION Glow Color GL_SHININESS Surface Smoothness Material Properties • Define the surface properties of a primitive • glMaterialfv( face, property, value ); • separate materials for front and back
Per Vertex Poly. Frag FB Raster CPU DL Texture Pixel SurfaceNormals • Normals define how a surface reflects light • glNormal3f( x, y, z ) • Current normal is used to compute vertex’s color • Use unit normals for proper lighting • scaling affects a normal’s length glEnable( GL_NORMALIZE )orglEnable( GL_RESCALE_NORMAL )
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. • Create, select, and position one or more light sources. • Create and select a lighting model, which defines the level of global ambient light and the effective location of the viewpoint. • Define material properties for the objects in the scene.
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