Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. #ifdef __APPLE__
  2. #  define GL_SILENCE_DEPRECATION
  3. #  include <OpenGL/gl.h>
  4. #  include <OpenGL/glu.h>
  5. #  include <GLUT/glut.h>
  6. #else
  7. #  include <GL/gl.h>
  8. #  include <GL/glu.h>
  9. #  include <GL/freeglut.h>
  10. #endif
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. GLdouble eye[] = { 0, 0, 10 };
  15. GLdouble lookAt[] = { 0, 0, 0 };
  16. GLdouble up[] = { 0, 1, 0 };
  17.  
  18.  
  19. /**
  20.  * We have two lights so we need two sets of lighting properties.
  21.  *
  22.  * NOTE: This would be better done with a struct or class that contains the
  23.  * ambient, diffuse, specular, and position for each light. We've avoided that
  24.  * here to keep this lab simple. Consider this for your assignments/project!
  25.  */
  26. GLfloat ambient[2][4] = {
  27.     { 1, 1, 1, 1 },
  28.     { 1, 1, 1, 1 }
  29. };
  30. GLfloat diffuse[2][4] = {
  31.     { 1, 0, 0, 1 },
  32.     { 0, 0, 1, 1 }
  33. };
  34. GLfloat specular[2][4] = {
  35.     { 1, 1, 1, 1 },
  36.     { 1, 1, 1, 1 }
  37. };
  38. GLfloat lightPos[2][4] = {
  39.     { 5, -25, 5, 1 },
  40.     { -5, 5, 5, 0 },
  41. };
  42.  
  43. /**
  44.  * The 0th material in each of these arrays is the default one. This is useful
  45.  * for resetting the material after using a custom one.
  46.  *
  47.  * Find more materials here: http://devernay.free.fr/cours/opengl/materials.html
  48.  */
  49. GLfloat materialAmbient[2][4] = {
  50.     { 0.2, 0.2, 0.2, 1.0 },
  51.     { 0.0215, 0.1745, 0.0215, 1.0 }
  52. };
  53. GLfloat materialDiffuse[2][4] = {
  54.     { 0.8, 0.8, 0.8, 1.0 },
  55.     { 0.07568, 0.61424, 0.07568, 1.0 }
  56. };
  57. GLfloat materialSpecular[2][4] = {
  58.     { 0, 0, 0, 1 },
  59.     { 0.633, 0.727811, 0.633, 1.0 }
  60. };
  61. GLfloat materialShiny[2] = {
  62.     0,
  63.     0.6
  64. };
  65.  
  66. GLfloat lightPosKeyboardStep = 0.5;
  67.  
  68. void setMaterials(unsigned int index) {
  69.     /**
  70.      * Call glMaterialfv three times to set GL_AMBIENT, GL_DIFFUSE, and GL_SPECULAR.
  71.      * Call glMaterialf to set GL_SHININESS.
  72.      *
  73.      * Note: shininess is a float scalar and thus uses the f suffix, the other
  74.      * properties are vectors and thus use the fv suffix.
  75.      *
  76.      * ADD CODE HERE!
  77.      */
  78.     // The first one is done for you, uncomment below.
  79.     //glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materialAmbient[index]);
  80. }
  81.  
  82. void display(void) {
  83.     for (unsigned int i = 0; i < 2; i++) {
  84.         /**
  85.          * We've stored the lighting properties in a 2D array.
  86.          * There are two outer elements in the array, one for each light.
  87.          * We can use `GL_LIGHT0 + i` to set the `i`th light.
  88.          *
  89.          * ADD CODE HERE!
  90.          */
  91.         // The first one is done for you, uncomment below.
  92.         //glLightfv(GL_LIGHT0 + i, GL_POSITION, lightPos[i]);
  93.     }
  94.  
  95.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  96.     glLoadIdentity();
  97.     gluLookAt(
  98.         eye[0], eye[1], eye[2],
  99.         lookAt[0], lookAt[1], lookAt[2],
  100.         up[0], up[1], up[2]
  101.     );
  102.  
  103.     glColor3f(0.5, 0.5, 0.5);
  104.     glPushMatrix();
  105.         glTranslatef(2, 0, 0);
  106.         glRotatef(45, 0.25, -1, -0.25);
  107.         setMaterials(0);
  108.         // A bug in some GLUT libraries gives the wrong winding order for the
  109.         // teapot vertices. This causes the vertex normals to be incorrect!
  110.         // Without the right vertex normals our lighting will look rather
  111.         // wrong. You do not need to worry about these glFrontFace calls for
  112.         // other objects.
  113.         glFrontFace(GL_CW);
  114.         glutSolidTeapot(1);
  115.         glFrontFace(GL_CCW);
  116.     glPopMatrix();
  117.  
  118.     glPushMatrix();
  119.         glTranslatef(-1.5, -0.5, -3);
  120.         setMaterials(0);
  121.         glutWireSphere(1, 20, 20);
  122.     glPopMatrix();
  123.  
  124.     glPushMatrix();
  125.         glTranslatef(0.5, -1, -15);
  126.         setMaterials(1);
  127.         glutSolidSphere(1, 30, 30);
  128.     glPopMatrix();
  129.  
  130.     glFlush();
  131. }
  132.  
  133. void handleReshape(int w, int h) {
  134.     glViewport(0, 0, w, h);
  135.     glMatrixMode(GL_PROJECTION);
  136.     glLoadIdentity();
  137.     gluPerspective(45, 1, 1, 100);
  138.  
  139.     glMatrixMode(GL_MODELVIEW);
  140. }
  141.  
  142. void handleKeyboard(unsigned char key, int _x, int _y) {
  143.     if (key == 'q') {
  144.         exit(0);
  145.     }
  146.  
  147.     if (key == 'a') {
  148.         lightPos[1][0] -= lightPosKeyboardStep;
  149.     } else if (key == 'd') {
  150.         lightPos[1][0] += lightPosKeyboardStep;
  151.     }
  152.  
  153.     display();
  154. }
  155.  
  156. int main(int argc, char** argv) {
  157.     glutInit(&argc, argv);
  158.     glutInitWindowSize(600,600);
  159.     glutInitWindowPosition(300,300);
  160.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
  161.     glutCreateWindow("Tutorial 4");
  162.  
  163.     glutKeyboardFunc(handleKeyboard);
  164.     glutReshapeFunc(handleReshape);
  165.     glutDisplayFunc(display);
  166.  
  167.     /** Enable lighting calculations and two lights here!
  168.      *
  169.      * ADD CODE HERE
  170.      */
  171.  
  172.  
  173.     glEnable(GL_DEPTH_TEST);
  174.     glEnable(GL_CULL_FACE);
  175.     glCullFace(GL_BACK);
  176.  
  177.     glutMainLoop();
  178.  
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement