Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

16
Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8

Transcript of Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Page 1: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Computer GraphicsWorld, View and Projection Matrices

CO2409 Computer Graphics

Week 8

Page 2: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Lecture ContentsLecture Contents

1. Viewing a 3D Scene

2. Camera Positioning

3. The View Matrix

4. Camera Internals

5. Projecting 3D Vertices to 2D

6. Projection Matrices

Page 3: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Model SpaceModel Space

• Scene models are defined in their own local coordinate system called Model Space

• E.g. this square is defined as four vertices in model space:– A(5,5,0), B(-5,5,0), C(-5,-5,0), D(5,-5,0)

• The origin in model space is set to the model “centre”– We want rotation and scaling to work with a

sensible centre– Recall the standard rotation & scaling matrices

operate around the origin

Page 4: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Model Space to World SpaceModel Space to World Space

– To position it in world space

• This is called the World Matrix– Transforms the vertices of the model

from model space into world space

• Build the matrix by combining (multiplying) basic transforms:– Model 1 uses just a translation matrix

• It is moved away from the origin

– Model 2 uses a combination of a Z-rotation (first), then a translation

• It is rotated first, then moved

101510

0100

0045cos45sin

0045sin45cos

2

101015

0100

0010

0001

1

xWorldMatri

xWorldMatri

• Every scene model has a positioning matrix

Page 5: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Model PositioningModel Positioning

• Recall the position and rotation of a model can be specified by defining its local space:– Position P and axes X,Y & Z (right, up

and forward directions)– Defined in world space

• Can be written in a matrix…– …this time add a fourth column from the

identity (explained last lecture):

• It turns out that this is another way to get the world matrix of the model– Same result as multiplying together

basic transformations to create it

• So two ways to think about a world matrix…

1

0

0

0

ZYX

ZYX

ZYX

ZYX

PPP

FFF

UUU

RRR

Page 6: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Building World MatricesBuilding World Matrices

• Two ways to build a world matrix for a model:(a) Make a matrix from the desired position and axes

• i.e. fill in the values of the matrix on the last slide

(b) Combine some basic transformations to move/rotate/scale the model from its original location

• E.g. WorldMatrix = Rot(X) * Rot(Y) * Trans(P)

• Every time we render our scene we either:– Rebuild the world matrices from scratch

• From simpler variables, which we update to move the model• E.g. Store x,y,z position and x,y,z rotation as floats, then build world

matrix from basic transformation matrices made using these values

– Or store the matrix persistently, only changing it as necessary• Update a world matrix by multiplying by another transform• E.g. to rotate a model, multiply its world matrix by a rotation matrix

Page 7: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Viewing a 3D SceneViewing a 3D Scene

• We have only considered the models in a scene– We haven’t considered the viewer

• We treat the viewer as an actual object in the 3D environment, looking around– This is different from 2D where the viewer is considered to be

outside of the scene, looking in

• The viewer has position and orientation just like a model

• The viewer (or viewpoint) is usually considered to be a person (shown as an eye) or a camera– Will use the camera analogy in this module– But you will see the eye used in some text books / web pages

Page 8: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Camera PositioningCamera Positioning

• The camera is positioned like any other object in world space– Its local coordinate system is called

Camera Space– It views down its own z-axis

• We can create a matrix to position the camera just like a model– But it is not called the world matrix– We’ll call it the camera matrix

• This matrix can be manipulated like the world matrix of model– To position and orient the camera– Note: scaling the camera is unusual, it

would effectively scale the entire world

1

0

0

0

CZCYCX

CZCYCX

CZCYCX

CZCYCX

PPP

FFF

UUU

RRR

Page 9: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

World to Camera SpaceWorld to Camera Space

• To render pixels on the screen, need to know how the models are positioned relative to the camera

– What is in front of the camera, what is behind it etc…

• Need to transform model vertices from world space (top diagram) to camera space (bottom diagram)

• Maybe the camera matrix described on last slide can do this transformation?

• No, it goes the opposite way…– It would transform a vertex from camera

space into world space – not very useful

Page 10: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Inverting the Camera MatrixInverting the Camera Matrix

• We need another matrix that performs the inverse of the camera matrix described above

• Finding the inverse of a matrix is a well-known mathematical technique– We won’t cover the maths here, easily found in text book / web– Process is slow and best done rarely

• Finding the inverse is easy if the camera matrix is built from basic transforms:If Cam = Rot(Z) * Rot(Y) * Rot(X) * Translate(P)then Cam Inverse = Translate(-P) * Rot(-X) * Rot(-Y) * Rot(-Z)– Build the inverse by combining same basic transformations– But reverse the amounts & reverse the order of the multiplication

Page 11: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

The View MatrixThe View Matrix

• This inverted camera matrix is called the View Matrix• We can calculate its general form:

– Same result as multiplication on the last slide– Although we rarely utilise this form

• So three ways to make the view matrix– Use a camera matrix – just like a world matrix for the camera,

then calculate the view matrix using the mathematical inverse– Create it from basic transformations (in reverse)– Fill in the matrix above

1

0

0

0

CCCCCC

CZCZCZ

CYCYCY

CXCXCX

ZPYPXP

FUR

FUR

FUR

(Dot products on the bottom line)

Page 12: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Using World & View MatricesUsing World & View Matrices

• Start with a model made of vertices provided by artist

• Give each model a world matrix and a create a view matrix for the camera

• Transform (multiply) each model vertex by the world matrix– To convert the models from model space

into world space

• Then transform these world space models by view matrix– Into camera space

• Now have the 3D world viewed from the perspective of the camera

Page 13: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Camera Internals 1Camera Internals 1

• Camera space is 3D, we actually display a 2D projection of it

• We project the 3D camera space models onto the 2D viewport

• The viewport is assumed to be an actual rectangle in the world

• Fixed in front of the camera

• Rays are projected from the 3D camera space vertices to the position of the camera– Passing through the viewport– Where they strike are the 2D vertices

Page 14: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Camera Internals 2Camera Internals 2

• The viewport sits at a fixed distance in front of the camera– Sometimes called the near clip

distance because objects cannot be seen nearer than this distance

• It covers an angle from the camera called the field of view (FOV)– FOV can be different in X & Y

• Rays from the 3D models that hit the viewport define 2D vertices– And meshes become 2D polygons– We can render this with 2D methods

Page 15: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Projection MatricesProjection Matrices

• The details of the camera calculations are in an optional extra set of notes for this week (not examinable)

• The process is performed in two steps:– Projection– Perspective divide and scaling to viewport pixel coordinates

• The first step uses the projection matrix– We transform the camera space vertices into projection space

using this matrix– The matrix is unlike previous matrices you have seen

• It includes the FOV and viewport distance• We will look how they can be created in the lab

– But it is applied to the vertices just like the world and view matrix

• The second step is performed by the graphics API

Page 16: Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.

Process OverviewProcess Overview

• For each vertex in our original model:– Multiply by the world, view, then projection matrix

• We can combine these matrices together for efficiency• Very common to combine view and project matrix to make a single

view-projection matrix as both are related to the camera

– The vertices pass through world space, camera space and end up in projection space

– A final perspective divide and scale is performed in hardware to create a final viewport space pixel position for the vertex

• In DirectX:– We only need to create the three matrices

• Create one view / projection matrix each frame for camera• Create a different world matrix for each model

– Vertex transformations done by shaders– Final perspective divide is done in hardware