introduction to 3d graphics lecture 5: from realism to real-time anthony steed university college...
TRANSCRIPT
Introduction to 3D GraphicsIntroduction to 3D Graphics
Lecture 5: From Realism to Lecture 5: From Realism to Real-TimeReal-Time
Anthony SteedAnthony Steed
University College LondonUniversity College London
OverviewOverview
ProjectingProjecting– As an optimisation of ray-castingAs an optimisation of ray-casting– New issues and problemsNew issues and problems
Clipping PolygonsClipping Polygons– 2D2D– 3D3D
Visibility CullingVisibility Culling
Reflection on Ray CastingReflection on Ray Casting
The process of casting rays is very The process of casting rays is very slowslow– E.G. for a cow we have 10,000 triangles, E.G. for a cow we have 10,000 triangles,
but to render a 1000x1000 pixel image but to render a 1000x1000 pixel image we have 10000000 primary rays to cast we have 10000000 primary rays to cast (and then reflections, shadow rays, etc…)(and then reflections, shadow rays, etc…)
Exploit point coherencyExploit point coherency– If ray through pixel (x,y) hits triangle T, If ray through pixel (x,y) hits triangle T,
then rays through adjacent pixels will then rays through adjacent pixels will likely hit the same trianglelikely hit the same triangle
Think OptimisationThink Optimisation
But although point coherency is But although point coherency is useful, we can easily enumerate useful, we can easily enumerate ALL the rays that hit the triangleALL the rays that hit the triangle– Find which rays would hit the vertices Find which rays would hit the vertices
of the triangleof the triangle– These rays are defined by a 2D These rays are defined by a 2D
triangle on the image planetriangle on the image plane– Scan this 2D triangle outScan this 2D triangle out
COP
Why Triangles?Why Triangles?
In general for an object In general for an object representation (bezier, CSG) is it representation (bezier, CSG) is it far from easy to find the 2D far from easy to find the 2D projection of the shapeprojection of the shape
Projection Problems (1)Projection Problems (1)
Projecting the verticesProjecting the vertices– Needs to be done efficientlyNeeds to be done efficiently
Clipping to the view volumeClipping to the view volume– Triangle might lie partially or totally Triangle might lie partially or totally
off the screenoff the screen Visible surface determinationVisible surface determination
– We now have to iterate through all We now have to iterate through all the objects to see if they are visible.the objects to see if they are visible.
Projection Problems (2)Projection Problems (2)
LightingLighting– Now we have to re-construct the Now we have to re-construct the
lighting across the screenlighting across the screen Depth test determinationDepth test determination
– Have to cope with overlapping and Have to cope with overlapping and potential “difficult” coverage relationspotential “difficult” coverage relations
Shadows and Inter-ReflectionsShadows and Inter-Reflections– Much more complicated Much more complicated
Full camera specificationFull camera specification
We have already seen:We have already seen:– VRP, VPN, VUV, COP, view plane VRP, VPN, VUV, COP, view plane
windowwindow Some more parameters:Some more parameters:
– Viewplane DistanceViewplane Distance– Type of projection Type of projection
Perspective – all rays converge to the COPPerspective – all rays converge to the COP Parallel – parallel rays from points in the Parallel – parallel rays from points in the
scene (DOP)scene (DOP) Front and back clip planesFront and back clip planes
Full CameraFull CameraV
NVRP=O
COP
VP
truncated View Volume
view window
fron
t cli
ppin
g pl
ane
back
cli
ppin
g pl
ane
Perspective ProjectionPerspective Projection
In order to get a natural looking In order to get a natural looking image we need the perspectiveimage we need the perspective
For a simple arrangement it is easy For a simple arrangement it is easy to find the projectionto find the projection
COP
C
QP
AO
viewplane
Z
Y
Canonical FramesCanonical Frames We use these as intermediate stages We use these as intermediate stages
from which we know how to proceedfrom which we know how to proceed
Canonical Perspective: Canonical Perspective: – Cop at (0,0,-1)Cop at (0,0,-1)– Viewplane coincident Viewplane coincident
with U-V planewith U-V plane– Viewplane window Viewplane window
bounded by –1 to +1bounded by –1 to +1– p’ = (x/z+1, y/z+1, 0)p’ = (x/z+1, y/z+1, 0)
View volume
Canonical ParallelCanonical Parallel Orthographic parallel projectionOrthographic parallel projection Direction of projection (DOP) is (0,0,-1)Direction of projection (DOP) is (0,0,-1) View volume bounded by –1 and +1 on U and VView volume bounded by –1 and +1 on U and V And by 0 and 1 on the N axisAnd by 0 and 1 on the N axis p’ = (x, y, 0)p’ = (x, y, 0)
V
+
DOP
View volume
From General Perspective to From General Perspective to Canonical Parallel (Projection Canonical Parallel (Projection
Space)Space)
+
Perspective to Canonical Perspective to Canonical PerspectivePerspective
We will apply a set of We will apply a set of transformation matrices (4 in total)transformation matrices (4 in total)
Each one ‘corrects’ a particular Each one ‘corrects’ a particular aspect of the projectionaspect of the projection
Then we put them all together to Then we put them all together to get one matrixget one matrix
Step 1: Move the View Step 1: Move the View Plane to the UV Plane (n = Plane to the UV Plane (n =
0)0)
000
0100
0010
0001
d
cop
Viewplane
d cop
V
N
Viewplane
V
N
Step 2: Translate the COP Step 2: Translate the COP so that it Lies on the N so that it Lies on the N
axisaxis
Viewplane
V
N
Viewplane
V
N
cop
cop
10
0100
0010
0001
yx cc
Step 3: Change the View Step 3: Change the View Volume into a Regular Volume into a Regular
PyramidPyramid
N
Viewplane
V
Ncop
V
copD
D
10
01
0020
0002
DdypyDdx
pxdy
pydx
pxdy
Ddx
D WhereD = d – cz
dx = x2 – x1
dy = y2 – y1
px = x2 + x1
py = y2 + y1
Step 4: Scale by 1/DStep 4: Scale by 1/D
N
V
cop1
1
N
V
copD
D
0000
0100
0010
0001
D
D
D
Multiplying Steps 1-4Multiplying Steps 1-4
11212
0111
002
0
0002
D
d
dy
py
Ddycc
dx
px
Ddxc
DDdypy
Ddx
pxdx
dx
yzx
Canonical Perspective To Canonical Perspective To Canonical ParallelCanonical Parallel
Projection spaceProjection space– Divide through by ZDivide through by Z
Incorporation of front and back Incorporation of front and back clipping planes (canonical parallel)clipping planes (canonical parallel)
+
Dmin Dmax
+
01
Projection SpaceProjection Space
A use for the right hand column!A use for the right hand column!– x maps to x/z+1x maps to x/z+1
1000
1100
0010
0001
Incorporate Front and Back Incorporate Front and Back Clipping PlanesClipping Planes
(Actually this step occurs before (Actually this step occurs before the previous one)the previous one)
1minmax
)1max.(min00
1minmax
1max00
0010
0001
DD
DDDD
D
The composite matrix T = The composite matrix T = MQMQ
2u1D-n1(px)
Ddx
2v1D-n1(py)
Ddy
n1
D 0
2u2D-n2(px)
Ddx
2v2D-n2(py)
Ddy
n2
D 0
2u3D-n3(px)
Ddx
2v3D-n3(py)
Ddy
n3
D 0
-2(qu)D-(qn)(px) + 2cxD-(px)cz
Ddx -2(qv)D-(qn)(py) + 2cyD-(py)cz
Ddy -(qn)+d
D 1
OverviewOverview
ProjectingProjecting– As an optimisation of ray-castingAs an optimisation of ray-casting– New issues and problemsNew issues and problems
Clipping PolygonsClipping Polygons– 2D2D– 3D3D
Visibility CullingVisibility Culling
Clipping ProblemClipping Problem
Once we start projecting polygons Once we start projecting polygons we have to cope with cases where we have to cope with cases where only some of the vertices project to only some of the vertices project to the view windowthe view window
Sutherland-Hodgman Sutherland-Hodgman Algorithm(2D)Algorithm(2D)
Clip the polygon Clip the polygon against each against each boundary of the clip boundary of the clip region successivelyregion successively
Result is possibly NUL Result is possibly NUL if polygon is outsideif polygon is outside
Can be generalised to Can be generalised to work for any work for any polygonal clip region, polygonal clip region, not just rectangularnot just rectangular
Clip to top
Clip toright
etc
Clipping to a Region Clipping to a Region BoundaryBoundary
To find the new To find the new polygon polygon – iterate through each iterate through each
of the polygon edges of the polygon edges and construct a new and construct a new sequence of pointssequence of points
– starting with an starting with an empty sequenceempty sequence
– for each edge there for each edge there are 4 possible cases are 4 possible cases to considerto consider
clip region
right clipboundary
P0
P1
P2
P3
Clipping the Polygon EdgeClipping the Polygon Edge
Given an edge Given an edge PP00,P,P11 we we have 4 case. It can be:have 4 case. It can be:– entering the clip region, entering the clip region,
add add PP and and PP11
– leaving the region, add leaving the region, add only only PP
– entirely outside, do entirely outside, do nothingnothing
– entirely inside, add only entirely inside, add only P1P1 Where Where PP is the point of is the point of
intersectionintersection
Visible side of
boundary
p0
p1
p0
p0
p0
p1
p1
p1
p
p
OUT IN
Wrapping UpWrapping Up
We can determine which of the 4 cases and also We can determine which of the 4 cases and also the point of intersection with just if statementsthe point of intersection with just if statements
To sum it up, an example:To sum it up, an example:
P0
P1 P2
P3P0
Pa Pb
P3
Weiler-Atherton AlgorithmWeiler-Atherton Algorithm
When we have non-convex polygons When we have non-convex polygons then the algorithm above might then the algorithm above might produce polygons with coincident produce polygons with coincident edgesedges
This is fine for rendering but maybe This is fine for rendering but maybe not for other applications (eg shadows)not for other applications (eg shadows)
The Weiler-Atherton algorithm The Weiler-Atherton algorithm produces separate polygons for each produces separate polygons for each visible fragmentvisible fragment
Weiler-Atherton AlgorithmWeiler-Atherton Algorithm
1
2
3
5
4
6
8
7
9
0
a
b
c
d
a b
cd
0
1
2
34
56
78
9
A
B
i j
k
l
clip region
polygon
loop of polygon vertices
loop of region vertices
After Inserting the first After Inserting the first Intersection Vertex iIntersection Vertex i
a
b
c
d
i
l
k
j
1
2
3
5
4
6
8
7
9
0
a b
cd
0
1
2
34
56
78
9
A
B
i j
k
l
clip region
polygon
After Insertion of all After Insertion of all Intersection VerticesIntersection Vertices
1
2
3
5
4
6
8
7
9
0
a
b
c
d
i
l
k
j
a b
cd
0
1
2
34
56
78
9
A
B
i j
k
l
clip region
polygon
Collect Your PolygonsCollect Your Polygons
Traverse the vertex list from the Traverse the vertex list from the toptop
When the intersection point is an When the intersection point is an exit switch to traversing the clip exit switch to traversing the clip boundaryboundary
When the intersection point is an When the intersection point is an entry switch to traversing the entry switch to traversing the polygon verticespolygon vertices
Clipping polygons in 3DClipping polygons in 3D
The Sutherland-Hodgman can The Sutherland-Hodgman can easily be extended to 3Deasily be extended to 3D– the clipping boundaries are 6 planes the clipping boundaries are 6 planes
instead of 4 linesinstead of 4 lines– intersection calculation is done by intersection calculation is done by
comparing an edge to a plane instead comparing an edge to a plane instead of edge to edgeof edge to edge
It can either be done in Projection It can either be done in Projection Space or in Canonical PerspectiveSpace or in Canonical Perspective
Clipping in Projection Clipping in Projection SpaceSpace
The view volume is defined by:The view volume is defined by:11 x
11 y
11 z
Clipping in Canonical Clipping in Canonical PerspectivePerspective
When we have an When we have an edge that extends edge that extends from the front to from the front to behind the COP, behind the COP, then if we perform then if we perform the clipping after the clipping after the projection of the projection of the points (which in the points (which in effect is what the effect is what the PS does) we might PS does) we might get wrong resultsget wrong results
V
N
+1
-1
COPO
p1
p2
q1
q2
top
bottom
View plane
projected external lines
Clipping in Homogeneous Clipping in Homogeneous Co-OrdinatesCo-Ordinates
The Sutherland-Hodgman can also The Sutherland-Hodgman can also be used for clipping in 4D before be used for clipping in 4D before dividing the points by the dividing the points by the ww
This can have the advantage that This can have the advantage that is even more general, it even is even more general, it even allows for the front clip plane to be allows for the front clip plane to be behind the COPbehind the COP
OverviewOverview
ProjectingProjecting– As an optimisation of ray-castingAs an optimisation of ray-casting– New issues and problemsNew issues and problems
Clipping PolygonsClipping Polygons– 2D2D– 3D3D
Visibility CullingVisibility Culling
Visibility ProblemVisibility Problem
Not all objects are visibleNot all objects are visible– In ray-tracing only rays inside the view In ray-tracing only rays inside the view
frustum could be createdfrustum could be created Not all polygons are visibleNot all polygons are visible
– Visibility “solved” by default by ray-tracing Visibility “solved” by default by ray-tracing because of object intersection test because of object intersection test
Visibility ordering is a problemVisibility ordering is a problem– Ray-tracing “solved” this problem implicitly Ray-tracing “solved” this problem implicitly
because closest ray intersection point was because closest ray intersection point was chosenchosen
Object CullingObject Culling
Any object outside the view frustum can be Any object outside the view frustum can be discardeddiscarded– Bounding sphere/plane test is commonBounding sphere/plane test is common
Remaining objects must be sent to clipping Remaining objects must be sent to clipping processprocess
Back Face CullingBack Face Culling
Polygons facing away from the Polygons facing away from the COP can be discarded COP can be discarded – Often over-ridden to model thin Often over-ridden to model thin
walled objectswalled objects
COP
List Priority AlgorithmsList Priority Algorithms
Must sort the objects (or polygons or pixels)Must sort the objects (or polygons or pixels)
Commonest approach is to sort the objects Commonest approach is to sort the objects on their distance from the COP (z-sort)on their distance from the COP (z-sort)
Order of PolygonsOrder of Polygons
For a single closed polyhedron ANY For a single closed polyhedron ANY order of polygon drawing is correctorder of polygon drawing is correct
With multiple overlapping objects With multiple overlapping objects z-sort the individual polygons z-sort the individual polygons – Special cases galore!Special cases galore!
In general these techniques are In general these techniques are not sufficient, and we will buffer not sufficient, and we will buffer individual pixels with a z-bufferindividual pixels with a z-buffer
ConclusionConclusion
We have moved a considerable We have moved a considerable way from realism to real-timeway from realism to real-time– Reduced a 3D problems to a 2D Reduced a 3D problems to a 2D
problem (via a 4D problem!)problem (via a 4D problem!) Remaining issuesRemaining issues
– Filling polygons to cope with local Filling polygons to cope with local illumination and visibilityillumination and visibility