Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<windows.h>
- #include <GL/freeglut.h>
- #include <stdlib.h>
- #include <math.h>
- #include<iostream>
- #include<fstream>
- using namespace std;
- GLdouble pc;//retine pozitia curenta a primului dreptunghi
- GLdouble pcd;//retine pozitia curenta a dreptnghiului care depaseste
- static GLdouble ww, wh;//dimesiunile ferestrei
- static GLdouble i = 0.0;
- static GLdouble j = 0.0;
- static GLdouble alpha1 = 1.0;
- static GLdouble alpha2 = 1.0;
- struct Punct
- {
- int x, y;
- }A, C, Adep, Cdep;
- enum
- {
- DESCRIERE, DEPASIRE
- };
- int rendermode = DESCRIERE;
- void menu(int selection)
- {
- rendermode = selection;
- glutPostRedisplay();
- }
- void init(void)
- {
- glClearColor(0.2, 1.0, 0.1, 0.0);
- gluOrtho2D(0.0, 800.0, 0.0, 600.0);
- glShadeModel(GL_FLAT);
- }
- void citire()
- {
- ifstream f("in.txt");
- f >> ww >> wh;
- f >> A.x >> A.y >> C.x >> C.y;//coordonatel a 2 punte opuse pt primul dreptunghi
- f >> Adep.x >> Adep.y >> Cdep.x >> Cdep.y;//coordonatele a 2 pct opuse ale dreptunghiului care depasaseste
- pc = A.x;
- pcd = Adep.x;
- }
- void desen_strada()
- {
- glBegin(GL_QUADS);
- glColor3f(0.0, 0.0, 0.0);
- glVertex2f(0, wh / 2 - 100);
- glVertex2f(ww, wh / 2 - 100);
- glVertex2f(ww+100, wh / 2 + 100);
- glVertex2f(100, wh / 2 + 100);
- glEnd();
- glEnable(GL_LINE_STIPPLE);//desenare linie din mijloc cu linie punctata
- glLineStipple(1, 0X00FF);
- glLineWidth(15);
- glBegin(GL_LINES);
- glColor3f(1.0, 1.0, 1.0);
- glVertex2i(50, wh / 2);
- glVertex2i(ww, wh / 2);
- glEnd();
- glDisable(GL_LINE_STIPPLE);
- glFlush();
- }
- void desen2(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glLineWidth(15);
- glBegin(GL_LINES);
- glColor3f(0.0, 0.0, 0.0);
- glVertex2f(0, wh / 2);
- glVertex2f(ww, wh / 2);
- glEnd();
- glPushMatrix();//primul dreptunghi
- glTranslated(i / 3, 0.0, 0.0);
- glColor3f(0.0, 0.0, 1.0);
- glRecti(A.x, A.y, C.x, C.y);
- glPopMatrix();
- //pc = A.x + i / 3;
- //if(abs(pc - pcd) > C.x - A.x)
- //{
- //
- // glPushMatrix(); //dreptunghiul care depaseste
- // glTranslated(j / 2, 0.0, 0.0);
- //
- // glColor3f(0.9, 0.1, 0.2);
- // glRecti(Adep.x, Adep.y, Cdep.x, Cdep.y);
- // glPopMatrix();
- // pcd = Adep.x + j / 2;
- //}
- //else
- //{
- //
- // glPushMatrix(); //dreptunghiul care depaseste
- //
- // glTranslated(j / 2, 100.0, 0.0); // dreptunghiul care depaseste este scalat pt ca atunci cad depaseste este mai mic
- // glScaled(0.8, 0.8, 0);
- // pcd = Adep.x + j / 2;
- // glColor3f(0.9, 0.1, 0.2);
- // glRecti(Adep.x, Adep.y, Cdep.x, Cdep.y);
- // glPopMatrix();
- //}
- //
- glutSwapBuffers();
- glFlush();
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, GLdouble(w), 0.0, GLdouble(h), -1.0, 1.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void miscad1(void)
- {
- i = i + alpha1;
- if (i > 3 * ww)
- alpha1 = -1.0;
- else if (i < 0.0)alpha1 = 1.0;
- j = j + alpha2;
- if (j > 2 * ww)
- alpha2 = -1.0;
- else if (j < 0.0)
- alpha2 = 1.0;
- glutPostRedisplay();
- }
- void stop(void)
- {
- return;
- }
- void Specialkey(int key, int x, int y)
- {
- switch (key)
- {
- case GLUT_KEY_RIGHT:
- alpha1 = 1.0;
- alpha2 = 1.0;
- glutIdleFunc(miscad1);
- break;
- case GLUT_KEY_DOWN:
- glutIdleFunc(stop);
- break;
- }
- glutPostRedisplay();
- }
- void glPrintf(int x, int y, char* sir, int lungime) {
- for (int i = 0; i < lungime; i++) {
- glColor3f(0.0, 0.0, 0.0);
- glRasterPos2i(x + 15 * i, y);
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, sir[i]);
- }
- }
- void desen(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- switch (rendermode) {
- case DESCRIERE:
- {char sir1[] = "Titlu:DEPASIRE";
- glPrintf(ww / 2, wh - 50, sir1, strlen(sir1));
- char sir2[] = " Dovleac Stefania CTI 252 ";
- glPrintf(ww / 2 + 100, 100, sir2, strlen(sir2));
- char sir3[] = "Cerinta:Simulati o depasire folosind 2 dreptunghiuri.";
- glPrintf(0, wh / 2 + 100, sir3, strlen(sir3));
- char sir4[] = "Click dreapta pentru meniu ";
- glPrintf(0, wh / 2, sir4, strlen(sir4));
- }
- break;
- case DEPASIRE:
- {
- char sir5[] = "Apasa tasta 's' pentru a porni dreptunghiurile ";
- glPrintf(0, 40, sir5, strlen(sir5));
- char sir6[] = "Apasa tasta 'p' pentru pauza ";
- glPrintf(0, 20, sir6, strlen(sir6));
- char sir7[] = "Apasa tasta 'e' pantru a incheia executia ";
- glPrintf(0, 0, sir7, strlen(sir7));
- desen_strada();
- glPushMatrix();//primul dreptunghi
- glTranslated(i / 3, 0.0, 0.0);
- glColor3f(0.0, 0.0, 1.0);
- glRecti(A.x, A.y, C.x, C.y);
- glPopMatrix();
- pc = A.x + i / 3;
- if (abs(pc - pcd) > C.x - A.x)
- {
- glPushMatrix(); //dreptunghiul care depaseste
- glTranslated(j / 2, 0.0, 0.0);
- glColor3f(0.9, 0.1, 0.2);
- glRecti(Adep.x, Adep.y, Cdep.x, Cdep.y);
- glPopMatrix();
- pcd = Adep.x + j / 2;
- }
- else
- {
- glPushMatrix(); //dreptunghiul care depaseste
- glTranslated(j / 2, 130.0, 0.0); // dreptunghiul care depaseste este scalat pt ca atunci cad depaseste este mai mic
- glScaled(0.8, 0.8, 0);
- glColor3f(0.9, 0.1, 0.2);
- glRecti(Adep.x, Adep.y, Cdep.x, Cdep.y);
- glPopMatrix();
- pcd = Adep.x + j / 2;
- }
- }
- break;
- };
- glutSwapBuffers();
- glFlush();
- }
- void Normalkey(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 's': alpha1 = 1.0;
- alpha2 = 1.0;
- glutIdleFunc(miscad1);
- break;
- case 'p':
- glutIdleFunc(stop);
- break;
- case 'e':
- exit(0);
- break;
- }
- }
- void main(int argc, char** argv)
- {
- citire();
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(ww, wh);
- glutInitWindowPosition(0, 0);
- glutCreateWindow("DEPASIRE");
- init();
- glutDisplayFunc(desen);
- //glutDisplayFunc(desen2);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(Normalkey);
- glutSpecialFunc(Specialkey);
- glutCreateMenu(menu);
- glutAddMenuEntry("DESCRIEREA PROIECTULUI ", DESCRIERE);
- glutAddMenuEntry("DEPASIRE ", DEPASIRE);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement