CS 354 Transformation, Clipping, and Culling

2,947 views
2,727 views

Published on

CS 354 Computer Graphics

University of Texas, Austin

January 31, 2012

Published in: Technology, Art & Photos
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,947
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
132
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

CS 354 Transformation, Clipping, and Culling

  1. 1. CS 354 Transformation, Clipping, and Culling Mark Kilgard University of Texas January 31, 2012
  2. 2. Today’s material <ul><li>Homework #2 assigned, due Thursday start of class! </li></ul><ul><ul><li>Math problems </li></ul></ul><ul><ul><li>http://www.cs.utexas.edu/~mjk/teaching/cs354_s12/hw2.pdf </li></ul></ul><ul><li>In-class quiz </li></ul><ul><li>Lecture topic: graphics math & transformations </li></ul><ul><ul><li>How do vertex positions transformed to NDC space? </li></ul></ul><ul><ul><li>Generalized clipping and culling </li></ul></ul><ul><li>Assignment </li></ul><ul><ul><li>Reading </li></ul></ul><ul><ul><ul><li>Chapter 6, 310-322 </li></ul></ul></ul><ul><ul><li>Next project (Project #1) to be assigned Thursday, February 2 </li></ul></ul><ul><ul><ul><li>Building a 3D object model loader </li></ul></ul></ul><ul><ul><ul><li>Due Thursday, February 16 </li></ul></ul></ul>
  3. 3. Course Information Reminders <ul><li>Piazza </li></ul><ul><ul><li>Working well now </li></ul></ul><ul><ul><li>https://piazza.com/utexas/cs354 </li></ul></ul><ul><li>Public CS course web site </li></ul><ul><ul><li>http://www.cs.utexas.edu/~mjk/teaching/cs354_s12/ </li></ul></ul><ul><ul><ul><li>Lecture slides in PDF form </li></ul></ul></ul><ul><ul><li>Now has class lecture schedule </li></ul></ul><ul><li>Slideshare.net </li></ul><ul><ul><li>http://www.slideshare.net/Mark_Kilgard </li></ul></ul><ul><ul><ul><li>Lecture slides for web browser viewing </li></ul></ul></ul>
  4. 4. My Office Hours <ul><li>Tuesday, before class </li></ul><ul><ul><li>Painter (PAI) 5.35 </li></ul></ul><ul><ul><li>8:45 a.m. to 9:15 </li></ul></ul><ul><li>Thursday, after class </li></ul><ul><ul><li>ACE 6.302 </li></ul></ul><ul><ul><li>11:00 a.m. to 12:00 </li></ul></ul>
  5. 5. Last time, this time <ul><li>Last lecture, we discussed </li></ul><ul><ul><li>Setup for interpolation </li></ul></ul><ul><ul><li>Basic hidden surface removal via depth testing </li></ul></ul><ul><ul><li>Simplistic pixel updates </li></ul></ul><ul><li>This lecture </li></ul><ul><ul><li>Getting coordinates from object space to NDC space </li></ul></ul><ul><ul><li>Generalized clipping and culling </li></ul></ul>
  6. 6. Daily Quiz <ul><li>Scenario: Depth buffering is enabled, the depth buffer is cleared to 1.0, the depth function is “less than”; and color writes are enabled (blending is disabled)… A stream of shaded fragments are rasterized to the pixel location (24.5, 11.5) in the following order with their depth and color indicated: 1: [ depth = 0.7, red ] 2: [ depth = 0.9, green ] 3: [ depth = 0.3, pink ] 4: [ depth = 0.4, green ] What is the final depth and color of the pixel at (24.5, 11.5)? </li></ul><ul><li>Setting up the plane equation for an interpolated attribute for a triangle to rasterize involves which of the following? A) Solving a system of equations with 3 unknowns B) Watertight perimeter computation C) Evaluating the quadratic equation D) Inverting a 5x5 matrix </li></ul><ul><li>For barycentric triangle interpolation, the three barycentric weights for each vertex sum to what value? </li></ul><ul><li>On a sheet of paper </li></ul><ul><li>Write your EID, name, and date </li></ul><ul><li>Write #1, #2, #3, followed by its answer </li></ul>
  7. 7. Programmer’s View: OpenGL API Example <ul><li>Let’s draw a triangle </li></ul>glShadeModel ( GL_SMOOTH ); // smooth color interpolation glEnable ( GL_DEPTH_TEST ); // enable hidden surface removal glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBegin (GL_TRIANGLES); { // every 3 vertexes makes a triangle glColor4ub (255, 0, 0, 255); // RGBA=(1,0,0,100%) glVertex3f (-0.8, 0.8, 0.3); // XYZ=(-8/10,8/10,3/10) glColor4ub (0, 255, 0, 255); // RGBA=(0,1,0,100%) glVertex3f ( 0.8, 0.8, -0.2); // XYZ=(8/10,8/10,-2/10) glColor4ub (0, 0, 255, 255); // RGBA=(0,0,1,100%) glVertex3f ( 0.0, -0.8, -0.2); // XYZ=(0,-8/10,-2/10) } glEnd (); Pro Tip: use curly braces to “bracket” nested OpenGL usage; no semantic meaning, just highlights grouping
  8. 8. Programmer’s View: GLUT API Example <ul><li>Windowing code </li></ul>#include <GL/glut.h> // includes necessary OpenGL headers void display() { // << insert code on prior slide here >> glutSwapBuffers (); } void main(int argc, char **argv) { // request double-buffered color window with depth buffer glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); glutInit (&argc, argv); glutCreateWindow (“simple triangle”); glutDisplayFunc (display); // function to render window glutMainLoop (); } FYI: GLUT = OpenGL Utility Toolkit
  9. 9. A Simplified Graphics Pipeline Application Vertex batching & assembly Triangle assembly Triangle clipping Triangle rasterization Fragment shading Depth testing Color update Application- OpenGL API boundary Framebuffer NDC to window space Depth buffer several operations left out for simplicity in explaining the simple_triangle example
  10. 10. A few more steps expanded Application Vertex batching & assembly Lighting View frustum clipping Triangle rasterization Fragment shading Depth testing Color update Application- OpenGL API boundary Framebuffer NDC to window space Depth buffer Vertex transformation User defined clipping Back face culling Perspective divide Triangle assembly Texture coordinate generation was just “triangle clipping” before
  11. 11. Conceptual Vertex Transformation glVertex* API commands Modelview matrix User-defined clip planes View-frustum clip planes to primitive rasterization object-space coordinates (x o ,y o ,z o ,w o ) eye-space coordinates (x e ,y e ,z e ,w e ) clipped eye-space coordinates clipped clip-space coordinates Perspective division Projection matrix Viewport + Depth Range transformation (x c ,y c ,z c ,w c ) window-space coordinates (x w ,y w ,z w ,1/w c ) normalized device coordinates (NDC) (x n ,y n ,z n ,1/w c ) clip-space coordinates (x c ,y c ,z c ,w c ) (x e ,y e ,z e ,w e ) (x e ,y e ,z e ,w e )
  12. 12. User Clip Planes in Practice [ParaView] [IVoR] Primarily used scientific visualization and Computer Aided Design (CAD) applications
  13. 13. Remember Back to clipspace Example Six user-defined clip planes enabled to clip teapot to left scene’s clip space view Without user clip planes
  14. 14. Four-component positions! <ul><li>Conventional geometry represents 3D points at (x,y,z) positions </li></ul><ul><ul><li>Affine 3D positions, Cartesian coordinates </li></ul></ul><ul><li>Projective position concept </li></ul><ul><ul><li>Use fourth coordinate: W </li></ul></ul><ul><ul><ul><li>So (x,y,z,w) </li></ul></ul></ul><ul><ul><li>(x/w, y/w, z/w) is the corresponding affine 3D position </li></ul></ul><ul><ul><li>Known as “homogeneous coordinates” </li></ul></ul><ul><li>Advantages </li></ul><ul><ul><li>Represents perspective cleanly </li></ul></ul><ul><ul><li>Allows rasterization of external triangles </li></ul></ul><ul><ul><li>Puts off (expensive) division </li></ul></ul>
  15. 15. Example, All Identical Positions <ul><li>Affine 3D </li></ul><ul><ul><li>(x,y,z) </li></ul></ul><ul><li>Projective 3D </li></ul><ul><ul><li>(x,y,z,w) -> (x/w,y/w,z/w) </li></ul></ul>(2,-5,10) (2,-5,10,1) (4,-10,20,2) (1,-2.5,5,0.5) (-2,5,-10,-1)
  16. 16. Affine View Frustum Clip Equations <ul><li>The idea of a [-1,+1] 3 view frustum cube </li></ul><ul><ul><li>Regions outside this cube get clipped </li></ul></ul><ul><ul><li>Regions inside the cube get rasterized </li></ul></ul><ul><li>Equations </li></ul><ul><ul><li>-1 ≤ x c ≤ +1 </li></ul></ul><ul><ul><li>-1 ≤ y c ≤ +1 </li></ul></ul><ul><ul><li>-1 ≤ z c ≤ +1 </li></ul></ul>
  17. 17. Projective View Frustum Clip Equations <ul><li>Generalizes clip cube as a projective space </li></ul><ul><ul><li>Uses (x c ,y c ,z c ,w c ) clip-space coordinates </li></ul></ul><ul><li>Equations </li></ul><ul><ul><li>-w c ≤ x c ≤ +w c </li></ul></ul><ul><ul><li>-w c ≤ y c ≤ +w c </li></ul></ul><ul><ul><li>-w c ≤ z c ≤ +w c </li></ul></ul><ul><li>Notice </li></ul><ul><ul><li>Impossible for w c < 0 to survive clipping </li></ul></ul><ul><ul><li>Interpretation: w c is distance in front of the eye </li></ul></ul><ul><ul><ul><li>So negative w c values are “behind your head” </li></ul></ul></ul>
  18. 18. glVertex3f Generalized <ul><li>glVertex3f (x,y,z) </li></ul><ul><ul><li>Provides (x,y,z) affine position </li></ul></ul><ul><ul><li>Implicit w of 1 supplied </li></ul></ul><ul><li>Alternatively glVertex4f (x,y,z,w) </li></ul><ul><ul><li>Provides (x,y,z,w) projective position </li></ul></ul><ul><ul><li>Value of w is explicit </li></ul></ul><ul><ul><li>Rarely used in practice but does work </li></ul></ul><ul><li>… Also glVertex2f (x,y) </li></ul><ul><ul><li>Implicit z of 0 and w of 1 supplied </li></ul></ul>
  19. 19. Vertex Transformation <ul><li>So far, we’ve specified vertex positions directly in NDC coordinates </li></ul><ul><ul><li>Simple approach </li></ul></ul><ul><ul><li>Everything in [-1,+1] 3 cube gets drawn </li></ul></ul><ul><li>You can now handle the truth </li></ul><ul><ul><li>OpenGL actually provides a projective transformation from the glVertex3f parameters to NDC coordinates </li></ul></ul><ul><ul><li>Expressed as a combination of two 4x4 matrix transforms on the (x,y,z,w) object-space position </li></ul></ul><ul><ul><ul><li>Initial value of both matrices are identity transforms </li></ul></ul></ul><ul><ul><ul><li>Hence OpenGL initially takes glVertex * positions directly to NDC space </li></ul></ul></ul><ul><ul><ul><ul><li>The simple_trianlge example relies on this </li></ul></ul></ul></ul>
  20. 20. Vertex Transformation <ul><li>Object-space vertex position transformed by a general linear projective transformation </li></ul><ul><ul><li>Expressed as a 4x4 matrix </li></ul></ul>
  21. 21. Matrix Multiplication <ul><li>v’ = M v </li></ul><ul><ul><li>Where M is a matrix and v is a vector </li></ul></ul><ul><ul><li>Use: coordinate system changes </li></ul></ul><ul><li>v = M -1 v’ </li></ul><ul><ul><li>Inverse matrix multiplication </li></ul></ul><ul><ul><li>Uses: back projection, plane equation setup </li></ul></ul>
  22. 22. Two Transforms in Sequence <ul><li>OpenGL thinks of the projective transform as really two matrix transforms </li></ul>FIRST object-space to eye-space SECOND eye-space to clip-space 16 Multiply-Add operations Another 16 Multiply-Add operations
  23. 23. Modelview-Projection Transform <ul><li>Matrixes can associate (combine) </li></ul><ul><ul><li>Combination of the modelview and projection matrix = modelview-projection matrix </li></ul></ul><ul><ul><ul><li>or often simply the “MVP” matrix </li></ul></ul></ul>concatenation is 64 Multiply-Add operations, done by OpenGL driver Matrix multiplication is associative (but not commutative) A(BC) = (AB)C, but ABC≠CBA
  24. 24. Setting the Modelview and Projection Matrices <ul><li>Easy way </li></ul><ul><ul><li>glLoadMatrixf (const GLfloat *matrix) </li></ul></ul><ul><ul><ul><li>matrix is 4x4 array of floats </li></ul></ul></ul><ul><ul><ul><li>Warning: stored in column-major order </li></ul></ul></ul><ul><ul><ul><ul><li>So like FORTRAN, not C </li></ul></ul></ul></ul><ul><ul><li>Replaces the current matrix with the new matrix </li></ul></ul><ul><li>Which matrix is replaced? </li></ul><ul><ul><li>Could be modelview or projection matrix? </li></ul></ul><ul><ul><li>Depends on last call to glMatrixMode </li></ul></ul><ul><ul><ul><li>glMatrixMode ( GL_MODELVIEW ) </li></ul></ul></ul><ul><ul><ul><li>glMatrixMode ( GL_PROJECTION ) </li></ul></ul></ul>
  25. 25. Careful: Beware of Selectors <ul><li>Danger: glMatrixMode is error-prone </li></ul><ul><ul><li>If you forget the state of glMatrixMode , you can easily update the wrong matrix </li></ul></ul><ul><ul><li>Leads to very frustrating bugs </li></ul></ul><ul><ul><li>Problem is glMatrixMode sets a selector which changes which state “other” matrix commands update </li></ul></ul><ul><ul><li>The better API would have the matrix mode as a parameter to matrix commands, instead of a selector </li></ul></ul><ul><li>New EXT_direct_state_access extension has a “direct” command for setting matrix state </li></ul><ul><ul><li>glMatrixLoadfEXT ( GL_MODELVIEW , matrix) </li></ul></ul><ul><ul><li>glMatrixLoadfEXT ( GL_PROJECTION , matrix) </li></ul></ul>
  26. 26. Many Matrix Manipulation Commands <ul><li>glLoadIdentity () </li></ul><ul><ul><li>Resets matrix to identity matrix </li></ul></ul><ul><ul><li>Restores initial setting </li></ul></ul><ul><li>glMultMatrixf (const GLfloat *m) </li></ul><ul><ul><li>Multiplies current matrix by specified 4x4 matrix </li></ul></ul><ul><li>glTranslatef (x,y,z) </li></ul><ul><ul><li>Applies 3D translation to the current matrix </li></ul></ul><ul><li>glScalef (xs,ys,zs) </li></ul><ul><ul><li>Applies 3D (non-uniform) scaling to the current matrix </li></ul></ul><ul><li>glRotatef (angle,x,y,z) </li></ul><ul><ul><li>Applies rotation around a 3D axis to the current matrix </li></ul></ul><ul><li>glPushMatrix () </li></ul><ul><ul><li>Pushes current matrix to the matrix state </li></ul></ul><ul><ul><li>Matrix now can be modified and later popped back </li></ul></ul><ul><li>glPopMatrix () </li></ul><ul><ul><li>The way to pop back to the prior matrix </li></ul></ul><ul><ul><li>Push and pop is good for hierarchical modeling </li></ul></ul><ul><li>glOrtho (left, right, bottom, top, near, far) </li></ul><ul><ul><li>Multiplies current matrix by a 3D orthographic “box” </li></ul></ul><ul><ul><li>Maps a box to the NDC [-1,+1] 3 cube </li></ul></ul><ul><li>glFrustum (left, right, bottom, top, near, far) </li></ul><ul><ul><li>Multiplies current matrix by a 3D perspective frustum </li></ul></ul><ul><ul><li>Maps a camera view to the NDC [-1,+1] 3 cube </li></ul></ul>OpenGL API convention f-suffixed means takes single-precision float parameters d-suffixed means takes double-precision parameters Examples: glTranslate d , glScale d , glRotate d
  27. 27. EXT_direct_state_access Versions of Matrix Commands Convetions All f -suffixed entry points have d -suffixed versions for double precision too All selector-free routines take a GLenum matrix mode parameter, same as glMatrixMode glMatrixMultTransposefEXT glMultTransposeMatrixf glMatrixLoadTransposefEXT glLoadTransposeMatrixf glMatrixPopEXT glPopMatrix glMatrixPushEXT glPushMatrix glMatrixFrustumEXT glFrustumf glMatrixOrthoEXT glOrthof glMatrixTranslatefEXT glTranslatef glMatrixScalefEXT glScalef glMatrixRotatefEXT glRotatef glMatrixLoadIdentityEXT glLoadIdentity glMatrixMultfEXT glMultMatrixf glMatrixLoadfEXT glLoadMatrixf Selector-free DSA Command Conventional Command
  28. 28. Scale, Rotate, and Translate Example <ul><li>Add matrix code </li></ul>glShadeModel ( GL_SMOOTH ); // smooth color interpolation glEnable ( GL_DEPTH_TEST ); // enable hidden surface removal glMatrixMode ( GL_MODELVIEW ); glLoadIdentity (); // reset to null transform glScalef (0.7, 0.4, 1.0); // scale by 70% in X and 40% in Y (and 100% in Z) glRotatef (30, 0, 0, 1); // rotate in XY plane (around Z axis) 30 degrees glTranslatef (-0.1, 0.3, 0.1); // shift -0.1 to the left and 0.3 up (and 0.1 back in Z) glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBegin (GL_TRIANGLES); { // every 3 vertexes makes a triangle glColor4ub (255, 0, 0, 255); // RGBA=(1,0,0,100%) glVertex3f (-0.8, 0.8, 0.3); // XYZ=(-8/10,8/10,3/10) glColor4ub (0, 255, 0, 255); // RGBA=(0,1,0,100%) glVertex3f ( 0.8, 0.8, -0.2); // XYZ=(8/10,8/10,-2/10) glColor4ub (0, 0, 255, 255); // RGBA=(0,0,1,100%) glVertex3f ( 0.0, -0.8, -0.2); // XYZ=(0,-8/10,-2/10) } glEnd ();
  29. 29. Load Identity Transform <ul><li>Prototype </li></ul><ul><ul><li>glLoadIdentity () </li></ul></ul><ul><li>Replace current matrix with this matrix </li></ul>
  30. 30. Non-uniform Scale Transform <ul><li>Prototype </li></ul><ul><ul><li>glScalef ( GLfloat xs, GLfloat ys, GLfloat zs) </li></ul></ul><ul><li>Post-concatenates this matrix </li></ul>
  31. 31. Translate Transform <ul><li>Prototype </li></ul><ul><ul><li>glTranslatef ( GLfloat x, GLfloat y, GLfloat z) </li></ul></ul><ul><li>Post-concatenates this matrix </li></ul>
  32. 32. Rotation Transform <ul><li>Prototype </li></ul><ul><ul><li>glRotatef ( GLfloat a, // angle in degrees GLfloat x, GLfloat y, GLfloat z) </li></ul></ul><ul><li>Post-concatenates a rotation matrix </li></ul>glRotatef (a,1,0,0) glRotatef (a,0,1,0) glRotatef (a,0,0,1) Axis could be arbitrary un-normalized direction vector
  33. 33. Orthographic Transform <ul><li>Prototype </li></ul><ul><ul><li>glOrthof (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far) </li></ul></ul><ul><li>Post-concatenates an orthographic matrix </li></ul>
  34. 34. Frustum Transform <ul><li>Prototype </li></ul><ul><ul><li>glFrustumf (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far) </li></ul></ul><ul><li>Post-concatenates a frustum matrix </li></ul>
  35. 35. Two Transforms Concatenated <ul><li>Multiplying the position by </li></ul><ul><ul><li>First by the modelview </li></ul></ul><ul><ul><li>Then second by the projection </li></ul></ul><ul><ul><li>…is equivalent to multiplying by their concatenation so </li></ul></ul>Just 16 Multiply-Add operations… even though 2 (combined) transformation matrices!
  36. 36. Perspective Divide <ul><li>Divide clip-space (x,y,z) by clip-space w </li></ul><ul><ul><li>To get Normalized Device Coordinate (NDC) space </li></ul></ul><ul><li>Means reciprocal operation is done once </li></ul><ul><ul><li>And done after clipping </li></ul></ul><ul><ul><li>Minimizes division by zero concern </li></ul></ul>
  37. 37. Viewport and Depth Range <ul><li>Prototypes </li></ul><ul><ul><li>glViewport ( GLint vx, GLint vy, GLsizei vw, GLsizei vh) </li></ul></ul><ul><ul><li>glDepthRange ( GLclampd n, GLclampd f) </li></ul></ul><ul><li>Equations </li></ul><ul><ul><li>Maps NDC space to window space </li></ul></ul>
  38. 38. Conceptual Vertex Transformation glVertex* API commands Modelview matrix User-defined clip planes View-frustum clip planes to primitive rasterization object-space coordinates (x o ,y o ,z o ,w o ) eye-space coordinates (x e ,y e ,z e ,w e ) clipped eye-space coordinates clipped clip-space coordinates Perspective division Projection matrix Viewport + Depth Range transformation (x c ,y c ,z c ,w c ) window-space coordinates (x w ,y w ,z w ,1/w c ) normalized device coordinates (NDC) (x n ,y n ,z n ,1/w c ) clip-space coordinates (x c ,y c ,z c ,w c ) (x e ,y e ,z e ,w e ) (x e ,y e ,z e ,w e )
  39. 39. View Frustum Clipping Generalizes Cleanly <ul><li>Recall moving left vertex so it’s X = -1.8 </li></ul><ul><ul><li>Result is a clipped triangle </li></ul></ul>(-1.8, 0.8, 0.3, 1) (-0.8, 0.8, -0.2,1) (0, -0.8, -0.2, 1) origin at (0,0,0,1)   
  40. 40. Clipped Triangle Visualized Clipped and Rasterized Normally Visualization of NDC space Notice triangle is “poking out” of the cube; this is the reason that should be clipped
  41. 41. Break Clipped Triangle into Two Triangles But how do we find these “new” vertices? The edge clipping the triangle is the line at X = -1 so we know X = -1 at these points—but what about Y?
  42. 42. Use Ratios to Interpolate Clipped Positions (-1.8, 0.8, 0.3, 1) (-0.8, 0.8, -0.2,1) (0, -0.8, -0.2) origin at (0,0,0,1) X = -1 Y = (1.8/2.6)×0.8 + (0.8/2.6)×0.8 = 0.8 Z = (1.8/2.6)×0.3 + (0.8/2.6)×-0.2 = 0.1461538 W = (1.8/2.6)×1 + (0.8/2.6)×1 = 1 -1-(-1.8)=0.8 0.8-(-1)=1.8 0.8-(-1.8)=2.6 (-1,0.8,0.146153,1) Straightforward because all the edges are orthogonal Weights: 1.8/2.6 0.8/2.6, sum to 1
  43. 43. Use Ratios to Interpolate Clipped Positions (-1.8, 0.8, 0.3) (-0.8, 0.8, -0.2) (0, -0.8, -0.2) origin at (0,0,0) 0-(-1.8) = 1.8 0-(-1) = 1 X = -1 Y = (1/1.8)×0.8 + (0.8/1.8)×-0.8 = 0.08888… Z = (1/1.8)×0.3 + (0.8/1.8)×-0.2 = 0.07777… (-1,0.0888,0.0777) -1-(-1.8) = 0.8 Weights: 1/1.8 0.8/1.8, sum to 1
  44. 44. Generalize to Non-1 W <ul><li>Affine clipping plane in example </li></ul><ul><ul><li>-1 ≤ x c </li></ul></ul><ul><li>Generalizes to </li></ul><ul><ul><li>1 x c + 0 y c + 0z c + 1 w c ≥ 0 </li></ul></ul><ul><ul><li>Looks like a plane equation </li></ul></ul><ul><ul><ul><li>A x c + B y c + C z c + D w c ≥ 0 </li></ul></ul></ul><ul><ul><ul><li>with coefficients A, B, C, and D </li></ul></ul></ul>
  45. 45. View Frustum Plane Equations <ul><li>All six view frustum planes can be described by simple projective plane equations </li></ul>-1 z c + 1w c ≥ 0 1 -1 0 0 Top 1 z c + 1w c ≥ 0 1 1 0 0 Near -1 y c + 1w c ≥ 0 1 0 -1 0 Top 1 y c + 1w c ≥ 0 1 0 1 0 Bottom -1 x c + 1w c ≥ 0 1 0 0 -1 Right 1 x c + 1w c ≥ 0 1 0 0 1 Left Plane equation D C B A Name
  46. 46. Projective Clipping <ul><li>Each vertex computes its clip distance w.r.t. a plane </li></ul><ul><ul><li>Plug vertex’s (x,y,z,w) into Ax+By+Cz+Dw≥0 plane equation… provides a clip distance </li></ul></ul><ul><li>For two vertexes forming a triangle edge </li></ul><ul><ul><li>Both negative? Discard the edge </li></ul></ul><ul><ul><li>Both positive? Accept the edge (no clipping) </li></ul></ul><ul><ul><li>One negative, one positive </li></ul></ul><ul><ul><ul><li>Clipping is needed </li></ul></ul></ul><ul><ul><ul><li>Compute t as s / (s + p) where s and p are clip distances </li></ul></ul></ul><ul><ul><ul><ul><li>s is the “inside” distance; p is the “outside” distance </li></ul></ul></ul></ul><ul><ul><ul><li>Weight all per-vertex attributes based on t </li></ul></ul></ul><ul><ul><ul><li>Makes new “clipped” vertex on the clip plane </li></ul></ul></ul><ul><ul><ul><li>Generate 1 triangle if 1 of 3 vertices is inside; if 2 inside, generate 2 triangles </li></ul></ul></ul><ul><li>Repeat process (recursively) for all clip planes </li></ul><ul><ul><li>Only slightly more complicated than prior clipping algorithm </li></ul></ul>
  47. 47. Readily Extends to User-defined Clip Planes <ul><li>In addition to the six view frustum planes of clip space… </li></ul><ul><ul><li>OpenGL supports user-defined clip planes </li></ul></ul><ul><ul><li>Allows slicing into geometry </li></ul></ul><ul><li>Operate in eye space instead of clip space </li></ul><ul><ul><li>Enabled with glEnable ( GL_CLIP_PLANE0 + num ) </li></ul></ul><ul><ul><li>Plane equation set by glClipPlane </li></ul></ul><ul><ul><ul><li>Clip planes are transformed current modelview projection matrix </li></ul></ul></ul><ul><ul><li>Plane equation is Ax e +By e +Cz e +Wz e ≥0 </li></ul></ul><ul><ul><ul><li>Instead of using (x c , y c , z c , w c ) as view frustum planes do </li></ul></ul></ul>
  48. 48. (Clip) Plane Transformation <ul><li>Vertex positions (and direction vectors) are transformed like column vectors </li></ul><ul><li>Plane equations are transformed like row vectors </li></ul>glClipPlane parameters glVertex4f parameters
  49. 49. Conceptual Vertex Transformation glVertex* API commands Modelview matrix User-defined clip planes View-frustum clip planes to primitive rasterization object-space coordinates (x o ,y o ,z o ,w o ) eye-space coordinates (x e ,y e ,z e ,w e ) clipped eye-space coordinates clipped clip-space coordinates Perspective division Projection matrix Viewport + Depth Range transformation (x c ,y c ,z c ,w c ) window-space coordinates (x w ,y w ,z w ,w w ) normalized device coordinates (NDC) (x n ,y n ,z n ,w n ) clip-space coordinates (x c ,y c ,z c ,w c ) (x e ,y e ,z e ,w e ) (x e ,y e ,z e ,w e )
  50. 50. Vertex Shaders in the Pipeline Geometry Program 3D Application or Game OpenGL API GPU Front End Vertex Assembly Vertex Shader Clipping, Setup, and Rasterization Fragment Shader Texture Fetch Raster Operations Framebuffer Access Memory Interface CPU – GPU Boundary OpenGL 3.3 Attribute Fetch Primitive Assembly Parameter Buffer Read programmable fixed-function Legend So far, we’ve discussed “fixed-function” vertex transformation Modern GPUs make vertex processing programmable Via vertex shaders!
  51. 51. Vertex Programmability Paletted matrix skinning Twister vertex program Per-vertex cartoon shading
  52. 52. Other Vertex Processing Tasks <ul><li>Per-vertex lighting </li></ul><ul><ul><li>Given normal vectors at each vertex, view vector, material parameters, and light parameters, compute the lit appearance of the surface </li></ul></ul><ul><li>Texture coordinate generation </li></ul><ul><ul><li>Texture coordinates help map images (called “textures”) onto rasterized primitives </li></ul></ul><ul><ul><li>Often texture coordinates are passed explicitly with each vertex </li></ul></ul><ul><ul><li>Alternatively, a vertex shader can compute them </li></ul></ul><ul><li>Non-linear transformation </li></ul><ul><ul><li>Animation by blending various matrix transforms </li></ul></ul><ul><ul><li>Twisting or morphing geometry </li></ul></ul><ul><li>Fog computations </li></ul><ul><li>Arbitrary setup for fragment shaders </li></ul>
  53. 53. Vertex Shader Example: Key Frame Blending Frame A Frame B Blended Frame 47% = + 53%
  54. 54. Key Frame Blending Vertex Shader Frame A Frame B Other possible key frames
  55. 55. Non-linear Vertex Transform + Fragment Shader Setup Fragment program Vertex program 2D grid over (s,t)  [0,1] Tessellated torus
  56. 56. Next Lecture <ul><li>Graphics Math </li></ul><ul><ul><li>Interpolation, vector math, and number representations for computer graphics </li></ul></ul><ul><ul><li>As usual, expect a short quiz on today’s lecture </li></ul></ul><ul><ul><ul><li>Know the form of a translate and scale matrix </li></ul></ul></ul><ul><ul><ul><li>Know the order of operations in transforming a vertex position </li></ul></ul></ul><ul><li>Assignments </li></ul><ul><ul><li>Reading </li></ul></ul><ul><ul><ul><li>Chapter 6, 310-322 </li></ul></ul></ul><ul><ul><li>Next project (Project #1) to be assigned Thursday, February 2 </li></ul></ul><ul><ul><ul><li>Building a 3D object model loader </li></ul></ul></ul><ul><ul><ul><li>Due Thursday, February 16 </li></ul></ul></ul>2nd homework is on class web site, 5 pages, 10 problems

×