glut教程六 move the camera.docVIP

  • 5
  • 0
  • 约6.76千字
  • 约 9页
  • 2017-02-13 发布于江苏
  • 举报
Keyboard Example: Moving the Camera Prev:?Keyboard Next:?Advanced Keyboard ? OK, so lets see a more exciting use for the keyboard using GLUT. In this section we’re going to go through the code of an application that will draw a small world populated with snowmen, and we’re going to use the direction keys to move the camera in this world. The left and right keys will rotate the camera around the Y axis, i.e. in the XZ plane, whereas the up and down keys will move the camera forward and backwards in the current direction. The code for this sample application is now presented with comments where appropriate. First we need some global variables to store the camera parameters. The variables will store both the camera position and the vector that gives us the aiming direction. We will also store the angle. There is no need to store the?y?component since it is constant. Hence, we need: angle: the angle of rotation in the y axis. this variable will allow us to rotate the camera x,z: The camera position in the XZ plane lx,lz: A vector defining our line of sight Lets deal with the variable declarations: // angle of rotation for the camera direction float angle=0.0; // actual vector representing the cameras direction float lx=0.0f,lz=-1.0f; // XZ position of the camera float x=0.0f,z=5.0f; The code to draw a snowman is now presented. The result looks like this void drawSnowMan() { glColor3f(1.0f, 1.0f, 1.0f); // Draw Body glTranslatef(0.0f ,0.75f, 0.0f); glutSolidSphere(0.75f,20,20); // Draw Head glTranslatef(0.0f, 1.0f, 0.0f); glutSolidSphere(0.25f,20,20); // Draw Eyes glPushMatrix(); glColor3f(0.0f,0.0f,0.0f); glTranslatef(0.05f, 0.10f, 0.18f); glutSolidSphere(0.05f,10,10); glTranslatef(-0.1f, 0.0f, 0.0f); glutSolidSphere(0.05f,10,10); glPopMatrix(); // Draw Nose glColor3f(1.0f, 0.5f , 0.5f); glRotatef(0.0f,1.0f, 0.0f, 0.0f); glutSolidCone(0.08f,0.5f,10,2); } Next we have the new render function. It contains all the commands to draw our little wor

文档评论(0)

1亿VIP精品文档

相关文档