Computer Graphics -...
Transcript of Computer Graphics -...
![Page 1: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/1.jpg)
Computer Graphics
Bing-Yu ChenNational Taiwan University
![Page 2: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/2.jpg)
Introduction to OpenGLGeneral OpenGL IntroductionAn Example OpenGL ProgramDrawing with OpenGLTransformationsAnimation and Depth BufferingLightingEvaluation and NURBSTexture MappingAdvanced OpenGL TopicsImaging modified from
Dave Shreiner, Ed Angel, and Vicki Shreiner.An Interactive Introduction to OpenGL Programming.ACM SIGGRAPH 2001 Conference Course Notes #54.
& ACM SIGGRAPH 2004 Conference Course Notes #29.
![Page 3: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/3.jpg)
Lighting Principles
Lighting simulates how objects reflect lightmaterial composition of objectlight’s color and positionglobal lighting parameters
ambient lighttwo sided lighting
available in both color indexand RGBA mode
![Page 4: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/4.jpg)
How OpenGL Simulates Lights
Phong lighting modelComputed at vertices
Lighting contributorsSurface material propertiesLight propertiesLighting model properties
![Page 5: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/5.jpg)
SurfaceNormals
Normals define how a surface reflects lightglNormal3f( x, y, z )
Current normal is used to compute vertex’s colorUse unit normals for proper lighting
scaling affects a normal’s lengthglEnable( GL_NORMALIZE )
orglEnable( GL_RESCALE_NORMAL )
CPUCPU DLDL
Poly.Poly. PerVertex
PerVertex
RasterRaster FragFrag FBFB
PixelPixelTextureTexture
![Page 6: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/6.jpg)
Normal for Triangle
p0
p1
p2
nplane n ·(p - p0 ) = 0
n = (p2 - p0 ) ×(p1 - p0 )
normalize n ← n/ |n|p
Note that right-hand rule determines outward face
![Page 7: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/7.jpg)
Material PropertiesDefine the surface properties of a primitiveglMaterialfv( face, property, value );
separate materials for front and back
GL_DIFFUSE Base color
GL_SPECULAR Highlight Color
GL_AMBIENT Low-light Color
GL_EMISSION Glow Color
GL_SHININESS Surface Smoothness
![Page 8: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/8.jpg)
Light PropertiesglLightfv( light, property, value );
light specifies which lightmultiple lights, starting with GL_LIGHT0glGetIntegerv( GL_MAX_LIGHTS, &n );
propertiescolorsposition and typeattenuation
![Page 9: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/9.jpg)
Light Sources (cont.)
Light color propertiesGL_AMBIENT GL_DIFFUSEGL_SPECULAR
![Page 10: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/10.jpg)
Types of Lights
OpenGL supports two types of LightsLocal (Point) light sourcesInfinite (Directional) light sources
Type of light controlled by w coordinate( )
( )wz
wy
wxw
zyxwat positionedLight Local0
along directedLight Infinite0≠=
![Page 11: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/11.jpg)
Turning on the Lights
Flip each light’s switchglEnable( GL_LIGHTn );
Turn on the powerglEnable( GL_LIGHTING );
![Page 12: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/12.jpg)
Light Material Tutorial
![Page 13: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/13.jpg)
Controlling a Light’s Position
Modelview matrix affects a light’s positionDifferent effects based on when position is specified
eye coordinatesworld coordinatesmodel coordinates
Push and pop matrices to uniquely control a light’s position
![Page 14: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/14.jpg)
Light Position Tutorial
![Page 15: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/15.jpg)
Advanced Lighting Features
Spotlightslocalize lighting affects
GL_SPOT_DIRECTIONGL_SPOT_CUTOFFGL_SPOT_EXPONENT
![Page 16: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/16.jpg)
Spotlights
Use glLightv to set Direction GL_SPOT_DIRECTIONCutoff GL_SPOT_CUTOFF
AttenuationGL_SPOT_EXPONENT
Proportional to cosαφ
θ−θ φ
![Page 17: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/17.jpg)
Advanced Lighting Features
Light attenuationdecrease light intensity with distance
GL_CONSTANT_ATTENUATIONGL_LINEAR_ATTENUATIONGL_QUADRATIC_ATTENUATION
2
1dkdkk
fqlc
i ++=
![Page 18: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/18.jpg)
Light Model PropertiesglLightModelfv( property, value );Enabling two sided lighting
GL_LIGHT_MODEL_TWO_SIDE
Global ambient colorGL_LIGHT_MODEL_AMBIENT
Local viewer modeGL_LIGHT_MODEL_LOCAL_VIEWER
Separate specular colorGL_LIGHT_MODEL_COLOR_CONTROL
![Page 19: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/19.jpg)
Front and Back Faces
The default is shade only front faces which works correct for convex objectsIf we set two sided lighting, OpenGL will shaded both sides of a surfaceEach side can have its own properties which are set by using GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in glMaterialf
back faces not visible back faces visible
![Page 20: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/20.jpg)
EfficiencyBecause material properties are part of the state, if we change materials for many surfaces, we can affect performanceWe can make the code cleaner by defining a material structure and setting all materials during initialization
We can then select a material by a pointer
typedef struct materialStruct {GLfloat ambient[4];GLfloat diffuse[4];GLfloat specular[4];GLfloat shineness;
} MaterialStruct;
![Page 21: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/21.jpg)
Tips for Better Lighting
Recall lighting computed only at verticesmodel tessellation heavily affects lighting results
better results but more geometry to process
Use a single infinite light for fastest lighting
minimal computation per vertex
![Page 22: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/22.jpg)
Steps in OpenGL shading
1. Enable shading and select model2. Specify normals3. Specify material properties4. Specify lights
![Page 23: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/23.jpg)
Transparency
Material properties are specified as RGBA valuesThe A value can be used to make the surface translucentThe default is that all surfaces are opaque regardless of A
![Page 24: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/24.jpg)
Polygonal Shading
Shading calculations are done for each vertex
Vertex colors become vertex shadesBy default, vertex colors are interpolated across the polygon
glShadeModel(GL_SMOOTH);
If we use glShadeModel(GL_FLAT); the color at the first vertex will determine the color of the whole polygon
![Page 25: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/25.jpg)
Polygon NormalsPolygons have a single normal
Shades at the vertices as computed by the Phong model can be almost same Identical for a distant viewer (default) or if there is no specular component
Consider model of sphereWant different normals ateach vertex even though thisconcept is not quite correctmathematically
![Page 26: Computer Graphics - graphics.im.ntu.edu.twgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08_ogl_05light.pdf · Computer Graphics Bing-Yu Chen National Taiwan University . Introduction](https://reader030.fdocuments.us/reader030/viewer/2022041210/5dd08816d6be591ccb617140/html5/thumbnails/26.jpg)
Smooth Shading
We can set a new normal at each vertexEasy for sphere model
If centered at origin n = p
Now smooth shading worksNote silhouette edge