Advertisement
Rejuan706

Moving Bus

May 29th, 2023
1,543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <cmath>
  5.  
  6. void reshape(int w, int h)
  7. {
  8.     glClearColor(255,255,255, 0);
  9.     glViewport(0, 0 ,w, h);
  10.     glMatrixMode(GL_PROJECTION);
  11.     glLoadIdentity();
  12.     float h2w= (float)h / (float) w;
  13.     float w2h= (float)w / (float) h;
  14.  
  15.     if(w<=h)
  16.     {
  17.         gluOrtho2D(0.0, 80.0, 0.0, 80.0*h2w);
  18.     }
  19.     else
  20.     {
  21.         gluOrtho2D(0.0*w2h, 80.0, 0.0, 80.0);
  22.     }
  23.     glMatrixMode(GL_MODELVIEW);
  24. }
  25.  
  26. //void init()
  27. //{
  28. //    glClearColor(0.0, 0.5, 0.0, 0.0f);
  29. //    glMatrixMode(GL_PROJECTION);
  30. //    glLoadIdentity();
  31. //    gluOrtho2D(0.0, 80.0, 0.0, 80.0 );
  32. //    glMatrixMode(GL_MODELVIEW);
  33. //}
  34. void drawCircle(int r, int x_center, int y_center)
  35. {
  36.     int deg =0;
  37.     double theta, x, y;
  38.  
  39.     glBegin(GL_POLYGON);
  40.     while (deg<360)
  41.     {
  42.         theta = (deg*M_PI)/180;
  43.  
  44.         x = r*cos(theta) + x_center;
  45.         y = r*sin(theta) + y_center;
  46.         deg++;
  47.  
  48.         glVertex2d(x , y);
  49.     }
  50.     glEnd();
  51. }
  52. bool ismoving = true;
  53. float bus_pos = 0.0;
  54. int wheelangle = 0;
  55. float bus1=0.0, bus2=-50.0;
  56. void drawbus( float x, float y)
  57. {
  58.     glColor3ub(200, 100, 10);
  59.     glPushMatrix();
  60.     glTranslatef(x, y, 0.0f);
  61.     glTranslatef(bus_pos, 0.0f, 0.0f);
  62.     glBegin(GL_POLYGON);
  63.     glVertex2i(10,15);
  64.     glVertex2i(10,30);
  65.     glVertex2i(20,30 );
  66.     glVertex2i(20,31);
  67.     glVertex2i(30,31);
  68.     glVertex2i(30,30);
  69.     glVertex2i(39, 30);
  70.     glVertex2i(45, 27);
  71.     glVertex2i(45,15);
  72.     glEnd();
  73.  
  74.     glColor3ub(100, 100, 100);
  75.     glRecti(16,20, 22,25);
  76.     glRecti(24,20, 30,25);
  77.     glRecti(32,20, 38,25);
  78.  
  79.     glPushMatrix();
  80.  
  81.     glTranslatef(18,15 ,0);
  82.     glRotatef(wheelangle, 0, 0, 1);
  83.     glTranslatef(-18, -15, 0);
  84.     glColor3ub(0, 0, 0);
  85.     drawCircle(3, 18,15);
  86.  
  87.     glColor3ub(255,255,255);
  88.     glBegin(GL_LINES);
  89.     glVertex2i(15,15);
  90.     glVertex2i(21,15);
  91.     glVertex2i(18,12);
  92.     glVertex2i(18,18);
  93.     glEnd();
  94.  
  95.     glPopMatrix();
  96.  
  97.     glPushMatrix();
  98.  
  99.     glTranslatef(36,15 ,0);
  100.     glRotatef(wheelangle, 0, 0, 1);
  101.     glTranslatef(-36, -15, 0);
  102.     glColor3ub(0, 0, 0);
  103.     drawCircle(3, 36,15);
  104.     glColor3ub(255,255,255);
  105.     glLineWidth(2);
  106.     glBegin(GL_LINES);
  107.     glVertex2i(33,15);
  108.     glVertex2i(39,15);
  109.     glVertex2i(36,12);
  110.     glVertex2i(36,18);
  111.     glEnd();
  112.     glPopMatrix();
  113.  
  114.     glPopMatrix();
  115.  
  116.  
  117. }
  118.  
  119. void timer(int value)
  120. {
  121.     if(ismoving)
  122.     {
  123.         bus_pos +=0.1;
  124.         if(bus_pos>130)
  125.         (bus_pos = -40);
  126.         wheelangle -=10;
  127.     }
  128.  
  129.     glutPostRedisplay();
  130.     glutTimerFunc(10, timer, 0);
  131. }
  132.  
  133. void display()
  134. {
  135.     glClear(GL_COLOR_BUFFER_BIT);
  136.     //glLoadIdentity();
  137.  
  138.     drawbus(bus1, 0.0);
  139.     drawbus(bus2, 0.0);
  140.  
  141.     glFlush();
  142.  
  143. }
  144.  
  145. void keyboard(unsigned char key, int x, int y)
  146. {
  147.     switch(key)
  148.     {
  149.         case 'a':
  150.  
  151.             ismoving= false;
  152.             glutPostRedisplay();
  153.             break;
  154.  
  155.         case 's':
  156.  
  157.             ismoving = true;
  158.             glutPostRedisplay();
  159.             break;
  160.  
  161.     }
  162. }
  163.  
  164. int main(int argc, char** argv)
  165. {
  166.     glutInit(&argc, argv);
  167.     glutInitDisplayMode(GLUT_RGB);
  168.  
  169.     glutInitWindowPosition(200,0);
  170.     glutInitWindowSize(1000,700);
  171.  
  172.     glutCreateWindow("Bus");
  173.     glutDisplayFunc(display);
  174.     glutReshapeFunc(reshape);
  175.     glutKeyboardFunc(keyboard);
  176.     glutTimerFunc(0, timer, 0);
  177. //    init();
  178.     glutMainLoop();
  179.  
  180.     return 0;
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement