Upload
truongtram
View
221
Download
3
Embed Size (px)
Citation preview
4/23/13
1
Grand Challenges of Compu7ng
– Graphics Projects –
2
Hidden Surface Removal
4/23/13
2
3
Occlusion • For most interes7ng scenes, some polygons overlap
• To render the correct image, we need to determine which polygons occlude which
4
Painter’s Algorithm • Simple: render the polygons from back to front, “pain7ng over” previous polygons
– draw blue, then green, then orange • Will this work in the general case?
4/23/13
3
5
Painter’s Algorithm: Problems
• Intersec(ng polygons present a problem
6
The Z-‐Buffer Algorithm
• Key idea: – resolve visibility independently at each pixel
• We know how to rasterize polygons into an image discre7zed into pixels:
4/23/13
4
7
The Z-‐Buffer Algorithm
• What happens if mul7ple primi7ves occupy the same pixel on the screen? – which is allowed to paint the pixel?
8
The Z-‐Buffer Algorithm
• Idea: retain depth aZer projec7on transform – each vertex keeps z coordinate (rela7ve to eye point) – can do this with canonical viewing volumes
• Augment color framebuffer with Z-‐buffer or depth buffer which stores Z value at each pixel – at frame beginning, ini7alize all pixel depths to ∞ – when rasterizing, interpolate depth (Z) across polygon – storing pixel color in framebuffer only if Z value smaller than the Z value stored in depth buffer
4/23/13
5
9
Z-‐Buffer • Store (R,G,B,Z) for each pixel – typically 8+8+8+24 bits, can be more
for all i,j { Depth[i,j] = MAX_DEPTH Image[i,j] = BACKGROUND_COLOUR } for all polygons P { for all pixels in P { if (Z_pixel < Depth[i,j]) { Image[i,j] = C_pixel Depth[i,j] = Z_pixel } } }
10
Z-‐Buffer Pros • Simple!!! • Easy to implement in hardware
– hardware support in all graphics cards today • Polygons can be processed in arbitrary order • Easily handles polygon interpenetra7on • Enables deferred shading
– rasterize shading parameters (e.g., surface normal) and only shade final visible fragments
4/23/13
6
11
Z-‐Buffer Cons • Poor for scenes with high depth complexity
– need to render all polygons, even if most are invisible
• Shared edges are handled inconsistently – ordering dependent
eye
13
Z-‐Buffer Alterna7ve • Determine visibility on object or polygon level
– using camera coordinates
• Resolu7on independent – explicitly compute visible por7ons of polygons
• Early in pipeline – aZer clipping
• Requires depth-‐sor7ng – Binary Search Par77on trees
4/23/13
7
GCC Project • Disable depth tes7ng (glDisable(GL_DEPTH_TEST)) and design your own algorithm for hidden surface removal.
• Star7ng point: look into BSP – Binary Search Par77on trees
Ray Tracing
4/23/13
8
Local vs. Global Illumina7on
Local Global
Illumina7on depends on local object and light sources only
Illumina7on at a point can depend
on any other objects in the scene
Review: Phong Illumina7on
• Vertex illumina7on from a light of intensity I = ambient + diffuse + specular
Ambient Term _____________
Diffuse Term _____________
Specular Term _____________
• Used to determine the color of ver7ces • Used by OpenGL
ksI (V ⋅R)α
kdI(N ⋅L)kaI
4/23/13
9
• Provides rendering method with – Refrac7on/Transparent surfaces – Reflec7ve surfaces – Shadows
18
Ray Tracing
Recursive Ray Tracing • Recur for reflec7ve/transparent objects
4/23/13
10
Recursive Ray Tracing • Recur for reflec7ve/transparent objects
Rendering via Ray Tracing
Alternative: Rasterization
4/23/13
11
• Eye point • Screen posi7on/orienta7on • Objects
– Material proper7es – Reflec7on/Refrac7on coefficients – Index of refrac7on
• Light sources
22
Essen7al Info for Ray Tracing
• Four ray types: – Eye rays: originate at the eye – Shadow rays: from surface point toward light source – Reflec7on rays: from surface point in mirror direc7on – Transparent rays: refracted from surface point
Ray Types for Ray Tracing
shadow ray
reflec7on ray
transparent ray
Backward ray tracing
Forward ray tracing
4/23/13
12
Illumina7on / Shadow Rays • Cast a ray from viewer’s eye through each pixel • Compute intersec7on of ray with first object in scene • Cast shadow rays from intersec7on point to light sources
– Light not occluded: light contributes to point color – Light occluded: point is in shadow
Occluder
Pixels
shadow rays
primary ray
Reflected Rays • If the surface is specular, a reflected ray is emioed from the intersec7on point.
• The contribu7on is called the specular reflec7on.
n θ θ
4/23/13
13
…
Reflected Ray Contribu7on Example
Refracted Rays • Some objects are transparent or translucent. • The transmioed light contributes to the surface color, called specular transmission.
• The ray is refracted based on the object’s composi7on.
4/23/13
14
Refracted Ray Contribu7on Example
Puqng It Together …
I(p) = Ilocal + kR Ireflected + kT Itransmitted Local term (Phong) Reflected Transmioed
Ireflected is the intensity of light from the reflected ray Itransmi5ed is the intensity of light from the transmioed ray kR and kT are the reflec7on and transmission coefficients
+ +=p
4/23/13
15
Recursive Ray Tracing
1
2
3
4
L1 L2
I = Ilocal + kRIreflected + kT Itransmitted
Ilocal = Iambient + Idiffuse + Ispecular
Recursive Ray Tracing
1
2
3
4
L1 L2
I = Ilocal + kRIreflected + kT Itransmitted
4/23/13
16
Recursive Ray Tracing
1
2
3
4
L1 L2
Recursive Ray Tracing
1
2
3
4
L1 L2
4/23/13
17
Recursive Ray Tracing and Light Paths
1
2
3
4
L1 L2
Ray tracing paths just reverses real light paths!
Recursive Ray Tracing and Light Paths
1
2
3
4
L1 L2
Ray tracing paths just reverses real light paths!
4/23/13
18
Recursive Ray Tracing and Light Paths
1
2
3
4
L1 L2
Ray tracing paths just reverses real light paths!
Recursive Ray Tracing and Light Paths
1
2
3
4
L1 L2
Ray tracing paths just reverses real light paths!
4/23/13
19
Ray Tree
Object 1 L2
Shadow of Obj 4
Reflec7on Transmission
Object 3
Reflec7on L2
L1 Object 2
Reflec7on L2
L1
Eye
Recursive Ray Tracing • For each pixel
– Shoot ray from eye through pixel – Find closest (posi7ve) intersec7on point – Shade point (Phong) using shadow rays – Recur for reflected and refracted rays (if necessary)
4/23/13
20
Ray Tracing Algorithm For each pixel • Cast a primary ray and find closest intersec7on p • If intersect something
pixelColor = ComputeShade(p) color ComputeShade(p) 1. Extend shadow (feeler) ray and find closest intersec7on 2. Calculate Phong local intensity I (ambient term only, if intersec7on) 3. If reflec7ve
Form reflec7ve ray and find closest intersec7on pR IR = ComputeShade(pR)
4. If transparent Form transmission ray and find closest intersec7on pT IT = ComputeShade(pT)
5. return I + kRIR + kT IT
Demo @ hop://www.siggraph.org/educa7on/materials/HyperGraph/raytrace/rt_java/raytrace.html
No Diffuse Ray Tracing? • Theore7cally, the scaoering at each point of intersec7on generates an infinite number of new rays that should be traced
• In prac7ce, we only trace the transmioed and reflected rays, but use the Phong model to compute shade at point of intersec7on
4/23/13
21
44
Termina7on Criteria 1. The ray intersects no surfaces
2. The ray intersects a light source that is not a reflec7ng surface
3. The tree has been generated to its maximum allowable depth
45
Three Issues 1. Ray-‐object intersec7on
– hidden surface removal
2. Reflec7on direc7on – mirror direc7on
3. Refrac7on direc7on – Snell’s law
4/23/13
22
Ray-object Intersection
Op7miza7ons • Lots of rays to cast! • Ray-‐Surface intersec7ons are expensive
• Associate with each object a bounding volume • If ray doesn’t intersect bounding volume, then ray doesn’t intersect object
4/23/13
23
48
Bounding Volume Trade-‐offs
increasing complexity & 7ghtness of fit
decreasing cost of (overlap tests + volume update)
AABB OBB Sphere Convex Hull 6-dop
Reflec7on • Mirror-‐like/Shiny objects
V
N
θ
Surface
θ
R
VNNVR −⋅= )(2
4/23/13
24
Refrac7on – Snell’s Law • A (semi-‐)transparent object has an index of refrac7on ni
V N1θ
Surface
2θ
N− R
1
2
2
1
2
1
)sin()sin(
nn
cc==
θθ
Refrac7on – Snell’s Law • A (semi-‐)transparent object has an index of refrac7on ni
V N1θ
Surface
2θ
N− R
1
2
2
1
2
1
)sin()sin(
nn
cc==
θθ
4/23/13
25
Refrac7on – Snell’s Law
V N1θ
Surface
2θ
N− R
)sin()sin(
2
1
2
1
θθ
=cc
))(()(1))(1(1
2
1
222
21 VNNV
ccN
cNVccR −⋅+−⋅−−=
Pros and Cons of Ray Tracing
• Advantages of ray tracing – All advantages of local illumina7on model – Also handles shadows, reflec7on, and refrac7on
• Disadvantages of ray tracing – Computa7onally expense – No diffuse inter-‐reflec7on between surfaces
4/23/13
26
GCC Projects 1. Implement Efficient Ray Tracing in OpenGL. Render a
scene with and without ray tracing and compare the results.
2. Write your own vertex / fragment shader using the OpenGL Shading Language GLSL (part of OpenGL 2.0 standard).
Mo7va7on for Shaders • The GPU is basically a bunch of small processors. • Back before shaders, the por7on of the graphics pipeline that handled ligh7ng and texturing was hardcoded into what we call the Fixed Func7onality Pipeline.
• Now, we can write our own shaders to change the way the pipeline works
4/23/13
27
Shaders • A shader is a program that basically rewrites a por7on of the graphics pipeline.
• Used in different places of graphics pipeline -‐ vertex shader, fragment shader etc.
• Can be wrioen in different languages – OpenGL’s GLSL – MicrosoZ HLSL – Nvidia’s Cg
GLSL • Part of OpenGL 2.0 • High level C-‐like language, but different • New data types
– Matrices – Vectors – Samplers
• Built-‐in variables and func7ons • Each shader has a main() func7on as entry point • Compiler built into driver
– Presumably they know your card best
4/23/13
28
Recall: OpenGL Pipeline
Vertex Opera7ons
Rasteriza7on (Interpola7on)
Fragment Opera7ons
Vertex Data
Framebuffer
GL_MODELVIEW Ligh7ng calcula7ons
Interpolate into fragments
Texturing Fog calcula7on Blending
Vertex and Fragment Shaders • Vertex Shader
– Operates on vertex data (normal, posi7on, tex_coord) – One vertex at a 7me
• Fragment Shader – Operates on fragments (smallest units being shaded) – One fragment at a 7me
4/23/13
29
Useful Resources: GLSL • GLSL Full Language Specifica7on:
– hop://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf
• GLSL Tutorials: – hop://www.lighthouse3d.com/opengl/glsl/ – hop://www.opengl.org/sdk/docs/tutorials/TyphoonLabs/ – hop://duriansoZware.com/joe/An-‐intro-‐to-‐modern-‐OpenGL.-‐Chapter-‐2.2:-‐Shaders.html
62
Interac7ve Programs
4/23/13
30
63
Interac7ve Object Selec7on • Move cursor over object, click
– how to decide what is below? • Ambiguity
– many 3D world objects map to same 2D point
• Four common approaches 1. manual ray intersec7on 2. bounding extents 3. backbuffer color coding 4. selec7on region with hit list
• Model space – local to each object
• World space – common to all objects
• Eye space – derived from view frustum
• Normalized space (NDC) [-‐1,-‐1,-‐1] → [1,1,1]
• Screen space
Recall: Coordinate Systems
4/23/13
31
• Model space – local to each object
• World space – common to all objects
• Eye space – derived from view frustum
• Normalized space (NDC) [-‐1,-‐1,-‐1] → [1,1,1]
• Screen space
Recall: Coordinate Systems
peye = MODELVIEW * p
pndc = PROJECTION * peye
pscreen = VIEWPORT * pndc
gl(Translate,Rotate,Scale,uLookAt)
gl(Ortho,uPerspective)
gl(Viewport)
Recall: OpenGL Viewing Volume
gluPerspec7ve (fovy, aspect, N, F)
4/23/13
32
Mouse Click to 3D World
1. Picking • Have point on screen (clicked by user) • Need to go backwards from screen to world space • Tricky part is “inver7ng” projec7on • Can use gluUnProject, or manually
Model World Eye Screen NDC
4/23/13
33
OpenGL Support: gluUnProject • Reverses the transforma7on pipeline • Can be used to transform screen to world coordinates • Can be used to determine the ray into the scene from cursor posi7on
OpenGL Support for Picking • glGetDoublev(GL_PROJECTION_MATRIX, proj);
– Returns the projec7on matrix
• glGetDoublev(GL_MODELVIEW_MATRIX, model); – Returns the current modelview matrix
• glGetIntegerv(GL_VIEWPORT, view); – Returns the viewport (xmin, ymin, width, height)
• Convert clicked point (x,y) to world coordinates:
gluUnProject(xs,ys,zs,model,proj,view,*xw, *yw, *zw);
4/23/13
34
71
OpenGL Picking Hints • gluUnproject gluUnProject(xs,ys,zs,model,proj,view,*xw, *yw, *zw);
– transform screen coordinates (xs, ys) to world coordinates (xw, yw, zw) given current transforma7on matrices
• zs = ? • Compute xw and yw at some ‘depth’ into the world:
– Depth corresponding to NEAR: supply (xs,ys,0) – Depth corresponding to FAR: supply (xs,ys,1) – subtract near result from far result to get ray direc7on – use this ray for line/polygon intersec7on
72
Selec7on: Manual Ray Intersec7on • Do all computa7on at applica7on level
– map selec7on point to a ray – intersect ray with all objects in scene
• Advantage – no library dependence
x VCS
y • Disadvantage – May be slow
4/23/13
35
73
2. Selec7on: Bounding Extents • Keep track of axis-‐aligned bounding rectangles
• Advantages – conceptually simple – easy to keep track of boxes in world space
74
Selec7on: Bounding Extents • Disadvantages
– low precision – must keep track of object-‐rectangle rela7onship
• Extensions – do more sophis7cated bound bookkeeping
• first level: box check; second level: object check
4/23/13
36
75
Bounding Volume Trade-‐offs
increasing complexity & 7ghtness of fit
decreasing cost of (overlap tests + volume update)
AABB OBB Sphere Convex Hull 6-dop
GCC Project • Mathema7cally derive picking and selec7on in OpenGL without using glUnProject
• Advantage: language-‐independent • Use this method to implement an interac7ve game in OpenGL
4/23/13
37
Character Anima7on
Contact pose -‐ Both legs are at their extreme point. Opposite arm is forward.
Recoil -‐ Weight falls on the recoil pose. Front foot fully touches. Body at lowest posi7on, arms at extreme.
Passing -‐ Back leg passes the contact leg. Arms at sides. High point -‐ Body at highest pose. Back leg is pushing body forward.
Back toe peels off the ground.
Anima7on – Walk Cycle
4/23/13
38
Anima7on – Walk Cycle
Anima7on – Walk Cycle
Robot Leg Human Leg
4/23/13
39
Bone System in Blender
• Bones form a hierarchical skeletal structure • Each mesh vertex is associated with a bone • Moving a bone results in moving associated ver7ces • Keyframes set for bones (not the en7re mesh)
– Interpolate bone posi7ons between keyframes
GCC Project • Implement a Bone system and use it for skeleton anima7on in OpenGL
• Star7ng point: hop://content.gpwiki.org/index.php/ OpenGL:Tutorials:Basic_Bones_System