SlideShare a Scribd company logo
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 Reading 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
Administrative 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” 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)
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 Think about 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 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

More Related Content

PDF
OpenGL 4.6 Reference Guide
PDF
PyQGIS와 PyQt를 이용한 QGIS 기능 확장
PDF
Mask-RCNN for Instance Segmentation
PPTX
Computer graphics.
PPTX
High Dynamic Range color grading and display in Frostbite
PPT
CS 354 Introduction
PPTX
OpenGL Introduction
PDF
סיכום הקורס בבינה מלאכותית
OpenGL 4.6 Reference Guide
PyQGIS와 PyQt를 이용한 QGIS 기능 확장
Mask-RCNN for Instance Segmentation
Computer graphics.
High Dynamic Range color grading and display in Frostbite
CS 354 Introduction
OpenGL Introduction
סיכום הקורס בבינה מלאכותית

What's hot (20)

PPTX
Python qgis advanced
PDF
Tutorial on Polynomial Networks at CVPR'22
PPTX
Opengl presentation
PPT
Projection Matrices
PPTX
A Short Study of Galois Field
PDF
OpenGL NVIDIA Command-List: Approaching Zero Driver Overhead
PPTX
Matrix chain multiplication by MHM
PPTX
Mid point line Algorithm - Computer Graphics
PDF
Conditional Image Generation with PixelCNN Decoders
PDF
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
PDF
Lecture 1: Deep Learning for Computer Vision
PPTX
DDA algorithm
PPTX
Computer graphics - bresenham line drawing algorithm
PPTX
Introduction to computer graphics
PDF
FOSS4G Firenze 2022 참가기
PPTX
QGIS 활용
PPT
Open Graphics Library
PPTX
Object rendering
PDF
An introduction on normalizing flows
PPTX
Physically Based and Unified Volumetric Rendering in Frostbite
Python qgis advanced
Tutorial on Polynomial Networks at CVPR'22
Opengl presentation
Projection Matrices
A Short Study of Galois Field
OpenGL NVIDIA Command-List: Approaching Zero Driver Overhead
Matrix chain multiplication by MHM
Mid point line Algorithm - Computer Graphics
Conditional Image Generation with PixelCNN Decoders
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Lecture 1: Deep Learning for Computer Vision
DDA algorithm
Computer graphics - bresenham line drawing algorithm
Introduction to computer graphics
FOSS4G Firenze 2022 참가기
QGIS 활용
Open Graphics Library
Object rendering
An introduction on normalizing flows
Physically Based and Unified Volumetric Rendering in Frostbite
Ad

Viewers also liked (8)

PPTX
Top 10 Graphic Design Mistakes - Part 1
PDF
FREE - Top 200 Graphic Design Terms Reference Guide
PDF
Creo tookit geometric traversal and evaluator
PDF
OpenGL Interaction
PPT
CS 354 Shadows
PDF
20090924 姿勢推定と回転行列
PPTX
Projection In Computer Graphics
PPT
Lecture 11 Perspective Projection
Top 10 Graphic Design Mistakes - Part 1
FREE - Top 200 Graphic Design Terms Reference Guide
Creo tookit geometric traversal and evaluator
OpenGL Interaction
CS 354 Shadows
20090924 姿勢推定と回転行列
Projection In Computer Graphics
Lecture 11 Perspective Projection
Ad

Similar to CS 354 Graphics Math (20)

PPT
CS 354 Object Viewing and Representation
DOC
Chapter 04 answers
PPT
CS 354 More Graphics Pipeline
PPT
Transforms UNIt 2
PPT
2 d transformation
PDF
The Day You Finally Use Algebra: A 3D Math Primer
PPT
CS 354 Transformation, Clipping, and Culling
PDF
AppsDiff3c.pdf
PPTX
3D Graphics : Computer Graphics Fundamentals
PDF
Visual surface detection computer graphics
PPT
1533 game mathematics
PPT
MT T4 (Bab 3: Fungsi Kuadratik)
PDF
Gremlin's Graph Traversal Machinery
PDF
DataStax | Graph Computing with Apache TinkerPop (Marko Rodriguez) | Cassandr...
PPT
Calc 7.1a
PPT
Modeling Transformations
PDF
ML-CheatSheet (1).pdf
PDF
7.curves Further Mathematics Zimbabwe Zimsec Cambridge
PDF
VoxelNet
PPT
Chapter 3 Image Processing: Basic Transformation
CS 354 Object Viewing and Representation
Chapter 04 answers
CS 354 More Graphics Pipeline
Transforms UNIt 2
2 d transformation
The Day You Finally Use Algebra: A 3D Math Primer
CS 354 Transformation, Clipping, and Culling
AppsDiff3c.pdf
3D Graphics : Computer Graphics Fundamentals
Visual surface detection computer graphics
1533 game mathematics
MT T4 (Bab 3: Fungsi Kuadratik)
Gremlin's Graph Traversal Machinery
DataStax | Graph Computing with Apache TinkerPop (Marko Rodriguez) | Cassandr...
Calc 7.1a
Modeling Transformations
ML-CheatSheet (1).pdf
7.curves Further Mathematics Zimbabwe Zimsec Cambridge
VoxelNet
Chapter 3 Image Processing: Basic Transformation

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)

PPT
Teaching material agriculture food technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
cuic standard and advanced reporting.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
A Presentation on Artificial Intelligence
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Approach and Philosophy of On baking technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectral efficient network and resource selection model in 5G networks
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Network Security Unit 5.pdf for BCA BBA.
cuic standard and advanced reporting.pdf
Review of recent advances in non-invasive hemoglobin estimation
A Presentation on Artificial Intelligence
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Unlocking AI with Model Context Protocol (MCP)
Chapter 3 Spatial Domain Image Processing.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

CS 354 Graphics Math

  • 1. CS 354 Graphics Math Mark Kilgard University of Texas February 2, 2012
  • 2. Today’s material Homework #2 due today In-class quiz Lecture topic: graphics math & projective transformations Homogenous coordinates Orthographic and frustum projective transformations Assignment Reading 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
  • 3. Administrative 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
  • 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 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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 )
  • 10. 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
  • 11. 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)
  • 12. 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
  • 13. 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
  • 14. Consistent Denominators for Homogenous Coordinates Think about 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
  • 15. 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
  • 16. 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”
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. Vertex Transformation Object-space vertex position transformed by a general linear projective transformation Expressed as a 4x4 matrix
  • 21. Orthographic Transform Prototype glOrtho ( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) Post-concatenates an orthographic matrix
  • 22. 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
  • 23. 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)
  • 24. 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
  • 25. 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”
  • 26. 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
  • 27. 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
  • 28. Frustum Transform Prototype glFrustum ( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) Post-concatenates a frustum matrix
  • 29. 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
  • 30. 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
  • 31. Box Corners in Clip Space 8 “eye space” corners in column vector (position) form
  • 32. 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)
  • 33. Frustum Visualization Think of a frustum as a camera’s view
  • 34. 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
  • 35. 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
  • 36. Perspective in Art History [Pietro Perugino, 1482]
  • 37. Perspective in Art History [Pietro Perugino, 1482] Vanishing point
  • 38. Humanist Analysis of Perspective [Albrecht Dürer, 1471]
  • 39. 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
  • 40. 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]
  • 41. 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
  • 42. 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 )
  • 43. 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!
  • 44. 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
  • 45. 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
  • 46. 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 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