CzarnyBarszcz

Reprezentacja brzegowa

May 11th, 2021
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. //Micha³ Szuleta g1 lab1
  2. #include <windows.h>
  3. #ifdef __APPLE__
  4. #include <GLUT/glut.h>
  5. #else
  6. #include <GL/glut.h>
  7. #endif
  8. #include <math.h>
  9. #include <stdlib.h>
  10. #include <iostream>
  11. #include <time.h>
  12. #include <istream>
  13. #include <fstream>
  14.  
  15. using namespace std;
  16.  
  17. enum
  18. {
  19. ORTO,
  20. ASPECT_1_1,
  21. FRUST,
  22. PERSP,
  23. EXIT,
  24. FULL_WINDOW,
  25. };
  26.  
  27. int rzut,skala;
  28. int wcisk = 0;
  29.  
  30. GLdouble PI = 3.14;
  31. GLdouble alfa = -PI / 2;
  32. GLdouble skok_point = 10.0;
  33. GLdouble skok_eye = 1.0;
  34.  
  35. GLdouble eyex=0.0;
  36. GLdouble eyey=0.0;
  37. GLdouble eyez=30.0;
  38.  
  39. GLdouble pointx = skok_point * cos (alfa);
  40. GLdouble pointy = 0.0;
  41. GLdouble pointz = skok_point * sin (alfa);
  42.  
  43. GLdouble fovy =45;
  44.  
  45. struct wezel
  46. {
  47. double x;
  48. double y;
  49. double z;
  50. };
  51. const int nW=14;
  52. const int nE=11;
  53. wezel w[nW];
  54.  
  55. struct element
  56. {
  57. int lW; //liczba wezlow
  58. int nrO; // nr obiektu
  59. int w[4];
  60. };
  61. element e[nE];
  62.  
  63. int czytanie(const string & nazwaPlik)
  64. {
  65. int lWez,lEle;
  66. int pomI;
  67. ifstream plik(nazwaPlik.c_str());
  68. plik>>lWez;
  69. if(lWez!=nW) cout<<"popraw w programie liczbe wezlow"<<endl;
  70. for(int i=0;i<nW;i++)
  71. {
  72. plik>>pomI>>w[i].x>>w[i].y>>w[i].z;
  73. cout<<"w"<<i<<" ( "<<w[i].x<<", "<<w[i].y<<", "<<w[i].z<<" )"<<endl;
  74. }
  75. plik>>lEle;
  76. if(lEle!=nE) cout<<"popraw w programie liczbe elementow obiektu 1"<<endl;
  77. for(int i=0;i<nE;i++)
  78. {
  79. plik>>pomI;
  80. plik>>e[i].lW;
  81. for(int j=0;j<e[i].lW;j++)
  82. {
  83. plik>>e[i].w[j];
  84. cout<<e[i].w[j]<< " ";
  85. }
  86. plik>>e[i].nrO;
  87. cout<<endl;
  88. }
  89. plik.close();
  90. return 0;
  91. }
  92.  
  93. static void Reshape(int width, int height)
  94. {
  95. const float ar = (float) width / (float) height;
  96.  
  97. GLdouble aspect = -1.0;
  98. GLdouble zakres = 1.0;
  99. GLdouble blisko = 2.0;
  100. GLdouble daleko = 100.0;
  101.  
  102. GLdouble zakresORTO = 15.0;
  103.  
  104. glViewport(0, 0, width, height);
  105. glMatrixMode(GL_PROJECTION);
  106. glLoadIdentity();
  107. //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  108. //glOrtho(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  109. if (rzut==FRUST)
  110. {
  111. if (skala==ASPECT_1_1)
  112. {
  113. if (width < height && width > 0)
  114. glFrustum (-zakres,zakres,-zakres*height/width,zakres*height/width,blisko,daleko);
  115. else if (width >= height && height > 0)
  116. glFrustum (-zakres*width/height,zakres*width/height,-zakres,zakres,blisko,daleko);
  117. }
  118. else
  119. glFrustum (-zakres,zakres,-zakres,zakres,blisko,daleko);
  120. }
  121. if (rzut==ORTO)
  122. {
  123. if (skala==ASPECT_1_1)
  124. {
  125. if (width < height && width > 0)
  126. glOrtho (-zakresORTO,zakresORTO,-zakresORTO*height/width,zakresORTO*height/width,-blisko,daleko);
  127. else if (width >= height && height > 0)
  128. glOrtho (-zakresORTO*width/height,zakresORTO*width/height,-zakresORTO,zakresORTO,-blisko,daleko);
  129. }
  130. else
  131. glOrtho (-zakresORTO,zakresORTO,-zakresORTO,zakresORTO,-blisko,daleko);
  132. }
  133. if (rzut==PERSP)
  134. {
  135. if (height > 0)
  136. aspect = width/(GLdouble)height;
  137. gluPerspective (90,aspect,blisko,daleko);
  138. }
  139.  
  140. gluLookAt(eyex,eyey,eyez,pointx,pointy,pointz,0,1,0);
  141. glMatrixMode(GL_MODELVIEW);
  142. glLoadIdentity() ;
  143. }
  144. static void display(void)
  145. {
  146. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  147. const double a = t*90.0;
  148.  
  149. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  150. glColor3d(1,0,0);
  151.  
  152. glPushMatrix();
  153. //glTranslated(-2.4,1.2,-6);
  154. //glTranslated(0.0,0.0,-6);
  155. //glRotated(a,0.5,1,0.2);
  156. for(int i=0;i<nE;i++)
  157. {
  158. glColor3d(0.5/i,0,i/10.0);
  159. if(e[i].lW==3)
  160. {
  161. glColor3d(0.5/i,0.5/i,i/10.0);
  162. cout<<"TROJKAT"<<endl;
  163. glBegin(GL_TRIANGLES);
  164. for(int j=0;j<e[i].lW;j++)
  165. {
  166. glVertex3d(w[e[i].w[j]].x,w[e[i].w[j]].y,w[e[i].w[j]].z);
  167. }
  168. glEnd();
  169. }
  170. if(e[i].lW==4)
  171. {
  172. glColor3d(0,0.5/i,i/10.0);
  173. cout<<"KWADRAT"<<endl;
  174. glBegin(GL_QUADS);
  175. for(int j=0;j<e[i].lW;j++)
  176. {
  177. glVertex3d(w[e[i].w[j]].x,w[e[i].w[j]].y,w[e[i].w[j]].z);
  178. cout<<"x : "<<w[e[i].w[j]].x<<" y: "<<w[e[i].w[j]].y<<" z: "<<w[e[i].w[j]].z<<endl;
  179. }
  180. glEnd();
  181. }
  182. }
  183. glPopMatrix();
  184. glFlush();
  185. glutSwapBuffers();
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. void xyzoka()
  193. {
  194. cout<<"OKO -X : "<<eyex<<"OKO -Y : "<<eyey<<"OKO -Z : "<<eyez<<endl;
  195. }
  196. void xyzcentrum()
  197. {
  198. cout<<"centrum -X : "<<pointx<<"centrum -Y : "<<pointy<<"centrum -Z : "<<pointz<<endl;
  199. }
  200.  
  201. static void key(unsigned char key, int x, int y)
  202. {
  203. switch (key)
  204. {
  205. case '-' :
  206. eyez+=0.1;
  207. xyzoka();
  208. xyzcentrum();
  209. break;
  210. case '+' :
  211. eyez-=0.1;
  212. xyzoka();
  213. xyzcentrum();
  214. break;
  215. case '1' :
  216. fovy-=0.1;
  217. break;
  218. case '2' :
  219. fovy+=0.1;
  220. break;
  221. case 'w':
  222. skok_eye += 1.0f;
  223. eyex = skok_eye * cos (alfa) + eyex;
  224. eyez = skok_eye * sin (alfa) + eyez;
  225. pointx = skok_point * cos (alfa) + eyex;
  226. pointz = skok_point * sin (alfa) + eyez;
  227. skok_eye = 1.0f;
  228. break;
  229. case 's':
  230. skok_eye -= 1.0f;
  231. eyex = skok_eye * cos (alfa) + eyex;
  232. eyez = skok_eye * sin (alfa) + eyez;
  233. pointx = skok_point * cos (alfa) + eyex;
  234. pointz = skok_point * sin (alfa) + eyez;
  235. skok_eye = 0.0f;
  236. xyzoka();
  237. break;
  238.  
  239.  
  240. }
  241. Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
  242. }
  243.  
  244. void SpecialKeys( int key, int x, int y )
  245. {
  246. switch( key )
  247. {
  248. case GLUT_KEY_LEFT:
  249. alfa -= PI / 32;
  250. pointx = skok_point * cos (alfa) + eyex;
  251. pointz = skok_point * sin (alfa) + eyez;
  252. break;
  253. case GLUT_KEY_RIGHT:
  254. alfa += PI / 32;
  255. pointx = skok_point * cos (alfa) + eyex;
  256. pointz = skok_point * sin (alfa) + eyez;
  257. break;
  258. case GLUT_KEY_UP:
  259. alfa += PI / 32;
  260. pointy = skok_point * cos (alfa) + eyey;
  261. pointz = skok_point * sin (alfa) + eyez;
  262. break;
  263. case GLUT_KEY_DOWN:
  264. alfa -= PI / 32;
  265. pointy = skok_point * cos (alfa) + eyey;
  266. pointz = skok_point * sin (alfa) + eyez;
  267. break;
  268. }
  269.  
  270. Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
  271. }
  272.  
  273. static void idle(void)
  274. {
  275. glutPostRedisplay();
  276. }
  277.  
  278. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  279. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  280. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  281. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  282.  
  283. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  284. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  285. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  286. const GLfloat high_shininess[] = { 100.0f };
  287.  
  288. void Menu (int value)
  289. {
  290. switch (value)
  291. {
  292. case EXIT:
  293. exit (0);
  294. break;
  295. }
  296. }
  297. void resh(int value)
  298. {
  299. switch (value)
  300. {
  301. case ORTO:
  302. rzut=ORTO;
  303. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  304. break;
  305. case FRUST:
  306. rzut=FRUST;
  307. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  308. break;
  309. case PERSP:
  310. rzut=PERSP;
  311. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  312. break;
  313. }
  314. }
  315. void skal(int value)
  316. {
  317. switch (value)
  318. {
  319. case FULL_WINDOW:
  320. skala=FULL_WINDOW;
  321. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  322. break;
  323. case ASPECT_1_1:
  324. skala=ASPECT_1_1;
  325. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  326. break;
  327. }
  328. }
  329.  
  330. int main(int argc, char *argv[])
  331. {
  332. rzut =FRUST;
  333. czytanie("c:\\tmp\\proba2.txt");
  334. glutInit(&argc, argv);
  335. glutInitWindowSize(640,480);
  336. glutInitWindowPosition(10,10);
  337. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  338.  
  339. glutCreateWindow("GLUT Shapes");
  340.  
  341. glutReshapeFunc(Reshape);
  342. glutDisplayFunc(display);
  343. glutKeyboardFunc(key);
  344. glutSpecialFunc(SpecialKeys);
  345. glutIdleFunc(idle);
  346. int sub1=glutCreateMenu(skal);
  347. glutAddMenuEntry ("Rodzaj skalowania - cale okno",FULL_WINDOW);
  348. glutAddMenuEntry ("Rodzaj skalowania - skala 1:1",ASPECT_1_1);
  349. int sub2=glutCreateMenu(resh);
  350. glutAddMenuEntry ("Rzutowanie ortogonalne", ORTO);
  351. glutAddMenuEntry ("Rzutowanie frustum", FRUST);
  352. glutAddMenuEntry ("Rzutowanie perspective", PERSP);
  353. int mainmenu =glutCreateMenu(Menu);
  354. glutAddSubMenu("Skalowanie",sub1);
  355. glutAddSubMenu("Rzutowanie", sub2);
  356. glutAddMenuEntry ("Wyjscie",EXIT);
  357. glutAttachMenu (GLUT_RIGHT_BUTTON);
  358.  
  359. glClearColor(0,0,0,1);
  360.  
  361. glEnable(GL_CULL_FACE);
  362. glCullFace(GL_BACK);
  363.  
  364. glEnable(GL_DEPTH_TEST);
  365. glDepthFunc(GL_LESS);
  366.  
  367. glEnable(GL_LIGHT0);
  368. glEnable(GL_NORMALIZE);
  369. glEnable(GL_COLOR_MATERIAL);
  370. glEnable(GL_LIGHTING);
  371.  
  372. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  373. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  374. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  375. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  376.  
  377. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  378. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  379. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  380. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  381.  
  382. glutMainLoop();
  383.  
  384. return EXIT_SUCCESS;
  385. }
  386. /*
  387. 14
  388. 0 -3 -3 6
  389. 1 3 -3 6
  390. 2 3 3 6
  391. 3 -3 3 6
  392. 4 -3 -3 0
  393. 5 3 -3 0
  394. 6 3 3 0
  395. 7 -3 3 0
  396. 8 -3 3 0
  397. 9 3 3 0
  398. 10 0 6 0
  399. 11 -3 3 6
  400. 12 3 3 6
  401. 13 0 6 6
  402. 11
  403. 1 4 0 1 2 3 1
  404. 2 4 5 4 7 6 1
  405. 3 4 4 0 3 7 1
  406. 4 4 1 5 6 2 1
  407. 5 4 4 5 1 0 1
  408. 6 4 3 2 6 7 1
  409.  
  410. 1 3 10 9 8 2
  411. 2 3 11 12 13 2
  412. 3 4 8 9 12 11 2
  413. 4 4 9 10 13 12 2
  414. 5 4 11 13 10 8 2
  415.  
  416. */
  417.  
Advertisement
Add Comment
Please, Sign In to add comment