CzarnyBarszcz

Zadanie5_lepsze

Mar 22nd, 2021 (edited)
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.36 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,-zakresORTO,zakresORTO);
  97.             else if (width >= height && height > 0)
  98.                 glOrtho (-zakresORTO*width/height,zakresORTO*width/height,-zakresORTO,zakresORTO,-zakresORTO,zakresORTO);
  99.         }
  100.         else
  101.             glOrtho (-zakresORTO,zakresORTO,-zakresORTO,zakresORTO,-zakresORTO,zakresORTO*1.2);
  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);
  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,0,0,-1);
  133.             glutSolidSphere(0.4,slices,stacks);
  134.             glPushMatrix();
  135.                 glColor3d(1,1,1);
  136.                 glTranslated(0,1,0);
  137.                 glRotated(a,0,0,-1);
  138.                 glutWireSphere(0.1,slices,stacks);
  139.             glPopMatrix();
  140.     glPopMatrix();
  141.     glPopMatrix();
  142.  
  143.         glFlush();
  144.         glutSwapBuffers();
  145. }
  146.  
  147. void xyzoka()
  148. {
  149.     cout<<"OKO -X : "<<eyex<<"OKO -Y : "<<eyey<<"OKO -Z : "<<eyez<<endl;
  150. }
  151. void xyzcentrum()
  152. {
  153.     cout<<"centrum -X : "<<pointx<<"centrum -Y : "<<pointy<<"centrum -Z : "<<pointz<<endl;
  154. }
  155.  
  156. static void key(unsigned char key, int x, int y)
  157. {
  158.     switch (key)
  159.     {
  160.         case '-' :
  161.             eyez+=0.1;
  162.             xyzoka();
  163.             xyzcentrum();
  164.         break;
  165.         case '+' :
  166.             eyez-=0.1;
  167.             xyzoka();
  168.             xyzcentrum();
  169.         break;
  170.         case '1' :
  171.             fovy-=0.1;
  172.         break;
  173.         case '2' :
  174.             fovy+=0.1;
  175.         break;
  176.  
  177.  
  178.     }
  179.     Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
  180. }
  181.  
  182. void SpecialKeys( int key, int x, int y )
  183. {
  184.     switch( key )
  185.     {
  186.     case GLUT_KEY_LEFT:
  187.         alfa -= PI / 32;
  188.         pointx = skok_point * cos (alfa) + eyex;
  189.         pointz = skok_point * sin (alfa) + eyez;
  190.         break;
  191.     case GLUT_KEY_RIGHT:
  192.         alfa += PI / 32;
  193.         pointx = skok_point * cos (alfa) + eyex;
  194.         pointz = skok_point * sin (alfa) + eyez;
  195.         break;
  196.     case GLUT_KEY_UP:
  197.         skok_eye += 1.0f;
  198.         eyex = skok_eye * cos (alfa) + eyex;
  199.         eyez = skok_eye * sin (alfa) + eyez;
  200.         pointx = skok_point * cos (alfa) + eyex;
  201.         pointz = skok_point * sin (alfa) + eyez;
  202.         skok_eye = 1.0f;
  203.         break;
  204.     case GLUT_KEY_DOWN:
  205.         skok_eye -= 1.0f;
  206.         eyex = skok_eye * cos (alfa) + eyex;
  207.         eyez = skok_eye * sin (alfa) + eyez;
  208.         pointx = skok_point * cos (alfa) + eyex;
  209.         pointz = skok_point * sin (alfa) + eyez;
  210.         skok_eye = 0.0f;
  211.         break;
  212.     }
  213.  
  214.     Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
  215. }
  216.  
  217. static void idle(void)
  218. {
  219.     glutPostRedisplay();
  220. }
  221.  
  222. const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
  223. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
  224. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  225. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  226.  
  227. const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
  228. const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
  229. const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
  230. const GLfloat high_shininess[] = { 100.0f };
  231.  
  232. void Menu (int value)
  233. {
  234.     switch (value)
  235.     {
  236. // wyjœcie
  237.     case EXIT:
  238.         exit (0);
  239.     case FULL_WINDOW:
  240.         skala=FULL_WINDOW;
  241.         Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  242.         break;
  243.     case ASPECT_1_1:
  244.         skala=ASPECT_1_1;
  245.         Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  246.         break;
  247.     case ORTO:
  248.         rzut=ORTO;
  249.         Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  250.         break;
  251.     case FRUST:
  252.         rzut=FRUST;
  253.         Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  254.         break;
  255.     case PERSP:
  256.         rzut=PERSP;
  257.         Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
  258.         break;
  259.     }
  260. }
  261.  
  262. int main(int argc, char *argv[])
  263. {
  264.     rzut =ORTO;
  265.     glutInit(&argc, argv);
  266.     glutInitWindowSize(640,480);
  267.     glutInitWindowPosition(10,10);
  268.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  269.  
  270.     glutCreateWindow("GLUT Shapes");
  271.  
  272.     glutReshapeFunc(Reshape);
  273.     glutDisplayFunc(display);
  274.     glutKeyboardFunc(key);
  275.     glutSpecialFunc(SpecialKeys);
  276.     glutIdleFunc(idle);
  277.     glutCreateMenu (Menu);
  278.     glutAddMenuEntry ("Rodzaj skalowania - cale okno",FULL_WINDOW);
  279.     glutAddMenuEntry ("Rodzaj skalowania - skala 1:1",ASPECT_1_1);
  280.     glutAddMenuEntry ("Rzutowanie ortogonalne", ORTO);
  281.     glutAddMenuEntry ("Rzutowanie frustum", FRUST);
  282.     glutAddMenuEntry ("Rzutowanie perspective", PERSP);
  283.     glutAddMenuEntry ("Wyjscie",EXIT);
  284.     glutAttachMenu (GLUT_RIGHT_BUTTON);
  285.  
  286.     glClearColor(0,0,0,1);
  287.     glEnable(GL_CULL_FACE);
  288.     glCullFace(GL_BACK);
  289.  
  290.     glEnable(GL_DEPTH_TEST);
  291.     glDepthFunc(GL_LESS);
  292.  
  293.     glEnable(GL_LIGHT0);
  294.     glEnable(GL_NORMALIZE);
  295.     glEnable(GL_COLOR_MATERIAL);
  296.     glEnable(GL_LIGHTING);
  297.  
  298.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
  299.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
  300.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  301.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  302.  
  303.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
  304.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
  305.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
  306.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  307.  
  308.     glutMainLoop();
  309.  
  310.     return EXIT_SUCCESS;
  311. }
  312.  
Add Comment
Please, Sign In to add comment