1 00:00:00,331 --> 00:00:03,659 - We are going to continue showcasing some of the 2 00:00:03,659 --> 00:00:07,205 features of OpenGL, and in this segment 3 00:00:07,205 --> 00:00:10,288 we are going to talk about animation. 4 00:00:13,261 --> 00:00:17,011 First, let me show you a demo of the program. 5 00:00:18,461 --> 00:00:22,628 In this case, I will set the DEMO is equal to 3. 6 00:00:33,242 --> 00:00:36,241 We now can see that we have a teapot, 7 00:00:36,241 --> 00:00:40,587 and if I hit the P key, the teapot animates. 8 00:00:40,587 --> 00:00:44,822 While it is animating, I can zoom in and zoom out 9 00:00:44,822 --> 00:00:48,989 as before, and I can also stop and restart the animation. 10 00:00:51,877 --> 00:00:54,188 Let's look at the code for this purpose. 11 00:00:54,188 --> 00:00:57,705 We want to put a teapot in the middle that animates. 12 00:00:57,705 --> 00:01:02,191 The first line of code is pushMatrix of modelview. 13 00:01:02,191 --> 00:01:05,529 Then you multiply the modelview matrix on the right, 14 00:01:05,529 --> 00:01:10,070 as in old OpenGL, by a translation corresponding 15 00:01:10,070 --> 00:01:12,591 to the teapotloc in the X coordinates, 16 00:01:12,591 --> 00:01:14,813 no translation in Y and Z. 17 00:01:14,813 --> 00:01:17,515 That's what causes the teapot to animate 18 00:01:17,515 --> 00:01:19,323 from the left to the right. 19 00:01:19,323 --> 00:01:23,145 We'll just need to increase the teapotloc variable. 20 00:01:23,145 --> 00:01:26,180 Notice again that this translation command 21 00:01:26,180 --> 00:01:30,857 where the original matrix input is the identity, 22 00:01:30,857 --> 00:01:33,693 that ensures that glm::translate 23 00:01:33,693 --> 00:01:36,149 returns a translation matrix. 24 00:01:36,149 --> 00:01:40,722 Alright, so the next two transforms setup and center the teapot. 25 00:01:40,722 --> 00:01:43,371 And again, the transforms right multiply 26 00:01:43,371 --> 00:01:46,595 the modelview matrix, which is the top of the stack. 27 00:01:46,595 --> 00:01:50,604 So these transforms, which are multiplied by translation, 28 00:01:50,604 --> 00:01:55,419 by Z is equal to 0.1, just set up and center the teapot. 29 00:01:55,419 --> 00:01:58,198 There's also rotations so that the teapot appears 30 00:01:58,198 --> 00:02:00,258 in the correct orientation. 31 00:02:00,258 --> 00:02:03,104 Notice that all of these transforms are right-multiplying 32 00:02:03,104 --> 00:02:07,171 the top of the stack, so in fact the last transform in code 33 00:02:07,171 --> 00:02:10,111 will be the first transform applied. 34 00:02:10,111 --> 00:02:14,403 So glm::scale sets the scale for the teapot appropriately, 35 00:02:14,403 --> 00:02:17,328 then you rotate the teapot into the correct location, 36 00:02:17,328 --> 00:02:20,290 then you translate it so it appears on the table, 37 00:02:20,290 --> 00:02:23,120 and finally you do what we're interested in, 38 00:02:23,120 --> 00:02:25,130 which is the animation part of it, 39 00:02:25,130 --> 00:02:28,361 which is translation by teapotloc. 40 00:02:28,361 --> 00:02:31,278 Notice here the glUniformMatrix4fv command, 41 00:02:32,754 --> 00:02:37,576 which sets modelviewPos to the correct value 42 00:02:37,576 --> 00:02:40,940 for the modelview matrix, and this a uniform matrix, 43 00:02:40,940 --> 00:02:44,348 and once you've done all this, you pop the modelview matrix 44 00:02:44,348 --> 00:02:47,905 from the stack so the rest of the geometry is not affected 45 00:02:47,905 --> 00:02:50,277 by what you've been doing for the teapot. 46 00:02:50,277 --> 00:02:53,636 And this a very common idiom, that a particular object 47 00:02:53,636 --> 00:02:57,370 is defined between push and pops for the modelview matrix. 48 00:02:57,370 --> 00:03:01,789 We did the same thing for each of the four pillars in drawing. 49 00:03:01,789 --> 00:03:04,255 Here is my animation routine. 50 00:03:04,255 --> 00:03:08,268 So the void animation command simply states that 51 00:03:08,268 --> 00:03:11,851 teapotloc is equal to teapotloc plus 0.005. 52 00:03:13,025 --> 00:03:16,581 That means at each step, it slightly increments 53 00:03:16,581 --> 00:03:19,000 the location of the teapot, which is reported 54 00:03:19,000 --> 00:03:22,912 for the animation and if the teapot goes off the right edge, 55 00:03:22,912 --> 00:03:25,389 you simply bring it back to the left edge. 56 00:03:25,389 --> 00:03:29,648 glutPostRedisplay() just redisplays the scene 57 00:03:29,648 --> 00:03:33,070 and tells OpenGL to call the display function. 58 00:03:33,070 --> 00:03:35,070 drawteapot() in geometry.h 59 00:03:36,237 --> 00:03:38,800 binds the vertex array for the teapot, 60 00:03:38,800 --> 00:03:42,060 draws the elements corresponding to the teapot element 61 00:03:42,060 --> 00:03:44,129 in the scenes and unbinds the array. 62 00:03:44,129 --> 00:03:47,347 The keyboard callback is where p pauses 63 00:03:47,347 --> 00:03:49,563 and restarts the animation. 64 00:03:49,563 --> 00:03:52,730 Notice that I have this animate variable, 65 00:03:52,730 --> 00:03:55,156 which is originally zero, 66 00:03:55,156 --> 00:03:59,239 and then I can toggle it to 1 and back to 0. 67 00:04:00,594 --> 00:04:03,494 In the keyboard, I have escape to quit as before, 68 00:04:03,494 --> 00:04:05,710 but now I added a new case for p 69 00:04:05,710 --> 00:04:08,178 to pause and restart the animation. 70 00:04:08,178 --> 00:04:12,075 It toggles the animate variable; and notice this code. 71 00:04:12,075 --> 00:04:14,474 If the animate variable is on, 72 00:04:14,474 --> 00:04:17,425 it sets glutIdleFunct to the animation. 73 00:04:17,425 --> 00:04:20,109 So even if no keys or nothing has been pressed, 74 00:04:20,109 --> 00:04:24,206 glut will idly call the animation function, 75 00:04:24,206 --> 00:04:26,242 which will move the teapot. 76 00:04:26,242 --> 00:04:30,129 If animate is off, then the glutIdleFunc() is null. 77 00:04:30,129 --> 00:04:31,390 It does nothing.