Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far

以下是前面几节的完整整合代码:

#include <stdlib.h>
#include <math.h> #ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif // angle of rotation for the camera direction
float angle = 0.0f;
// actual vector representing the camera's direction
float lx=0.0f,lz=-1.0f;
// XZ position of the camera
float x=0.0f, z=5.0f;
// the key states. These variables will be zero
//when no key is being presses
float deltaAngle = 0.0f;
float deltaMove = ; void changeSize(int w, int h) { // Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == )
h = ;
float ratio = w * 1.0 / h; // Use the Projection Matrix
glMatrixMode(GL_PROJECTION); // Reset Matrix
glLoadIdentity(); // Set the viewport to be the entire window
glViewport(, , w, h); // Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 100.0f); // Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
} void drawSnowMan() { glColor3f(1.0f, 1.0f, 1.0f); // Draw Body glTranslatef(0.0f ,0.75f, 0.0f);
glutSolidSphere(0.75f,,); // Draw Head
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f,,); // Draw Eyes
glPushMatrix();
glColor3f(0.0f,0.0f,0.0f);
glTranslatef(0.05f, 0.10f, 0.18f);
glutSolidSphere(0.05f,,);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f,,);
glPopMatrix(); // Draw Nose
glColor3f(1.0f, 0.5f , 0.5f);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
glutSolidCone(0.08f,0.5f,,);
} void computePos(float deltaMove) { x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
} void computeDir(float deltaAngle) { angle += deltaAngle;
lx = sin(angle);
lz = -cos(angle);
} void renderScene(void) { if (deltaMove)
computePos(deltaMove);
if (deltaAngle)
computeDir(deltaAngle); // Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt( x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f); // Draw ground glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd(); // Draw 36 SnowMen for(int i = -; i < ; i++)
for(int j=-; j < ; j++) {
glPushMatrix();
glTranslatef(i*10.0,,j * 10.0);
drawSnowMan();
glPopMatrix();
} glutSwapBuffers();
} void pressKey(int key, int xx, int yy) { switch (key) {
case GLUT_KEY_LEFT : deltaAngle = -0.01f; break;
case GLUT_KEY_RIGHT : deltaAngle = 0.01f; break;
case GLUT_KEY_UP : deltaMove = 0.5f; break;
case GLUT_KEY_DOWN : deltaMove = -0.5f; break;
}
} void releaseKey(int key, int x, int y) { switch (key) {
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT : deltaAngle = 0.0f;break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = ;break;
}
} int main(int argc, char **argv) { // init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(,);
glutInitWindowSize(,);
glutCreateWindow("Lighthouse3D - GLUT Tutorial"); // register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene); glutSpecialFunc(pressKey); // here are the new entries
glutIgnoreKeyRepeat();
glutSpecialUpFunc(releaseKey); // OpenGL init
glEnable(GL_DEPTH_TEST); // enter GLUT event processing cycle
glutMainLoop(); return ;
}

最新文章

  1. Android-Parcelable
  2. .NET 常用框架
  3. HDU 2102 A计划(三维BFS)
  4. Swift中数组集合-b
  5. poj1511/zoj2008 Invitation Cards(最短路模板题)
  6. bzoj1632 [Usaco2007 Feb]Lilypad Pond
  7. win10提示管理员已阻止你运行此应用,如何强制运行
  8. git ignore 总结
  9. SpringAop注解实现日志的存储
  10. LSTM/RNN中的Attention机制
  11. Pycharm2018的激活方法或破解方法
  12. Collections.sort 给集合排序
  13. Linux下使用bind,epoll对网络编程封装
  14. 【HDU 5858】Hard problem(圆部分面积)
  15. UVa 1471 Defense Lines - 线段树 - 离散化
  16. ubuntu16.04服务器apache的ssl证书配置
  17. 通过Nagios监控weblogic服务
  18. TVS二极管的主要参数与选型
  19. Spring Data JPA 参考指南 中文版
  20. 卡常三连(快读快写+re)

热门文章

  1. Wannafly挑战赛22 A-计数器(gcd,裴蜀定理)
  2. Guess Number Higher or Lower II -- LeetCode
  3. SPOJ705 SUBST1 - New Distinct Substrings(后缀数组)
  4. linux-系统资源查看-动态
  5. JavaScript indexOf() 方法,获取元素的位置;Object.keys()获取对象的所有key的数组
  6. 最新IP地址数据库Dat格式-高性能高并发版(2019年3月)
  7. 转: java语法与ide级入门介绍 from: IBM dev
  8. 使用Spring开发和监控线程池服务
  9. /profile文件修改后立即生效
  10. Laravel之命令