Upcoming SlideShare
×

Like this presentation? Why not share!

# CS 354 Graphics Math

## on Feb 07, 2012

• 1,214 views

CS 354 Computer Graphics

CS 354 Computer Graphics

University of Texas, Austin

February 2, 2012

### Views

Total Views
1,214
Views on SlideShare
1,213
Embed Views
1

Likes
1
58
0

### Categories

Uploaded via as Microsoft PowerPoint

### Report content

• Comment goes here.
Are you sure you want to
Your message goes here

## CS 354 Graphics MathPresentation Transcript

• CS 354 Graphics Math Mark Kilgard University of Texas February 2, 2012
• Today’s material
• Homework #2 due today
• In-class quiz
• Lecture topic: graphics math & projective transformations
• Homogenous coordinates
• Orthographic and frustum projective transformations
• Assignment
• Chapter 6, pages 323-330
• Project #1 to be assigned soon
• Building a 3D object model loader
• Due Thursday, February 16
• Look for announcement on piazza and course web site for details
• Final exam will be moved to the exam period
• (Instead of last day of class)
• Making last day of class a review session instead
• Details coming
• I’m going to start providing daily quiz solutions on piazza
• Provide you more feedback
• Also opportunity to expound on significance of the quiz questions
• Expect versions of quiz questions to re-appear on mid-term and final exams
• 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
• Getting coordinates from object space to NDC space
• Generalized clipping and culling
• OpenGL’s matrix manipulation API
• This lecture
• Consequences of homogeneous coordinates
• More graphics mathematics
• Daily Quiz
• In homogenous 3D space, which one of these (x,y,z,w) positions is NOT co-located (meaning specifying the same position): (5, -2, 7, 2) (2.5, 1, 3.5, 1) (-15, 6, -21, -6) (5000,-2000,7000,2000)
• A scalar “MAD” performs either a multiply (A×B) or multiply-add (A×B+C) operation. How many scalar MAD operations are required to multiply a general 4x4 matrix by a 4-component vector?
• Arrange in “first to last” order the following coordinate transforms in the conceptual vertex transformation pipeline: a) modelview transform b) projection transform c) perspective divide d) viewport & depth range transform
• On a sheet of paper
• Write your EID, name, and date
• Write #1, #2, #3, followed by its answer
• 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 )
• 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”
• 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)
• Subtracting Homogeneous Coordinates
• Say we have
• A = (xa,ya,za,wa)
• B = (xb,yb,zb,wb)
• What if we want to know the difference between these two homogeneous positions?
• We want B-A
• What does that mean?
• If wa=1 and wb=1, then we might expect B-A = (xb-xa,yb-ya,zb-za, 1 )
• Actually w result will be zero so B-A = (xb-xa,yb-ya,zb-za, 0 )
• When w=0, that indicates a direction rather than a position
• Let’s explore why this is—and why it makes sense
• Thinking about Fractions
• Say I want to subtract 2/3 from 4/5 so
• 4/5 – 2/3
• Clearly the answer is not (4-2)/(5-3)=2/2=1
• Calculator says 0.8-0.6666=0.13334
• Instead we need to homogenize the denominator so
• 3×(4/5) - 5×(2/3) = 12/15 – 10/15 = 2/15
• And 2/15 ≈ 0.13334
• Homogenous coordinates are like fractions
• (x,y,z,w) ≈ (x/w,y/w,z/w,w/w)
• So let’s try making a consistent denominator
• Consistent Denominators for Homogenous Coordinates
• A=wa×wb×(xa/wa,ya/wa,za/wa,wa/wa)
• a.k.a. A=(wb×xa,wb×ya,wb×za,wa×wb)
• B=wa×wb×(xb/wb,yb/wb,zb/wb,wb/wb)
• a.k.a. B=(wa×xb,wa×yb,wa×zb,wa×wb)
• Now subtract B-A and get an answer
• B-A = (wa×xb-wb×xa,wa×yb-wb×ya,wa×zb-wb×za,0)
• Think of a vector with w=0 as being a position “at infinity” in the direction of (x,y,z)
• Very powerful idea—expect us to revisit this
• 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”
• NDC Space Clip Cube (-1,-1,-1) (+1,-1,-1) (+1,+1,-1) (-1,+1,-1) (-1,-1,+1) (+1,+1,+1) (+1,-1,+1) (-1,+1,+1) Post-perspective divide puts the region surviving clipping within the [-1,+1] 3
• Clip Space Clip Cube (x min /w,y min /w,z min /w) Pre-perspective divide puts the region surviving clipping within -w ≤ x ≤ w, -w ≤ y ≤ w, -w ≤ z ≤ w (x max /w,y min /w,z min /w) (x max /w,y min /w,z max /w) (x min /w,y min /w,z max /w) (x max /w,y max /w,z max /w) (x max /w,y max /w,z min /w) (x min /w,y max /w,z min /w) (x min /w,y max /w,z max /w) Constraints x min = -w x max = w y min = -w y max = w z min = -w z max = w w>0
• Window Space Clip Cube (x,y,zNear) (x+w,y,zNear) (x+w,y+h,zNear) (x,y+h,zNear) (x,y,zFar) (x+w,y+h,zFar) (x+w,y,zFar) (x,y+h,zFar) Assuming glViewport(x,y,w,h) and glDepthRange(zNear,zFar) Constraints w>0 h>0 0 ≤ zNear ≤ 1 0 ≤ zFar ≤ 1
• Vertex Transformation
• Object-space vertex position transformed by a general linear projective transformation
• Expressed as a 4x4 matrix
• Orthographic Transform
• Prototype
• glOrtho ( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)
• Post-concatenates an orthographic matrix
• glOrtho Example
• Consider
• glLoadIdentity (); glOrtho (-20, 30, 10, 60, 15, -25)
• left=-20, right=30, bottom=10, top=50, near=15, far=-25
• Matrix
= -Z axis
• Transform All Box Corners
• Consider
• glLoadIdentity (); glOrtho (-20, 30, 10, 60, 15, -25);
• l=-20, r=30, b=10, t=50, n=15, f=-25
• Eight box corners: (-20,10,-15), (-20,10,25), (-20, 50,-15), (-20, 50,-25), (30,10,-15), (30,10,25), (30,50,-15), (30,50,25)
• Transform each corner by the 4x4 matrix
8 corners in column vector (position) form keep in mind : looking down the negative Z axis… so Z box coordinates are negative n (-15) and negative f (+25)
• Box Corners in Clip Space Observe: result is “corners” of clip space (and NDC) clip cube Think of glOrtho as a transform parameterization to map an axis-aligned box to the clip cube 8 “eye space” corners in column vector (position) form
• Orientation of OpenGL’s Eye-space Coordinate System -Z direction “ looking into the screen” +X -X +Y -Y +Z direction “ poking out of the screen”
• OpenGL Rule-of-Thumb for Handedness of Coordinate Systems
• When
• Object coordinate system is right-handed,
• Modelview transform is generated from one or more of the commands glTranslate , glRotate , and glScale with positive scaling values,
• Projection transform is loaded with glLoadIdentity followed by exactly one of glOrtho or glFrustum,
• Near value specified for glDepthRange is less than the far value;
• Then
• Eye coordinate system is right-handed
• Clip, NDC, and window coordinate systems are left-handed
• Conventional OpenGL Handedness
• Right-handed
• Object space
• Eye space
• Left-handed
• Clip space
• Normalized Device Coordinate (NDC) space
• Window space
Positive depth is further from viewer In eye space, eye is “looking down” the negative Z axis
• Frustum Transform
• Prototype
• glFrustum ( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)
• Post-concatenates a frustum matrix
• glFrustum Example
• Consider
• glLoadIdentity (); glFrustum (-30, 30, -20, 20, 1, 1000)
• left=-30, right=30, bottom=-20, top=20, near=1, far=1000
• Matrix
= -Z axis symmetric left/right & top/bottom so zero
• Transform All Box Corners
• Consider
• glLoadIdentity (); glFrustum (-30, 30, -20, 20, 1, 1000)
• left=-30, right=30, bottom=-20, top=20, near=1, far=1000
• Eight box corners: (-30,-20,-1), (-30,-20,-1000), (-30, 20,-1), (-30, 20,-1000), (30,10,-1), (30,10,-1000), (30,50,-1), (30,50,-1000)
• Transform each corner by the 4x4 matrix
8 in column vector (position) form keep in mind : looking down the negative Z axis… so Z box coordinates are negative n (-1) and negative f (-1000) near near near near far far far far
• Box Corners in Clip Space 8 “eye space” corners in column vector (position) form
• Box Corners in NDC Space
• Perform perspective divide
Observe: W component is 1 (at near plane) or 1/1000 (at far plane) Also observe that Z component is always -1 (assuming W=1 eye-space positions)
• Frustum Visualization
• Think of a frustum as a camera’s view
• Conclusions about glOrtho and glFrustum
• These OpenGL commands provide a parameterized transform mapping eye space into the “clip cube”
• Each command
• glOrtho is orthographic
• glFrustum is single-point perspective
• 1-, 2-, and 3-point Perspective
• A 4x4 matrix can represent 1, 2, or 3 vanishing points
• As well as zero for orthographic views
3-point perspective 2-point perspective 1-point perspective
• Perspective in Art History [Pietro Perugino, 1482]
• Perspective in Art History [Pietro Perugino, 1482] Vanishing point
• Humanist Analysis of Perspective [Albrecht Dürer, 1471]
• 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
• Correspondence of Eye Space to NDC Space Eye space Normalized Device Coordinate (NDC) space “ behind the eye” “ beyond the far clip plane” “ between eye and near clip plane” “ rendered (visible) region” [Eric Lengyel]
• 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 )
• 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 Transformation in Vertex Shaders
• GLSL
• Cg/HLSL
void transform( float4 pos : POSITION , float4 col : COLOR , out float4 oPos : POSITION , out float4 oCol : COLOR , uniform float4x4 mvp) { // Pass through color oCol = color; // Transform position from object // space to clip space oPos = mul (mvp, pos); } void main(void) { gl_FrontColor = gl_Color ; gl_Position = ftransform (); } void main(void) { gl_FrontColor = gl_Color ; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex } Or
• Intrigued by Homogenous Coordinates and Projective Transformations?
• Read A Trip Down the Graphics Pipeline
• By Jim Blinn
• Particularly the “The Homogeneous Perspective Transform” chapter
• Entire book is great
• Informal style
• Based on Blinn’s IEEE Computer Graphics & Applications column
• Opportunity to develop your intuition for graphics
• Two more books in the series—also great
• Next Lecture
• Graphics Math
• Viewing, modeling, and quaternions
• As usual, expect a short quiz on today’s lecture
• Know how frustum and ortho matrices transform points
• Assignments