Space. Projection Matrix. Homogeneous Clip Space. Perspective Divide. Normalized Device Coordinates. Coordinates. Viewport Transform.

Similar documents
Image Processing and Computer Graphics. Rendering Pipeline. Matthias Teschner. Computer Science Department University of Freiburg

INTRODUCTION TO RENDERING TECHNIQUES

3D Viewing. Chapter 7. Projections. 3D clipping. OpenGL viewing functions and clipping planes

Monash University Clayton s School of Information Technology CSE3313 Computer Graphics Sample Exam Questions 2007

The In and Out: Making Games Play Right with Stereoscopic 3D Technologies. Samuel Gateau

CS 4204 Computer Graphics

Grassmann Algebra in Game Development. Eric Lengyel, PhD Terathon Software

1 Determinants and the Solvability of Linear Systems

Geometry Chapter Point (pt) 1.1 Coplanar (1.1) 1.1 Space (1.1) 1.2 Line Segment (seg) 1.2 Measure of a Segment

Realtime 3D Computer Graphics Virtual Reality

Lesson 26: Reflection & Mirror Diagrams

Geometric Optics Converging Lenses and Mirrors Physics Lab IV

Section Continued

Light and its effects

2x + y = 3. Since the second equation is precisely the same as the first equation, it is enough to find x and y satisfying the system

Equations, Lenses and Fractions

Lecture 2: Homogeneous Coordinates, Lines and Conics

Scan-Line Fill. Scan-Line Algorithm. Sort by scan line Fill each span vertex order generated by vertex list

The Graphical Method: An Example

Lenses and Telescopes

L 2 : x = s + 1, y = s, z = 4s Suppose that C has coordinates (x, y, z). Then from the vector equality AC = BD, one has

Shader Model 3.0. Ashu Rege. NVIDIA Developer Technology Group

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Section 8.2 Solving a System of Equations Using Matrices (Guassian Elimination)

EXPERIMENT 6 OPTICS: FOCAL LENGTH OF A LENS

Beyond 2D Monitor NVIDIA 3D Stereo

Convex Mirrors. Ray Diagram for Convex Mirror

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Introduction to Computer Graphics

4BA6 - Topic 4 Dr. Steven Collins. Chap. 5 3D Viewing and Projections

Largest Fixed-Aspect, Axis-Aligned Rectangle

A =

Dynamic Resolution Rendering

Chapter 6: Sensitivity Analysis

Optimizing AAA Games for Mobile Platforms

Pigeonhole Principle Solutions

The GED math test gives you a page of math formulas that

Lezione 4: Grafica 3D*(II)

Computer Graphics. Introduction. Computer graphics. What is computer graphics? Yung-Yu Chuang

Solving Simultaneous Equations and Matrices

Geometric Camera Parameters

Deferred Shading & Screen Space Effects

GUI GRAPHICS AND USER INTERFACES. Welcome to GUI! Mechanics. Mihail Gaianu 26/02/2014 1

Algebra 2 Chapter 1 Vocabulary. identity - A statement that equates two equivalent expressions.

Graph Theory Lecture 3: Sum of Degrees Formulas, Planar Graphs, and Euler s Theorem Spring 2014 Morgan Schreffler Office: POT 902

COMP175: Computer Graphics. Lecture 1 Introduction and Display Technologies

Thin Lenses Drawing Ray Diagrams

4. CAMERA ADJUSTMENTS

Anamorphic Projection Photographic Techniques for setting up 3D Chalk Paintings

VRayPattern also allows to curve geometry on any surface

Basic Problem: Map a 3D object to a 2D display surface. Analogy - Taking a snapshot with a camera

8 Square matrices continued: Determinants

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

The Four Seasons. A Warm Up Exercise. A Warm Up Exercise. A Warm Up Exercise. The Moon s Phases

Chapter 17: Light and Image Formation

Computer Graphics Hardware An Overview

Continued Fractions and the Euclidean Algorithm

Triangle Scan Conversion using 2D Homogeneous Coordinates

521493S Computer Graphics. Exercise 2 & course schedule change

Game Development in Android Disgruntled Rats LLC. Sean Godinez Brian Morgan Michael Boldischar

