Dvortsov_D1

движение окружностей усовершенст

Mar 27th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 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. void render_square(double x, double y) {
  14.     glPushMatrix();
  15.  
  16.     double dx = 0.5;
  17.     double dy = 0.5;
  18.     glBegin(GL_POLYGON);
  19.     glVertex2f(x, y);
  20.     glVertex2f(x + dx, y);
  21.     glVertex2f(x + dx, y - dy);
  22.     glVertex2f(x, y - dy);
  23.     glEnd();
  24.     glPopMatrix();
  25. }
  26.  
  27. void draw_line(double x, double y, double x1, double y1) {
  28.     glBegin(GL_LINES);
  29.     glVertex2d(x, y);
  30.     glVertex2d(x1, y1);
  31.     glEnd();
  32. }
  33.  
  34. class Circle{
  35. public:
  36.     float x, y, radius, vx, vy;
  37.     Circle(float cx, float cy, float vx, float vy, float rad) : x(cx), y(cy), radius(rad), vx(vx), vy(vy) {}
  38.     void draw(){
  39.         float step = 0.04;
  40.         for (float f = 0; f <= M_PI * 2; f += step){
  41.             draw_line(x + radius * cos(f), y + radius * sin(f), x + radius * cos(f+step), y + radius * sin(f+step));
  42.         }
  43.     }
  44.     void update(){
  45.         if (x > 4.0 || x < -4.0){
  46.             vx = -vx;
  47.         }
  48.         x += vx;
  49.         y += vy;
  50.  
  51.     }
  52. };
  53.  
  54. Circle c1(0,0,0.1,0.0,0.2);
  55. Circle c2(0,1,-0.2,0.0,0.5);
  56. static float TIME = 0.0;
  57.  
  58. void draw_circle(float x0, float y0, float vx, float vy, float r, int n){
  59.     float step = M_PI*2.0f / n;
  60.     if (x0 + vx * TIME < -5.0){
  61.         vx = -vx;
  62.     }
  63.  
  64.     for (float f = 0; f <= M_PI * 2; f += step){
  65.         draw_line(x0 + vx * TIME + r * cos(f), y0 + vy * TIME + r * sin(f), x0 + vx * TIME + r * cos(f+step), y0 + vy * TIME + r * sin(f+step));
  66.     }
  67. }
  68.  
  69. void Render() {
  70.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  71.         glLoadIdentity();
  72.         glTranslatef(0, 0, -10);
  73.         glColor3d(1, 1, 1);
  74.         glScalef(scale, scale, scale);
  75.         //draw_circle(0.0, 0.2, 0.01, 0.0, 0.2, 8);
  76.         //draw_circle(0.0, 0.0, -0.01, 0.0, 0.4, 6);
  77.         //draw_circle(0.0, -0.2, 0.000, 0.0, 0.6, 18);
  78.         //TIME += 2.5;
  79.         c1.draw();
  80.         c1.update();
  81.         c2.draw();
  82.         c2.update();
  83.         glFlush();
  84. }
  85.  
  86. void update(int t) {
  87.     Render();
  88.     glutTimerFunc(50, update, 0);
  89. }
  90.  
  91. void keyb(unsigned char key, int x, int y) {
  92.     if(key == '+') {
  93.         scale *= 1.1;
  94.     }
  95.     if(key == '-') {
  96.         scale *= 0.9;
  97.     }
  98.  
  99.     Render();
  100. }
  101.  
  102. void reshape(int w, int h) {
  103.  
  104.     // предупредим деление на ноль
  105.     // если окно сильно перетянуто будет
  106.     if(h == 0)
  107.         h = 1;
  108.     float ratio = 1.0* w / h;
  109.  
  110.     // используем матрицу проекции
  111.     glMatrixMode(GL_PROJECTION);
  112.  
  113.         // Reset матрицы
  114.     glLoadIdentity();
  115.  
  116.     // определяем окно просмотра
  117.     glViewport(0, 0, w, h);
  118.  
  119.     // установить корректную перспективу.
  120.     gluPerspective(45,ratio,1,1000);
  121.  
  122.     // вернуться к модели
  123.     glMatrixMode(GL_MODELVIEW);
  124. }
  125.  
  126. int main(int argc, char **argv)
  127. {
  128.     glutInit(&argc, argv);
  129.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  130.     glutInitWindowSize(800, 600);
  131.     glutCreateWindow("Physics Engine");
  132.     glEnable(GL_DEPTH_TEST);
  133.     glMatrixMode(GL_PROJECTION);
  134.     glLoadIdentity();
  135.     glMatrixMode(GL_MODELVIEW);
  136.     glLoadIdentity();
  137.     glClearColor(0., 0.0, 128.0, 0.0);
  138.  
  139.     glutTimerFunc(50, update, 0);
  140.     glutReshapeFunc(reshape);
  141.     glutDisplayFunc(Render);
  142.     glutKeyboardFunc(keyb);
  143.     glutMainLoop();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment