Download presentation
Presentation is loading. Please wait.
Published byDoddy Sudirman Modified over 6 years ago
1
Class 11 timers Iron Maiden example Double Buffering BallAndTorus
throwBall – incorporating physics Complex object with moving parts Viewing Transformations: glLookAt Cummulative Rotation Orientation and Euler Angles selection
2
rotatingHelix3.cpp draw picture once
get on line in the event queue for another turn to add to angle get on line in the event queue ...
3
glutTimerFunc(period, timerFunction, value)
put the timerFunction on the event queue period milliseconds from now. void timerFunction(int value); value, an input value that will be passed to the timerFunction value is often not needed so is set to anything.
4
using a timer Main starts the timer function
glutTimerFunc(5, animate, 1); run the function animate in 5 millisecs. Have timer function call itself, ie, get in queue again. At the end of animate have the call glutTimerFunc(animationPeriod, animate, 1);
5
Go through all of rotatingHelix3.cpp to see how whole process works.
6
Double Buffering front buffer = viewable buffer
back buffer = drawable buffer Two ring circus: spotlight on one ring : viewable in the dark, set up the next act in other ring : drawable
7
Double Buffering - how to
In main, glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); instead of GLUT_SINGLE Can have one window double buffered and the other single. at end of display function use glutSwapBuffers(); instead of glFlush();
8
Iron maiden example maiden project
Nate Robbins gets credit. (Check out, but don't copy, his tutorials.)
9
ballAndTorus.cpp run it.
10
the Torus glutWireTorus(2.0, 12.0, 20, 20); center is the origin.
11
the Sphere glutWireSphere(2.0, 10, 10); radius 2 center at the origin
12
ballAndTorus.cpp
13
move the sphere to the right, outside the torus
glTranslatef(20.0, 0.0, 0.0); glutWireSphere(2.0, 10, 10);
14
rotate sphere about one spot on the torus (revolve about the torus)
"latitudinal rotation" about the line through (12,0,0), parallel to the y axis in a circle of radius 8. on the xz plane. The TRICK glTranslatef(12.0, 0.0, 0.0); glRotatef(latAngle, 0.0, 1.0, 0.0); glTranslatef(-12.0, 0.0, 0.0);
15
move the sphere in big circle about the torus
"longitudinal rotation" about the line through (0,0,1), parallel to the z axis in a circle of radius 20. on the xy plane. glRotatef(longAngle, 0.0, 0.0, 1.0);
16
spiral motion about the torus
combine the two motions.
17
throwBall.cpp run it. incorporate physics x(t)=h*t
y(t)=v*t - g/2 * t*t // Apply equations of motion to transform sphere. glTranslatef(h*t, v*t - (g/2.0)*t*t, 0.0);
18
Using a for loop for circle or time (or even color!)
On the white board 10 points on a circle 10 seconds of a flying ball 50 shades of grey
19
throwBall.cpp note routine to print float to string
also note special key functions Try out ballAndTorusWithFriction.cpp - more physics
20
Assembling a Clown Head Run clown3.cpp
head radius 2 ear radius 0.5 Hat: height 4 radius 2 Brim: inner radius 0.2 outer radius 2.2 ear to head 0 - 2 Hat sits 2 units from center of head Tilted 30o from vertical
21
clown3.cpp figure out placement of ears
// Transformations of the left ear. glTranslatef(sin((PI/180.0)*angle), 0.0, 0.0); glTranslatef(3.5, 0.0, 0.0); // Transformations of the right ear. glTranslatef(-sin((PI/180.0)*angle), 0.0, 0.0); glTranslatef(-3.5, 0.0, 0.0);
22
clown3.cpp: spring to left ear
// Transformations of the spring to the left ear. glTranslatef(-2.0, 0.0, 0.0); glScalef(-1 - sin( (PI/180.0) * angle ), 1.0, 1.0); // Spring to left ear. glColor3f(0.0, 1.0, 0.0); glBegin(GL_LINE_STRIP); for(t = 0.0; t <= 1.0; t += 0.05) glVertex3f(t, 0.25 * cos(10.0 * PI * t), 0.25 * sin(10.0 * PI * t)); glEnd();
23
clown3.cpp Study the code. Notice the order of transformations
Notice the use of glPushMatrix() and glPopMatrix(). Observe how the animation is done.
24
floweringPlant.cpp run uses depth testing
uses 3 sequential display list: stem segment, sepal, petal
25
Viewing Transformation
gluLookAt( eyex, eyey,eyez, centerx, centery,centerz, upx, upy, upz ) eye is where the camera should be center is where it should be aimed up determines how we hold the camera
26
gluLookAt( eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz )
Line of Sight ( los) goes through eye and center. Vertex of pyramid that gives frustum is at eye. Near and Far measured from there. Line Of Sight runs through center of frustum "True up" is component of up perpendicular to los.
27
gluLookAt(0.0, 0.0, 0.0, 2.0, 0.0, 5.0, , 1.0, 0.0 ); 2 1 -4 -3 -2 -1 1 2 3 4 -1 -2 -3
28
gluLookAt(0.0, 0.0, 0.0, 2.0, 0.0, 5.0, , 0.0, 0.0 ); 2 1 -4 -3 -2 -1 1 2 3 4 -1 -2 -3
29
gluLookAt(2.0, 1.0, 0.0, 2.0, 0.0, 5.0, , 1.0, 0.0 ); 2 1 -4 -3 -2 -1 1 2 3 4 -1 -2 -3
30
gluLookAt( ); 2 1 -4 -3 -2 -1 1 2 3 4 -1 -2 -3
31
boxWithLookAt.cpp Run. Just like box, but with gluLookAt instead of glTranslate.
32
clown3.cpp Run. Modify to use gluLookAt instead of glTranslate.
Look from front of sphere, just touching. What do you see?
33
clown3.cpp Run. Modify to use gluLookAt instead of glTranslate.
Look from front of sphere, just touching. What do you see? Have to adjust glFrustum!
34
clown3.cpp Run. Modify to use glLookAt instead of glTranslate.
Look down from well above hat. What do you want "up" to be?
35
gluLookAt( eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz )
Apply to the MODELVIEW matrix, not the PROJECTION matrix Code: Load the MODELVIEW matrix Set matrix to the identiry Place your cameral Build your world using reasonable world coordinates
36
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz,upx, upy, upz)
move the camera to (eyex, eyey, eyez) rotate till it is facing the center rotate about the line of sight till up is in the right direction. Is equivalent to moving the surrounding world: glRotate(B,0,0,1); glRotate(A, wx, wy, wz); glTranslate(-eyex, -eyey, -eyez);
37
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)
is equivalent to reverse of: Translate camera from eye to origin rotate the camera about the x-axis, till the line of sight lies on the x-z plane rotate the camera about the y-axis, (staying in the x-z plane) till the line of sight is pointing in the -z direction. rotate the camera about the line of sight ( = z-axis) till the top is pointing in the +y direction.
38
The 3 angles from the 3 "rotates" are the Euler angles.
39
Picking and Selection Run BallAndTorusPicking.cpp to understand the problem. How computer knows which shape or color to use for drawing. How can the computer "know" which shape belongs to a pixel to modify?
40
glRenderMode(GL_SELECT)
Selection Tell computer you will be selecting glRenderMode(GL_SELECT) Specify a (new, small) viewing volume selection volume Redraw the scene, invisibly, recording which items hit the viewing volume hit list
41
when hit records are written
A hit record is written into the hit buffer when both a name stack manipulation or a glRenderMode() command is encountered a hit has occured - a primitive has been drawn that intersects the selection volume
42
what hit records contain
the number of names in the name stack at the time of writing the record the min z-value‡ of primitives that hit selection volume since last hit record was written the max z-value‡ of primitives that hit selection volume since last hit record was written the sequence of names in the name stack at time of writing, bottom one first, may be none.
43
min and max values ‡ min and max are "normalized" by dividing by depth of selection volume, so value is in range [0,1]. Then multiplied by and stored as an unsigned int.
44
How to PICK the right selection volume?
45
gluPickMatrix(pickX, pickY, width, height, viewport[4])
glLoadIdentity(); gluPickMatrix(pickX, pickY,width,height, viewport[4]); glFrustrum(); or gluPerspective; or glOrtho; (same as resize function)
46
gluPickMatrix(pickX, pickY, width, height, viewport[4])
pickX and pickY are in gl screen coords. front face of selection volume is centered at (pickX, pickY) with given width and height. In pixels. viewport array supplies current viewport boundaries. Can use glGetIntegerv(GL_VIEWPORT,viewport)
47
ballAndTorusPicking.cpp void drawBallAndTorus(void)
void drawScene(void) // The mouse callback routine. void pickFunction(int button, int state, int x, int y) void findClosestHit(int hits, unsigned int buffer[]) void animate(int value)
48
collision detection Run spaceTravel.cpp
Go through code - non-collision parts Notice class, constructors Notice viewports. Notice display list. Notice rand. In setup I added srand(time(0)); Need to include ctime I also changed FILL_PROBABILITY
49
geometry of the spacecraft
radius 5 height 10 red center to edge of circle =sqrt(50) = 7.072
50
geometry of collisions
51
collision detection spaceTravel.cpp
static int isCollision = 0; // Is there collision between the spacecraft and an asteroid? functions: int checkSpheresIntersection(...) int asteroidCraftCollision(...) see specialKeyInput function
52
Articulated Figure On your own, run animateMan.cpp.
Do the experiments in the book. Come back with questions.
53
simple shadow run ballAndTorusShadowed.cpp
in drawFlyingBallAndTorus see if(shadow) code. in drawScene see 2 calls to drawFlyingBallAndTorus Light is at infinity, directly overhead.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.