Dvortsov_D1

dvigenie strelki

Apr 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include<cmath>
  4. #include <algorithm>
  5. #include <vector>
  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. float update_time = 1000;
  15. float angle_step = M_PI / 30.0 / 1000.0 * update_time;
  16.  
  17. class vector2d {
  18. public:
  19.     float x ;
  20.     float y;
  21.     vector2d() : x(0) , y(0){}
  22.     vector2d (float a ,float  b ) : x(a) , y(b)  {}
  23.     void print(){
  24.         cout << x <<'\t' << y <<  endl;
  25.  
  26.     }
  27.     float dot(vector2d other){
  28.         return (x * other.x + y * other.y);
  29.     }
  30.     vector2d operator + (vector2d other){
  31.         return vector2d(x + other.x, y + other.y);
  32.     }
  33.     vector2d operator - (vector2d other){
  34.         return vector2d(x - other.x, y - other.y);
  35.     }
  36.     vector2d operator * (float c){
  37.         return vector2d (x * c , y * c);
  38.     }
  39.     vector2d operator / (float c){
  40.         return vector2d (x / c , y / c);
  41.     }
  42. };
  43.  
  44. void render_square(double x, double y) {
  45.     glPushMatrix();
  46.  
  47.     double dx = 0.5;
  48.     double dy = 0.5;
  49.     glBegin(GL_POLYGON);
  50.     glVertex2f(x, y);
  51.     glVertex2f(x + dx, y);
  52.     glVertex2f(x + dx, y - dy);
  53.     glVertex2f(x, y - dy);
  54.     glEnd();
  55.     glPopMatrix();
  56. }
  57.  
  58. void draw_line(double x, double y, double x1, double y1) {
  59.     glBegin(GL_LINES);
  60.     glVertex2d(x, y);
  61.     glVertex2d(x1, y1);
  62.     glEnd();
  63. }
  64.  
  65. void draw_arrow(vector2d a){
  66.     draw_line(0, 0, a.x, a.y);
  67.     vector2d n1(-a.y, a.x);
  68.     vector2d n2(a.y, -a.x);
  69.     vector2d ar_1 = (n1 - a * 3) / 10.0;
  70.     vector2d ar_2 = (n2 - a * 3) / 10.0;
  71.     draw_line(a.x, a.y, a.x + ar_1.x, a.y + ar_1.y);
  72.     draw_line(a.x, a.y , a.x + ar_2.x, a.y + ar_2.y);
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. void Render() {
  80.         static float arrow_angle = -0.5 * M_PI;
  81.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  82.         glLoadIdentity();
  83.         glTranslatef(0, 0, -10);
  84.         glColor3d(1, 1, 1);
  85.         glScalef(scale, scale, scale);
  86.         vector2d a(cos(-arrow_angle),sin(-arrow_angle));
  87.         arrow_angle += angle_step;
  88.         if (arrow_angle > 2 * M_PI) arrow_angle -= 2 * M_PI;
  89.         //draw_line(0,0,1,1);
  90.         draw_arrow(a);
  91.  
  92.  
  93.         glFlush();
  94. }
  95.  
  96. void update(int t) {
  97.     Render();
  98.     glutTimerFunc(update_time, update, 0);
  99. }
  100.  
  101. void keyb(unsigned char key, int x, int y) {
  102.     if(key == '+') {
  103.         scale *= 1.1;
  104.     }
  105.     if(key == '-') {
  106.         scale *= 0.9;
  107.     }
  108.  
  109.     Render();
  110. }
  111.  
  112. void reshape(int w, int h) {
  113.  
  114.     // предупредим деление на ноль
  115.     // если окно сильно перетянуто будет
  116.     if(h == 0)
  117.         h = 1;
  118.     float ratio = 1.0* w / h;
  119.  
  120.     // используем матрицу проекции
  121.     glMatrixMode(GL_PROJECTION);
  122.  
  123.         // Reset матрицы
  124.     glLoadIdentity();
  125.  
  126.     // определяем окно просмотра
  127.     glViewport(0, 0, w, h);
  128.  
  129.     // установить корректную перспективу.
  130.     gluPerspective(45,ratio,1,1000);
  131.  
  132.     // вернуться к модели
  133.     glMatrixMode(GL_MODELVIEW);
  134. }
  135.  
  136. int main(int argc, char **argv)
  137. {
  138.     glutInit(&argc, argv);
  139.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  140.     glutInitWindowSize(800, 600);
  141.     glutCreateWindow("Physics Engine");
  142.     glEnable(GL_DEPTH_TEST);
  143.     glMatrixMode(GL_PROJECTION);
  144.     glLoadIdentity();
  145.     glMatrixMode(GL_MODELVIEW);
  146.     glLoadIdentity();
  147.     glClearColor(0., 0.0, 128.0, 0.0);
  148.  
  149.     glutTimerFunc(50, update, 0);
  150.     glutReshapeFunc(reshape);
  151.     glutDisplayFunc(Render);
  152.     glutKeyboardFunc(keyb);
  153.     glutMainLoop();
  154. }
Advertisement
Add Comment
Please, Sign In to add comment