Guest User

Code

a guest
Mar 3rd, 2015
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <gl\glut.h>
  3. #include <gl\glaux.h>
  4. #include <time.h>
  5.  
  6. float cx=0, cy=0,bx = 0,by = 0;
  7.  
  8. int counter[4] = {1,1,1,1};
  9.  
  10. void key(unsigned char key, int x, int y)
  11. {
  12. switch (key)
  13. {
  14. case 'd':
  15. cx+=0.5f;
  16. std::cout << "Vi nazhali D " << counter[0] << " raz(a)." << std::endl;
  17. counter[0]++;
  18. break;
  19. case 'a':
  20. cx-=0.5f;
  21. std::cout << "Vi nazhali A " << counter[1] << " raz(a)." << std::endl;
  22. counter[1]++;
  23. break;
  24. case 'w':
  25. cy+=0.5f;
  26. std::cout << "Vi nazhali W " << counter[2] << " raz(a)." << std::endl;
  27. counter[2]++;
  28. break;
  29. case 's':
  30. cy-=0.5f;
  31. std::cout << "Vi nazhali S " << counter[3] << " raz(a)." << std::endl;
  32. counter[3]++;
  33. break;
  34. }
  35. }
  36. void coolimg(int width, int height) {
  37. const float ar = (float) width / (float) height;
  38.  
  39. glViewport(0, 0, width , height);
  40. glMatrixMode(GL_PROJECTION);
  41. glLoadIdentity();
  42. glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  43.  
  44. glMatrixMode(GL_MODELVIEW);
  45. glLoadIdentity() ;
  46. }
  47. void display() {
  48. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  49. const double a = t*90.0;
  50. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  51. glColor3d(1,0,0); // цвет 3d
  52. glPushMatrix();
  53. glTranslated(cx + 0.92,cy + 0.92,-30);
  54. glRotated(60,1,0,0);
  55. glRotated(a,0,0,1);
  56. glutSolidSphere(1,5000,5000);
  57. glPopMatrix();
  58. glPushMatrix();
  59. if (bx = cx, by = cy) {
  60. srand(time(0));
  61. glTranslated(bx + rand()%15,by + rand()%15, -30);
  62. }
  63. glRotated(60,1,0,0);
  64. glRotated(a,0,0,1);
  65. glutSolidSphere(1,5000,5000);
  66. glPopMatrix();
  67. glutSwapBuffers();
  68. glutPostRedisplay(); // перерисовка окна
  69. }
  70.  
  71. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  72. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  73. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  74. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  75.  
  76. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  77. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  78. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  79. const GLfloat high_shininess[] = { 100.0f };
  80.  
  81.  
  82. int main(int argc, char **argv) {
  83.  
  84. // инициализация
  85. glutInit(&argc, argv);
  86. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  87. glutInitWindowPosition(100,100);
  88. glutInitWindowSize(400,400);
  89. glutCreateWindow("Game");
  90. // Функции
  91. glutReshapeFunc(coolimg);
  92. glutDisplayFunc(display);
  93. glutKeyboardFunc(key);
  94. // 3d
  95. glClearColor(1,1,1,1);
  96. glEnable(GL_CULL_FACE);
  97. glCullFace(GL_BACK);
  98.  
  99. glEnable(GL_DEPTH_TEST);
  100. glDepthFunc(GL_LESS);
  101.  
  102. glEnable(GL_LIGHT0);
  103. glEnable(GL_NORMALIZE);
  104. glEnable(GL_COLOR_MATERIAL);
  105. glEnable(GL_LIGHTING);
  106.  
  107. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  108. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  109. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  110. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  111.  
  112. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  113. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  114. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  115. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  116. // Основной цикл GLUT
  117. glutMainLoop();
  118.  
  119. return 1;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment