400 likes | 614 Views
3-D Transformations. Kurt Akeley CS248 Lecture 8 18 October 2007 http://graphics.stanford.edu/courses/cs248-07/. Overview. Tuesday 2-D transformation Composition of 2-D transformations Line equations, 2-D normals, and inverses Today Apply 2-D concepts to 3-D
E N D
3-D Transformations Kurt Akeley CS248 Lecture 8 18 October 2007 http://graphics.stanford.edu/courses/cs248-07/
Overview Tuesday • 2-D transformation • Composition of 2-D transformations • Line equations, 2-D normals, and inverses Today • Apply 2-D concepts to 3-D • Look at OpenGL in some detail
Specify rotation, scale, and shear Specify translation (Almost) always have these values Homogeneous 3-D transformation
Homogeneous translation and scale Translation: Scaling (about the origin):
Homogeneous rotation (about a coordinate axis) y x z Right-hand rule for rotations by positive θ
Transforming the plane equation Plane equations are transformed by the inverse of the matrix used to transform vertexes
Transformation of normals (correct) Convert to plane equation, transform, and convert back:
The vertex pipeline Application struct { float x,y,z,w;float nx,ny,nz; float r,g,b,a;} vertex; Vertex assembly Vertex operations Primitive assembly Primitive operations Rasterization Fragment operations Framebuffer Display
Conceptual coordinate systems no vo Object coordinates Transform vo by MTransform no by M-1 Model transformation World coordinates(lighting calculations) nw vw Transform vw by V Vertex operations View transformation ve Eye coordinates Transform ve by P Projection transformation vc Clip coordinates
Why not collapse to a single transformation? Move world-coordinate and eye-coordinate operations (e.g., vertex lighting) to object coordinates Collapse the M, V, and P matrixes to a single MMVP matrix Advantages: • Efficient implementation (less arithmetic) • Simplified semantics Costs: • Must transform lights to object coordinates • And how would the light positions be defined? • Incorrect answers …
Rigid-body transformations Change orientation and position, but not shape Preserve geometric lengths and (relative) angles Rigid-body transformations are: • Translation and Rotation • Not Scale and Shear (with occasional exceptions) A homogeneous matrix is a rigid-body matrix if its upper-left 3x3 matrix is orthogonal A matrix is orthogonal if and only if • All columns are orthogonal and unit-length, and • All rows are orthogonal and unit-length
Conceptual coordinate systems no vo Object coordinates Transform vo by MTransform no by M-1 Model transformation(non-rigid) World coordinates(lighting calculations) nw vw Transform vw by V View transformation(rigid) Vertex operations ve Eye coordinates Transform ve by P Projection transformation(non-rigid) vc Clip coordinates
Collapsed coordinate systems (obvious) no vo Object coordinates Transform vo by MTransform no by M-1 Model transformation(non-rigid) World coordinates(lighting calculations) nw vw Vertex operations View and projection transformation(non-rigid) Transform vw by MVE vc Clip coordinates
OpenGL coordinate systems (actual) no vo Object coordinates “Model-view” transformation(non-rigid) Transform vo by MTransform no by M-1 Eye coordinates(lighting calculations) ne ve Vertex operations Transform ve by P Projection transformation(non-rigid) vc Clip coordinates
Why compute lighting in eye coordinates? Simplicity • Eliminates the need to explicitly specify viewer location Efficiency • In eye coordinates • View location is (0 0 0) • View direction is (0 0 -1) • These zeros and ones lead to efficient lighting calculations • But the optimizations are no longer important You are free to treat the intermediate coordinates as world coordinates: • Compose view transform V into P rather than M • There is little penalty for doing so: • Diffuse, ambient, and emission lighting work well • But specular lighting, which depends on viewer location, doesn’t • In X-Window terminology: “Mechanism, not policy” • Write your own vertex “shader” • OpenGL 2.0 allows you to redefine everything I’ll describe today
OpenGL matrix commands no vo Object coordinates glMatrixMode(GL_MODELVIEW); “Model-view” transformation(non-rigid) Transform vo by MTransform no by M-1 Eye coordinates(lighting calculations) ne ve glMatrixMode(GL_PROJECTION); Transform ve by P Projection transformation(non-rigid) vc Clip coordinates
OpenGL matrix assignment commands glLoadIdentity(); Remember to do this! glLoadMatrixf(float m[16]); glLoadMatrixd(double m[16]); glLoadTransposeMatrixf(float m[16]); glLoadTransposeMatrixd(double m[16]);
OpenGL matrix composition commands glMultMatrixf(float m[16]); glMultMatrixd(double m[16]); glMultTransposeMatrixf(float m[16]); glMultTransposeMatrixd(double m[16]);
OpenGL transformation commands glRotatef(float θ, float x, float y, float z); glRotated(double θ, double x, double y, double z); glTranslatef(float x, float y, float z); glTranslated(double x, double y, double z); glScalef(float x, float y, float z); glScaled(double x, double y, double z);
Why post-multiplication ? glTranslated(px, py, pz); glRotated(θ, a); glTranslated(-px, -py, -pz); DrawObject();
Duality Transform the coordinate system Transform the object glMultMatrixf(mat1); glMultMatrixf(mat2); glMultMatrixf(mat3); DrawObject();
Rotation about a specified point Transform the coordinate system Transform the object glTranslated(2, 3, 0); glRotated(45, 0, 0, 1); glTranslated(-2, -3, 0); DrawObject();
Transform the object Transformations are applied opposite the specified order Each transformation operates in the fixed coordinate system (2.5 3.5 0)
Transform the coordinate system Transformations are applied in the order specified Each transformation operates relative to the current coordinate system (2.5 3.5 0)
OpenGL matrix stack Separate stacks are maintained for M and P Minimum (queryable) depths are 32 for M and 2 for P glPushMatrix(); // pushes C onto the appropriate stack // leaves C unchanged glPopMatrix(); // restores C from top of stack and pops
Transforming the coordinate system glPushMatrix(); glRotatef(shoulder, 1, 0, 0); SolidBox(1.5*unit, 1.5*unit, 5*unit, RED); glTranslatef(0, 0, 5*unit); glPushMatrix(); glRotatef(elbow, 1, 0, 0); SolidBox(1.25*unit, 1.25*unit, 4*unit, WHITE); glTranslatef(0, 0, 4*unit); glPushMatrix(); glRotatef(wrist, 1, 0, 0); SolidBox(unit, unit, 1.5*unit, GREEN); glTranslatef(0, 0, 1.5*unit); glPushMatrix(); glTranslatef(0, 0.25*unit, 0); glRotatef(finger, 1, 0, 0); SolidBox(0.75*unit, 0.5*unit, 1.5*unit, YELLOW); glPopMatrix(); glPushMatrix(); glTranslatef(0, -0.25*unit, 0); glRotatef(thumb, 1, 0, 0); SolidBox(0.75*unit, 0.5*unit, 1.5*unit, YELLOW); glPopMatrix(); glPopMatrix(); glPopMatrix(); glPopMatrix();
Details (x y z0) specifies a 3-D direction • Division by zero pushes the point to infinity • Normals are a special kind of direction • Transformed by the inverse of the vertex matrix • Other directions are correctly transformed by the vertex matrix itself • E.g., surface tangents OpenGL provides no mechanism to pre-multiply C • Why?
Summary Our 2-D understanding translated easily to 3-D • Lines planes • Rotation becomes more involved • Normals are transformed by the upper-left 3x3 of M There is a duality to transformation • Transform the coordinate system • Transformations are applied in the specified order • Each transformation is relative to the current coordinate system • Transform the object • Transformations are applied opposite the specified order • Each transformation operates in the fixed coordinate system OpenGL matrixes are post-multiplied • Matches vertex semantics (interesting) • Enables hierarchical modeling (important)
Assignments Reading assignment for Thursday’s class • FvD chapter 6 • OpenGL chapter 3 Project 2: • Assigned this past Tuesday • Due next Tuesday, 23 October (midnight, almost Wednesday) • Help session this Friday from 4:15 to 5 pm, Gates B03