Computer Graphics BingYu Chen National Taiwan University Introduction
Computer Graphics Bing-Yu Chen National Taiwan University
Introduction to Open. GL o o o o o General Open. GL Introduction An Example Open. GL Program Drawing with Open. GL Transformations Animation and Depth Buffering Lighting Evaluation and NURBS Texture Mapping Advanced Open. GL Topics modified from Imaging Dave Shreiner, Ed Angel, and Vicki Shreiner. An Interactive Introduction to Open. GL Programming. ACM SIGGRAPH 2001 Conference Course Notes #54. & ACM SIGGRAPH 2004 Conference Course Notes #29.
Lighting Principles o Lighting simulates how objects reflect light n material composition of object n light’s color and position n global lighting parameters o ambient light o two sided lighting n available in both color index and RGBA mode
How Open. GL Simulates Lights o Phong lighting model n Computed at vertices o Lighting contributors n Surface material properties n Lighting model properties
Surface Normals Poly. CPU Per Vertex Raster DL Frag Texture Pixel o Normals define how a surface reflects light gl. Normal 3 f( x, y, z ) n Current normal is used to compute vertex’s color n Use unit normals for proper lighting o scaling affects a normal’s length o gl. Enable( GL_NORMALIZE ) or gl. Enable( GL_RESCALE_NORMAL ) FB
Normal for Triangle plane n n ·(p - p 0 ) = 0 p 2 n = (p 2 - p 0 ) ×(p 1 - p 0 ) p normalize n n/ |n| p 0 Note that right-hand rule determines outward face p 1
Material Properties o Define the surface properties of a primitive o gl. Materialfv( face, property, value ); GL_DIFFUSE Base color GL_SPECULAR Highlight Color GL_AMBIENT Low-light Color GL_EMISSION Glow Color GL_SHININESS Surface Smoothness n separate materials for front and back
Light Properties o gl. Lightfv( light, property, value ); n light specifies which light o multiple lights, starting with GL_LIGHT 0 o gl. Get. Integerv( GL_MAX_LIGHTS, &n ); n properties o colors o position and type o attenuation
Light Sources (cont. ) o Light color properties n GL_AMBIENT n GL_DIFFUSE n GL_SPECULAR
Types of Lights o Open. GL supports two types of Lights n Local (Point) light sources n Infinite (Directional) light sources o Type of light controlled by w coordinate
Turning on the Lights o Flip each light’s switch gl. Enable( GL_LIGHTn ); o Turn on the power gl. Enable( GL_LIGHTING );
Light Material Tutorial
Controlling a Light’s Position o Modelview matrix affects a light’s position n Different effects based on when position is specified o eye coordinates o world coordinates o model coordinates n Push and pop matrices to uniquely control a light’s position
Light Position Tutorial
Advanced Lighting Features o Spotlights n localize lighting affects o GL_SPOT_DIRECTION o GL_SPOT_CUTOFF o GL_SPOT_EXPONENT
Spotlights o Use gl. Lightv to set n Direction GL_SPOT_DIRECTION n Cutoff GL_SPOT_CUTOFF n Attenuation GL_SPOT_EXPONENT o Proportional to cosaf -q f q
Advanced Lighting Features o Light attenuation n decrease light intensity with distance o GL_CONSTANT_ATTENUATION o GL_LINEAR_ATTENUATION o GL_QUADRATIC_ATTENUATION
Light Model Properties gl. Light. Modelfv( property, value ); o Enabling two sided lighting n GL_LIGHT_MODEL_TWO_SIDE o Global ambient color n GL_LIGHT_MODEL_AMBIENT o Local viewer mode n GL_LIGHT_MODEL_LOCAL_VIEWER o Separate specular color n GL_LIGHT_MODEL_COLOR_CONTROL
Front and Back Faces o The default is shade only front faces which works correct for convex objects o If we set two sided lighting, Open. GL will shaded both sides of a surface o Each side can have its own properties which are set by using GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in gl. Materialf back faces not visible back faces visible
Efficiency o Because material properties are part of the state, if we change materials for many surfaces, we can affect performance o We can make the code cleaner by defining a material structure and setting all materials during initialization typedef struct material. Struct { GLfloat ambient[4]; GLfloat diffuse[4]; GLfloat specular[4]; GLfloat shineness; } Material. Struct; o We can then select a material by a pointer
Tips for Better Lighting o Recall lighting computed only at vertices n model tessellation heavily affects lighting results o better results but more geometry to process o Use a single infinite light for fastest lighting n minimal computation per vertex
Steps in Open. GL shading 1. 2. 3. 4. Enable shading and select model Specify normals Specify material properties Specify lights
Transparency o Material properties are specified as RGBA values o The A value can be used to make the surface translucent o The default is that all surfaces are opaque regardless of A
Polygonal Shading o Shading calculations are done for each vertex n Vertex colors become vertex shades o By default, vertex colors are interpolated across the polygon n gl. Shade. Model(GL_SMOOTH); o If we use gl. Shade. Model(GL_FLAT); the color at the first vertex will determine the color of the whole polygon
Polygon Normals o Polygons have a single normal n Shades at the vertices as computed by the Phong model can be almost same n Identical for a distant viewer (default) or if there is no specular component o Consider model of sphere o Want different normals at each vertex even though this concept is not quite correct mathematically
Smooth Shading o We can set a new normal at each vertex o Easy for sphere model n If centered at origin n = p o Now smooth shading works o Note silhouette edge
- Slides: 26