icatalin

tema 6 grafica

Apr 29th, 2020
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. //SURSA:  lighthouse3D:  http://www.lighthouse3d.com/tutorials/glut-tutorial/keyboard-example-moving-around-the-world/
  2.  
  3. #include<gl/freeglut.h>
  4. #include<math.h>
  5. // angle of rotation for the camera direction
  6. float angle=0.0;
  7. // actual vector representing the camera's direction
  8. float lx=0.0f,lz=-1.0f;
  9. // XZ position of the camera
  10. float x=0.0f,z=5.0f;
  11.  
  12. void changeSize(int w, int h)
  13. {
  14.  
  15. // Prevent a divide by zero, when window is too short
  16. // (you cant make a window of zero width).
  17. if (h == 0)
  18. h = 1;
  19. float ratio = w * 1.0 / h;
  20.  
  21. // Use the Projection Matrix
  22. glMatrixMode(GL_PROJECTION);
  23.  
  24. // Reset Matrix
  25. glLoadIdentity();
  26.  
  27. // Set the viewport to be the entire window
  28. glViewport(0, 0, w, h);
  29.  
  30. // Set the correct perspective.
  31. gluPerspective(45.0f, ratio, 0.1f, 100.0f);
  32.  
  33. // Get Back to the Modelview
  34. glMatrixMode(GL_MODELVIEW);
  35. }
  36.  
  37. enum {
  38.   IARNA, PRIMAVARA
  39. };
  40.  
  41. int rendermode = IARNA;
  42.  
  43. void menu(int selection)
  44. {
  45.   rendermode = selection;
  46.   glutPostRedisplay();
  47. }
  48.  
  49.  
  50. void drawTree() {
  51.  
  52.     glColor3d (0.4, 0.25, 0.12);
  53.     GLUquadricObj *cylinder;
  54.     glPushMatrix ( );
  55.     glRotatef (-90.0, 1.0, 0, 0);
  56.     cylinder = gluNewQuadric ( );
  57.     gluCylinder (cylinder, 0.33, 0.33, 1.5, 40, 20);
  58.     glPopMatrix ( );
  59.  
  60.     glColor3d(0.2,0.8,0.0);
  61.     glTranslatef(0.0f,1.7f,0.0f);
  62.     glutSolidSphere(0.70f,20,20);
  63.     glutPostRedisplay();
  64. }
  65.  
  66. void drawSnowMan() {
  67.  
  68.     glColor3f(1.0f, 1.0f, 1.0f);
  69.  
  70. // Draw Body
  71.     glTranslatef(0.0f ,0.75f, 0.0f);
  72.     glutSolidSphere(0.75f,20,20);
  73.  
  74. // Draw Head
  75.     glTranslatef(0.0f, 1.0f, 0.0f);
  76.     glutSolidSphere(0.25f,20,20);
  77.  
  78. // Draw Eyes
  79.     glPushMatrix();
  80.     glColor3f(0.0f,0.0f,0.0f);
  81.     glTranslatef(0.05f, 0.10f, 0.18f);
  82.     glutSolidSphere(0.05f,10,10);
  83.     glTranslatef(-0.1f, 0.0f, 0.0f);
  84.     glutSolidSphere(0.05f,10,10);
  85.     glPopMatrix();
  86.  
  87. // Draw Nose
  88.     glColor3f(1.0f, 0.5f , 0.5f);
  89.     glutSolidCone(0.08f,0.5f,10,2);
  90. }
  91.  
  92. void renderScene(void) {
  93.  
  94.     // Clear Color and Depth Buffers
  95.  
  96.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  97.  
  98.     // Reset transformations
  99.     glLoadIdentity();
  100.     // Set the camera
  101.     gluLookAt(  x, 1.0f, z,
  102.             x+lx, 1.0f,  z+lz,
  103.             0.0f, 1.0f,  0.0f);
  104.  
  105.     glClearColor(0.529, 0.808, 0.980,0.0);
  106.  
  107.     switch(rendermode)
  108.     {
  109.     case PRIMAVARA:
  110.                 // Draw ground
  111.             glColor3f(0.0f, 0.6f, 0.0f);
  112.             glBegin(GL_QUADS);
  113.                 glVertex3f(-100.0f, 0.0f, -100.0f);
  114.                 glVertex3f(-100.0f, 0.0f,  100.0f);
  115.                 glVertex3f( 100.0f, 0.0f,  100.0f);
  116.                 glVertex3f( 100.0f, 0.0f, -100.0f);
  117.             glEnd();
  118.  
  119.  
  120.             glColor3f(1.000, 0.843, 0.000);
  121.             glPushMatrix();
  122.             glTranslatef(10.0,12.0,-25.0);
  123.             glutSolidSphere(2.0f,30,30);
  124.             glPopMatrix();
  125.  
  126.  
  127.                 // Draw 36 trees
  128.             for(int i = -3; i < 3; i++)
  129.                 for(int j=-3; j < 3; j++) {
  130.                     glPushMatrix();
  131.                     glTranslatef(i*10.0,0,j * 10.0);
  132.                     drawTree();
  133.                     glPopMatrix();
  134.                 }
  135.     break;
  136.     case IARNA:
  137.             glColor3f(0.9f, 0.9f, 0.9f);
  138.             glBegin(GL_QUADS);
  139.                 glVertex3f(-100.0f, 0.0f, -100.0f);
  140.                 glVertex3f(-100.0f, 0.0f,  100.0f);
  141.                 glVertex3f( 100.0f, 0.0f,  100.0f);
  142.                 glVertex3f( 100.0f, 0.0f, -100.0f);
  143.             glEnd();
  144.  
  145.             for(int i = -3; i < 3; i++)
  146.                 for(int j=-3; j < 3; j++) {
  147.                     glPushMatrix();
  148.                     glTranslatef(i*10.0,0,j * 10.0);
  149.                     drawSnowMan();
  150.                     glPopMatrix();
  151.                 }
  152.     break;
  153.  
  154.     }
  155.     glutSwapBuffers();
  156. }
  157.  
  158. void processNormalKeys(unsigned char key, int x, int y)
  159. {
  160.     float fraction = 0.1f;
  161.  
  162.     switch (key) {
  163.         case 'l' :
  164.             angle -= 0.01f;
  165.             lx = sin(angle);
  166.             lz = -cos(angle);
  167.             break;
  168.         case 'a' :
  169.             angle -= 0.01f;
  170.             lx = sin(angle);
  171.             lz = -cos(angle);
  172.             break;
  173.         case 'd' :
  174.             angle += 0.01f;
  175.             lx = sin(angle);
  176.             lz = -cos(angle);
  177.             break;
  178.         case 'w' :
  179.             x += lx * fraction;
  180.             z += lz * fraction;
  181.             break;
  182.         case 's' :
  183.             x -= lx * fraction;
  184.             z -= lz * fraction;
  185.             break;
  186.  
  187.     }
  188. if (key == 27)
  189. exit(0);
  190. }
  191.  
  192. void processSpecialKeys(int key, int xx, int yy) {
  193.  
  194.     float fraction = 0.1f;
  195.  
  196.     switch (key) {
  197.         case GLUT_KEY_LEFT :
  198.             angle -= 0.01f;
  199.             lx = sin(angle);
  200.             lz = -cos(angle);
  201.             break;
  202.         case GLUT_KEY_RIGHT :
  203.             angle += 0.01f;
  204.             lx = sin(angle);
  205.             lz = -cos(angle);
  206.             break;
  207.         case GLUT_KEY_UP :
  208.             x += lx * fraction;
  209.             z += lz * fraction;
  210.             break;
  211.         case GLUT_KEY_DOWN :
  212.             x -= lx * fraction;
  213.             z -= lz * fraction;
  214.             break;
  215.     }
  216. }
  217.  
  218.  
  219. int main(int argc, char **argv) {
  220.  
  221.     // init GLUT and create window
  222.  
  223.     glutInit(&argc, argv);
  224.     glColor3f(0.3f, 0.7f, 0.9f);
  225.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  226.     glutInitWindowPosition(100,100);
  227.     glutInitWindowSize(320,320);
  228.     glutCreateWindow("Scena 3D cu oameni de zapada");
  229.  
  230.     // register callbacks
  231.     glutDisplayFunc(renderScene);
  232.     glutReshapeFunc(changeSize);
  233.     glutIdleFunc(renderScene);
  234.     glutKeyboardFunc(processNormalKeys);
  235.     glutSpecialFunc(processSpecialKeys);
  236.  
  237.     // OpenGL init
  238.     glEnable(GL_DEPTH_TEST);
  239.  
  240.     glutCreateMenu(menu);
  241.     glutAddMenuEntry("Iarna", IARNA);
  242.     glutAddMenuEntry("Primavara", PRIMAVARA);
  243.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  244.  
  245.     // enter GLUT event processing cycle
  246.     glutMainLoop();
  247.  
  248.     return 1;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment