- 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
您可能关注的文档
最近下载
- RISC-V架构:开放源码处理器芯片的崛起.pptx VIP
- 3.2.1 水的利用与散失 课件-人教版(2024)生物七年级下册.pptx VIP
- 基于RISC-V架构处理器的通讯平台.pdf VIP
- RISC-V处理器芯片的电源网络设计.docx VIP
- 盘点一些用上先进制程工艺的RISC-V处理器.doc VIP
- 基于RISC-V参数化超标量处理器的优化设计.docx VIP
- 蜂鸟e200系列risc v开源处理器humming bird v1bob hu.pdf VIP
- 优捷通RISC处理器设计.pptx VIP
- 浅谈余华作品中的女性形象.doc VIP
- 中华人民共和国渔业法2025修订版课件.pptx VIP
原创力文档

文档评论(0)