Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include<cmath>
  4. #include <algorithm>
  5.  
  6. #include <GL/glut.h>
  7. #include <GL/gl.h>
  8. #include <GL/glu.h>
  9.  
  10. using namespace std;
  11.  
  12. double scale = 1.0;
  13.  
  14. double random(double a, double b ) {
  15.     double r = (rand() % 10000) / 10000.0;
  16.  
  17.     return a + r * (b - a);
  18.  
  19. }
  20.  
  21. void draw_line(double x, double y, double x1, double y1) {
  22.     glBegin(GL_LINES);
  23.     glVertex2d(x, y);
  24.     glVertex2d(x1, y1);
  25.     glEnd();
  26. }
  27.  
  28. void star(float sc, float x, float y) {
  29.     draw_line(5*sc+x,10*sc+y, 8*sc+x,0*sc+y);
  30.     draw_line(8*sc+x,0*sc+y, 0*sc+x,6*sc+y);
  31.     draw_line(0*sc+x,6*sc+y, 10*sc+x,6*sc+y);
  32.     draw_line(10*sc+x,6*sc+y, 2*sc+x,0*sc+y);
  33.     draw_line(2*sc+x,0*sc+y, 5*sc+x,10*sc+y);
  34.  
  35. }
  36.  
  37.  
  38. double sx1 = 0;
  39. double sx = 0;
  40. double sy1 = 0;
  41. double sy = 0;
  42. void Render() {
  43.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  44.     glLoadIdentity();
  45.     glTranslatef(0, 0, -10);
  46.     glColor3d(1, 0.843, 0);
  47.     glScalef(scale, scale, scale);
  48.  
  49.  
  50.     //draw_line(0,0, 0,1);
  51.     //draw_line(0,1, 1,1);
  52.     //draw_line(1,1, 1,0);
  53.     //draw_line(1,0, 0,0);
  54.  
  55.     //star(0.1, -3, -3);
  56.     //star(0.2, -1, 0);
  57.     //star(0.3, -3, -2);
  58.  
  59.     star (random(0.4, 0.41),sx+sx1+random(-0.02, 0.02), sy+sy1+ random(-0.02, 0.02));
  60.  
  61.     glFlush();
  62. }
  63.  
  64. void update(int t) {
  65.     glutTimerFunc(50, update, 0);
  66.     Render();
  67. }
  68.  
  69. void keyb(unsigned char key, int x, int y) {
  70.     cout << key << endl;
  71.     if(key == 'w') {
  72.         sy1 += 0.1;
  73.     }
  74.     if(key == 'a') {
  75.         sx1 -= 0.1;
  76.     }
  77.     if(key == 's') {
  78.         sy -= 0.1;
  79.     }
  80.     if(key == 'd') {
  81.         sx += 0.1;
  82.     }
  83. }
  84.  
  85. void reshape(int w, int h) {
  86.  
  87.     // предупредим деление на ноль
  88.     // если окно сильно перетянуто будет
  89.     if(h == 0)
  90.         h = 1;
  91.     float ratio = 1.0* w / h;
  92.  
  93.     // используем матрицу проекции
  94.     glMatrixMode(GL_PROJECTION);
  95.  
  96.         // Reset матрицы
  97.     glLoadIdentity();
  98.  
  99.     // определяем окно просмотра
  100.     glViewport(0, 0, w, h);
  101.  
  102.     // установить корректную перспективу.
  103.     gluPerspective(45,ratio,1,1000);
  104.  
  105.     // вернуться к модели
  106.     glMatrixMode(GL_MODELVIEW);
  107. }
  108.  
  109. int main(int argc, char **argv)
  110. {
  111.     glutInit(&argc, argv);
  112.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  113.     glutInitWindowSize(1920, 980);
  114.     glutCreateWindow("Physics Engine");
  115.     glEnable(GL_DEPTH_TEST);
  116.     glMatrixMode(GL_PROJECTION);
  117.     glLoadIdentity();
  118.     glMatrixMode(GL_MODELVIEW);
  119.     glLoadIdentity();
  120.     glClearColor(1.0, 0.05, 0.5, 0.4);
  121.  
  122.     glutTimerFunc(50, update, 0);
  123.     glutReshapeFunc(reshape);
  124.     glutDisplayFunc(Render);
  125.     glutKeyboardFunc(keyb);
  126.     glutMainLoop();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement