Presentation is loading. Please wait.

Presentation is loading. Please wait.

MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Today: - MAT 594CM Forums (Ritesh) - Problem Set #1 online, due April 15th - Need.

Similar presentations


Presentation on theme: "MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Today: - MAT 594CM Forums (Ritesh) - Problem Set #1 online, due April 15th - Need."— Presentation transcript:

1 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Today: - MAT 594CM Forums (Ritesh) - Problem Set #1 online, due April 15th - Need to move to lower-level coding environment - Dot product, Vector product - Calculating normals for lighting - 6 DOF camera - Blend modes, Depth testing - Object “picking”, project/unproject methods - Simple animation

2 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Dot Product 1 The dot product lets us calculate the difference in direction between two vectors. Given two three-dimensional vectors P and Q, we calculate the dot product like so: P  Q = P x Q x + P y Q y + P z Q z Example: if P = (5, 4, 3) and Q = (2, 2, 4), P  Q = (5 * 2) + (4 * 2) + (3 * 4) = 10 + 8 + 12 = 30

3 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Dot Product 2 P  Q = |P||Q| cos θ or (P  Q) / (|P||Q|) = cos θ Example: if P = (0, 6, 0) and Q = (0, 3, 4), P  Q = (0 * 0) + (6 * 3) + (0 * 4) = 18 |P||Q| = (√0 2 + 6 2 + 0 2 ) * (√0 2 + 3 2 + 4 2 ) = √36 * √25 = 30 So, cos θ = 18/30 Solve for θ by taking the inverse cosine of both sides θ = cos -1 (.6) =.9273 radians = 53 degrees

4 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Cross Product The cross product lets us calculate a vector perpendicular to two vectors. It is used to calculate a surface normal, which is needed for lighting. It’s also needed for creating cameras with 6 DOF. PxQ = (P y Q z - P z Q y, P z Q x - P x Q z, P x Q y - P y Q z ) Example: P = (0, 6, 0) and Q = (0, 3, 4) PxQ = (6*4 - 0*3, 0*0 - 0*4, 0*3 - 6*4) = (24, -4, -24) = V our new vector, or normal, can be normalized by dividing each component by the length of the vector: Example, |V| = √(24 2 + (-4) 2 + (-24) 2 ) = 34.176 so our normalized vector V n = (24/34.176, -4/34.176, -24/34.176) = (.7022, -.117, -.7022), and |V n | = 1

5 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Cross Product to generate normals For each face (eg, a triangle or quad) you need 3 points that are not collinear. define a vector P from point A to point B, and a second vector Q from point A to point C P = (B x - A x, B y - A y, B z - A z ) Q = (C x - A x, C y - A y, C z - A z ) find V = PxQ and then normalize V to get the normalized normal N. All 4 points in a quad will use the same normal, which will be specified before calling the vertex commands. glBegin(GL_QUADS) glNormal3f(N x, N y, N z ); 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();

6 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Animation Although the frame rate of the OpenGL display loop attempts to run in sync with the refresh rate of the screen, you cannot count on this timing when animating your scene. - The OS can pause the display - Loading resources dynamically may take time - Different systems may have different refresh rates - You may be computing/drawing too much for your video card

7 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Animation A better way to do animation is to keep track of the current time and its position along a timeline defined by the start and end times of the movement. Then translate or rotate objects based on the percentage of time that has passed. May be problematic: void myDisplayLoop() { setUpCamera(); rotateObjectAroundYAxis(1f); //will be jittery & unpredictable drawObject(); }

8 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Animation Simple example: animating a 5-second translation 3 units along the Z axis void myInit(){ //normal stuff... //define animation long startTime = now(); long endTime = startTime + 5000ms; float distZ = 3f; } void myDisplayLoop() { //normal stuff... //handle animation current = now(); glTranslatef(0f, 0f, (current - start)/(end - start)*distZ); drawCoolShape(); }

9 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Animation Can also add “easing” interpolation functions to control the smoothness of the animation. The above psuedocode defined two “keyframes” and two different points in time, and did a linear interpolation between them to map the current to time to a current position along the z-axis. Easing functions provide a simple way to effect the interpolation between two points. Ex. float easedPerc = sin(origPerc * PI/2);

10 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes gluPerspective pseudocode gluPerspective(fovy, aspect, zNear, zFar) { ymax = zNear * tan(fovy * PI / 360.0); ymin = -ymax; xmin = ymin * aspect; xmax = ymax * aspect; glFrustum(xmin, xmax, ymin, ymax, zNear, zFar); }

11 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes glFrustum pseudocode glFrustum(xmin, xmax, ymin, ymax, zNear, zFar) { top = near_dist * tanf(fov * 0.5f); right = top * aspect; bottom = -top; left = -right; two_near_dist = 2.0f * near_dist; right_minus_left = right - left; top_minus_bottom = top - bottom; far_minus_near = far_dist - near_dist; m00 = two_near_dist / right_minus_left; m02 = (right + left) / right_minus_left; m11 = two_near_dist / top_minus_bottom; m12 = (top + bottom) / top_minus_bottom; m22 = -(far_dist + near_dist) / far_minus_near; m23 = -(2.0f * far_dist * near_dist) / far_minus_near; m32 = -1.0f; Projection.row0 =(m00, 0.0f, m02, 0.0f); Projection.row1 =(0.0f, m11, m12, 0.0f); Projection.row2 =(0.0f, 0.0f, m22, m23); Projection.row3 =(0.0f, 0.0f, m32, 0.0f); }

12 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Projection Matrix 3 The Projection Matrix encoding the perspective transform looks likes this: ( 2n / (r – l), 0, -(r + l) / (r - l), 0 ) ( 0 2n / ( t - b), (t + b) / (t - b), 0 ) ( 0 0, (f + n) / (f - n), - (2fn) / ( f - n) ) ( 0 0, 1, 0 ) Where n and f are the near and far planes, t and b are defined by the fovy And l and r are further defined by the aspect ratio

13 MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes


Download ppt "MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Today: - MAT 594CM Forums (Ritesh) - Problem Set #1 online, due April 15th - Need."

Similar presentations


Ads by Google