Download presentation
Presentation is loading. Please wait.
Published byCurtis Fielder Modified over 9 years ago
2
2 COEN 290 - Computer Graphics I Evening’s Goals n Discuss the fundamentals of lighting in computer graphics n Analyze OpenGL’s lighting model n Show basic geometric rasterization and clipping algorithms
3
3 COEN 290 - Computer Graphics I Simulating Lighting In CGI n Lighting is a key component in computer graphics n Provides cues on: shape and smoothness of objects distance from lights to objects objects orientation in the scene n Most importantly, helps CG images look more realistic
4
4 COEN 290 - Computer Graphics I Lighting Models n Many different models exist for simulating lighting reflections we’ll be concentrating on the Phong lighting model n Most models break lighting into constituent parts ambient reflections diffuse reflections specular highlights
5
5 COEN 290 - Computer Graphics I Lighting Model Components n Material Properties used to describe an objects reflected colors n Surface Normals n Light Properties used to describe a lights color emissions n Light Model Properties “global” lighting parameters
6
6 COEN 290 - Computer Graphics I Physics of Reflections
7
7 COEN 290 - Computer Graphics I Ambient Reflections n Color of an object when not directly illuminated light source not determinable n Think about walking into a room with the curtains closed and lights off
8
8 COEN 290 - Computer Graphics I Diffuse Reflections n Color of an object when directly illuminated often referred to as base color
9
9 COEN 290 - Computer Graphics I Specular Reflections n Highlight color of an object n Shininess exponent used to shape highlight
10
10 COEN 290 - Computer Graphics I Phong Lighting Model n Using surface normal n OpenGL’s lighting model based on Phong’s
11
11 COEN 290 - Computer Graphics I OpenGL Material Properties n GL_AMBIENT n GL_DIFFUSE n GL_SPECULAR n GL_SHININESS n GL_EMISSION
12
12 COEN 290 - Computer Graphics I Setting Material Properties glMaterial[fd]v ( face, prop, params ); face represents which side of a polygon GL_FRONT GL_BACK GL_FRONT_AND_BACK polygon facedness controlled by glFrontFace()
13
13 COEN 290 - Computer Graphics I OpenGL Lights n OpenGL supports at least eight simultaneous lights GL_LIGHT0 - GL_LIGHT[n-1] n Inquire number of lights using glGetIntegerv( GL_MAX_LIGHTS, &n ); glLight[fd]v( light, property, params );
14
14 COEN 290 - Computer Graphics I OpenGL Light Color Properties n GL_AMBIENT n GL_DIFFUSE n GL_SPECULAR
15
15 COEN 290 - Computer Graphics I Types of Lights n Point ( also called Local ) n Directional ( also called Infinite ) n Light’s type determined by its w value w = 0 infinite light w = 1 local light
16
16 COEN 290 - Computer Graphics I Positioning Lights n Light’s positions are modified by ModelView matrix n Three variations fixed in space fixed in a scene total freedom
17
17 COEN 290 - Computer Graphics I Setting up a Fixed Light n Light positioned in eye coordinates identity matrix on ModelView stack n Special case - creating a headlamp imagine wearing a miner’s helmet with a light pass (0 0 0 w) for light’s position GLfloat pos[] = { 0.0, 0.0, 0.0, 1.0 }; glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glLightfv( GL_LIGHT0, GL_POSITION, pos );
18
18 COEN 290 - Computer Graphics I Positioning a Light in a Scene n Light positioned in world coordinates viewing transform only on ModelView stack GLfloat pos[] = { 1.0, 2.0, 3.0, 0.0 }; glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( ex, ey, ez, lx, ly, lz, ux, uy, uz ); glLightfv( GL_LIGHT0, GL_POSITION, pos );
19
19 COEN 290 - Computer Graphics I Arbitrary Light Positioning n Any modeling and viewing transforms on ModelView stack Transform light separately by isolating with glPushMatrix() and glPopMatrix() n Unique motion variable allows light to animate independently of other objects
20
20 COEN 290 - Computer Graphics I Arbitrary Light Positioning (cont. ) GLfloat pos[] = { 0.0, 0.0, 0.0, 1.0 }; glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( ex, ey, ez, lx, ly, lz, ux, uy, uz ); glPushMatrix(); glRotatef( angle, axis.x, axis.y, axis.z ); glTranslatef( x, y, z ); glLightfv( GL_LIGHT0, GL_POSITION, pos ); glPopMatrix();
21
21 COEN 290 - Computer Graphics I Light Attenuation n Physical light’s brightness diminishes as the square of the distance n Simulate this in OpenGL GL_CONSTANT_ATTENUATION GL_LINEAR_ATTENUATION GL_QUADRATIC_ATTENUATION
22
22 COEN 290 - Computer Graphics I Everything Else … n “Global” lighting parameters are held in the light model glLightModel[fd]v( property, param ); GL_LIGHT_MODEL_AMBIENT GL_LIGHT_MODEL_TWO_SIDE GL_LIGHT_MODEL_LOCAL_VIEWER
23
23 COEN 290 - Computer Graphics I Turning on the Lights n To turn on lighting glEnable( GL_LIGHTING ); turns on the “power”, but not any lights n To turn on an individual light glEnable( GL_LIGHTn );
24
24 COEN 290 - Computer Graphics I OpenGL Lighting Model n At each vertex For each color component
25
25 COEN 290 - Computer Graphics I Computing Surface Normals n Lighting needs to know how to reflect light off the surface n Provide normals per face - flat shading vertex - Gouraud shading pixel - Phong shading –OpenGL does not support Phong natively
26
26 COEN 290 - Computer Graphics I Face Normals n Same normal for all vertices in a primitive results in flat shading for primitive glNormal3f( nx, ny, nz ); glBegin( GL_TRIANGLES ); glVertex3fv( v1 ); glVetrex3fv( v2 ); glVertex3fv( v3 ); glEnd();
27
27 COEN 290 - Computer Graphics I Computing Face Normals ( Polygons ) n We’re using only planar polygons n Can easily compute the normal to a plane use a cross product
28
28 COEN 290 - Computer Graphics I Computing Face Normals ( Algebraic ) n For algebraic surfaces, compute where
29
29 COEN 290 - Computer Graphics I Vertex Normals n Each vertex has its own normal primitive is Gouraud shaded based on computed colors glBegin( GL_TRIANGLES ); glNormal3fv( n1 ); glVertex3fv( v1 ); glNormal3fv( n2 ); glVetrex3fv( v2 ); glNormal3fv( n3 ); glVertex3fv( v3 ); glEnd();
30
30 COEN 290 - Computer Graphics I Computing Vertex Normals ( Algebraic ) n For algebraic surfaces, compute
31
31 COEN 290 - Computer Graphics I Computing Vertex Normals ( Polygons ) n Need two things face normals for all polygons know which polygons share a vertex
32
32 COEN 290 - Computer Graphics I Sending Normals to OpenGL glNormal3f( x, y, z ); Use between glBegin() / glEnd() Use similar to glColor*()
33
33 COEN 290 - Computer Graphics I Normals and Scale Transforms n Normals must be normalized non-unit length skews colors n Scales affect normal length rotates and translates do not glEnable( GL_NORMALIZE );
34
34 COEN 290 - Computer Graphics I Why? n Lighting computations are really done in eye coordinates this is why there are the projection and modelview matrix stacks n Lighting normals transformed by the inverse transpose of the ModelView matrix
35
35 COEN 290 - Computer Graphics I Rasterizing Points n Rendering a point should set one pixel n Which pixel should we set? n Use the following macro #define ROUND(x) ((int)(x + 0.5))
36
36 COEN 290 - Computer Graphics I Where should you draw n viewport is the part of the window where you can render n Need to clip objects to the viewport
37
37 COEN 290 - Computer Graphics I Clipping n Determination of visible primitives n Can clip to an arbitrary shape we’ll only clip to rectangles n Various clip boundaries window viewport scissor box
38
38 COEN 290 - Computer Graphics I Point Clipping n Simple point inside rectangle test xMin xMax yMin yMax (x, y)
39
39 COEN 290 - Computer Graphics I Mathematics of Lines n Point-Intercept form of a line x y b
40
40 COEN 290 - Computer Graphics I Digital Differential Analyzer ( DDA ) n Determine which pixels based on line’s equation slope determines which variable to iterate –for all of our examples, assume y = y1; for( x = x1; x <= x2; ++x ) { setPixel( x, ROUND(y) ); y += m; }
41
41 COEN 290 - Computer Graphics I Adding Color n Along with interpolating coordinates, we can interpolate colors.
42
42 COEN 290 - Computer Graphics I Digital Differential Analyzer ( cont. ) n Advantages simple to implement n Disadvantages requires floating point and type conversion –potentially slow if not in hardware –accumulation of round-off error
43
43 COEN 290 - Computer Graphics I Explicit Form of a Line x y C Another way of saying the same thing
44
44 COEN 290 - Computer Graphics I Why the Explicit Form is your Friend n Creates a Binary Space Partition tells which side of the line your on x y + -
45
45 COEN 290 - Computer Graphics I How does this help render lines? n We can use the explicit form to determine which pixel is closest to the line
46
46 COEN 290 - Computer Graphics I Midpoint Algorithm n Plot first point n Determine which side of the line the midpoint is on evaluate n Choose new pixel based on sign of result n Update
47
47 COEN 290 - Computer Graphics I We can do a little better n Keep a running sum of the error initialize n Choose next pixel based on sign of the error n Incrementally update error based on pixel choice
48
48 COEN 290 - Computer Graphics I Bresenham’s Algorithm dx = x2 - x1; dy = y2 - y1; x = ROUND(x1); y = ROUND(y1); d = 2*dy - dx; do { setPixel( x, y ); if ( d <= 0 ) // Choose d += 2*dy; else { // Choose y++; d += 2*(dy - dx); } } while( ++x < x2 );
49
49 COEN 290 - Computer Graphics I Cohen-Sutherland Line Clipping n Clip to rectangular region n Partition space into regions keep a bit-code to indicate which region a vertex is in 0001 0101 10011000 0000 0100 0010 1010 0110
50
50 COEN 290 - Computer Graphics I Cohen-Sutherland Line Clipping (cont.) n Quickly determine if a line is outside the viewport if (mask v1 & mask v2 ) return False; // Don’t render n Or inside if (!(mask v1 | mask v2 )) return True; // render! No clipping needed
51
51 COEN 290 - Computer Graphics I n If quick tests fail … need to clip vertices n Render horizontal span between each edge’s pixel Cohen-Sutherland Line Clipping (cont.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.