Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TRIUNGHI ROTIT / STATIC
- #include <windows.h>
- #include <math.h>
- #include <gl/freeglut.h>
- #include <iostream>
- using namespace std;
- double PI = 3.1415;
- double angle = 0;
- double scl = 0;
- int n = 4; int radius = 20;
- double theta = 0, theta_inc;
- double truck_speed = 0.15, q = 1;
- double car_speed = 0.2;
- float truck_start_x = 300;
- float car_start_x = 10; float x_center = car_start_x + radius, y_center = 230;
- double k = 0.0;
- double angle1 = 0;
- double angle2 = 0;
- void init(void) // initializare fereastra de vizualizare
- {
- glClearColor(1.0, 1.0, 1.0, 0.0); // precizeaza culoarea de fond a ferestrei de vizualizare
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION); // se precizeaza este vorba de o reprezentare 2D, realizata prin proiectie ortogonala
- gluOrtho2D(0.0, 1200.0, 0, 800.0); // sunt indicate coordonatele extreme ale ferestrei de vizualizare
- glClear(GL_COLOR_BUFFER_BIT); // reprezentare si colorare fereastra de vizualizare
- }
- void circle(float x, float y, float r, int segments)
- {
- glBegin(GL_TRIANGLE_FAN);
- glVertex2f(x, y);
- for (int n = 0; n <= segments; ++n) {
- float const t = 2 * 3.1415 * (float)n / (float)segments;
- glVertex2f(x + sin(t) * r, y + cos(t) * r);
- }
- glEnd();
- glBegin(GL_LINES);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(x, y, 0.0);
- glVertex3f(x+ r/2, y+ r/2, 0);
- glEnd();
- }
- void rotirePoligon(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //DESENARE DRUM
- glLineWidth(2.5);
- glColor3f(0.0, 0.0, 0.0);
- glBegin(GL_LINES);
- glVertex3f(0.0, 200.0, 0.0);
- glVertex3f(1200, 200, 0);
- glVertex3f(0.0, 275.0, 0.0);
- glVertex3f(90, 275, 0);
- glVertex3f(200.0, 275.0, 0.0);
- glVertex3f(300, 275, 0);
- glVertex3f(400.0, 275.0, 0.0);
- glVertex3f(500, 275, 0);
- glVertex3f(600.0, 275.0, 0.0);
- glVertex3f(700, 275, 0);
- glVertex3f(800.0, 275.0, 0.0);
- glVertex3f(900, 275, 0);
- glVertex3f(1000.0, 275.0, 0.0);
- glVertex3f(1100, 275, 0);
- glVertex3f(1180.0, 275.0, 0.0);
- glVertex3f(1200, 275, 0);
- glVertex3f(0.0, 350.0, 0.0);
- glVertex3f(1200, 350, 0);
- glEnd();
- angle += 0.05;
- truck_speed += 0.2;
- car_speed += 1;
- k++;
- //DESENARE MASINA
- glPushMatrix();
- if (car_start_x + 100 + car_speed < truck_start_x + truck_speed || car_start_x + 100 + car_speed > truck_start_x + truck_speed + 450)
- glTranslated(car_speed, 1, 0);
- else
- glTranslated(car_speed, 100, 0);
- glColor3f(0.3 , 0.0, 0.6);
- angle1 -= 1;
- angle2 -=2;
- glPushMatrix();
- glTranslatef(x_center, y_center, 0); // Moves rotated circle to desired location.
- glRotatef(angle2, 0, 0, 1);
- glTranslatef(-x_center, -y_center, 0); // Centers circle at origin.
- circle(car_start_x + radius, 230, radius / 2, 100);
- glPopMatrix();
- glColor3f(0.3, 0.0, 0.6);
- glPushMatrix();
- glTranslatef(car_start_x + 130 - radius, y_center, 0);
- glRotatef(angle2, 0, 0, 1);
- glTranslatef(-(car_start_x + 130 - radius), -y_center, 0);
- circle(car_start_x + 130 - radius, 230, radius / 2, 100);
- glPopMatrix();
- glBegin(GL_POLYGON);
- glColor3f(1.0, 1.0, 0.0);
- glVertex2f(car_start_x, 230);
- glVertex2f(car_start_x + 130, 230);
- glVertex2f(car_start_x + 130, 250);
- glColor3f(0.0, 1.0, 1.0);
- glVertex2f(car_start_x + 110, 250);
- glVertex2f(car_start_x + 90, 280);
- glColor3f(1.0, 0.0, 1.0);
- glVertex2f(car_start_x + 30, 280);
- glVertex2f(car_start_x + 30, 250);
- glVertex2f(car_start_x, 250);
- glEnd();
- glPopMatrix();
- // DESENARE CAMION
- glPushMatrix();
- glTranslated(truck_speed, q, 0.0);
- glTranslated(x_center, y_center, 0);
- //glRotated(angle, 0, 0, 1);
- glTranslated(-x_center, -y_center, 0);
- glPushMatrix();
- glLineWidth(6.0);
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glColor3f(0.5, 0.2, 0.8);
- glPushMatrix();
- glTranslatef(truck_start_x + 110 + radius, y_center, 0);
- glRotatef(angle1, 0, 0, 1);
- glTranslatef(-(truck_start_x + 110 + radius), -y_center, 0);
- circle(truck_start_x + 110 + radius, 230, radius, 100);
- glPopMatrix();
- glColor3f(0.5, 0.2, 0.8);
- glPushMatrix();
- glTranslatef(truck_start_x + 290 - radius, y_center, 0);
- glRotatef(angle1, 0, 0, 1);
- glTranslatef(-(truck_start_x + 290 - radius), -y_center, 0);
- circle(truck_start_x + 290 - radius, 230, radius, 100);
- glPopMatrix();
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 1);
- glVertex2f(truck_start_x + 100, 230);
- glVertex2f(truck_start_x + 300, 230);
- glVertex2f(truck_start_x + 300, 300);
- glVertex2f(truck_start_x + 240, 300);
- glVertex2f(truck_start_x + 240, 320);
- glVertex2f(truck_start_x + 100, 320);
- glEnd();
- glPopMatrix();
- glPopMatrix();
- glutSwapBuffers();
- glutPostRedisplay();
- glFlush();
- }
- void main(int argc, char** argv)
- {
- glutInit(&argc, argv); // initializare GLUT
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // se utilizeaza un singur buffer | modul de colorare RedGreenBlue (= default)
- glutInitWindowPosition(100, 100); // pozitia initiala a ferestrei de vizualizare (in coordonate ecran)
- glutInitWindowSize(1200, 800); // dimensiunile ferestrei
- glutCreateWindow("Proiect DEPASIRE - Cojocaru Andrei & Tordoi Laurentiu"); // creeaza fereastra
- init();
- glutDisplayFunc(rotirePoligon); // procedura desen este invocata ori de cate ori este nevoie
- glutMainLoop(); // ultima instructiune a programului, asteapta (eventuale) noi date de intrare
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement