190 likes | 326 Vues
This lecture series by David Luebke introduces the fundamental concepts of computer graphics, specifically focusing on 3D transformations using OpenGL. Key topics covered include translating, rotating, and scaling objects in 3D space, as well as matrix manipulation techniques that facilitate these transformations. Students will learn about specifying colors and normals, using modeling matrices effectively, and understanding common pitfalls in OpenGL. Practical examples illustrate how to manipulate objects and achieve desired visual effects in graphics programming.
E N D
Transformations CS 445/645Introduction to Computer Graphics David Luebke, Spring 2003
Admin • Call roll • Assn 1 • News flash: Assn 1 not necessarily a gimme • Turn-in instructions by email soon • Problems with lab? David Luebke 210/15/2014
Recap: OpenGL: Modeling Transforms • OpenGL provides several commands for performing modeling transforms: • glTranslate{fd}(x, y, z) • Creates a matrix T that transforms an object by translating (moving) it by the specified x, y, and z values • glRotate{fd}(angle, x, y, z) • Creates a matrix R that transforms an object by rotating it counterclockwise angle degrees about the vector {x, y, z} • glScale{fd}(x, y, z) • Creates a matrix S that scales an object by the specified factors in the x, y, and z directions David Luebke 310/15/2014
Recap:OpenGL: Matrix Manipulation • Each of these postmultiplies the current matrix • E.g., if current matrix is C, then C=CS • The current matrix is either the modelview matrix or the projection matrix (also a texture matrix, won’t discuss) • Set these with glMatrixMode(), e.g.: glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_PROJECTION); • WARNING: common mistake ahead! • Be sure that you are in GL_MODELVIEW mode before making modeling or viewing calls! • Ugly mistake because it can appear to work, at least for a while… David Luebke 410/15/2014
Recap:OpenGL: Matrix Manipulation • More matrix manipulation calls • To replace the current matrix with an identity matrix: glLoadIdentity() • Postmultiply the current matrix with an arbitrary matrix: glMultMatrix{fd}(float/double m[16]) • Copy the current matrix and push it onto a stack: glPushMatrix() • Discard the current matrix and replace it with whatever’s on top of the stack: glPopMatrix() • Note that there are matrix stacks for both modelview and projection modes David Luebke 510/15/2014
OpenGL: Specifying Color • Can specify other properties such as color • To produce a single aqua-colored triangle: glColor3f(0.1, 0.5, 1.0); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); • To produce a Gouraud-shaded triangle: glColor3f(1, 0, 0); glVertex3fv(v0); glColor3f(0, 1, 0); glVertex3fv(v1); glColor3f(0, 0, 1); glVertex3fv(v2); • In OpenGL, colors can also have a fourth component (opacity) • Generally want = 1.0 (opaque); David Luebke 610/15/2014
OpenGL: Specifying Normals • Calling glColor() sets the color for all vertices following, until the next call to glColor() • Calling glNormal() sets the normal vector for the following vertices, till next glNormal() • So flat-shaded lighting requires: glNormal3f(Nx, Ny, Nz); glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2); • While smooth shading requires: glNormal3f(N0x, N0y, N0z); glVertex3fv(v0); glNormal3f(N1x, N1y, N1z); glVertex3fv(v1); glNormal3f(N2x, N2y, N2z); glVertex3fv(v2); • (Of course, lighting requires additional setup…) David Luebke 710/15/2014
More OpenGL • Other things you’ll need to know: • To clear the screen: glClearColor(r, g, b, a); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); • Nate Robins has an excellent set of OpenGL tutorials that help illustrate many of these concepts & functions: http://www.cs.utah.edu/~narobins/opengl.html Also http://xmission.com/~nate/tutors.html • Next: the math and concepts underlying the transformation calls David Luebke 810/15/2014
Translations • For convenience we usually describe objects in relation to their own coordinate system • We can translate or move points to a new position by adding offsets to their coordinates: • Note that this translates all points uniformly David Luebke 910/15/2014
Scaling • Scaling a coordinate means multiplying each of its components by a scalar • Uniform scaling means this scalar is the same for all components: 2 David Luebke 1010/15/2014
Scaling • Non-uniform scaling: different scalars per component: • How can we represent this in matrix form? X 2,Y 0.5 David Luebke 1110/15/2014
Scaling • Scaling operation: • Or, in matrix form: scaling matrix David Luebke 1210/15/2014
(x’, y’) (x, y) 2-D Rotation x’ = x cos() - y sin() y’ = x sin() + y cos() (Draw it) David Luebke 1310/15/2014
2-D Rotation • This is easy to capture in matrix form: • 3-D is more complicated • Need to specify an axis of rotation • Simple cases: rotation about X, Y, Z axes David Luebke 1410/15/2014
3-D Rotation • What does the 3-D rotation matrix look like for a rotation about the Z-axis? • Build it coordinate-by-coordinate David Luebke 1510/15/2014
3-D Rotation • What does the 3-D rotation matrix look like for a rotation about the Y-axis? • Build it coordinate-by-coordinate David Luebke 1610/15/2014
3-D Rotation • What does the 3-D rotation matrix look like for a rotation about the X-axis? • Build it coordinate-by-coordinate David Luebke 1710/15/2014
3-D Rotation • General rotations in 3-D require rotating about an arbitrary axis of rotation • Deriving the rotation matrix for such a rotation directly is a good exercise in linear algebra • Standard approach: express general rotation as composition of canonical rotations • Rotations about X, Y, Z David Luebke 1810/15/2014
Composing Canonical Rotations • Goal: rotate about arbitrary vector A by • Idea: we know how to rotate about X,Y,Z So, rotate about Y by until A lies in the YZ plane Then rotate about X by until A coincides with +Z Then rotate about Z by Then reverse the rotation about X (by -) Then reverse the rotation about Y (by -) David Luebke 1910/15/2014