240 likes | 339 Views
COS 60 - 3D Computer Graphics. Trigonometry in review Parametric functions Parametric surfaces Swept surfaces Time permitting: Linear Interpolation Animating parametric surfaces. Trigonometry Review. Sin() and cos() connect angles to coordinates. Y+. P = (x,y). theta. X-. X+. Y-.
E N D
COS 60 - 3D Computer Graphics Trigonometry in review Parametric functions Parametric surfaces Swept surfaces Time permitting: Linear Interpolation Animating parametric surfaces
Trigonometry Review • Sin() and cos() connect angles to coordinates. Y+ P = (x,y) theta X- X+ Y-
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. • 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°= π... Y+ (x,y) theta X+
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. Y+ • Ex: cos(0°) = 1; sin(0°) = 0; so if P=(X,Y) and X = cos(0) Y = sin(0) then P=(1,0) (x,y) theta X+
Trigonometry Review • 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°). 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. (x,y) theta X+
Trigonometry Review • X = r * cos(theta): • Y = r * sin(theta): X theta Y theta
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.
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.
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...
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(); } } • Rendering 2D surfaces is more complex--you need to render all four points of a quad. 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); }
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(θ) ]
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 ]
Swept Surfaces • So, if we were to calculate an interesting one-dimensional function: Y=X2
Swept Surfaces • And then we tilt our heads into 3D: X=u2 Y=u
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=u Z= Rz(u2 )where R is rotation by v°...we have aswept surface!
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
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
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; } • }
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
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]
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