Lecture 4 Advanced Computer Graphics (CS & SE 233.420)
Topics Covered Animation Matrices Viewing Lighting
Animating Interactive Programs Consider planet.c Want to animate rotating the Earth around the Sun Let year denote current rotation value 0 < year < 360 Global variable Display the Earth with the display function Suppose we want to increase year by a fixed amount whenever nothing else is happening
Animating with glutidlefunction First define the function void idle() { year+=2; if (year > 360) year-=360; glutpostredisplay(); } Next specify the callback glutidlefunction(idle);
Interactive Routine to Change Idle Use the mouse callback to change the idle function Define mouse callback glutmousefunc(mouse); Mouse function can be written as void mouse(int button, int state, int x, int y) { if(button==glut_left_button&state==glut_down) glutidlefunc(idle); f(button==glut_middle_button&state==glut_down) glutidlefunc(null); } Result Left mouse button starts Earth rotating Middle mouse button stop the rotation
Refresh Rate Want to refresh the screen sufficiently fast enough so the viewer cannot notice the clearing and redrawing of the screen To alleviate display flickering Refresh screen at appropriate refresh rate Most refresh rates are between 58Hz-85Hz Do not change the contents of the frame buffer
What Happens When You Change the Frame Buffer Contents? See undesirable artifacts of how we generate the display May only draw a part of the object May see different parts of objects on successive screens Moving object can appear distorted Solution Double buffering
Double Buffering Technique for the tricking the eye into seeing smooth animation of rendered scenes Color buffer is usually divided into two equal halves Front buffer and the back buffer Render in front buffer back buffer is displayed Swap buffers Render in back buffer front buffer is displayed Front Buffer 1 2 4 8 16 1 2 4 8 16 Display Back Buffer
Double Buffering Instead of one color buffer, we use two Front Buffer: one that is displayed but not written to Back Buffer: one that is written to but not displayed Program then requests a double buffer in main.c glutinitdisplaymode(gl_rgb GL_DOUBLE) At the end of the display callback buffers are swapped void mydisplay() { glclear() /* draw graphics here */ glutswapbuffers() }
Smoother Animation Routine gluttimerfunc(msec, value) Registers a timer callback to be triggered in a specified number of milliseconds The value parameter to the timer callback will be the value of the value parameter to gluttimerfunc. The number of milliseconds is a lower bound on the time before the callback is generated
What is a Matrix? A 2 dimensional array of numeric data where each row or column consist of a numeric values A way to describe n-dimensional transformations in one convenient package OpenGL uses 4x4 matrices
Identity Square matrix with 1 s along the diagonal 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Translate Used to position an object within 3D space If the translation is define by (x,y,z) OpenGL call gltranslate(x,y,z); 1 0 0 x 0 1 0 y 0 0 1 z 0 0 0 1
Scale Used to enlarge or shrink a 3D object If the scaling is define by (x,y,z) glscale(x,y,z) x 0 0 1 0 y 0 1 0 0 z 1 0 0 0 1
Rotate Used to rotate a 3D object Matrix used is axis specific X Axis 1 0 0 0 0 cos sin 0 0 -sin -cos 0 0 0 0 1 glrotate(x,0,0,0); Uses radians, not degrees
PUSH/POP Matrix Suppose you have 2 objects Want to translate one object without affecting the other Use glpushmatrix and glpopmatrix glpushmatrix() /* translate object */ glpopmatrix()
OpenGL and Matrices OpenGL has predefined matrices Modelview matrix Specifies location of camera Moves objects around the scene glpush(modelview); Pushes model view matrix onto the stack Think of this matrix as the world view
Viewing - Camera Analogy Set up the tripod and point the camera (view transform) Arrange the objects in the scene (model transform) Choose a lens and adjust zoom (projection transform) Choose photo print size (viewport transform)
3D Graphics is a Synthetic Camera Model camera viewing volume tripod model
Transformations and Camera Analogy Synthetic camera model is used Can synthesize lens OpenGL Viewing Volume Perspective pyramid shaped Human vision camera lens have cone-shaped viewing volumes Computer will see differently from natural viewpoints Especially around edges
Camera Analogy and Transformations Modeling transformations Moving the model Projection transformations Adjust the lens of the camera Viewing transformations Tripod define position and orientation of the viewing volume in the world Viewport transformations Enlarge or reduce the physical photograph
OpenGL and Transformations There are five transforms in OpenGL Vertices pass through: 1. Viewing Specifies location of camera 2. Modeling Moves objects around the scene 3. Projection Defines the viewing volume 4. Viewport Maps the 2D projection to the rendering window 5. Modelview Combination of viewing and modeling transforms Must be executed in a specific order.
Following the Pipeline Transformations Much of the work in the pipeline is in converting object representations from one coordinate system to another Synthetic World coordinates Camera coordinates Screen coordinates Every change of coordinates is equivalent to a matrix transformation Modelview matrix Converts object coordinates into eye coordinates Model transformations move the object View transformations move the eyepoint
Clipping Just as a real camera cannot see the whole world, the virtual camera can only see part of the world space Objects that are not within this volume are said to be clipped out of the scene
Projection Must carry out the process that combines the 3D viewer with the 3D objects to produce the 2D image Perspective projections: all projectors meet at the center of projection Parallel projection: projectors are parallel, center of projection is replaced by a direction of projection
Rasterization If an object is visible in the image, the appropriate pixels in the frame buffer must be assigned colors Vertices assembled into objects Effects of lights and materials must be determined Polygons filled with interior colors/shades Must have also determine which objects are in front (hidden surface removal)
Lighting Principles Lighting simulates how objects reflect light material composition of object light s color and position global lighting parameters ambient light two sided lighting available in both color index and RGBA mode
Turn on the Power gldisable(gl_lighting) Objects are colored according to glcolor state value for that object glenable(gl_lighting) Use lighting Need to specify more about the surface other than just color How shiny is it? Does glow in the dark? Does light scatter uniformly? Flip the light switch glenable(light0)
Lighting Deals with Two Parameters Lighting properties Parameters indexed to light numbers Upto 8 lights (LIGHT0-LIGHT7) Light positions are affected by the modelview matrix stack Material Properties State variables assigned to polygons Can be changes as you draw different polygons
How OpenGL Simulates Light Lighting is based on the Phong lighting model At each vertex in the primitive, a color is computed using primitive's material properties along with light settings The color for the vertex is computed by adding four computed colors for he final vetex color The four contributors to the vertex color are: Ambient Color of the object from all the undirected light in a scene Diffuse Base color of the object under current lighting Specular Contribution of the shiny highlights on the object Emission Contribution added in if the object emits light
Light Properties gllight(light, property, value) Used to set the parameters for a light OpenGL has at least 8 lights GL_LIGHT 0,..., GL_LIGHT (GL_MAX_LIGHTS-1) Characteristics (properties) of lights that can change include Colors Position and type Attenuation
OpenGL Light Properties Light color properties GL_AMBIENT GL_DIFFUSE GL_SPECULAR
Setting Ambient Light Light that comes from all directions equally and is scattered in all directions Set up ambience GLfloat ambient = {0.2,0.2,0.2,1}; Enable ambient light for a specific light gllightfv(gl_light0, GL_AMBIENT, ambient);
Types of Lights OpenGL supports two types of Lights Local (Point) light sources Infinite (Directional) light sources Type of light controlled by w coordinate w = 0 w 0 Infinite Light directed along Local Light positioned at ( x y z) ( x y z ) w w w
Controlling a Light s Position Modelview matrix affects a light s position Different effects based on when position is specified eye coordinates world coordinates model coordinates Push and pop matrices to uniquely control a light s position
Surface Normals Surface normals tell OpenGL how the object reflects light around a vertex Imagine there is a small mirror at the vertex, the lighting normal describes how the mirror is oriented, and consequently how the light is reflected Normals define how a surface reflects light
Lighting Normals glnormal*() Sets the current normal, which is used in the lighting computation for all vertices until a new normal is provided Normals should be normalized to unit length for correct lighting glscale*() affects normals as well as vertices, which can change the normal's length and cause it to no longer be normalized
Lighting Normals OpenGL routines to automatically normalize normals glenable(gl_normalize) Handles all normalization situations Requires square root computation Potentially lower performance glenable(gl_rescale_normal) Special mode for when normals are uniformally scaled
Material Properties Describe the color and surface properties of a material (dull, shiney, etc) OpenGL supports material properties for both the front and back of objects Can be set for each face separately by specifying either GL_FRONT, GL_BACK or GL_FRONT_AND BACK for both faces
OpenGL Material Properties GL_DIFFUSE Base color of object GL_SPECULAR Color of highlights of an object GL_AMBIENT Color of an object when not directly illuminated GL_EMISSION Color emitted from the object GL_SHININESS Concentration of highlights on objects
Light Material Tutorial
Light Position Tutorial
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 f i = k c + k l 1 d + k q d 2
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
References Open GL web page www.opengl.org http://www.opengl.org/resources/faq/technical http://www.newcyber3d.com/asia/selfstudy/tips/selection.htm http://www.lighthouse3d.com/opengl/displaylists http://www.mfcogl.com/multiview%20animation%20mfc_opengl.htm www.cs.colorado.edu/~janem/cs4229/lec6_ch3-4.pdf http://www.codecolony.de/docs/camera.htm http://mathcs.holycross.edu/~croyden/graphics/assignments/sol5/soluti on5.html www.cs.stevens-tech.edu/~quynh/courses/cs537- fa03/projection_gl.ppt http://gamecode.tripod.com/tut/tut03.htm http://www.basic4gl.net/introduction%20to%20opengl%20part%202.ht m http://www.falloutsoftware.com/tutorials/gl/gl0.htm