Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <cmath>
- void reshape(int w, int h)
- {
- glClearColor(255,255,255, 0);
- glViewport(0, 0 ,w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- float h2w= (float)h / (float) w;
- float w2h= (float)w / (float) h;
- if(w<=h)
- {
- gluOrtho2D(0.0, 80.0, 0.0, 80.0*h2w);
- }
- else
- {
- gluOrtho2D(0.0*w2h, 80.0, 0.0, 80.0);
- }
- glMatrixMode(GL_MODELVIEW);
- }
- //void init()
- //{
- // glClearColor(0.0, 0.5, 0.0, 0.0f);
- // glMatrixMode(GL_PROJECTION);
- // glLoadIdentity();
- // gluOrtho2D(0.0, 80.0, 0.0, 80.0 );
- // glMatrixMode(GL_MODELVIEW);
- //}
- void drawCircle(int r, int x_center, int y_center)
- {
- int deg =0;
- double theta, x, y;
- glBegin(GL_POLYGON);
- while (deg<360)
- {
- theta = (deg*M_PI)/180;
- x = r*cos(theta) + x_center;
- y = r*sin(theta) + y_center;
- deg++;
- glVertex2d(x , y);
- }
- glEnd();
- }
- bool ismoving = true;
- float bus_pos = 0.0;
- int wheelangle = 0;
- float bus1=0.0, bus2=-50.0;
- void drawbus( float x, float y)
- {
- glColor3ub(200, 100, 10);
- glPushMatrix();
- glTranslatef(x, y, 0.0f);
- glTranslatef(bus_pos, 0.0f, 0.0f);
- glBegin(GL_POLYGON);
- glVertex2i(10,15);
- glVertex2i(10,30);
- glVertex2i(20,30 );
- glVertex2i(20,31);
- glVertex2i(30,31);
- glVertex2i(30,30);
- glVertex2i(39, 30);
- glVertex2i(45, 27);
- glVertex2i(45,15);
- glEnd();
- glColor3ub(100, 100, 100);
- glRecti(16,20, 22,25);
- glRecti(24,20, 30,25);
- glRecti(32,20, 38,25);
- glPushMatrix();
- glTranslatef(18,15 ,0);
- glRotatef(wheelangle, 0, 0, 1);
- glTranslatef(-18, -15, 0);
- glColor3ub(0, 0, 0);
- drawCircle(3, 18,15);
- glColor3ub(255,255,255);
- glBegin(GL_LINES);
- glVertex2i(15,15);
- glVertex2i(21,15);
- glVertex2i(18,12);
- glVertex2i(18,18);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(36,15 ,0);
- glRotatef(wheelangle, 0, 0, 1);
- glTranslatef(-36, -15, 0);
- glColor3ub(0, 0, 0);
- drawCircle(3, 36,15);
- glColor3ub(255,255,255);
- glLineWidth(2);
- glBegin(GL_LINES);
- glVertex2i(33,15);
- glVertex2i(39,15);
- glVertex2i(36,12);
- glVertex2i(36,18);
- glEnd();
- glPopMatrix();
- glPopMatrix();
- }
- void timer(int value)
- {
- if(ismoving)
- {
- bus_pos +=0.1;
- if(bus_pos>130)
- (bus_pos = -40);
- wheelangle -=10;
- }
- glutPostRedisplay();
- glutTimerFunc(10, timer, 0);
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- //glLoadIdentity();
- drawbus(bus1, 0.0);
- drawbus(bus2, 0.0);
- glFlush();
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch(key)
- {
- case 'a':
- ismoving= false;
- glutPostRedisplay();
- break;
- case 's':
- ismoving = true;
- glutPostRedisplay();
- break;
- }
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB);
- glutInitWindowPosition(200,0);
- glutInitWindowSize(1000,700);
- glutCreateWindow("Bus");
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutTimerFunc(0, timer, 0);
- // init();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement