Projection Matrix Tricks. Eric Lengyel

Similar documents
CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

Projection and viewing. Computer Graphics CSE 167 Lecture 4

Robust Stencil Shadow Volumes. CEDEC 2001 Tokyo, Japan

Triangle Rasterization

Rasterization Overview

Mouse Ray Picking Explained

Overview. By end of the week:

Visibility: Z Buffering

CSE 167: Introduction to Computer Graphics Lecture #5: Projection. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2017

Shadows in the graphics pipeline

Game Architecture. 2/19/16: Rasterization

CS 381 Computer Graphics, Fall 2012 Midterm Exam Solutions. The Midterm Exam was given in class on Tuesday, October 16, 2012.

Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Overview. A real-time shadow approach for an Augmented Reality application using shadow volumes. Augmented Reality.

E.Order of Operations

Shadow Algorithms. CSE 781 Winter Han-Wei Shen

Lecture 3 Sections 2.2, 4.4. Mon, Aug 31, 2009

Computer Graphics Shadow Algorithms

CSE 167: Lecture #4: Vertex Transformation. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Computergrafik. Matthias Zwicker. Herbst 2010

Transforms 3: Projection Christian Miller CS Fall 2011

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

Computer Graphics with OpenGL ES (J. Han) Chapter VII Rasterizer

Computer Graphics 10 - Shadows

3D Viewing Episode 2

Z-Buffer hold pixel's distance from camera. Z buffer

Culling. Computer Graphics CSE 167 Lecture 12

Computer Graphics. Shadows

Rendering Objects. Need to transform all geometry then

Notes on Assignment. Notes on Assignment. Notes on Assignment. Notes on Assignment

CMSC427 Transformations II: Viewing. Credit: some slides from Dr. Zwicker

The Graphics Pipeline

Lecture 4. Viewing, Projection and Viewport Transformations

Perspective matrix, OpenGL style

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Performance OpenGL Programming (for whatever reason)

Evening s Goals. Mathematical Transformations. Discuss the mathematical transformations that are utilized for computer graphics

Models and Architectures

Perspective Shadow Maps. Shadow Map Aliasing. in particular for large scenes shadow maps impossible for infinite scenes

Geometry: Outline. Projections. Orthographic Perspective

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

3D Viewing. CS 4620 Lecture 8

Practical Shadow Mapping

The Graphics Pipeline. Interactive Computer Graphics. The Graphics Pipeline. The Graphics Pipeline. The Graphics Pipeline: Clipping

3D Polygon Rendering. Many applications use rendering of 3D polygons with direct illumination

CSE 167: Introduction to Computer Graphics Lecture #9: Visibility. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2018

OpenGL Transformations

Lecture 17: Shadows. Projects. Why Shadows? Shadows. Using the Shadow Map. Shadow Maps. Proposals due today. I will mail out comments

Three-Dimensional Viewing Hearn & Baker Chapter 7

Real-Time Shadows. Last Time? Today. Why are Shadows Important? Shadows as a Depth Cue. For Intuition about Scene Lighting

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

3D Programming. 3D Programming Concepts. Outline. 3D Concepts. 3D Concepts -- Coordinate Systems. 3D Concepts Displaying 3D Models

Advanced Lighting Techniques Due: Monday November 2 at 10pm

CMSC427 Advanced shading getting global illumination by local methods. Credit: slides Prof. Zwicker

Movie: For The Birds. Announcements. Ray Tracing 1. Programming 2 Recap. Programming 3 Info Test data for part 1 (Lines) is available

CSE528 Computer Graphics: Theory, Algorithms, and Applications

Viewing and Projection Transformations

Pipeline Operations. CS 4620 Lecture 14

CS 130 Final. Fall 2015

1.2.3 The Graphics Hardware Pipeline

3D Viewing. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 9

Viewing/Projections IV. Week 4, Fri Feb 1

CS4620/5620: Lecture 14 Pipeline

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

CSE328 Fundamentals of Computer Graphics

INTRODUCTION TO COMPUTER GRAPHICS. It looks like a matrix Sort of. Viewing III. Projection in Practice. Bin Sheng 10/11/ / 52

Pipeline Operations. CS 4620 Lecture 10

Problem Set 4 Part 1 CMSC 427 Distributed: Thursday, November 1, 2007 Due: Tuesday, November 20, 2007

Real-Time Shadows. MIT EECS 6.837, Durand and Cutler

S U N G - E U I YO O N, K A I S T R E N D E R I N G F R E E LY A VA I L A B L E O N T H E I N T E R N E T

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

CS 418: Interactive Computer Graphics. Projection

CSE 167: Introduction to Computer Graphics Lecture #11: Visibility Culling

Real-Time Graphics Architecture

COMS 4160: Problems on Transformations and OpenGL

Graphics and Interaction Rendering pipeline & object modelling

CSE 167: Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Volume Shadows Tutorial Nuclear / the Lab

CS452/552; EE465/505. Intro to Lighting

Virtual Cameras and The Transformation Pipeline

Reading on the Accumulation Buffer: Motion Blur, Anti-Aliasing, and Depth of Field

3D Graphics for Game Programming (J. Han) Chapter II Vertex Processing

Introduction to Computer Graphics with WebGL

Real-Time Shadows. Last Time? Schedule. Questions? Today. Why are Shadows Important?

RSX Best Practices. Mark Cerny, Cerny Games David Simpson, Naughty Dog Jon Olick, Naughty Dog

3D Viewing. CMPT 361 Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

Real-Time Shadows. Last Time? Textures can Alias. Schedule. Questions? Quiz 1: Tuesday October 26 th, in class (1 week from today!

TDA362/DIT223 Computer Graphics EXAM (Same exam for both CTH- and GU students)

What for? Shadows tell us about the relative locations. Vienna University of Technology 2

Prof. Feng Liu. Fall /19/2016

Last Time. Why are Shadows Important? Today. Graphics Pipeline. Clipping. Rasterization. Why are Shadows Important?

Hardware-driven Visibility Culling Jeong Hyun Kim

15. Clipping. Projection Transformation. Projection Matrix. Perspective Division

Announcements. Submitting Programs Upload source and executable(s) (Windows or Mac) to digital dropbox on Blackboard

3D Viewing Episode 2

CS184 : Foundations of Computer Graphics Professor David Forsyth Final Examination

COMP30019 Graphics and Interaction Perspective & Polygonal Geometry

Computer Viewing. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Perspective Mappings. Contents

Viewing Transformation

Transcription:

Projection Matrix Tricks Eric Lengyel

Outline Projection Matrix Internals Infinite Projection Matrix Depth Modification Oblique Near Clipping Plane Slides available at http://www.terathon.com www.terathon.com/

From Camera to Screen Camera Space Homogeneous Clip Space Normalized Device Coordinates Viewport Coordinates Projection Matrix Perspective Divide Viewport Transform

Projection Matrix The 4 4 projection matrix is really just a linear transformation in homogeneous space It doesn t actually perform the projection, but just sets things up right for the next step The projection occurs when you divide by w to get from homogenous coordinates to 3-space3

OpenGL projection matrix n, f = distances to near, far planes e = focal length = 1 / tan(fov / 2) a = viewport height / width e 0 0 0 0 ea 0 0 f + n 2fn 0 0 f n f n 0 0 1 0

Infinite Projection Matrix Take limit as f goes to infinity e 0 0 0 e 0 0 0 0 ea 0 0 0 ea 0 0 lim f + n 2fn = f 0 0 0 0 1 2n f n f n 0 0 1 0 0 0 1 0

Infinite Projection Matrix Directions are mapped to points on the infinitely distant far plane A direction is a 4D vector with w = 0 (and at least one nonzero x, y, z) Good for rendering sky objects Skybox, sun, moon, stars Also good for rendering stencil shadow volume caps

Infinite Projection Matrix The important fact is that z and w are equal after transformation to clip space: e 0 0 0 x ex 0 ea 0 0 y ( ea) y = 0 0 1 2n z z 0 0 1 0 0 z

Infinite Projection Matrix After perspective divide, the z coordinate should be exactly 1.0, meaning that the projected point is precisely on the far plane: ex ea y z z ( ) ex z ey az 1

Infinite Projection Matrix But there s a problem... The hardware doesn t actually perform the perspective divide immediately after applying the projection matrix Instead, the viewport transformation is applied to the (x,( y, z) ) coordinates first

Infinite Projection Matrix Ordinarily, z is mapped from the range [ 1, 1] in NDC to [0, 1] in viewport space by multiplying by 0.5 and adding 0.5 This operation can result in a loss of precision in the lowest bits Result is a depth slightly smaller than 1.0 or slightly bigger than 1.0

Infinite Projection Matrix If the viewport-space z coordinate is slightly bigger than 1.0, then fragment culling occurs The hardware thinks the fragments are beyond the far plane Can be corrected by enabling GL_DEPTH_CLAMP_NV, but this is a vendor-specific solution

Infinite Projection Matrix Universal solution is to modify projection matrix so that viewport-space z is always slightly less than 1.0 for points on the far plane: e 0 0 0 0 ea 0 0 0 0 ε 1 2 0 0 1 0 ( ε ) n

Infinite Projection Matrix This matrix still maps the near plane to 1, but the infinite far plane is now mapped to 1 ε ε 1 ( ε 2) n n n = 1 0 1 n ε 1 ( ε 2) n z z( ε 1) = 1 0 0 z

Depth Modification Several methods exist for performing polygon offset Hardware support through glpolygonoffset Fiddle with gldepthrange Tweak the projection matrix

Depth Modification glpolygonoffset works well, but Can adversely affect hierarchical z culling performance Not guaranteed to be consistent across different GPUs Adjusting depth range Reduces overall depth precision Both require extra state changes

Depth Modification NDC depth is given by a function of the lower-right right 2 2 portion of the projection matrix: f + n 2fn f + n 2fn z z f n f n = f n f n 1 1 0 z z NDC f + n 2fn = + f n z f n ( )

Depth Modification We can add a constant offset ε to the NDC depth as follows: f + n 2fn f n 2fn ε + ε z f n f n z f n f n = 1 1 0 z z NDC f + n 2fn = + + ε f n z f n ( )

Depth Modification w-coordinate unaffected Thus, x and y coordinates unaffected z offset is constant in NDC But this is not constant in camera space For a given offset in camera space, the corresponding offset in NDC depends on the depth

Depth Modification What happens to a camera-space offset δ? f + n 2fn f + n 2fn ( z δ + ) f n f n z + δ f n f n = 1 1 0 ( z + δ ) z NDC f + n 2fn 2fn δ = + f n z( f n) f n z( z+ δ )

Depth Modification NDC offset as a function of camera- space offset δ and camera-space z: ε( δ, z) 2fn δ = f n z( z+ δ ) Remember, δ is negative for an offset toward camera

Depth Modification Need to make sure that ε is big enough to make a difference in a typical 24-bit integer z buffer NDC range of [ 1,1] is divided into 2 24 possible depth values So ε should be at least 2/2 24 = 2 23

Depth Modification But we re adding ε to (f( + n)/( )/(f n), which is close to 1.0 Not enough bits of precision in 32-bit float for this So in practice, it s necessary to use 21 7 ε 2 4.77 10

Oblique Near Clipping Plane It s sometimes necessary to restrict rendering to one side of some arbitrary plane in a scene For example, mirrors and water surfaces Using an extra hardware clipping plane seems like the ideal solution

Oblique Near Clipping Plane But some older hardware doesn t support user clipping planes Enabling a user clipping plane could require modifying your vertex programs There s a slight chance that a user clipping plane will slow down your fragment programs

Oblique Near Clipping Plane Extra clipping plane almost always redundant with near plane No need to clip against both planes

Oblique Near Clipping Plane We can modify the projection matrix so that the near plane is moved to an arbitrary location No user clipping plane required No redundancy

Oblique Near Clipping Plane In NDC, the near plane has coordinates (0, 0, 1, 1)

Oblique Near Clipping Plane Planes are transformed from NDC to camera space by the transpose of the projection matrix So the plane (0, 0, 1, 1) becomes M 3 + M 4, where M i is the i-th row of the projection matrix M 4 must remain (0, 0, 1, 0) so that perspective correction still works right

Oblique Near Clipping Plane Let C = (C( x, C y, C z, C w ) be the camera- space plane that we want to clip against instead of the conventional near plane We assume the camera is on the negative side of the plane, so C w < 0 We must have C = M 3 + M 4, where M 4 = (0, 0, 1, 0)

Oblique Near Clipping Plane M 3 = C M 4 = (C( x, C y, C z + 1, C w ) e 0 0 0 0 ea 0 0 M = Cx Cy Cz + 1 Cw 0 0 1 0 This matrix maps points on the plane C to the z = 1 plane in NDC

Oblique Near Clipping Plane But what happens to the far plane? F = M 4 M 3 = 2M 4 C

Oblique Near Clipping Plane Far plane is completely hosed! Depths in NDC no longer represent distance from camera plane, but correspond to the position between the oblique near and far planes We can minimize the effect, and in practice it s not so bad

Oblique Near Clipping Plane We still have a free parameter: the clipping plane C can be scaled Scaling C has the effect of changing the orientation of the far plane F We want to make the new view frustum as small as possible while still including the conventional view frustum

Oblique Near Clipping Plane Let F = 2M 4 ac Choose the point Q which lies opposite the near plane in NDC: Q = ( sgn ( Cx),sgn( Cy),1,1) Solve for a such that Q lies in plane F: a = 2M4 Q CQ

Oblique Near Clipping Plane Near plane doesn t move, but far plane becomes optimal

Oblique Near Clipping Plane This also works for infinite view frustum Far plane ends up being parallel to one of the edges between two side planes For more analysis, see Journal of Game Development, Vol 1, No 2

Questions? lengyel@terathon.com