Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Micha³ Szuleta g1 lab1
- #include <windows.h>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <math.h>
- #include <stdlib.h>
- #include <iostream>
- #include <time.h>
- #include <istream>
- #include <fstream>
- using namespace std;
- enum
- {
- ORTO,
- ASPECT_1_1,
- FRUST,
- PERSP,
- EXIT,
- FULL_WINDOW,
- };
- int rzut,skala;
- int wcisk = 0;
- GLdouble PI = 3.14;
- GLdouble alfa = -PI / 2;
- GLdouble skok_point = 10.0;
- GLdouble skok_eye = 1.0;
- GLdouble eyex=0.0;
- GLdouble eyey=0.0;
- GLdouble eyez=30.0;
- GLdouble pointx = skok_point * cos (alfa);
- GLdouble pointy = 0.0;
- GLdouble pointz = skok_point * sin (alfa);
- GLdouble fovy =45;
- struct wezel
- {
- double x;
- double y;
- double z;
- };
- const int nW=14;
- const int nE=11;
- wezel w[nW];
- struct element
- {
- int lW; //liczba wezlow
- int nrO; // nr obiektu
- int w[4];
- };
- element e[nE];
- int czytanie(const string & nazwaPlik)
- {
- int lWez,lEle;
- int pomI;
- ifstream plik(nazwaPlik.c_str());
- plik>>lWez;
- if(lWez!=nW) cout<<"popraw w programie liczbe wezlow"<<endl;
- for(int i=0;i<nW;i++)
- {
- plik>>pomI>>w[i].x>>w[i].y>>w[i].z;
- cout<<"w"<<i<<" ( "<<w[i].x<<", "<<w[i].y<<", "<<w[i].z<<" )"<<endl;
- }
- plik>>lEle;
- if(lEle!=nE) cout<<"popraw w programie liczbe elementow obiektu 1"<<endl;
- for(int i=0;i<nE;i++)
- {
- plik>>pomI;
- plik>>e[i].lW;
- for(int j=0;j<e[i].lW;j++)
- {
- plik>>e[i].w[j];
- cout<<e[i].w[j]<< " ";
- }
- plik>>e[i].nrO;
- cout<<endl;
- }
- plik.close();
- return 0;
- }
- static void Reshape(int width, int height)
- {
- const float ar = (float) width / (float) height;
- GLdouble aspect = -1.0;
- GLdouble zakres = 1.0;
- GLdouble blisko = 2.0;
- GLdouble daleko = 100.0;
- GLdouble zakresORTO = 15.0;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
- //glOrtho(-ar, ar, -1.0, 1.0, 2.0, 100.0);
- if (rzut==FRUST)
- {
- if (skala==ASPECT_1_1)
- {
- if (width < height && width > 0)
- glFrustum (-zakres,zakres,-zakres*height/width,zakres*height/width,blisko,daleko);
- else if (width >= height && height > 0)
- glFrustum (-zakres*width/height,zakres*width/height,-zakres,zakres,blisko,daleko);
- }
- else
- glFrustum (-zakres,zakres,-zakres,zakres,blisko,daleko);
- }
- if (rzut==ORTO)
- {
- if (skala==ASPECT_1_1)
- {
- if (width < height && width > 0)
- glOrtho (-zakresORTO,zakresORTO,-zakresORTO*height/width,zakresORTO*height/width,-blisko,daleko);
- else if (width >= height && height > 0)
- glOrtho (-zakresORTO*width/height,zakresORTO*width/height,-zakresORTO,zakresORTO,-blisko,daleko);
- }
- else
- glOrtho (-zakresORTO,zakresORTO,-zakresORTO,zakresORTO,-blisko,daleko);
- }
- if (rzut==PERSP)
- {
- if (height > 0)
- aspect = width/(GLdouble)height;
- gluPerspective (90,aspect,blisko,daleko);
- }
- gluLookAt(eyex,eyey,eyez,pointx,pointy,pointz,0,1,0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- }
- static void display(void)
- {
- const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
- const double a = t*90.0;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3d(1,0,0);
- glPushMatrix();
- //glTranslated(-2.4,1.2,-6);
- //glTranslated(0.0,0.0,-6);
- //glRotated(a,0.5,1,0.2);
- for(int i=0;i<nE;i++)
- {
- glColor3d(0.5/i,0,i/10.0);
- if(e[i].lW==3)
- {
- glColor3d(0.5/i,0.5/i,i/10.0);
- cout<<"TROJKAT"<<endl;
- glBegin(GL_TRIANGLES);
- for(int j=0;j<e[i].lW;j++)
- {
- glVertex3d(w[e[i].w[j]].x,w[e[i].w[j]].y,w[e[i].w[j]].z);
- }
- glEnd();
- }
- if(e[i].lW==4)
- {
- glColor3d(0,0.5/i,i/10.0);
- cout<<"KWADRAT"<<endl;
- glBegin(GL_QUADS);
- for(int j=0;j<e[i].lW;j++)
- {
- glVertex3d(w[e[i].w[j]].x,w[e[i].w[j]].y,w[e[i].w[j]].z);
- cout<<"x : "<<w[e[i].w[j]].x<<" y: "<<w[e[i].w[j]].y<<" z: "<<w[e[i].w[j]].z<<endl;
- }
- glEnd();
- }
- }
- glPopMatrix();
- glFlush();
- glutSwapBuffers();
- }
- void xyzoka()
- {
- cout<<"OKO -X : "<<eyex<<"OKO -Y : "<<eyey<<"OKO -Z : "<<eyez<<endl;
- }
- void xyzcentrum()
- {
- cout<<"centrum -X : "<<pointx<<"centrum -Y : "<<pointy<<"centrum -Z : "<<pointz<<endl;
- }
- static void key(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case '-' :
- eyez+=0.1;
- xyzoka();
- xyzcentrum();
- break;
- case '+' :
- eyez-=0.1;
- xyzoka();
- xyzcentrum();
- break;
- case '1' :
- fovy-=0.1;
- break;
- case '2' :
- fovy+=0.1;
- break;
- case 'w':
- skok_eye += 1.0f;
- eyex = skok_eye * cos (alfa) + eyex;
- eyez = skok_eye * sin (alfa) + eyez;
- pointx = skok_point * cos (alfa) + eyex;
- pointz = skok_point * sin (alfa) + eyez;
- skok_eye = 1.0f;
- break;
- case 's':
- skok_eye -= 1.0f;
- eyex = skok_eye * cos (alfa) + eyex;
- eyez = skok_eye * sin (alfa) + eyez;
- pointx = skok_point * cos (alfa) + eyex;
- pointz = skok_point * sin (alfa) + eyez;
- skok_eye = 0.0f;
- xyzoka();
- break;
- }
- Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
- }
- void SpecialKeys( int key, int x, int y )
- {
- switch( key )
- {
- case GLUT_KEY_LEFT:
- alfa -= PI / 32;
- pointx = skok_point * cos (alfa) + eyex;
- pointz = skok_point * sin (alfa) + eyez;
- break;
- case GLUT_KEY_RIGHT:
- alfa += PI / 32;
- pointx = skok_point * cos (alfa) + eyex;
- pointz = skok_point * sin (alfa) + eyez;
- break;
- case GLUT_KEY_UP:
- alfa += PI / 32;
- pointy = skok_point * cos (alfa) + eyey;
- pointz = skok_point * sin (alfa) + eyez;
- break;
- case GLUT_KEY_DOWN:
- alfa -= PI / 32;
- pointy = skok_point * cos (alfa) + eyey;
- pointz = skok_point * sin (alfa) + eyez;
- break;
- }
- Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
- const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
- const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
- const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat high_shininess[] = { 100.0f };
- void Menu (int value)
- {
- switch (value)
- {
- case EXIT:
- exit (0);
- break;
- }
- }
- void resh(int value)
- {
- switch (value)
- {
- case ORTO:
- rzut=ORTO;
- Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
- break;
- case FRUST:
- rzut=FRUST;
- Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
- break;
- case PERSP:
- rzut=PERSP;
- Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
- break;
- }
- }
- void skal(int value)
- {
- switch (value)
- {
- case FULL_WINDOW:
- skala=FULL_WINDOW;
- Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
- break;
- case ASPECT_1_1:
- skala=ASPECT_1_1;
- Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
- break;
- }
- }
- int main(int argc, char *argv[])
- {
- rzut =FRUST;
- czytanie("c:\\tmp\\proba2.txt");
- glutInit(&argc, argv);
- glutInitWindowSize(640,480);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("GLUT Shapes");
- glutReshapeFunc(Reshape);
- glutDisplayFunc(display);
- glutKeyboardFunc(key);
- glutSpecialFunc(SpecialKeys);
- glutIdleFunc(idle);
- int sub1=glutCreateMenu(skal);
- glutAddMenuEntry ("Rodzaj skalowania - cale okno",FULL_WINDOW);
- glutAddMenuEntry ("Rodzaj skalowania - skala 1:1",ASPECT_1_1);
- int sub2=glutCreateMenu(resh);
- glutAddMenuEntry ("Rzutowanie ortogonalne", ORTO);
- glutAddMenuEntry ("Rzutowanie frustum", FRUST);
- glutAddMenuEntry ("Rzutowanie perspective", PERSP);
- int mainmenu =glutCreateMenu(Menu);
- glutAddSubMenu("Skalowanie",sub1);
- glutAddSubMenu("Rzutowanie", sub2);
- glutAddMenuEntry ("Wyjscie",EXIT);
- glutAttachMenu (GLUT_RIGHT_BUTTON);
- glClearColor(0,0,0,1);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHTING);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
- /*
- 14
- 0 -3 -3 6
- 1 3 -3 6
- 2 3 3 6
- 3 -3 3 6
- 4 -3 -3 0
- 5 3 -3 0
- 6 3 3 0
- 7 -3 3 0
- 8 -3 3 0
- 9 3 3 0
- 10 0 6 0
- 11 -3 3 6
- 12 3 3 6
- 13 0 6 6
- 11
- 1 4 0 1 2 3 1
- 2 4 5 4 7 6 1
- 3 4 4 0 3 7 1
- 4 4 1 5 6 2 1
- 5 4 4 5 1 0 1
- 6 4 3 2 6 7 1
- 1 3 10 9 8 2
- 2 3 11 12 13 2
- 3 4 8 9 12 11 2
- 4 4 9 10 13 12 2
- 5 4 11 13 10 8 2
- */
Advertisement
Add Comment
Please, Sign In to add comment