SlideShare a Scribd company logo
CS 354 Transformation, Clipping, and Culling Mark Kilgard University of Texas January 31, 2012
Today’s material Homework #2 assigned,  due Thursday  start of class! Math problems http://www.cs.utexas.edu/~mjk/teaching/cs354_s12/hw2.pdf In-class quiz Lecture topic: graphics math & transformations How do vertex positions transformed to NDC space? Generalized clipping and culling Assignment Reading Chapter 6, 310-322 Next project (Project #1) to be assigned Thursday, February 2 Building a 3D object model loader Due Thursday, February 16
Course Information Reminders Piazza Working well now https://piazza.com/utexas/cs354 Public CS course web site http://www.cs.utexas.edu/~mjk/teaching/cs354_s12/ Lecture slides in PDF form Now has class lecture schedule Slideshare.net http://www.slideshare.net/Mark_Kilgard Lecture slides for web browser viewing
My Office Hours Tuesday, before class Painter (PAI) 5.35 8:45 a.m. to 9:15 Thursday, after class ACE 6.302 11:00 a.m. to 12:00
Last time, this time Last lecture, we discussed Setup for interpolation Basic hidden surface removal via depth testing Simplistic pixel updates This lecture Getting coordinates from object space to NDC space Generalized clipping and culling
Daily Quiz 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)? 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 For barycentric triangle interpolation, the three barycentric weights for each vertex sum to what value? On a sheet of paper Write your EID, name, and date Write #1, #2, #3, followed by its answer
Programmer’s View: OpenGL API Example Let’s draw a triangle 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
Programmer’s View: GLUT API Example Windowing code #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
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
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
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 )
User Clip Planes in Practice [ParaView] [IVoR] Primarily used scientific visualization and Computer Aided Design (CAD) applications
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
Four-component positions! Conventional geometry represents 3D points at (x,y,z) positions Affine 3D positions, Cartesian coordinates Projective position concept Use fourth coordinate:  W So (x,y,z,w) (x/w, y/w, z/w) is the corresponding affine 3D position Known as “homogeneous coordinates” Advantages Represents perspective cleanly Allows rasterization of external triangles Puts off (expensive) division
Example, All Identical Positions Affine 3D (x,y,z) Projective 3D (x,y,z,w) -> (x/w,y/w,z/w) (2,-5,10) (2,-5,10,1) (4,-10,20,2) (1,-2.5,5,0.5) (-2,5,-10,-1)
Affine  View Frustum Clip Equations The idea of a [-1,+1] 3  view frustum cube Regions outside this cube get clipped Regions inside the cube get rasterized Equations -1 ≤ x c  ≤ +1 -1 ≤ y c  ≤ +1 -1 ≤ z c  ≤ +1
Projective  View Frustum Clip Equations Generalizes clip cube as a projective space Uses (x c ,y c ,z c ,w c ) clip-space coordinates Equations -w c  ≤ x c  ≤ +w c -w c  ≤ y c  ≤ +w c -w c  ≤ z c  ≤ +w c Notice Impossible for w c  < 0 to survive clipping Interpretation:  w c  is distance in front of the eye So negative w c  values are “behind your head”
glVertex3f Generalized glVertex3f (x,y,z) Provides (x,y,z) affine position Implicit w of 1 supplied Alternatively  glVertex4f (x,y,z,w) Provides (x,y,z,w) projective position Value of w is explicit Rarely used in practice but does work …  Also  glVertex2f (x,y) Implicit z of 0 and w of 1 supplied
Vertex Transformation So far, we’ve specified vertex positions directly in NDC coordinates Simple approach Everything in [-1,+1] 3  cube gets drawn You can now handle the truth OpenGL actually provides a projective transformation from the  glVertex3f  parameters to NDC coordinates Expressed as a combination of two 4x4 matrix transforms on the (x,y,z,w) object-space position Initial value of both matrices are identity transforms Hence OpenGL initially takes  glVertex * positions directly to NDC space The  simple_trianlge  example relies on this
Vertex Transformation Object-space vertex position transformed by a general linear projective transformation Expressed as a 4x4 matrix
Matrix Multiplication v’ = M v Where M is a matrix and v is a vector Use:  coordinate system changes v = M -1  v’ Inverse matrix multiplication Uses:  back projection, plane equation setup
Two Transforms in Sequence OpenGL thinks of the projective transform as really two matrix transforms FIRST object-space to eye-space SECOND eye-space to clip-space 16 Multiply-Add operations Another 16 Multiply-Add operations
Modelview-Projection Transform Matrixes can associate (combine) Combination of the modelview and projection matrix = modelview-projection matrix or often simply the “MVP” matrix concatenation is 64 Multiply-Add operations, done by OpenGL driver Matrix multiplication is  associative  (but not commutative) A(BC) = (AB)C, but ABC≠CBA
Setting the Modelview and Projection Matrices Easy way glLoadMatrixf (const GLfloat *matrix) matrix is 4x4 array of floats Warning: stored in column-major order So like FORTRAN, not C Replaces the current matrix with the new matrix Which matrix is replaced? Could be modelview or projection matrix? Depends on last call to  glMatrixMode glMatrixMode ( GL_MODELVIEW ) glMatrixMode ( GL_PROJECTION )
Careful:  Beware of Selectors Danger:  glMatrixMode  is error-prone If you forget the state of  glMatrixMode , you can easily update the  wrong  matrix Leads to  very  frustrating bugs Problem is  glMatrixMode  sets a selector which changes which state “other” matrix commands update The better API would have the matrix mode as a parameter to matrix commands, instead of a selector New  EXT_direct_state_access  extension has a “direct” command for setting matrix state glMatrixLoadfEXT ( GL_MODELVIEW , matrix) glMatrixLoadfEXT ( GL_PROJECTION , matrix)
Many Matrix Manipulation Commands glLoadIdentity () Resets matrix to identity matrix Restores initial setting glMultMatrixf (const GLfloat *m) Multiplies current matrix by specified 4x4 matrix glTranslatef (x,y,z) Applies 3D translation to the current matrix glScalef (xs,ys,zs) Applies 3D (non-uniform) scaling to the current matrix glRotatef (angle,x,y,z) Applies rotation around a 3D axis to the current matrix glPushMatrix () Pushes current matrix to the matrix state Matrix now can be modified and later popped back glPopMatrix () The way to pop back to the prior matrix Push and pop is good for hierarchical modeling  glOrtho (left, right, bottom, top,   near, far) Multiplies current matrix by a 3D orthographic “box” Maps a box to the NDC [-1,+1] 3  cube glFrustum (left, right, bottom, top,   near, far) Multiplies current matrix by a 3D perspective frustum Maps a camera view to the NDC [-1,+1] 3  cube 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
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
Scale, Rotate, and Translate Example Add matrix code 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 ();
Load Identity Transform Prototype glLoadIdentity () Replace current matrix with this matrix
Non-uniform Scale Transform Prototype glScalef ( GLfloat  xs,  GLfloat  ys,  GLfloat  zs) Post-concatenates this matrix
Translate Transform Prototype glTranslatef ( GLfloat  x,  GLfloat  y,  GLfloat  z) Post-concatenates this matrix
Rotation Transform Prototype glRotatef ( GLfloat  a,  // angle in degrees   GLfloat  x,  GLfloat  y,  GLfloat  z) Post-concatenates a rotation matrix glRotatef (a,1,0,0)  glRotatef (a,0,1,0)  glRotatef (a,0,0,1)  Axis could be arbitrary un-normalized direction vector
Orthographic Transform Prototype glOrthof (GLfloat left, GLfloat right,   GLfloat bottom, GLfloat top,   GLfloat near, GLfloat far) Post-concatenates an orthographic matrix
Frustum Transform Prototype glFrustumf (GLfloat left, GLfloat right,   GLfloat bottom, GLfloat top,   GLfloat near, GLfloat far) Post-concatenates a frustum matrix
Two Transforms Concatenated Multiplying the position by First by the modelview Then second by the projection …is equivalent to multiplying by their concatenation so Just 16 Multiply-Add operations… even though 2 (combined) transformation matrices!
Perspective Divide Divide clip-space (x,y,z) by clip-space w To get Normalized Device Coordinate (NDC) space Means reciprocal operation is done once And done after clipping Minimizes division by zero concern
Viewport and Depth Range Prototypes glViewport ( GLint  vx,  GLint  vy,  GLsizei  vw,  GLsizei  vh) glDepthRange ( GLclampd  n,  GLclampd  f) Equations Maps NDC space to window space
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 )
View Frustum Clipping Generalizes Cleanly Recall moving left vertex so it’s X = -1.8 Result is a  clipped  triangle (-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)   
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
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?
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
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
Generalize to Non-1 W Affine clipping plane in example -1 ≤ x c Generalizes to 1 x c  + 0 y c  + 0z c  + 1 w c  ≥ 0 Looks like a plane equation A x c  + B y c  + C z c  + D w c  ≥ 0 with coefficients A, B, C, and D
View Frustum Plane Equations All six view frustum planes can be described by simple projective plane equations -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
Projective Clipping Each vertex computes its clip distance w.r.t. a plane Plug vertex’s (x,y,z,w) into Ax+By+Cz+Dw≥0 plane equation… provides a clip distance For two vertexes forming a triangle edge Both negative?  Discard the edge Both positive?  Accept the edge (no clipping) One negative, one positive Clipping is needed Compute t as s / (s + p) where s and p are clip distances s is the “inside” distance; p is the “outside” distance Weight all per-vertex attributes based on t Makes new “clipped” vertex on the clip plane Generate 1 triangle if 1 of 3 vertices is inside; if 2 inside, generate 2 triangles Repeat process (recursively) for all clip planes Only slightly more complicated than prior clipping algorithm
Readily Extends to User-defined Clip Planes In addition to the six view frustum planes of clip space… OpenGL supports user-defined clip planes Allows slicing into geometry Operate in eye space instead of clip space Enabled with  glEnable ( GL_CLIP_PLANE0 + num ) Plane equation set by  glClipPlane Clip planes are transformed current modelview projection matrix Plane equation is Ax e +By e +Cz e +Wz e ≥0 Instead of using (x c , y c , z c , w c ) as view frustum planes do
(Clip) Plane Transformation Vertex positions (and direction vectors) are transformed like column vectors Plane equations are transformed like row vectors glClipPlane  parameters glVertex4f parameters
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 )
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!
Vertex Programmability Paletted matrix skinning Twister vertex program Per-vertex cartoon shading
Other Vertex Processing Tasks Per-vertex lighting Given normal vectors at each vertex, view vector, material parameters, and light parameters, compute the lit appearance of the surface Texture coordinate generation Texture coordinates help map images (called “textures”) onto rasterized primitives Often texture coordinates are passed explicitly with each vertex Alternatively, a vertex shader can compute them Non-linear transformation Animation by blending various matrix transforms Twisting or morphing geometry Fog computations Arbitrary setup for fragment shaders
Vertex Shader Example: Key Frame Blending Frame A Frame B Blended Frame 47% = + 53%
Key Frame Blending Vertex Shader Frame A Frame B Other possible key frames
Non-linear Vertex Transform + Fragment Shader Setup Fragment program Vertex program 2D grid over (s,t)  [0,1] Tessellated torus
Next Lecture Graphics Math Interpolation, vector math, and number representations for computer graphics As usual, expect a short quiz on today’s lecture Know the form of a translate and scale matrix Know the order of operations in transforming a vertex position Assignments Reading Chapter 6, 310-322 Next project (Project #1) to be assigned Thursday, February 2 Building a 3D object model loader Due Thursday, February 16 2nd homework is on class web site, 5 pages, 10 problems

More Related Content

PPT
2 d geometric transformations
PPT
Midpoint circle algo
PPTX
3D Transformation in Computer Graphics
PPTX
Mid point circle algorithm
PPT
Longest common subsequence(dynamic programming).
PPTX
Coin Change : Greedy vs Dynamic Programming
PPT
Branch and bound
PPTX
8 queen problem
2 d geometric transformations
Midpoint circle algo
3D Transformation in Computer Graphics
Mid point circle algorithm
Longest common subsequence(dynamic programming).
Coin Change : Greedy vs Dynamic Programming
Branch and bound
8 queen problem

What's hot (20)

PDF
UNIT-6-Illumination-Models-and-Surface-Rendering-Methods.pdf
PPTX
Machine Learning - Convolutional Neural Network
PPT
Raster Scan and Raster Scan Displays
PPT
factoring
PPTX
Depth Buffer Method
PPT
Chap8 new
PPTX
Viewing transformation
PPT
Polygon filling
PPTX
Deep sarsa, Deep Q-learning, DQN
PPTX
Classical relations and fuzzy relations
PPTX
Divide and conquer
PDF
Artificial Neural Networks Lect3: Neural Network Learning rules
PDF
Transport layer
PPT
2 d transformations by amit kumar (maimt)
PPTX
An overview of gradient descent optimization algorithms
PPT
01 knapsack using backtracking
PPTX
PPTX
parallel Merging
PPT
Boundary Extraction
PPTX
M2M Protocols for Constrained Environments in the Context of IoT: A Compariso...
UNIT-6-Illumination-Models-and-Surface-Rendering-Methods.pdf
Machine Learning - Convolutional Neural Network
Raster Scan and Raster Scan Displays
factoring
Depth Buffer Method
Chap8 new
Viewing transformation
Polygon filling
Deep sarsa, Deep Q-learning, DQN
Classical relations and fuzzy relations
Divide and conquer
Artificial Neural Networks Lect3: Neural Network Learning rules
Transport layer
2 d transformations by amit kumar (maimt)
An overview of gradient descent optimization algorithms
01 knapsack using backtracking
parallel Merging
Boundary Extraction
M2M Protocols for Constrained Environments in the Context of IoT: A Compariso...
Ad

Viewers also liked (8)

PPT
Projection Matrices
PPT
3D transformation
PDF
OpenGL Transformation
DOCX
Homogeneous coordinate
PPT
Secrets of CryENGINE 3 Graphics Technology
PPTX
2 d transformations and homogeneous coordinates
PPT
2d/3D transformations in computer graphics(Computer graphics Tutorials)
PPTX
3d transformation computer graphics
Projection Matrices
3D transformation
OpenGL Transformation
Homogeneous coordinate
Secrets of CryENGINE 3 Graphics Technology
2 d transformations and homogeneous coordinates
2d/3D transformations in computer graphics(Computer graphics Tutorials)
3d transformation computer graphics
Ad

Similar to CS 354 Transformation, Clipping, and Culling (20)

PPT
CS 354 Object Viewing and Representation
PPT
CS 354 Graphics Math
PPT
CS 354 Pixel Updating
PPT
september11.ppt
PDF
Geometric objects and transformations
DOCX
computer graphics slides by Talha shah
PPTX
3 d graphics with opengl part 2
PPT
CS 354 Viewing Stuff
PDF
201707 SER332 Lecture10
PDF
OpenGL L03-Utilities
PPT
Introduction to OpenGL modern OpenGL program
PPTX
GFX Part 5 - Introduction to Object Transformations in OpenGL ES
PPT
CS 354 Blending, Compositing, Anti-aliasing
PPTX
CGLabLec6.pptx
PDF
OpenGL L02-Transformations
PPTX
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
PDF
The Day You Finally Use Algebra: A 3D Math Primer
PDF
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
PPTX
OpenGL_summer2012.ccccccccccccccccccpptx
CS 354 Object Viewing and Representation
CS 354 Graphics Math
CS 354 Pixel Updating
september11.ppt
Geometric objects and transformations
computer graphics slides by Talha shah
3 d graphics with opengl part 2
CS 354 Viewing Stuff
201707 SER332 Lecture10
OpenGL L03-Utilities
Introduction to OpenGL modern OpenGL program
GFX Part 5 - Introduction to Object Transformations in OpenGL ES
CS 354 Blending, Compositing, Anti-aliasing
CGLabLec6.pptx
OpenGL L02-Transformations
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
The Day You Finally Use Algebra: A 3D Math Primer
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
OpenGL_summer2012.ccccccccccccccccccpptx

More from Mark Kilgard (20)

PDF
D11: a high-performance, protocol-optional, transport-optional, window system...
PPT
Computers, Graphics, Engineering, Math, and Video Games for High School Students
PPT
NVIDIA OpenGL and Vulkan Support for 2017
PPT
NVIDIA OpenGL 4.6 in 2017
PPT
NVIDIA OpenGL in 2016
PPT
Virtual Reality Features of NVIDIA GPUs
PPTX
Migrating from OpenGL to Vulkan
PPT
EXT_window_rectangles
PPT
OpenGL for 2015
PPT
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
PDF
Accelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
PPT
NV_path rendering Functional Improvements
PPTX
OpenGL 4.5 Update for NVIDIA GPUs
PPT
SIGGRAPH Asia 2012: GPU-accelerated Path Rendering
PPT
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
PDF
Programming with NV_path_rendering: An Annex to the SIGGRAPH Asia 2012 paper...
PPT
GPU accelerated path rendering fastforward
PDF
GPU-accelerated Path Rendering
PPT
SIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
PPT
SIGGRAPH 2012: NVIDIA OpenGL for 2012
D11: a high-performance, protocol-optional, transport-optional, window system...
Computers, Graphics, Engineering, Math, and Video Games for High School Students
NVIDIA OpenGL and Vulkan Support for 2017
NVIDIA OpenGL 4.6 in 2017
NVIDIA OpenGL in 2016
Virtual Reality Features of NVIDIA GPUs
Migrating from OpenGL to Vulkan
EXT_window_rectangles
OpenGL for 2015
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
Accelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
NV_path rendering Functional Improvements
OpenGL 4.5 Update for NVIDIA GPUs
SIGGRAPH Asia 2012: GPU-accelerated Path Rendering
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
Programming with NV_path_rendering: An Annex to the SIGGRAPH Asia 2012 paper...
GPU accelerated path rendering fastforward
GPU-accelerated Path Rendering
SIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
SIGGRAPH 2012: NVIDIA OpenGL for 2012

Recently uploaded (20)

PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Mushroom cultivation and it's methods.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
August Patch Tuesday
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
project resource management chapter-09.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Chapter 5: Probability Theory and Statistics
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Unlocking AI with Model Context Protocol (MCP)
DP Operators-handbook-extract for the Mautical Institute
NewMind AI Weekly Chronicles - August'25-Week II
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
WOOl fibre morphology and structure.pdf for textiles
Mushroom cultivation and it's methods.pdf
A comparative analysis of optical character recognition models for extracting...
Zenith AI: Advanced Artificial Intelligence
Heart disease approach using modified random forest and particle swarm optimi...
August Patch Tuesday
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
A comparative study of natural language inference in Swahili using monolingua...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
project resource management chapter-09.pdf
cloud_computing_Infrastucture_as_cloud_p
MIND Revenue Release Quarter 2 2025 Press Release
Chapter 5: Probability Theory and Statistics
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Unlocking AI with Model Context Protocol (MCP)

CS 354 Transformation, Clipping, and Culling

  • 1. CS 354 Transformation, Clipping, and Culling Mark Kilgard University of Texas January 31, 2012
  • 2. Today’s material Homework #2 assigned, due Thursday start of class! Math problems http://www.cs.utexas.edu/~mjk/teaching/cs354_s12/hw2.pdf In-class quiz Lecture topic: graphics math & transformations How do vertex positions transformed to NDC space? Generalized clipping and culling Assignment Reading Chapter 6, 310-322 Next project (Project #1) to be assigned Thursday, February 2 Building a 3D object model loader Due Thursday, February 16
  • 3. Course Information Reminders Piazza Working well now https://piazza.com/utexas/cs354 Public CS course web site http://www.cs.utexas.edu/~mjk/teaching/cs354_s12/ Lecture slides in PDF form Now has class lecture schedule Slideshare.net http://www.slideshare.net/Mark_Kilgard Lecture slides for web browser viewing
  • 4. My Office Hours Tuesday, before class Painter (PAI) 5.35 8:45 a.m. to 9:15 Thursday, after class ACE 6.302 11:00 a.m. to 12:00
  • 5. Last time, this time Last lecture, we discussed Setup for interpolation Basic hidden surface removal via depth testing Simplistic pixel updates This lecture Getting coordinates from object space to NDC space Generalized clipping and culling
  • 6. Daily Quiz 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)? 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 For barycentric triangle interpolation, the three barycentric weights for each vertex sum to what value? On a sheet of paper Write your EID, name, and date Write #1, #2, #3, followed by its answer
  • 7. Programmer’s View: OpenGL API Example Let’s draw a triangle 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. Programmer’s View: GLUT API Example Windowing code #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. 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. 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. 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. User Clip Planes in Practice [ParaView] [IVoR] Primarily used scientific visualization and Computer Aided Design (CAD) applications
  • 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. Four-component positions! Conventional geometry represents 3D points at (x,y,z) positions Affine 3D positions, Cartesian coordinates Projective position concept Use fourth coordinate: W So (x,y,z,w) (x/w, y/w, z/w) is the corresponding affine 3D position Known as “homogeneous coordinates” Advantages Represents perspective cleanly Allows rasterization of external triangles Puts off (expensive) division
  • 15. Example, All Identical Positions Affine 3D (x,y,z) Projective 3D (x,y,z,w) -> (x/w,y/w,z/w) (2,-5,10) (2,-5,10,1) (4,-10,20,2) (1,-2.5,5,0.5) (-2,5,-10,-1)
  • 16. Affine View Frustum Clip Equations The idea of a [-1,+1] 3 view frustum cube Regions outside this cube get clipped Regions inside the cube get rasterized Equations -1 ≤ x c ≤ +1 -1 ≤ y c ≤ +1 -1 ≤ z c ≤ +1
  • 17. Projective View Frustum Clip Equations Generalizes clip cube as a projective space Uses (x c ,y c ,z c ,w c ) clip-space coordinates Equations -w c ≤ x c ≤ +w c -w c ≤ y c ≤ +w c -w c ≤ z c ≤ +w c Notice Impossible for w c < 0 to survive clipping Interpretation: w c is distance in front of the eye So negative w c values are “behind your head”
  • 18. glVertex3f Generalized glVertex3f (x,y,z) Provides (x,y,z) affine position Implicit w of 1 supplied Alternatively glVertex4f (x,y,z,w) Provides (x,y,z,w) projective position Value of w is explicit Rarely used in practice but does work … Also glVertex2f (x,y) Implicit z of 0 and w of 1 supplied
  • 19. Vertex Transformation So far, we’ve specified vertex positions directly in NDC coordinates Simple approach Everything in [-1,+1] 3 cube gets drawn You can now handle the truth OpenGL actually provides a projective transformation from the glVertex3f parameters to NDC coordinates Expressed as a combination of two 4x4 matrix transforms on the (x,y,z,w) object-space position Initial value of both matrices are identity transforms Hence OpenGL initially takes glVertex * positions directly to NDC space The simple_trianlge example relies on this
  • 20. Vertex Transformation Object-space vertex position transformed by a general linear projective transformation Expressed as a 4x4 matrix
  • 21. Matrix Multiplication v’ = M v Where M is a matrix and v is a vector Use: coordinate system changes v = M -1 v’ Inverse matrix multiplication Uses: back projection, plane equation setup
  • 22. Two Transforms in Sequence OpenGL thinks of the projective transform as really two matrix transforms FIRST object-space to eye-space SECOND eye-space to clip-space 16 Multiply-Add operations Another 16 Multiply-Add operations
  • 23. Modelview-Projection Transform Matrixes can associate (combine) Combination of the modelview and projection matrix = modelview-projection matrix or often simply the “MVP” matrix 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. Setting the Modelview and Projection Matrices Easy way glLoadMatrixf (const GLfloat *matrix) matrix is 4x4 array of floats Warning: stored in column-major order So like FORTRAN, not C Replaces the current matrix with the new matrix Which matrix is replaced? Could be modelview or projection matrix? Depends on last call to glMatrixMode glMatrixMode ( GL_MODELVIEW ) glMatrixMode ( GL_PROJECTION )
  • 25. Careful: Beware of Selectors Danger: glMatrixMode is error-prone If you forget the state of glMatrixMode , you can easily update the wrong matrix Leads to very frustrating bugs Problem is glMatrixMode sets a selector which changes which state “other” matrix commands update The better API would have the matrix mode as a parameter to matrix commands, instead of a selector New EXT_direct_state_access extension has a “direct” command for setting matrix state glMatrixLoadfEXT ( GL_MODELVIEW , matrix) glMatrixLoadfEXT ( GL_PROJECTION , matrix)
  • 26. Many Matrix Manipulation Commands glLoadIdentity () Resets matrix to identity matrix Restores initial setting glMultMatrixf (const GLfloat *m) Multiplies current matrix by specified 4x4 matrix glTranslatef (x,y,z) Applies 3D translation to the current matrix glScalef (xs,ys,zs) Applies 3D (non-uniform) scaling to the current matrix glRotatef (angle,x,y,z) Applies rotation around a 3D axis to the current matrix glPushMatrix () Pushes current matrix to the matrix state Matrix now can be modified and later popped back glPopMatrix () The way to pop back to the prior matrix Push and pop is good for hierarchical modeling glOrtho (left, right, bottom, top, near, far) Multiplies current matrix by a 3D orthographic “box” Maps a box to the NDC [-1,+1] 3 cube glFrustum (left, right, bottom, top, near, far) Multiplies current matrix by a 3D perspective frustum Maps a camera view to the NDC [-1,+1] 3 cube 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. 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. Scale, Rotate, and Translate Example Add matrix code 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. Load Identity Transform Prototype glLoadIdentity () Replace current matrix with this matrix
  • 30. Non-uniform Scale Transform Prototype glScalef ( GLfloat xs, GLfloat ys, GLfloat zs) Post-concatenates this matrix
  • 31. Translate Transform Prototype glTranslatef ( GLfloat x, GLfloat y, GLfloat z) Post-concatenates this matrix
  • 32. Rotation Transform Prototype glRotatef ( GLfloat a, // angle in degrees GLfloat x, GLfloat y, GLfloat z) Post-concatenates a rotation matrix glRotatef (a,1,0,0) glRotatef (a,0,1,0) glRotatef (a,0,0,1) Axis could be arbitrary un-normalized direction vector
  • 33. Orthographic Transform Prototype glOrthof (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far) Post-concatenates an orthographic matrix
  • 34. Frustum Transform Prototype glFrustumf (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far) Post-concatenates a frustum matrix
  • 35. Two Transforms Concatenated Multiplying the position by First by the modelview Then second by the projection …is equivalent to multiplying by their concatenation so Just 16 Multiply-Add operations… even though 2 (combined) transformation matrices!
  • 36. Perspective Divide Divide clip-space (x,y,z) by clip-space w To get Normalized Device Coordinate (NDC) space Means reciprocal operation is done once And done after clipping Minimizes division by zero concern
  • 37. Viewport and Depth Range Prototypes glViewport ( GLint vx, GLint vy, GLsizei vw, GLsizei vh) glDepthRange ( GLclampd n, GLclampd f) Equations Maps NDC space to window space
  • 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. View Frustum Clipping Generalizes Cleanly Recall moving left vertex so it’s X = -1.8 Result is a clipped triangle (-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. 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. 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. 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. 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. Generalize to Non-1 W Affine clipping plane in example -1 ≤ x c Generalizes to 1 x c + 0 y c + 0z c + 1 w c ≥ 0 Looks like a plane equation A x c + B y c + C z c + D w c ≥ 0 with coefficients A, B, C, and D
  • 45. View Frustum Plane Equations All six view frustum planes can be described by simple projective plane equations -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. Projective Clipping Each vertex computes its clip distance w.r.t. a plane Plug vertex’s (x,y,z,w) into Ax+By+Cz+Dw≥0 plane equation… provides a clip distance For two vertexes forming a triangle edge Both negative? Discard the edge Both positive? Accept the edge (no clipping) One negative, one positive Clipping is needed Compute t as s / (s + p) where s and p are clip distances s is the “inside” distance; p is the “outside” distance Weight all per-vertex attributes based on t Makes new “clipped” vertex on the clip plane Generate 1 triangle if 1 of 3 vertices is inside; if 2 inside, generate 2 triangles Repeat process (recursively) for all clip planes Only slightly more complicated than prior clipping algorithm
  • 47. Readily Extends to User-defined Clip Planes In addition to the six view frustum planes of clip space… OpenGL supports user-defined clip planes Allows slicing into geometry Operate in eye space instead of clip space Enabled with glEnable ( GL_CLIP_PLANE0 + num ) Plane equation set by glClipPlane Clip planes are transformed current modelview projection matrix Plane equation is Ax e +By e +Cz e +Wz e ≥0 Instead of using (x c , y c , z c , w c ) as view frustum planes do
  • 48. (Clip) Plane Transformation Vertex positions (and direction vectors) are transformed like column vectors Plane equations are transformed like row vectors glClipPlane parameters glVertex4f parameters
  • 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. 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. Vertex Programmability Paletted matrix skinning Twister vertex program Per-vertex cartoon shading
  • 52. Other Vertex Processing Tasks Per-vertex lighting Given normal vectors at each vertex, view vector, material parameters, and light parameters, compute the lit appearance of the surface Texture coordinate generation Texture coordinates help map images (called “textures”) onto rasterized primitives Often texture coordinates are passed explicitly with each vertex Alternatively, a vertex shader can compute them Non-linear transformation Animation by blending various matrix transforms Twisting or morphing geometry Fog computations Arbitrary setup for fragment shaders
  • 53. Vertex Shader Example: Key Frame Blending Frame A Frame B Blended Frame 47% = + 53%
  • 54. Key Frame Blending Vertex Shader Frame A Frame B Other possible key frames
  • 55. Non-linear Vertex Transform + Fragment Shader Setup Fragment program Vertex program 2D grid over (s,t)  [0,1] Tessellated torus
  • 56. Next Lecture Graphics Math Interpolation, vector math, and number representations for computer graphics As usual, expect a short quiz on today’s lecture Know the form of a translate and scale matrix Know the order of operations in transforming a vertex position Assignments Reading Chapter 6, 310-322 Next project (Project #1) to be assigned Thursday, February 2 Building a 3D object model loader Due Thursday, February 16 2nd homework is on class web site, 5 pages, 10 problems