Chapters 2 and 6 in Waseda. Lesson 8 Lattice Planes and Directions. Suggested Reading

How To Make A Solid Body In A Part Of A Part In A Model In A Toybox

2) A convex lens is known as a diverging lens and a concave lens is known as a converging lens. Answer: FALSE Diff: 1 Var: 1 Page Ref: Sec.

OpenGL Performance Tuning

Blender Notes. Introduction to Digital Modelling and Animation in Design Blender Tutorial - week 9 The Game Engine

NVPRO-PIPELINE A RESEARCH RENDERING PIPELINE MARKUS TAVENRATH MATAVENRATH@NVIDIA.COM SENIOR DEVELOPER TECHNOLOGY ENGINEER, NVIDIA

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Core Maths C1. Revision Notes

The Evolution of Computer Graphics. SVP, Content & Technology, NVIDIA

The Car Tutorial Part 1 Creating a Racing Game for Unity

Binary Adders: Half Adders and Full Adders

Linear Programming Notes VII Sensitivity Analysis

Linear Programming. March 14, 2014

Linear Algebra Notes

SketchUp Instructions

A Crash Course on Programmable Graphics Hardware

SkillsUSA 2014 Contest Projects 3-D Visualization and Animation

Columbus College of Art & Design 1 Charlotte Belland

Introduction to GPGPU. Tiziano Diamanti

Digital Photography Composition. Kent Messamore 9/8/2013

Chapter 9. Systems of Linear Equations

Linear Programming. Solving LP Models Using MS Excel, 18

Reflection and Refraction

Advanced Visual Effects with Direct3D

Linear Equations in Linear Algebra

The small increase in x is. and the corresponding increase in y is. Therefore

Sum of Degrees of Vertices Theorem

What Does Your Quadratic Look Like? EXAMPLES

TWO-DIMENSIONAL TRANSFORMATION

WAVELENGTH OF LIGHT - DIFFRACTION GRATING

Cloud function tutorial

EQUATIONS and INEQUALITIES

MAT 200, Midterm Exam Solution. a. (5 points) Compute the determinant of the matrix A =

OPTICAL IMAGES DUE TO LENSES AND MIRRORS *

Transformations in the pipeline

3D Stereoscopic Game Development. How to Make Your Game Look

Affine Transformations

Geometry Review Flash Cards

CHAPTER 5 Round-off errors

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/

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

Projection Matrix The 4 44 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 ide by w to get from homogenous coordinates to 3-space

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 it 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 it 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 it 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 2nz z 0 0 1 0 0 z

Infinite it 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 eay z z ex z ey az 1

Infinite it 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 it 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 it 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 it 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 it 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 1 2 n n 1 0 1 n 1 2n z z 1 1 0 0 z

Infinite it Projection Matrix Because we re calculating 1 and 2, we need to choose 2 2.410 22 7 so that 32-bit floating-point precision limits aren t exceeded

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 22 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 2 fn 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 2 fn f n 2 fn 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 positive 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.810

Oblique Near Clipping i Plane It s sometimes necessary to restrict rendering to one side of some arbitrary plane in a scene For example, mirrors and water surfaces

Oblique Near Clipping i Plane Using an extra hardware clipping plane seems like the ideal solution 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 t a user clipping i plane will slow down your fragment programs

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

Oblique Near Clipping i 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 i Plane In NDC, the near plane has coordinates (0, 0, 1, 1)

Oblique Near Clipping i 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 i 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 4, where M 4 = (0, 0, 1, 0)

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

Oblique Near Clipping i Plane But what happens to the far plane? F = M 4 M 3 = 2M 4 C Near plane and far plane differ only in the z coordinate Thus, they must coincide where they intersect the z = 0 plane

Oblique Near Clipping i Plane Far plane is completely hosed!

Oblique Near Clipping i Plane 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 i 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 i Plane Let F = 2M 4 ac Choose the point Q which lies furthest opposite the near plane in NDC: Q M C C 1 sgn,sgn,1,1 1 Solve for a such that Q lies in plane F (i.e., F Q = 0): x y a 2 CQ 4 M Q

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

Oblique Near Clipping i 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