COS 60 - 3D Computer Graphics
description
Transcript of COS 60 - 3D Computer Graphics
![Page 1: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/1.jpg)
COS 60 - 3D Computer Graphics
Trigonometry in review
Parametric functions
Parametric surfaces
Swept surfaces
Time permitting:
Linear Interpolation
Animating parametric surfaces
![Page 2: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/2.jpg)
Trigonometry Review
Sin() and cos() connect angles to coordinates.
P = (x,y)
X+X-
Y-
Y+
theta
![Page 3: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/3.jpg)
Trigonometry Review
Theta can be measured in degrees or radians. Degrees range from 0° to 360°. Radians range from 0 to 2π. Think of degrees and radians as just two different ways to
measure the same thing; like miles and kilometers.
thetaX+
Y+
(x,y)
To convert: Radians to degrees:
(theta) * 180.0 / π Degrees to radians:
(theta) * π / 180.0
0°=0 radians; 45°= π/4; 90°= π/2; 135°= 3π/4; 180°= π...
![Page 4: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/4.jpg)
Trigonometry Review
Angles and co-ordinates The unit circle is the set of all points exactly one unit away from
the origin. In fact, radians measure arclength on the unit circle. (The unit circle has a circumference of 2π.)
sin() and cos() are functions that give the vertical and horizontal positions (respectively) of a point on the unit circle at angle theta.
thetaX+
Y+
(x,y)
Ex:
cos(0°) = 1; sin(0°) = 0; so
if P=(X,Y) and
X = cos(0)
Y = sin(0)
then
P=(1,0)
![Page 5: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/5.jpg)
Angles, co-ordinates, and radius sin() and cos() return the (X,Y) co-ordinates on the unit circle. To get a bigger circle, multiply sin() and cos() by a radius.
The radius is the distance from the origin. For example, say theta=45° and r=5.
Then X=r*cos(theta) and Y=r*sin(theta), or X=5*cos(45°) and Y=5*sin(45°).
Trigonometry Review
thetaX+
Y+
(x,y)
In general, when you work with sin() and cos(), just remember: if you multiply them both by the same value, you’re changing the size of the circle they work in.
![Page 6: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/6.jpg)
Trigonometry Review
X = r * cos(theta):
Y = r * sin(theta):
X
Y
theta
theta
![Page 7: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/7.jpg)
Parametric Functions
The idea of parametric functions is the idea that you can have two variables and one depends on the other but the other doesn’t depend on the one. X=cos(t) is a parametric function. X is a function of t, but t can
be whatever it wants to be. You can say that a parametric function is a function in n
dimensions where n is the number of independent variables.
Y=f(x) is in one dimension.
X=sin(t), Y=cos(t) are in one dimension and produce 2D results.
Z=f(x,y) is in two dimensions, and produces 3D output.
The Three Musketeers are not a parametric function.
![Page 8: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/8.jpg)
Parametric Functions - Examples
![Page 9: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/9.jpg)
Parametric Functions
To draw a parametric function in one dimension with 2D or 3D outputs in OpenGL:
void drawFunction(void){ double x, y; double t;
glBegin(GL_LINE_STRIP); for (t = -5; t<=5; t+=0.01) { evalFn(t, x, y); glVertex3f(x,y,0); } glEnd();}
void evalFn( double t, double &x, double &y){ x = cos(t); y = sin(t);}
...will draw a circle.
![Page 10: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/10.jpg)
Parametric Functions
If your function is in one dimension, you’ll have a single independent variable (t), and your function can trace out a line. It doesn’t have to be a straight line. It doesn’t have to just be in 2D, either--it can vary through (x,y,
AND z).
If you have two independent variables (u,v) your function is in two dimenions and defines a plane. It doesn’t have to be a flat plane. In fact, it can be more like a rubber sheet. Think of a two-dimensional mathematical space, a sheet, a grid--
warping and twisting into the third dimenion like a living thing...
![Page 11: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/11.jpg)
Parametric Functions - Examples
![Page 12: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/12.jpg)
Parametric Functions
void drawFunction(void)
{
Vec A, B, C, D;
for (double u = -1; u<=1; u+=0.1)
for (double v = -1; v<=1; v+=0.1)
{
A = evalFn(u, v);
B = evalFn(u, v+0.1);
C = evalFn(u+0.1, v+0.1);
D = evalFn(u+0.1, v);
glBegin(GL_QUADS);
glVertex3f(A.x(),A.y(),A.z());
glVertex3f(B.x(),B.y(),B.z());
glVertex3f(C.x(),C.y(),C.z());
glVertex3f(D.x(),D.y(),D.z());
glEnd();
}
}
Vec evalFn( double u, double v){ double x, y, z;
x = cos(PI*u)*sin(PI*(v-1)/2); y = cos(PI*(v-1)/2); z = sin(PI*u)*sin(PI*(v-1)/2); return Vec(x,y,z);}
Rendering 2D surfaces is more complex--you need to render all four points of a quad.
![Page 13: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/13.jpg)
Swept Surfaces
Swept Surfaces, or Surfaces of Revolution, are a standard type of parametric surface.
A swept surface is like a lump of clay on a potter’s wheel: it’s a 2D mathematical function that’s been spun around and around until it sweeps out a three-dimensional shape.
Remember the rotation matrix for rotation in two dimensions:
[ cos(θ) sin(θ) ]
[ -sin(θ) cos(θ) ]
![Page 14: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/14.jpg)
Swept Surfaces
The 2D rotation matrix:
[ cos(θ) sin(θ) ]
[ -sin(θ) cos(θ) ] If θ=0, the matrix is identity:
[x y] * [ 1 0 ]= [x y]
[ 0 1 ] If θ=90°, the matrix rotates ninety degrees:
[x y] * [ 0 1 ]= [-y x]
[ -1 0 ]
![Page 15: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/15.jpg)
Swept Surfaces
So, if we were to calculate an interesting one-dimensional function:
Y=X2
![Page 16: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/16.jpg)
Swept Surfaces
And then we tilt our heads into 3D:
X=u2
Y=u
![Page 17: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/17.jpg)
Swept Surfaces
And then we slowly start to spin it around the Y axis, by multiplying the X and Z coordinates by the rotation matrix:
X=Rx(u2 )
Y=uZ= Rz(u2 )
where R is rotation by v°
...we have aswept surface!
![Page 18: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/18.jpg)
Swept Surfaces
![Page 19: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/19.jpg)
Linear Interpolation
A common question in graphics is, “How do I slowly and gradually slide from one point to another?” In other words, how do you find the spot midway between two other spots?
The simplest approach is a method called linear interpolation:
P1 = (x1,y1)
P2 = (x2,y2)
Let t go from 0.0 to 1.0; then
P(t)=(1-t)*P1 + t*P2
![Page 20: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/20.jpg)
Linear Interpolation
Linear interpolation in action: P(t)=(1-t)*P1 + t*P2
t = 0:
P(0) = (1-0)*P1 + 0*P2 = P1
t = 0.5:
P(0.5) = (1-0.5)*P1 + 0.5*P2 = (P1+P1)/2
t = 1:
P(1) = (1-1)*P1 + 1*P2 = P2
![Page 21: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/21.jpg)
Linear Interpolation in Animation
Linear interpolation is particularly useful in animation, where t varies from zero to one (and sometimes back again) over time.
float t = 0;
float dt = 0.01;
void onIdle(void)
{
t = t+dt;
if (t>1) { t = 1; dt = -0.01; }
if (t<0) { t = 0; dt = 0.01; }
}
![Page 22: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/22.jpg)
Linear Interpolation in Animation
If you want t to vary from zero to one and back again more smoothly than a sawtooth’ed function, you can use sin() or cos():
float t = 0;
float time = 0;
void onIdle(void)
{
time = time + π/100.0;
t = (sin(time) + 1)/2;
}t
time
![Page 23: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/23.jpg)
Linear Interpolation with Parametric Functions
The great thing about a parametric function in two dimensions is that it defines a nice grid. And that grid is the same grid no matter what the 3D output is.
That means that you can have two completely different parametric functions, both in the same two-dimensional range of u, v; and you know that for any (u,v) in one function there’s a matching (u,v) in the other.
If that’s the case… then you can linearly interpolate between the two parametric functions over time… [cue animated demo from J: drive]
![Page 24: COS 60 - 3D Computer Graphics](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813b19550346895da3cb6c/html5/thumbnails/24.jpg)
Recap
Degrees vs radians; sin() and cos(); unit circle Parametric functions: using one independent
variable to find x and y, or two independent variables to find x, y, and z
Rendering parametric functions: using GL_LINE_STRIP for one-dimensional parametric functions and GL_QUADS for two-dimensional functions.
Swept surfaces (AKA surfaces of revolution) Linear interpolation Linear interpolation with parametric surfaces