computer graphics cse167: computer graphics instructor: ronen barzel ucsd, winter 2006
Post on 22-Dec-2015
224 views
TRANSCRIPT
2
CSE167 Computer Graphics Instructor: Ronen Barzel ([email protected])
TAs: Alex Kozlowski ([email protected]) Cameron Chrisman ([email protected])
Lecture: H&SS 1330 (TTh 8:00am-9:20am) Section: TBD Office: EBU3B 2104 (T 9:45-10:45am; Th 10:30-11:30am)
Web page: http://graphics.ucsd.edu/courses/cse167_w06
3
What is computer graphics?
Large field… 3D Modeling Image Synthesis (AKA Rendering) Animation Simulation Interaction Image Processing Vision Perception Hardware design Etc…
Way too much to cover in one quarter
4
What you’ll learn in this class
The Basics… How 3D models are defined
• Polygonal models• Smooth surfaces
How to render 3D models• Camera simulation• Lighting, shading
(Won’t make pretty pictures – not an art class)
Experience with Linear Algebra, C++, OpenGL
Basis for going on to advanced topics, game development, etc.
5
Prerequisites
Basic Familiarity with: Linear Algebra
• Vectors: dot products, cross products…• Matrices : matrix multiplication
C++ • (if you know Java you’ll be able to adapt)
Object oriented programming
You’ll get more comfortable with them. (And I’ll review Vectors & Matrices today)
6
Reading
3D Computer Graphics: A Mathematical Introduction with OpenGL (Buss)
Required pages are listed on the class web page
7
Programming ProjectsNOTE: Details of this may change. Check the web page for
updates Projects due every Tuesday Project 1: Matrices and basic graphics program Project 2: Matrix stack and hierarchical modeling Project 3: Scene graph & traversal Project 4: Bézier Curves & Surfaces Project 5: Camera Perspective & Clipping Project 6: Scan Conversion & Z-Buffer Project 7: Ray Casting & Lighting Final Project (due last day of class): Choose one of
the following Water fountain (particle system) Ray tracer Procedural tree Choose your own project (but talk to me first)
8
Tests
Midterm (in class) Thurs, Feb 9, 8:00am-9:20am H&SS 1330
Final Date & Time: TBD Location: TBD
9
Grading
5% Project 1 5% Project 2 8% Project 3 8% Project 4 8% Project 5 8% Project 6 8% Project 7 15% Final Project 15% Midterm 20% Final
10
About me
Pixar animation studios (12 years) Toy Story
•modeling, lighting, software Software tools development
Research Physically-based modeling Non-photorealistic rendering
11
Course Outline Introduction / Linear Algebra Review Geometry and Homogeneous Coordinates Hierarchical Transforms / Geometrical Calculations TA Lecture: C++, OpenGL, project implementation Hierarchical Modeling Cubic Curves Curved Surfaces Perspective & Viewing Clipping & Scan Conversion Lighting Ray Tracing Texture Mapping Antialiasing Advanced Topics, Guest Lecturers TBD Review
12
Outline for Today
1. Overview of the class2. Fundamental concepts of
graphics3. Linear Algebra Review
14
Modeling Creating 3D geometric data, AKA the “model” or the “scene”
By hand, often with an interactive editing program• Maya, Blender, AutoCAD, LightWave 3D, …
Procedurally, i.e. by writing programs Scanning real-world objects
Manipulating the data Deforming or editing the data
• Change this over time: you have animation! Converting one form into another.
• Convert higher order descriptions into things we know how to render
• Convert complex objects into simpler ones that we can render more quickly
Makes heavy use of differential geometry and computational geometry
Ultimately, typically defined as or reduced to polygonal models: Triangles Quadrilaterals Meshes
16
Modeling Primitives
Complex scenes: Usually built up from simpler objects Objects are built from individual primitives
Most fundamental and useful: 3D triangle Points and lines are also useful primitives
May have more complex “primitives”:•Polygons, spheres, curves, curved surfaces, …•Often automatically tessellated into triangles before rendering.
17
Rendering
Fancier term: Image Synthesis Synthesis of a 2D image from a 3D scene description
Result is a 2D array of pixels Red, Green, Blue values (range 0-255 or 0.0-1.0)
Can also have: opacity (“alpha”), depth (“Z”), …
Rendering style often classified as “Photorealistic”
• simulate light & camera “Nonphotorealistic” AKA stylized
• aesthetics, communication
19
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
Non-Photoreal Rendering
21
Rendering a scene
Define a “virtual camera” Tells renderer where to look in the scene
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
22
Rendering a Scene, cont’d.
Specify “virtual lights” (and shadows):
Specify “shaders” for model surfaces:
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
23
Hardware vs. Software Rendering
Highest quality rendering is done by software Algorithms such as “ray tracing”, “photon maps”, etc… Fanciest lighting, shadowing, surface shading, smoke &
fog, etc. Can take minutes or hours to compute an image RenderMan (Pixar), Dali (Henrik Wann Jensen), RADIANCE,
POVRay, …
Modern computers often have special-purpose 3D rendering hardware. “GPU” == Graphics Processing Unit. (Nvidia, ATI) Hardware implements the traditional 3D graphics
rendering pipeline Very fast, but relatively simple algorithm:
• Limits ability to get subtle shadows, reflections, etc.• Limits on complexity of surface shading description, etc.
Continually improving, driven by games industry.
(Modern graphics hardware is programmable, blurring the distinction between hardware & software rendering.)
24
OpenGL An API for rendering
Widely supported on many platforms Provides a standard interface to (platform-specific)
hardware. Other APIs: Java3D, Direct3D, …
Example–render a red triangle (this doesn’t include the ‘setup’ code, or camera or lights):glBegin(GL_TRIANGLES);glColor3f(1.0, 0.0, 0.0); // redglVertex3f(-4.0, -2.0, 0.0);glVertex3f(4.0, -2.0, 0.0);glVertex3f(0.0, 5.0, 0.0);glEnd();
Programming projects will use OpenGL. Lectures won’t focus on OpenGL Bulk of material is about the theory and algorithms (See Buss, ch. 1 for an intro on rendering triangles in
OpenGL.)
25
3-D Graphics Rendering Pipeline
Modeling Transformation
Viewing Transformation
Lighting & Shading
Clipping
Projection
Scan conversion, Hiding
Primitives
Image
Object coordinates
World space
Eye space
Normalized view space
Device coordinates
26
Outline for Today
1. Overview of the class2. Fundamental concepts of
graphics3. Linear Algebra Review
• Vectors• Matrices
27
Coordinate Systems
Right handed coordinate systems
(more on coordinate systems next class)
X
Y
Z
X
Y
Z
28
Vector Arithmetic
a =ax
ay
az
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
b =bx
by
bz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
a + b =ax + bx
ay + by
az + bz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
a −b =ax −bx
ay −by
az −bz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
sa =sax
say
saz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
−a =
−ax
−ay
−az
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
29
Vector Magnitude
The magnitude (length) of a vector is:
A vector with length=1.0 is called a unit vector
We can also normalize a vector to make it a unit vector:
v2 =vx
2 + vy2 + vz
2
v = vx2 + vy
2 + vz2
v
v
31
Dot Product
a ⋅b = aibi∑a⋅b =axbx + ayby + azbz
a⋅b = ax ay az⎡⎣ ⎤⎦
bx
by
bz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
a⋅b =aTb
a⋅b = a b cosθ
33
Example: Angle Between Vectors
a
b θ
a ⋅b = a b cosθ
cosθ =a⋅ba b
⎛
⎝⎜⎞
⎠⎟
θ =cos−1 a⋅ba b
⎛
⎝⎜⎞
⎠⎟
34
Dot Product Properties
The dot product is a scalar value that tells us something about the relationship between two vectors If a·b > 0 then θ < 90º
•Vectors point in the same general direction
If a·b < 0 then θ > 90º•Vectors point in opposite direction
If a·b = 0 then θ = 90º •Vectors are perpendicular•(or one or both of the vectors is degenerate (0,0,0))
35
Dot Products with One Unit Vector
a
u
a·u
If |u|=1.0 then a·u is the length of the projection of a onto u
36
Dot Products with Unit Vectors
bθ a
a·b = 00 < a·b < 1
a·b = -1
a·b = 1
-1 < a·b < 0 a·b a = b =1.0
a⋅b =cos θ( )
38
Properties of the Cross Product
area of parallelogram ab
is a vector perpendicular to both a and b, in the direction defined by the right hand rule
if a and b are parallel(or one or both degenerate)
a ×b
a×b = a b sinθa×b =
a×b =0
39
Example: Align two vectors
We are heading in direction h. We want to rotate so that we will align with a different direction d. Find a unit axis a and an angle θ to rotate around.
h
d
40
Example: Align two vectors
h
d
θ
a
a =h×dh×d
θ =sin−1 h×dh d
⎛
⎝⎜⎞
⎠⎟
θ =cos−1 h⋅dh d
⎛
⎝⎜⎞
⎠⎟
θ =tan−1 h×dh⋅d
⎛⎝⎜
⎞⎠⎟
t het a =at an2 h×d , h⋅d( )
41
Vector Class
class Vector3 {public:
Vector3() {x=0.0; y=0.0; z=0.0;}Vector3(float x0,float y0,float z0) {x=x0; y=y0; z=z0;}void Set(float x0,float y0,float z0) {x=x0; y=y0; z=z0;}void Add(Vector3 &a) {x+=a.x; y+=a.y; z+=a.z;}void Add(Vector3 &a,Vector3 &b) {x=a.x+b.x; y=a.y+b.y; z=a.z+b.z;}void Subtract(Vector3 &a) {x-=a.x; y-=a.y; z-=a.z;}void Subtract(Vector3 &a,Vector3 &b) {x=a.x-b.x; y=a.y-b.y; z=a.z-b.z;}void Negate() {x=-x; y=-y; z=-z;}void Negate(Vector3 &a) {x=-a.x; y=-a.y; z=-a.z;}void Scale(float s) {x*=s; y*=s; z*=s;}void Scale(float s,Vector3 &a) {x=s*a.x; y=s*a.y; z=s*a.z;}float Dot(Vector3 &a) {return x*a.x+y*a.y+z*a.z;}void Cross(Vector3 &a,Vector3 &b)
{x=a.y*b.z-a.z*b.y; y=a.z*b.x-a.x*b.z; z=a.x*b.y-a.y*b.x;}float Magnitude() {return sqrtf(x*x+y*y+z*z);}void Normalize() {Scale(1.0f/Magnitude());}
float x,y,z;};
42
Matrix Arithmetic
Multiply a matrix by a vector:
Each entry is dot product of row of M with v
M =mxx mxy mxz
myx myy myz
mzx mzy mzz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥ v =
vx
vy
vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
Mv( )i = mij∑ vj
Mv =
mxj∑ vj
myj∑ vj
mzj∑ vj
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
Mv =mxxvx + mxyvy + mxzvz
myxvx + myyvy + myzvz
mzxvx + mzyvy + mzzvz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
43
Identity
Multiplication by the identity matrix does not affect the vector
I =1 0 00 1 00 0 1
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
I v =1 0 00 1 00 0 1
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
vx
vy
vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥=
1* vx + 0 * vy + 0 * vz
0 * vx +1* vy + 0 * vz
0 * vx + 0 * vy +1* vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥=
vx
vy
vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥=v
v=I v
44
A diagonal matrix scales a vector
If s>1, the vector will grow by a factor of s If s=1, the vector won’t change If 0<s<1, the vector will shrink If s<0, the vector will point in the opposite
direction
Uniform Scaling
S =s 0 00 s 00 0 s
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
S v =s 0 00 s 00 0 s
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
vx
vy
vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥=
s* vx + 0 * vy + 0 * vz
0 * vx + s* vy + 0 * vz
0 * vx + 0 * vy + s* vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥=
svx
svy
svz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥=s
vx
vy
vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
sv =S v
45
Each dimension has its own scale factor
which leads to the equations:
Scaling by 0 in a dimension projects onto that plane
Scaling by -1 in a dimension reflects across that plane
Non-Uniform Scaling
′vx′vy′vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥=
sx 0 0
0 sy 0
0 0 sz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
vxvyvz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
′vx = sxvx′vy = syvy′vz = szvz
46
Rotation
Rotate in the xy plane by an angle θ, spinning around the z axis:
a positive angle will rotate counterclockwise when the rotation axis is pointing towards the observer
Right-hand rule Food for thought:
What is the matrix when θ is 0? 90 degrees? 180 degrees? How does a rotation by -θ compare to rotation by θ?
Rz (θ) =cos θ( ) −sin θ( ) 0sin θ( ) cos θ( ) 0
0 0 1
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
′v =R z(θ) v
′v =cos θ( )vx −sin θ( )vy
sin θ( )vx + cos θ( )vy
vz
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
47
Rotation about coordinate axes
We can define rotation matrices for each axis:
Rx θ( ) =1 0 00 cosθ −sinθ0 sinθ cosθ
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
Rx θ( ) =1 0 00 cosθ −sinθ0 sinθ cosθ
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
R z θ( ) =cosθ −sinθ 0sinθ cosθ 00 0 1
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
48
Rotation about arbitrary axis
This can be derived…
Note: a must be a unit vector!
Same right-hand rule applies for direction of rotation
R(a,θ) =1+ (1−cos(θ))(ax
2 −1) −azsin(θ) + (1−cos(θ))axay aysin(θ) + (1−cos(θ))axaz
azsin(θ) + (1−cos(θ))ayax 1+ (1−cos(θ))(ay2 −1) −axsin(θ) + (1−cos(θ))ayaz
−aysin(θ) + (1−cos(θ))azax axsin(θ) + (1−cos(θ))azay 1+ (1−cos(θ))(az2 −1)
⎡
⎣
⎢⎢⎢
⎤
⎦
⎥⎥⎥
a =1