3D游戏编程OpenGL光照.pdf
OpenGL:Lighting Yanci Zhang Game Programming IIGame Programming II Basic concepts of lighting Lighting in OpenGL Light properties Material properties Game Programming IIGame Programming II Outline Rendering result from last lesson Does it look like a 3D object?Certainly not!What are the problems?Lighting!Game Programming IIGame Programming II What We Have Now A Lit and an Unlit Sphere It Does Make a Difference!Objects do not have 3D appearance without lighting Lighting simulates how objects reflect light Two different lighting models in graphics Global illumination:high realistic but high cost Local illumination:low realistic but low cost Game Programming IIGame Programming II Lighting In the real world Light might be reflected multiple times Light might be occluded Global illumination tries to simulate these phenomena Game Programming IIGame Programming II Global Illumination The color of any point on surfaces is only influenced by Its own properties(position,normal,material)Light attributes The color is NOT influenced by any other points in scene Low computation cost but cannot produce shadows,multiple reflections Game Programming IIGame Programming II Local Illumination Local illumination Vertex-based lighting Lighting is a state which can be switched on or off OpenGL divides lighting into three parts:Material properties Light properties Global lighting parameters ambient light two sided lighting Game Programming IIGame Programming II Lighting in OpenGL 1/3 Game Programming IIGame Programming II Lighting in OpenGL 2/3 Light is modeled in four categories:Emission light(always ignored!)Ambient light View-independent Simulate light in the environment Diffuse light View-independent Simulate incident ray is reflected at many angles Specular light View-dependent Simulate incident ray from a single incoming direction is reflected into a single outgoing direction Game Programming IIGame Programming II Lighting in OpenGL 3/3 Formula:ambient +diffuse +specular =shading result Game Programming IIGame Programming II Ambient Lighting Approximate low level of light normally presenting everywhere in scene Scattered by many objects before reaching eye Constant terms Applies equally to all points on object Game Programming IIGame Programming II Diffuse Lighting Approximate light scattered by objects with rough surfaces Intensity depends on the angle between light source and surface normal Game Programming IIGame Programming II Specular Lighting Approximate light reflected by“shiny”objects with smooth surfaces Intensity depends on the angle between viewer and direction of reflected ray Game Programming IIGame Programming II The Equation I=Iambient+Idiffuse+Ispecular =kaIa+kdId(NL)+ksIs(RV)n illumination of surface ambient light intensity of the ambient light intensity of the diffuse light diffuse reflection of incident light specular reflection of incident light intensity of the specular light surface normal vector from point to light vector from point to eye reflected ray Game Programming IIGame Programming II Color in OpenGL Two color modes in OpenGL RGBA:Specifies color using either a 3 or 4 tuple value to represent r,g,b or r,g,b,a values glColor3/4b,s,i,f,d,ub,us,ui()Color index mode Color acts as a typical OpenGL state variable,so the color value is retain until another call to glColor Game Programming IIGame Programming II RGBA Mode Any color is represented by a combination of Red,Green,Blue For computer monitor,each component is represented by 8 bits at most Support 256*256*256=16,777,216 different colors White(255,255,255),black(0,0,0),red(255,0,0)R,G,B can also be represented by 0,1 float in OpenGL Value exceeds 0,1 will be clamped White(1.0,1.0,1.0),black(0,0,0),red(1.0,0,0)Game Programming IIGame Programming II OpenGL Light Source 1/3 Assumptions Light travels in a straight line Ideal point light source Modeled by position+color Can take into account decay with respect to distance Ambient light General brightness A rough model for inter-surface reflections from all light sources Game Programming IIGame Programming II OpenGL Light Source 2/3 Must remember to call glEnable(GL_LIGHTING)to turn on lighting Multiple light sources are supported At least eight light sources are supported in OpenGL Each light source has its own status glEnable(GL_LIGHTi)Game Programming IIGame Programming II OpenGL Light Source 3/3 To enable lights from a single source(Light0)glEnable(GL_LIGHTING)glEnable(GL_LIGHTi)Defaults Light 0:white light(1.0,1.0,1.0,1.0)for diffuse and specular components Other lights:black(0.0,0,0,0,0,1.0)Position:(0.0,0.0,1.0,0.0);w=0.0 means a direction,not a point IMPORTANT:Once lighting is enabled,colors assigned by glColor*()are no longer used Game Programming IIGame Programming II Configure Light Source Define attributes of light source by glLight(GLenum light,GLenum pname,TYPE param light:light ID.Identified by symbolic names GL_LIGHTi and at least eight light sources are supported pname:name of attributes param:value of attributes Game Programming IIGame Programming II Normals When specifying polygon vertices,the normal vectors to each vertex must be supplied void glNormal3*(TYPE dx,TYPE dy,TYPE dz)Lighting calculations require unitary normals To enable automatic normalization,use:glEnable(GL_NORMALIZE);2D Normal Normal Tangent Tangent Plane 3D Game Programming IIGame Programming II Normal for Triangle Plane:=Normal vector:=Note that right-hand rule determines outward face p0 p1 p2 n p Game Programming IIGame Programming II Specifying Normal Vector Example glBegin(GL_TRIANGLES);glNormal3f(0.0,0.0,1.0);glVertex3f(0.0,0.0,0.0);glNormal3f(0.0,0.0,1.0);glVertex3f(10.0,0.0,0.0);glNormal3f(0.0,0.0,1.0);glVertex3f(0.0,10.0,0.0);glEnd();Modelview matrix affects a lights position Different effects based on when position is specified eye coordinates world coordinates model coordinates Push and pop matrices to uniquely control a lights position Game Programming IIGame Programming II Controlling Lights Position Game Programming IIGame Programming II Light Intensity Ambient light:GL_AMBIENT Diffuse light:GL_DIFFUSE Specular light:GL_SPECULAR GLfloat position0=1.0,1.0,1.0,0.0;GLfloat diffuse0=1.0,0.0,0.0,1.0;/Id term-Red GLfloat specular0=1.0,1.0,1.0,1.0;/Is term-White GLfloat ambient0=0.1,0.1,0.1,1.0;/Ia term-Gray glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glLightfv(GL_LIGHT0,GL_POSITION,position0);glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse0);glLightfv(GL_LIGHT0,GL_SPECULAR,specular0);glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0);Game Programming IIGame Programming II Point Light A light source originating from a zero-volume point in the scene Casts light in all directions /Light position GLfloat light_position=50.0,100.0,-50.0,1.0;/Apply the light position glLightfv(GL_LIGHT0,GL_POSITION,light_position);Position For Positions this should be 1.0 Game Programming IIGame Programming II Directional Light A light infinitely far away from the drawn scene Used most often for emulating sunlight /Light direction down the negative x axis GLfloat light_direction=-1.0,0.0,0.0,0.0;/Apply the light direction glLightfv(GL_LIGHT0,GL_POSITION,light_direction);Direction-should be normalized-For Directions this should be 0.0 Game Programming IIGame Programming II Spot Light Spot light is a close light source with direction,angle and exponential drop off void glLight*(GLenum light,GLenum param,TYPE value);void glLight*v(GLenum light,GLenum param,TYPE*value);Param:GL_SPOT_DIRECTION:direction of light is focused on GL_SPOT_CUTOFF(default 180o):angle that defines light cone GL_SPOT_EXPONENT(default 0):concentration of the light Game Programming IIGame Programming II Attenuation of Light Real light intensity becomes weaker over distance Attenuation with distance generates a softer and more realistic image:d is the distance computed by OpenGL a=GL_CONSTANT_ATTENUATION(default 1.0)b=GL_LINEAR_ATTENUATION(default 0.0)c=GL_QUADRATIC_ATTENUATION(default 0.0)Default is not attenuation Affects diffuse component Game Programming IIGame Programming II Material You can only see the effect of light source when light reflects off objects surface Material describes the amount of light reflected by surface Material defines the reflection ratio for ambient,diffuse,specular light independently Material defines the reflection ratio for R,G,B components of light source independently Material can be specified for front and back faces Game Programming IIGame Programming II RGB in Light Source and Material Both light source and material use RGB mode Light:RGB represents the intensity (0.5,0.4,0):the intensity of red,green and blue light is 0.5,0.4,0 respectively Material RGB represents reflection ratio (0.5,0.4,0):50%,40%,0%of incident red,green and blue light is reflected respectively If light is(0.5,0.4,0)and material is(0.8,0,0.2),then the reflected light we see is(0.4,0,0)Game Programming IIGame Programming II Specifying a Material 1/2 Main functions to set scalar or vector parameters for OpenGL faces:void glMaterial*(GLenum face,GLenum param,TYPE value);void glMaterial*v(GLenum face,GLenum param,TYPE*value);face:GL_FRONT,GL_BACK,GL_FRONT_AND_BACK Game Programming IIGame Programming II Specifying a Material 2/2 param:GL_AMBIENT /ka term GL_DIFFUSE /kd term GL_SPECULAR /ks term GL_SHININESS /exponent n GL_AMBIENT_AND_DIFFUSE /ka=kd Game Programming IIGame Programming II Front and Back Face Each polygon face has two sides:inside or backside OpenGL has no notion of inside or backside,it can only distinguish between front and back face A face is a front face if its vertices are listed in CounterClockWise(CCW)order as seen by the eye Game Programming IIGame Programming II Specifying a Material:Example/Define GLfloat arrays to hold material data GLfloat mat_ambient=0.3,0.3,0.3,1.0;GLfloat mat_diffuse=0.8,0.8,0.8,1.0;GLfloat mat_specular=1.0,1.0,1.0,1.0;/Shininess will be an array of length 1 GLfloat mat_shininess=50.0;/Apply the material properties to the front facing glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);Game Programming IIGame Programming II Shading Model in OpenGL 1/3 Flat shading:glShadeModel(GL_FLAT)Selects the color of just one vertex and assigns it to all fragments generated by rasterizing a single primitive.Game Programming IIGame Programming II Shading Model in OpenGL 2/3 Smooth shading:glShadeModel(GL_SMOOTH)Default shading model in OpenGL Colors of vertices are interpolated as the primitive is rasterized Game Programming IIGame Programming II Shading Model in OpenGL 3/3 GL_FLAT(middle figure)Low computation cost Bad for smooth surface GL_SMOOTH(right figure)Need more computation Better result Game Programming IIGame Programming II 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 Game Programming IIGame Programming II DEMO Demo program illustrates Render simple geometries by GLUT GL_FLAT and GL_SMOOTH Configure light source Configure material The interaction between light source and material The affection of GL_SHININESS on specular light The relationship between shading results and geometric complexity Specify light sources position in different coordinate system Game Programming IIGame Programming II Assignment Render a scene including three teapots and three light sources Teapots Each teapot has different material Each teapot can be rotated by mouse independently(when one teapot is rotated by mouse,the other two teapots remain static)You can use keyboard to select the current active teapot Game Programming IIGame Programming II Assignment Light sources(turn on them simultaneously)Each light source has different diffuse and specular component Light source 1:Defined in world coordinate system,rotating around the three teapots Light source 2:Defined in camera coordinate system Light source 3:Defined in object coordinate system of teapot 1,it has to rotate with teapot 1 when you rotate teapot 1 with mouse Deadline:2011-10-30 Please submit your assignment to teach assistants by email