CzarnyBarszcz

zadani5_4

Mar 27th, 2021
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.58 KB | None | 0 0
  1.  
  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.  
  12. using namespace std;
  13.  
  14. enum
  15. {
  16. ORTO,
  17. ASPECT_1_1,
  18. FRUST,
  19. PERSP,
  20. EXIT,
  21. FULL_WINDOW,
  22. };
  23.  
  24. int rzut,skala;
  25. static int slices = 32;
  26. static int stacks = 32;
  27.  
  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=1.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.  
  46. /* GLUT callback Handlers */
  47. /*
  48. static void Reshape(int width, int height)
  49. {
  50. const float ar = (float) width / (float) height;
  51.  
  52. glViewport(0, 0, width, height);
  53. glMatrixMode(GL_PROJECTION);
  54. glLoadIdentity();
  55. glFrustum(-1, 1, -1.0, 1.0, 2.0, 100.0);
  56. //glOrtho(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  57. gluLookAt(eyex,eyey,eyez,pointx,pointy,pointz,0,1,0);
  58.  
  59. //glMatrixMode(GL_MODELVIEW);
  60. //glLoadIdentity() ;
  61. }
  62. */
  63. static void Reshape(int width, int height)
  64. {
  65. const float ar = (float) width / (float) height;
  66.  
  67. GLdouble aspect = -1.0;
  68. GLdouble zakres = 1.0;
  69. GLdouble blisko = 2.0;
  70. GLdouble daleko = 100.0;
  71.  
  72. GLdouble zakresORTO = 15.0;
  73.  
  74. glViewport(0, 0, width, height);
  75. glMatrixMode(GL_PROJECTION);
  76. glLoadIdentity();
  77. //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  78. //glOrtho(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  79. if (rzut==FRUST)
  80. {
  81. if (skala==ASPECT_1_1)
  82. {
  83. if (width < height && width > 0)
  84. glFrustum (-zakres,zakres,-zakres*height/width,zakres*height/width,blisko,daleko);
  85. else if (width >= height && height > 0)
  86. glFrustum (-zakres*width/height,zakres*width/height,-zakres,zakres,blisko,daleko);
  87. }
  88. else
  89. glFrustum (-zakres,zakres,-zakres,zakres,blisko,daleko);
  90. }
  91. if (rzut==ORTO)
  92. {
  93. if (skala==ASPECT_1_1)
  94. {
  95. if (width < height && width > 0)
  96. glOrtho (-zakresORTO,zakresORTO,-zakresORTO*height/width,zakresORTO*height/width,-blisko,daleko);
  97. else if (width >= height && height > 0)
  98. glOrtho (-zakresORTO*width/height,zakresORTO*width/height,-zakresORTO,zakresORTO,-blisko,daleko);
  99. }
  100. else
  101. glOrtho (-zakresORTO,zakresORTO,-zakresORTO,zakresORTO,-blisko,daleko);
  102. }
  103. if (rzut==PERSP)
  104. {
  105. if (height > 0)
  106. aspect = width/(GLdouble)height;
  107. gluPerspective (90,aspect,blisko,daleko);
  108. }
  109. //gluLookAt(eyex,eyey,eyez,pointx,pointy,pointz,0,1,0);
  110. gluLookAt(eyex,eyey,eyez,pointx,pointy,pointz,0,1,0);
  111. glMatrixMode(GL_MODELVIEW);
  112. glLoadIdentity() ;
  113. }
  114.  
  115. static void display(void)
  116. {
  117. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  118. const double a = t*90.0;
  119.  
  120. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  121.  
  122. glPushMatrix();
  123. glColor3d(1,1,0);
  124. glTranslated(0,0,-6);
  125. glutSolidSphere(3.5,slices,stacks);//Slonce
  126. glPushMatrix();
  127. glColor3d(0,0.5,1);
  128. glRotated(a/12,0,1,0);
  129. glTranslated(0.0,0.0,14.0);
  130. glScaled(1.0,1.03,1.0);
  131. glRotated(123,1,0,0);
  132. glRotated(a*30.4,0,0,-1);
  133. glutSolidSphere(0.4,slices,stacks);//Ziemia
  134. glPushMatrix();
  135. glColor3d(1,1,1);
  136. glRotated((a*30.4)+a,0,0,1);
  137. glTranslated(0,1,0);
  138. glutSolidSphere(0.1,slices,stacks);//Ksiezyc
  139. glPopMatrix();
  140. glPopMatrix();
  141. glPushMatrix();
  142. glColor3d(0,1,0);
  143. //glTranslated(0.0,2.0,16.0);
  144. glTranslated(0.0,0.0,16.0);
  145. //glRotated(a/12,0,1,0);
  146. glRotated(90,1,0,0);
  147. glutSolidSphere(0.5,slices,stacks);//Nowa Planeta
  148. glPushMatrix();
  149. glColor3d(1,0,0);
  150. glScaled(1,1,0.3);
  151. glTranslated(0.0,0.0,0.2);
  152. glRotated(60,1,0,0);
  153. glRotated(-a,0,0,1);
  154. glutSolidTorus(0.1,0.8,slices,stacks);//Pierscien 1
  155. glPopMatrix();
  156. glPushMatrix();
  157. glColor3d(0,1,1);
  158. glScaled(1,1,0.3);
  159. glTranslated(0.0,0.0,-0.2);
  160. glRotated(-60,1,0,0);
  161. glRotated(a,0,0,1);
  162. glutSolidTorus(0.1,0.8,slices,stacks);// pierscien 2
  163. glPopMatrix();
  164. glPopMatrix();
  165. glPopMatrix();
  166.  
  167. glFlush();
  168. glutSwapBuffers();
  169. }
  170.  
  171. void xyzoka()
  172. {
  173. cout<<"OKO -X : "<<eyex<<"OKO -Y : "<<eyey<<"OKO -Z : "<<eyez<<endl;
  174. }
  175. void xyzcentrum()
  176. {
  177. cout<<"centrum -X : "<<pointx<<"centrum -Y : "<<pointy<<"centrum -Z : "<<pointz<<endl;
  178. }
  179.  
  180. static void key(unsigned char key, int x, int y)
  181. {
  182. switch (key)
  183. {
  184. case '-' :
  185. eyez+=0.1;
  186. xyzoka();
  187. xyzcentrum();
  188. break;
  189. case '+' :
  190. eyez-=0.1;
  191. xyzoka();
  192. xyzcentrum();
  193. break;
  194. case '1' :
  195. fovy-=0.1;
  196. break;
  197. case '2' :
  198. fovy+=0.1;
  199. break;
  200. case 'w':
  201. skok_eye += 1.0f;
  202. eyex = skok_eye * cos (alfa) + eyex;
  203. eyez = skok_eye * sin (alfa) + eyez;
  204. pointx = skok_point * cos (alfa) + eyex;
  205. pointz = skok_point * sin (alfa) + eyez;
  206. skok_eye = 1.0f;
  207. break;
  208. case 's':
  209. skok_eye -= 1.0f;
  210. eyex = skok_eye * cos (alfa) + eyex;
  211. eyez = skok_eye * sin (alfa) + eyez;
  212. pointx = skok_point * cos (alfa) + eyex;
  213. pointz = skok_point * sin (alfa) + eyez;
  214. skok_eye = 0.0f;
  215. break;
  216.  
  217.  
  218. }
  219. Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
  220. }
  221.  
  222. void SpecialKeys( int key, int x, int y )
  223. {
  224. switch( key )
  225. {
  226. case GLUT_KEY_LEFT:
  227. alfa -= PI / 32;
  228. pointx = skok_point * cos (alfa) + eyex;
  229. pointz = skok_point * sin (alfa) + eyez;
  230. break;
  231. case GLUT_KEY_RIGHT:
  232. alfa += PI / 32;
  233. pointx = skok_point * cos (alfa) + eyex;
  234. pointz = skok_point * sin (alfa) + eyez;
  235. break;
  236. case GLUT_KEY_UP:
  237. alfa += PI / 32;
  238. pointy = skok_point * cos (alfa) + eyey;
  239. pointz = skok_point * sin (alfa) + eyez;
  240. break;
  241. case GLUT_KEY_DOWN:
  242. alfa -= PI / 32;
  243. pointy = skok_point * cos (alfa) + eyey;
  244. pointz = skok_point * sin (alfa) + eyez;
  245. break;
  246. }
  247.  
  248. Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
  249. }
  250.  
  251. static void idle(void)
  252. {
  253. glutPostRedisplay();
  254. }
  255.  
  256. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  257. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  258. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  259. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  260.  
  261. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  262. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  263. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  264. const GLfloat high_shininess[] = { 100.0f };
  265.  
  266. void Menu (int value)
  267. {
  268. switch (value)
  269. {
  270. // wyjœcie
  271. case EXIT:
  272. exit (0);
  273. case FULL_WINDOW:
  274. skala=FULL_WINDOW;
  275. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  276. break;
  277. case ASPECT_1_1:
  278. skala=ASPECT_1_1;
  279. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  280. break;
  281. case ORTO:
  282. rzut=ORTO;
  283. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  284. break;
  285. case FRUST:
  286. rzut=FRUST;
  287. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  288. break;
  289. case PERSP:
  290. rzut=PERSP;
  291. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  292. break;
  293. }
  294. }
  295. void resh(int value)
  296. {
  297. switch (value)
  298. {
  299. case ORTO:
  300. rzut=ORTO;
  301. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  302. break;
  303. case FRUST:
  304. rzut=FRUST;
  305. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  306. break;
  307. case PERSP:
  308. rzut=PERSP;
  309. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  310. break;
  311. }
  312. }
  313. void skal(int value)
  314. {
  315. switch (value)
  316. {
  317. case FULL_WINDOW:
  318. skala=FULL_WINDOW;
  319. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  320. break;
  321. case ASPECT_1_1:
  322. skala=ASPECT_1_1;
  323. Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  324. break;
  325. }
  326. }
  327.  
  328. int main(int argc, char *argv[])
  329. {
  330. rzut =FRUST;
  331. glutInit(&argc, argv);
  332. glutInitWindowSize(640,480);
  333. glutInitWindowPosition(10,10);
  334. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  335.  
  336. glutCreateWindow("GLUT Shapes");
  337.  
  338. glutReshapeFunc(Reshape);
  339. glutDisplayFunc(display);
  340. glutKeyboardFunc(key);
  341. glutSpecialFunc(SpecialKeys);
  342. glutIdleFunc(idle);
  343. int sub1=glutCreateMenu(skal);
  344. glutAddMenuEntry ("Rodzaj skalowania - cale okno",FULL_WINDOW);
  345. glutAddMenuEntry ("Rodzaj skalowania - skala 1:1",ASPECT_1_1);
  346. int sub2=glutCreateMenu(resh);
  347. glutAddMenuEntry ("Rzutowanie ortogonalne", ORTO);
  348. glutAddMenuEntry ("Rzutowanie frustum", FRUST);
  349. glutAddMenuEntry ("Rzutowanie perspective", PERSP);
  350. int mainmenu =glutCreateMenu(Menu);
  351. glutAddSubMenu("Skalowanie",sub1);
  352. glutAddSubMenu("Rzutowanie", sub2);
  353. glutAddMenuEntry ("Wyjscie",EXIT);
  354. glutAttachMenu (GLUT_RIGHT_BUTTON);
  355.  
  356. glClearColor(0,0,0,1);
  357. /*
  358. glEnable(GL_CULL_FACE);
  359. glCullFace(GL_BACK);
  360.  
  361. glEnable(GL_DEPTH_TEST);
  362. glDepthFunc(GL_LESS);
  363.  
  364. glEnable(GL_LIGHT0);
  365. glEnable(GL_NORMALIZE);
  366. glEnable(GL_COLOR_MATERIAL);
  367. glEnable(GL_LIGHTING);
  368.  
  369. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  370. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  371. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  372. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  373.  
  374. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  375. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  376. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  377. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  378. */
  379. glutMainLoop();
  380.  
  381. return EXIT_SUCCESS;
  382. }
  383.  
Advertisement
Add Comment
Please, Sign In to add comment