Download presentation
Presentation is loading. Please wait.
1
Joshua Barczak CMSC 435 UMBC
Viewing Joshua Barczak CMSC 435 UMBC
2
Rendering Image-Order(Raytracing) Object-Order(Rasterization)
For each pixel: What is under it? What color is it? Object-Order(Rasterization) For each object: What pixels does it cover? What color are they?
3
Object-Order Rendering
Object-Space Primitives World-space Primitives Camera-Space Primitives Clip-Space Primitives Clip Modeling XForm Viewing XForm Projection XForm Last time Today Clip-Space Primitives Rasterize Window XForm Viewport XForm Displayed Pixels Pixels Raster-Space Primitives
4
Coordinate Systems Model/Object/Local space World Space
Coordinates model is authored in Typically with Origin at object center World Space “The” coordinate system Model Space World Space
5
Coordinate Systems Camera/Eye/View Space Origin at Viewer
Image plane slightly in front… X/Y axes screen aligned Z axis points in/out of screen Camera Space
6
Coordinate Systems Camera Space Handedness Right handed Left handed
OGL convention Left handed D3D convention Half the world likes one, and half the other… Z X Y Z X
7
Coordinate Systems Clip Space NDC (-1,1) on x/y (-1,1) on Z (OpenGL)
(0,1) on Z (D3D) NDC Normalized Device Coordinates 0,0 1,0 0,1 There is much disagreement about what ‘NDC’ space actually is, the name isn’t very well standardized…
8
Coordinate Systems Raster Space Screen space
Width,0 0,0 Raster Space Continuous pixel coordinates 1 unit 1 pixel NDC scaled by resolution Screen space Raster space translated to window location 0,Height There is much disagreement about what ‘Screen’ space actually is, the name isn’t very well standardized…
9
The Major Transforms Projection Matrix Model-World Matrix View Matrix
How OpenGL sets it up… The right way to set it up… Changes for every object… Changes once for a given camera…
10
Viewing Transform The job of the viewing transform is:
Get us from these axes Onto these axes
11
Camera Coordinate System
From, At, Up Derive an orthonormal basis for the camera Right handed, to match OGL Make sure everything is normalized… Up Z Y From X Book calls these U,V,W At
12
Viewing Transform Two ways to think of this: Take 1: Moving the world
Take 2: Changing the coordinate system
13
The “Look-At” Transform
Take 1: Rotations and translations Translate to origin Subtract eye coords Put the Z axis on From-At Want matrix to turn this into 0,0,1
14
The “Look-At” Transform
Take 1: Rotations and translations Translate to origin Subtract eye coords Put Z axis on From-At Inverse of:
15
The “Look-At” Transform
Take 1: Rotations and translations Translate to origin Subtract eye coords Put Z axis on From-At Don’t forget to spend 3 hours flipping signs and axes…
16
The “Look-At” Transform
Take 2: Projecting onto new basis Use cross products to get a basis Subtract eye position Project onto new basis From At Up X Y Z IMO this one is a lot harder to screw up….
17
The “Look-At” Transform
From At Up X Y Vector form: Z Matrix form: IMO this one is a lot harder to screw up….
18
Camera Controls Orbit/Arcball camera Fixed target point
User pivots in a sphere around model ex = r*cos(azimuth)*sin(zenith); ey = r*sin(azimuth)*sin(zenith); ez = r*cos(zenith); gluLookAt( ex, ey, ez, 0,0,0, 0,0,1 );
19
Camera Controls Orbit/Arcball camera
Use mouse to rotate around the unit sphere Use mouse wheel/keys to adjust distance //On mouse drag: // record differences from last position float dx = x - oldX; float dy = y - oldY; oldX = x; // update stored position oldY = y; // rotation angle, scaled so across the // window = one rotation view->azimuth += 2*M_PI * dx / view->width; view->zenith -= M_PI * dy / view->height ; // constrain the polar angle so we don't // suddenly flip around float EPS = f; if( view->zenith < EPS ) view->zenith = EPS; else if( view->zenith > M_PI-EPS ) view->zenith = M_PI-EPS;
20
Camera Controls First Person Camera
Track Zenith/Azimuth as with orbit camera, construct Z axis Derive other axes using cross products Good idea to smooth it Mousepos = s * Mousepos + (1-s) * last_Mousepos From At Up X Y
21
Camera Controls First person camera Forward Motion:
Translate along Z Sideways (Strafe) Motion: Translate along X/Y From At Up X Y
22
Camera Controls First person camera Motion Up Y From X At velocity=0;
if( w ) velocity -= Z; // right handed if( s ) velocity += Z; if( a ) velocity -= X; if( d ) velocity += X; velocity *= SPEED/length(velocity); position += velocity * elapsed_time;
23
Projection Transform The job of the projection transform is:
Cram the visible universe into a unit box Project
24
Canonical View Volume (GL)
(-1,1,1) (1,1,1) Top Edge of Screen (0,1,-1) Center of screen Left Edge of Screen (0,0,-1) (-1,0,-1) (1,0,-1) (1,-1,1) Points outside the cube are not visible… (1,-1,-1) (0,-1,-1) D3D uses z=0. I like theirs better… Bottom Edge of Screen
25
Types of Projection Orthographic Perspective
Parallel lines stay parallel Often used by modellers Perspective Parallel Lines converge Used for first-person views
26
The View Frustum Figure: Olano Orthographic Perspective
27
Orthographic Projection
Parameters: l,b,t,f: Corners of viewing rectangle n,f: distances to near/far planes I’m using left handed, because its less confusing… (-1,-1,-1) (1,1,1) (r,t,f) (l,b,n)
28
Orthographic Projection
X,Y simply scales Z gets n:f range mapped to -1:1 XLate(-n) Scale(2/f-n) XLate(-1) (-1,-1,-1) (1,1,1) (r,t,f) (l,b,n)
29
Orthographic Projection
(-1,-1,-1) (1,1,1) (r,t,f) (l,b,n)
30
Orthographic Projection
The book’s version does not do what they say it does! Z is backwards! (-1,-1,-1) (1,1,1) (r,t,f) (l,b,n)
31
Orthographic Projection
Typical, Symmetric Case… (l=-r, b=-t) General case (off center) (-1,-1,-1) (1,1,1) (r,t,f) (l,b,n)
32
Perspective Raytracing: Given y’, find y
Perspective projection is the opposite: Given y, find y’ y’ y
33
Similar Triangles y y’ n z
34
Division by Z Black Up close WhiteFar away
35
Division by Z Black Up close WhiteFar away
36
Perspective Projection
Our desired transform… Q: How do we trick a matrix into dividing? Matrices don’t work this way…
37
Perspective Projection
A: Use a projective transform Homogeneous coordinates again…
38
Perspective Projection
Each homogeneous point projects to a w=1 point “Equivalent” points, different coordinates -w 1 w
39
Perspective Projection
Perspective Transform After homogeneous divide… Z isn’t quite right…
40
Perspective Projection
At near plane, z=n At far plane, z=f In between, z is non-linear… Fortunately, at least, its monotonically increasing……
41
Perspective Projection
Perspective matrix “squishes” geometry along Z Turns frustum into rectangular prism Add an ortho transform afterwards…
42
Perspective Projection
43
Perspective Projection
44
Perspective with FOV Set dimensions based on FOV/Aspect
Just like in raytracing Much more common… h 1 FOV
45
GL Transforms glPushMatrix/PopMatrix
Matrix stack glRotate/glTranslate/glScale, etc… glLoadIdentity glMatrixMode( mode ) Set current stack GL_MODELVIEW Modeling + Viewing Transforms GL_PROJECTION
46
GL Transforms gluLookAt( eye, at, up )
Apply a viewing transform glOrtho(left,right,bottom,top,near,far) Apply an orthographic projection glFrustum(left,right,bottom,top,near,far) Apply a perspective projection gluPerspective(fov,aspect,near,far) Perspective with FOV
47
OpenGL Ortho Our ortho: glOrtho:
-Z is reversed for right-handed camera space -They want absolute distances for n,f
48
OpenGL Perspective Ours: glFrustum: Differences: Right handed
Supports off-center projection
49
OpenGL Perspective FOV
Ours: gluPerspective: Differences: Right handed
50
D3D Ortho Our ortho: D3DXMatrixOrthoLH: Differences:
Maps Z to 0:1 not -1:1
51
D3D Perspective Ours: D3DXMatrixPerspectiveLH: Differences:
Maps Z to 0:1 not -1:1
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.