SHARE
TWEET

Untitled

PhoenixMee Apr 11th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include "GL/glew.h"
  3. #include "GL/3dgl.h"
  4. #include "GL/glut.h"
  5. #include "GL/freeglut_ext.h"
  6.  
  7. #pragma comment (lib, "glew32.lib")
  8.  
  9. using namespace std;
  10. using namespace _3dgl;
  11.  
  12. // THE matrix
  13. float matrix[16];
  14.  
  15. // window size (also used for shadowmaps)
  16. int ScreenWidth = 800;
  17. int ScreenHeight = 600;
  18.  
  19. // 3D models
  20. C3dglModel camera, lamp, LivingRoom, table, vase, utah_teapot_hires, apple, Lamp2, Lamp22, hanginglamp;
  21.  
  22. // textures
  23. GLuint TexOakWood, TexOakWoodNormals, TexChairCloth, TexShadowMap, ShadowMapFBO, TexCubeMapStatic, TexCubeMapAnimated, TexNone;
  24.  
  25. // 3D GLProgram
  26. C3dglProgram Program;
  27.  
  28. // declaring vertex and fragment shader
  29. C3dglShader VertexShader;
  30. C3dglShader FragmentShader;
  31.  
  32. // camera position (for first person type camera navigation)
  33. float matrixView[16];       // The View Matrix
  34. float angleTilt = 0;        // Tilt Angle
  35. float angleRot = 0.1f;      // Camera orbiting angle
  36. float deltaX = 0, deltaY = 0, deltaZ = 0;   // Camera movement values
  37. float cameraVector[3];
  38.  
  39. // on or offs for bulbs
  40. bool B1, B2;
  41.  
  42. // helper function: quick material setter (deprecated!)
  43. inline void glMaterial3f(GLenum face, GLenum pname, GLfloat r, GLfloat g, GLfloat b)            { GLfloat params[] = { r, g, b, 1.0f }; glMaterialfv(face, pname, params); }
  44.  
  45. //---- PYRAMID VBO ----//
  46. // buffers names
  47. unsigned vertexBuffer = 0;
  48. unsigned normalBuffer = 0;
  49. unsigned indexBuffer = 0;
  50.  
  51. float vertices[] = { -4, 0, -4, 4, 0, -4, 0, 7, 0, -4, 0, 4, 4, 0, 4, 0, 7, 0,-4, 0, -4, -4, 0, 4, 0, 7, 0, 4, 0, -4, 4, 0, 4, 0, 7, 0,-4, 0, -4, -4, 0, 4, 4, 0, -4, 4, 0, 4 };
  52. float normals[] = { 0, 4, -7, 0, 4, -7, 0, 4, -7, 0, 4, 7, 0, 4, 7, 0, 4, 7,-7, 4, 0, -7, 4, 0, -7, 4, 0, 7, 4, 0, 7, 4, 0, 7, 4, 0,0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 };
  53. unsigned indices[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 13, 14, 15 };
  54. //---------------------//
  55.  
  56. bool init()
  57. {
  58.     // rendering states
  59.     glEnable(GL_DEPTH_TEST);    // depth test is necessary for most 3D scenes
  60.     glEnable(GL_NORMALIZE);     // normalization is needed by AssImp library models
  61.     glShadeModel(GL_SMOOTH);    // smooth shading mode is the default one; try GL_FLAT here!
  62.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);  // this is the default one; try GL_LINE!
  63.  
  64.     // Initialise Shaders
  65.     if (!VertexShader.Create(GL_VERTEX_SHADER)) return false;
  66.     if (!VertexShader.LoadFromFile("shaders\\basic.vert")) return false;
  67.     if (!VertexShader.Compile()) return false;
  68.  
  69.     if (!FragmentShader.Create(GL_FRAGMENT_SHADER)) return false;
  70.     if (!FragmentShader.LoadFromFile("shaders\\basic.frag")) return false;
  71.     if (!FragmentShader.Compile()) return false;
  72.  
  73.     if (!Program.Create()) return false;
  74.     if (!Program.Attach(VertexShader)) return false;
  75.     if (!Program.Attach(FragmentShader)) return false;
  76.     if (!Program.Link()) return false;
  77.     if (!Program.Use(true)) return false;
  78.  
  79.     // glut additional setup
  80.     glutSetVertexAttribCoord3(Program.GetAttribLocation("aVertex"));
  81.     glutSetVertexAttribNormal(Program.GetAttribLocation("aNormal"));
  82.  
  83.     // loading 3D models
  84.     if (!table.load("models\\table.obj")) return false;
  85.     if (!vase.load("models\\vase.obj")) return false;
  86.     //if (!LivingRoom.load("models\\LivingRoom.obj")) return false;
  87.     if (!apple.load("models\\apple.3ds")) return false;
  88.     if (!Lamp2.load("models\\Lamp2.obj")) return false;
  89.     if (!Lamp22.load("models\\Lamp2.obj")) return false;
  90.     if (!hanginglamp.load("models\\hanginglamp.obj")) return false;
  91.     if (!LivingRoom.load("models\\LivingRoomObj\\LivingRoom.obj")) return false;
  92.     //LivingRoom.loadMaterials("models\\LivingRoomObj\\");
  93.  
  94.     //---- initializing pyramid VBO details ----//
  95.     // prepare vertex data
  96.     glGenBuffers(1, &vertexBuffer);
  97.     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  98.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  99.  
  100.     // prepare normal data
  101.     glGenBuffers(1, &normalBuffer);
  102.     glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
  103.     glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
  104.  
  105.     // prepare indices array
  106.     glGenBuffers(1, &indexBuffer);
  107.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  108.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  109.     //-------------------------------------//
  110.  
  111.     //---- loading and binding textures ----//
  112.     C3dglBitmap bm, bm2, bm3, bm4;
  113.  
  114.     bm.Load("models/oakwood.png", GL_RGBA);
  115.     if (!bm.GetBits()) return false;
  116.     bm2.Load("models/chaircloth.png", GL_RGBA);
  117.     if (!bm2.GetBits()) return false;
  118.     bm4.Load("models/oakwoodnormals.png", GL_RGBA);
  119.     if (!bm4.GetBits()) return false;
  120.  
  121.     // oakwood
  122.     glActiveTexture(GL_TEXTURE0);
  123.     glGenTextures(1, &TexOakWood);
  124.     glBindTexture(GL_TEXTURE_2D, TexOakWood);
  125.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  126.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm.GetWidth(), bm.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm.GetBits());
  127.  
  128.     // chaircloth
  129.     glActiveTexture(GL_TEXTURE0);
  130.     glGenTextures(1, &TexChairCloth);
  131.     glBindTexture(GL_TEXTURE_2D, TexChairCloth);
  132.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  133.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm2.GetWidth(), bm2.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm2.GetBits());
  134.  
  135.     // null
  136.     glActiveTexture(GL_TEXTURE0);
  137.     glGenTextures(1, &TexNone);
  138.     glBindTexture(GL_TEXTURE_2D, TexNone);
  139.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  140.     BYTE bytes[] = { 255, 255, 255 };
  141.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_BGR, GL_UNSIGNED_BYTE, &bytes);
  142.  
  143.     // vase cube map static
  144.     //glActiveTexture(GL_TEXTURE1);
  145.     //glGenTextures(1, &TexCubeMapStatic);
  146.     //glBindTexture(GL_TEXTURE_CUBE_MAP, TexCubeMapStatic);
  147.     //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  148.     //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  149.     //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  150.     //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  151.     //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  152.     //bm3.Load("models/right.png", GL_RGBA);
  153.     //glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
  154.     //bm3.Load("models/left.png", GL_RGBA);
  155.     //glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
  156.     //bm3.Load("models/down.png", GL_RGBA);
  157.     //glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
  158.     //bm3.Load("models/up.png", GL_RGBA);
  159.     //glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
  160.     //bm3.Load("models/back.png", GL_RGBA);
  161.     //glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
  162.     //bm3.Load("models/forward.png", GL_RGBA);
  163.     //glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
  164.     //if (!bm3.GetBits()) return false;
  165.  
  166.     // vase cube map animated
  167.     glActiveTexture(GL_TEXTURE1);
  168.     glGenTextures(1, &TexCubeMapAnimated);
  169.     glBindTexture(GL_TEXTURE_CUBE_MAP, TexCubeMapAnimated);
  170.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  171.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  172.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  173.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  174.     glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  175.  
  176.     // shadow map texture setup
  177.     glActiveTexture(GL_TEXTURE7);
  178.     glGenTextures(1, &TexShadowMap);
  179.     glBindTexture(GL_TEXTURE_2D, TexShadowMap);
  180.     // shadow map parameters - to get nice filtering & avoid artefact on the edges of the shadowmap
  181.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  182.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  183.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  184.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  185.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  186.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  187.     // This will associate the texture with the depth component in the Z-buffer
  188.     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, ScreenWidth * 2, ScreenHeight * 2, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
  189.     Program.SendUniform("shadowMap", 7); // tex channel 7
  190.     glActiveTexture(GL_TEXTURE0);
  191.  
  192.     // Shadow mapping
  193.     // Create a framebuffer object (FBO)
  194.     glGenFramebuffers(1, &ShadowMapFBO);
  195.     glBindFramebuffer(GL_FRAMEBUFFER_EXT, ShadowMapFBO);
  196.     // Instruct openGL that we won't bind a color texture with the currently binded FBO
  197.     glDrawBuffer(GL_NONE);
  198.     glReadBuffer(GL_NONE);
  199.     // attach the texture to FBO depth attachment point
  200.     glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,GL_TEXTURE_2D, TexShadowMap, 0);
  201.     // switch back to window-system-provided framebuffer
  202.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  203.  
  204.     // oakwood normal map
  205.     glActiveTexture(GL_TEXTURE2);
  206.     glGenTextures(1, &TexOakWoodNormals);
  207.     glBindTexture(GL_TEXTURE_2D, TexOakWoodNormals);
  208.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  209.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm4.GetWidth(), bm4.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm4.GetBits());
  210.  
  211.     // Lighting and effects declarations
  212.     Program.SendUniform("texture0", 0); // tex channel 0
  213.     Program.SendUniform("textureCubeMap", 1); // tex channel 1
  214.     Program.SendUniform("reflectionPower", 0.0); // turn off cubemap reflections, initially
  215.     Program.SendUniform("textureNormal", 2); // tex channel 2
  216.     Program.SendUniform("NormalMap", 0); // turn off normal maps, initially
  217.     Program.SendUniform("shadowMap", 7); // tex channel 7
  218.  
  219.     // ambient light
  220.     Program.SendUniform("lightAmbient.on", 1); // turn off ambient if you want other lights to light the scene
  221.     Program.SendUniform("lightAmbient.color", 0.25, 0.25, 0.25);
  222.     //Program.SendUniform("materialAmbient", 0.5, 0.5, 0.5);
  223.  
  224.     // directional light
  225.     Program.SendUniform("lightDir.on", 1);  // same as above
  226.     Program.SendUniform("lightDir.direction", 1.0, 0.5, 1.0);
  227.     Program.SendUniform("lightDir.diffuse", 0.2, 0.2, 0.2);
  228.     Program.SendUniform("materialDiffuse", 0.1, 0.1, 0.1);
  229.  
  230.     // point light (bulb 1)
  231.     //Program.SendUniform("lightPoint.on", 1); set by key input
  232.     Program.SendUniform("lightPoint.position", 7.0, 19.7, 3.4); // position needs to be identical to the model of the source
  233.     Program.SendUniform("lightPoint.diffuse", 0.5, 0.5, 0.5);
  234.     Program.SendUniform("lightPoint.specular", 1.0, 1.0, 1.0);
  235.  
  236.     // point light (bulb 2)
  237.     //Program.SendUniform("lightPoint2.on", 1); set by key input
  238.     Program.SendUniform("lightPoint2.position", -7.15, 19.7, -5.85); // position needs to be identical to the model of the source
  239.     Program.SendUniform("lightPoint2.diffuse", 0.5, 0.5, 0.5);
  240.     Program.SendUniform("lightPoint2.specular", 0.7, 0.7, 0.7);
  241.  
  242.     // use this for each model if you want shininess
  243.     // at the moment (setting it here) will make everything this shiny
  244.     Program.SendUniform("shininess", 5.0); // strength of the shininess, higher value is a smaller shine
  245.  
  246.     // enable spot light
  247.     //Program.SendUniform("lightSpot.on", 1); set by key input
  248.     Program.SendUniform("lightSpot.diffuse", 0.102f * 9, 0.051f * 9, 0.0f);
  249.     Program.SendUniform("lightSpot.specular", 0.102f * 9, 0.051f * 9, 0.0f);
  250.     Program.SendUniform("lightSpot.direction", 0.0, -1.0, 0.0);
  251.     Program.SendUniform("lightSpot.cutoff", 90.0);
  252.     Program.SendUniform("lightSpot.strength", 0.006); // keep between 0.1 (very weak) and 0.001 (very strong)
  253.     Program.SendUniform("lightSpot.attenuation", 2.0); // higher value is higher fade
  254.  
  255.     // fog
  256.     //Program.SendUniform("fogColor", 0.05f, 0.05f, 0.05f);
  257.     //Program.SendUniform("fogThickness", 0.05);
  258.  
  259.     // Initialise the View Matrix (initial position of the camera)
  260.     glMatrixMode(GL_MODELVIEW);
  261.     angleTilt = 15;
  262.     glLoadIdentity();
  263.     glRotatef(angleTilt, 1, 0, 0);
  264.     gluLookAt(25.0, 25.0, -25.0,
  265.               0.0, 25.0, 0.0,
  266.               0.0, 1.0, 0.0);
  267.     glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  268.  
  269.     cout << endl;
  270.     cout << "Use:" << endl;
  271.     cout << "  WASD or arrow key to navigate" << endl;
  272.     cout << "  QE or PgUp/Dn to move the camera up and down" << endl;
  273.     cout << "  Shift+AD or arrow key to auto-orbit" << endl;
  274.     cout << "  Drag the mouse to look around" << endl;
  275.     cout << endl;
  276.  
  277.     //glEnable(GL_CULL_FACE);
  278.  
  279.     return true;
  280. }
  281.  
  282. void done()
  283. {
  284. }
  285.  
  286. void getCamPos()
  287. {
  288.     // getting position of camera in 'world space'
  289.     GLfloat cameraMatrix[16];
  290.     glGetFloatv(GL_MODELVIEW_MATRIX, cameraMatrix);
  291.     cameraVector[0] = -(cameraMatrix[0] * cameraMatrix[12] + cameraMatrix[1] * cameraMatrix[13] + cameraMatrix[2] * cameraMatrix[14]); // x
  292.     cameraVector[1] = -(cameraMatrix[4] * cameraMatrix[12] + cameraMatrix[5] * cameraMatrix[13] + cameraMatrix[6] * cameraMatrix[14]); // y
  293.     cameraVector[2] = -(cameraMatrix[8] * cameraMatrix[12] + cameraMatrix[9] * cameraMatrix[13] + cameraMatrix[10] * cameraMatrix[14]); // z
  294.     cout << "x, y, z \n";
  295.     cout << cameraVector[0]; cout << "f,  ";
  296.     cout << cameraVector[1]; cout << "f,  ";
  297.     cout << cameraVector[2]; cout << "f,  ";
  298.     cout << "\n";
  299. }
  300.  
  301. // called before window opened or resized - to setup the Projection Matrix
  302. void reshape(int w, int h)
  303. {
  304.     // find screen aspect ratio
  305.     float ratio = w * 1.0f / h;      // we hope that h is not zero
  306.  
  307.                                      //  --- setup the projection matrix
  308.  
  309.     glMatrixMode(GL_PROJECTION);                            // projection matrix using fixed-pipeline vertex shader
  310.     glLoadIdentity();
  311.     glViewport(0, 0, w, h);
  312.     gluPerspective(60.0f, ratio, 0.02f, 1000.0f);
  313.     float matrix[16];                                       // projection matrix using "basic.vert" vertex shader
  314.     glGetFloatv(GL_PROJECTION_MATRIX, matrix);
  315.     Program.SendUniform("matrixProjection", matrix);
  316. }
  317.  
  318. void renderReflectiveObjects()
  319. {
  320.     // vase
  321.     Program.SendUniform("reflectionPower", 0.5);
  322.     glActiveTexture(GL_TEXTURE1);
  323.     glBindTexture(GL_TEXTURE_CUBE_MAP, TexCubeMapAnimated);
  324.     glPushMatrix();
  325.     glTranslatef(3.0f, 15.2f, 0.0f);
  326.     glScalef(0.4f, 0.4f, 0.4f);
  327.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  328.     Program.SendUniform("matrixModelView", matrix);
  329.     Program.SendUniform("materialAmbient", 0.1f, 0.2f, 0.3f);
  330.     Program.SendUniform("materialSpecular", 2.2, 2.2, 2.2);
  331.     vase.render();
  332.     glPopMatrix();
  333.     Program.SendUniform("reflectionPower", 0.0);
  334. }
  335.  
  336. void renderLamps()
  337. {
  338.     // lamp (with B1)
  339.     glPushMatrix();
  340.     glTranslatef(7.0f, 15.2f, 6.0f);
  341.     glRotatef(90, 0.0f, 1.0f, 0.0f);
  342.     glScalef(0.03f, 0.03f, 0.03f);
  343.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  344.     Program.SendUniform("matrixModelView", matrix);
  345.     Program.SendUniform("materialAmbient", 0.7f, 0.7f, 0.7f);
  346.     if (B1) Program.SendUniform("materialSpecular", 1.2f, 1.2f, 1.2f);
  347.     else Program.SendUniform("materialSpecular", 0.0f, 0.0f, 0.0f);
  348.     Lamp2.render();
  349.     glPopMatrix();
  350.  
  351.     // lamp (with B2)
  352.     glPushMatrix();
  353.     glTranslatef(-9.0f, 15.2f, -4.0f);
  354.     glRotatef(45, 0.0f, 1.0f, 0.0f);
  355.     glScalef(0.03f, 0.03f, 0.03f);
  356.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  357.     Program.SendUniform("matrixModelView", matrix);
  358.     Program.SendUniform("materialAmbient", 0.1f, 0.1f, 0.1f);
  359.     if (B2) Program.SendUniform("materialSpecular", 1.2f, 1.2f, 1.2f);
  360.     else Program.SendUniform("materialSpecular", 0.0f, 0.0f, 0.0f);
  361.     Lamp22.render();
  362.     glPopMatrix();
  363.  
  364.     // light bulb (B1)
  365.     glPushMatrix();
  366.     glTranslatef(7.0f, 19.7f, 3.4f);
  367.     glScalef(0.08f, 0.08f, 0.08f);
  368.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  369.     Program.SendUniform("matrixModelView", matrix);
  370.     if (B1) Program.SendUniform("materialAmbient", 50.0f, 50.0f, 50.0f);
  371.     else Program.SendUniform("materialAmbient", 0.3f, 0.3f, 0.3f);
  372.     glutSolidSphere(5, 32, 32);
  373.     glPopMatrix();
  374.  
  375.     // light bulb (B2)
  376.     glPushMatrix();
  377.     glTranslatef(-7.15f, 19.7f, -5.85f);
  378.     glScalef(0.08f, 0.08f, 0.08f);
  379.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  380.     Program.SendUniform("matrixModelView", matrix);
  381.     if (B2) Program.SendUniform("materialAmbient", 50.0f, 50.0f, 50.0f);
  382.     else Program.SendUniform("materialAmbient", 0.3f, 0.3f, 0.3f);
  383.     glutSolidSphere(5, 32, 32);
  384.     glPopMatrix();
  385. }
  386.  
  387. void renderObjects(float theta, float theta2, float theta3)
  388. {
  389.     //---- RENDERING PYRAMID VBO ----//
  390.     // Get / Setting Attribute Locations
  391.     glPushMatrix();
  392.     glTranslatef(8.0f, 16.6f, -3.0f);
  393.     glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
  394.     glRotatef(theta2, 0.0f, 1.0f, 0.0f);
  395.     glScalef(0.2f, 0.2f, 0.2f);
  396.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  397.     glBindTexture(GL_TEXTURE_2D, TexNone);
  398.     Program.SendUniform("matrixModelView", matrix);
  399.     Program.SendUniform("materialAmbient", 0.0f, 0.338f, 0.1f);
  400.     Program.SendUniform("materialSpecular", 0.4f, 0.4f, 0.4f);
  401.  
  402.     GLuint attribVertex = Program.GetAttribLocation("aVertex");
  403.     GLuint attribNormal = Program.GetAttribLocation("aNormal");
  404.  
  405.     // Enable vertex attribute arrays
  406.     glEnableVertexAttribArray(attribVertex);
  407.     glEnableVertexAttribArray(attribNormal);
  408.  
  409.     // Bind (activate) the vertex buffer and set the pointer to it
  410.     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  411.     glVertexAttribPointer(attribVertex, 3, GL_FLOAT, GL_FALSE, 0, 0);
  412.  
  413.     // Bind (activate) the normal buffer and set the pointer to it
  414.     glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
  415.     glVertexAttribPointer(attribNormal, 3, GL_FLOAT, GL_FALSE, 0, 0);
  416.  
  417.     // Draw triangles–using index buffer
  418.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  419.     glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, 0);
  420.  
  421.     // Disable arrays
  422.     glDisableVertexAttribArray(attribVertex);
  423.     glDisableVertexAttribArray(attribNormal);
  424.     glPopMatrix();
  425.     //-------------------------------//
  426.  
  427.     // table
  428.     glActiveTexture(GL_TEXTURE0);
  429.     glBindTexture(GL_TEXTURE_2D, TexOakWood);
  430.     glActiveTexture(GL_TEXTURE2);
  431.     glBindTexture(GL_TEXTURE_2D, TexOakWoodNormals);
  432.     Program.SendUniform("NormalMap", 1);
  433.     glPushMatrix();
  434.     glTranslatef(0.0f, 0, 0.0f);
  435.     glRotatef(180, 0.0f, 1.0f, 0.0f);
  436.     glScalef(0.02f, 0.02f, 0.02f);
  437.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  438.     Program.SendUniform("matrixModelView", matrix);
  439.     Program.SendUniform("materialAmbient", 0.278f, 0.138f, 0.038f);
  440.     Program.SendUniform("materialSpecular", 1.8, 1.8, 1.8);
  441.     table.render(1);
  442.     glPopMatrix();
  443.     Program.SendUniform("NormalMap", 0);
  444.     glActiveTexture(GL_TEXTURE2);
  445.     glBindTexture(GL_TEXTURE_2D, TexNone);
  446.  
  447.     // chair
  448.     glActiveTexture(GL_TEXTURE0);
  449.     glBindTexture(GL_TEXTURE_2D, TexChairCloth);
  450.     glPushMatrix();
  451.     glTranslatef(0.0f, 0, 0.0f);
  452.     glRotatef(180, 0.0f, 1.0f, 0.0f);
  453.     glScalef(0.02f, 0.02f, 0.02f);
  454.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  455.     Program.SendUniform("matrixModelView", matrix);
  456.     Program.SendUniform("materialAmbient", 0.222f * 3, 0.184f * 3, 0.135f * 3);
  457.     Program.SendUniform("materialSpecular", 0.2, 0.2, 0.2);
  458.     table.render(0);
  459.     glPopMatrix();
  460.  
  461.     // chair
  462.     glPushMatrix();
  463.     glTranslatef(0.0f, 0, 0.0f);
  464.     glRotatef(0, 0.0f, 1.0f, 0.0f);
  465.     glScalef(0.02f, 0.02f, 0.02f);
  466.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  467.     Program.SendUniform("matrixModelView", matrix);
  468.     Program.SendUniform("materialAmbient", 0.222f * 3, 0.184f * 3, 0.135f * 3);
  469.     Program.SendUniform("materialSpecular", 0.2, 0.2, 0.2);
  470.     table.render(0);
  471.     glPopMatrix();
  472.  
  473.     // chair
  474.     glPushMatrix();
  475.     glTranslatef(4.0f, 0, 0.0f);
  476.     glRotatef(90, 0.0f, 1.0f, 0.0f);
  477.     glScalef(0.02f, 0.02f, 0.02f);
  478.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  479.     Program.SendUniform("matrixModelView", matrix);
  480.     Program.SendUniform("materialAmbient", 0.222f * 3, 0.184f * 3, 0.135f * 3);
  481.     Program.SendUniform("materialSpecular", 0.2, 0.2, 0.2);
  482.     table.render(0);
  483.     glPopMatrix();
  484.  
  485.     // chair
  486.     glPushMatrix();
  487.     glTranslatef(-4.0f, 0, 0.0f);
  488.     glRotatef(-90, 0.0f, 1.0f, 0.0f);
  489.     glScalef(0.02f, 0.02f, 0.02f);
  490.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  491.     Program.SendUniform("matrixModelView", matrix);
  492.     Program.SendUniform("materialAmbient", 0.222f * 3, 0.184f * 3, 0.135f * 3);
  493.     Program.SendUniform("materialSpecular", 0.2, 0.2, 0.2);
  494.     table.render(0);
  495.     glPopMatrix();
  496.     glActiveTexture(GL_TEXTURE0);
  497.     glBindTexture(GL_TEXTURE_2D, TexNone);
  498.  
  499.     // teapot
  500.     glPushMatrix();
  501.     glTranslatef(-3.0f, 16.7f, 0.0f);
  502.     glRotatef(120, 0.0f, 1.0f, 0.0f);
  503.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  504.     Program.SendUniform("matrixModelView", matrix);
  505.     Program.SendUniform("materialAmbient", 0.288f, 0.143f, 0.143f);
  506.     Program.SendUniform("materialSpecular", 0.4f, 0.4f, 0.4f);
  507.     glutSolidTeapot(2.0);
  508.     glPopMatrix();
  509.  
  510.     // --- bouncing apple --- //
  511.     glPushMatrix();
  512.     if (theta3 <= 1) glTranslatef(7.0f, 16.5f + theta3, -3.0f);
  513.     if (theta3 >= 1) glTranslatef(7.0f, 18.5f - theta3, -3.0f);
  514.     glRotatef(90, 1.0f, 0.0f, 0.0f);
  515.     glScalef(0.02f, 0.02f, 0.02f);
  516.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  517.     Program.SendUniform("matrixModelView", matrix);
  518.     Program.SendUniform("materialAmbient", 0.6f, 0.6f, 0.0f);
  519.     Program.SendUniform("materialSpecular", 0.4f, 0.4f, 0.4f);
  520.     apple.render();
  521.     glPopMatrix();
  522.     // ------------------------//
  523.  
  524.     // living room
  525.     glPushMatrix();
  526.     glTranslatef(-16.0f, 0.0f, -10.0f);
  527.     glRotatef(90, 0.0f, 1.0f, 0.0f);
  528.     glScalef(0.13f, 0.13f, 0.13f);
  529.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  530.     Program.SendUniform("matrixModelView", matrix);
  531.     Program.SendUniform("materialAmbient", 0.4f, 0.4f, 0.4f);
  532.     Program.SendUniform("materialSpecular", 0.2f, 0.2f, 0.2f);
  533.     LivingRoom.render();
  534.     glPopMatrix();
  535.  
  536.     //---- hanging lamp ----//
  537.     // Pendulum mechanics
  538.     static float alpha4 = 0.0;
  539.     static float delta4 = 0.04;
  540.     delta4 -= alpha4 / 102400;
  541.     alpha4 += delta4;
  542.     //
  543.  
  544.     glPushMatrix(); //1
  545.     glTranslatef(-1.0, 50.0, 1.0);
  546.     glRotatef(alpha4, 0.5, 0, 1);
  547.     glTranslatef(-1.0, -25.0, 1.0);
  548.     glPushMatrix(); //2
  549.     glRotatef(-90, 1.0f, 0.0f, 0.0f);
  550.     glScalef(0.015f, 0.015f, 0.015f);
  551.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  552.     glBindTexture(GL_TEXTURE_2D, TexNone);
  553.     Program.SendUniform("matrixModelView", matrix);
  554.     Program.SendUniform("materialAmbient", 0.0f, 0.0f, 0.0f);
  555.     hanginglamp.render();
  556.     glPopMatrix(); //1
  557.     // bulb placement within hanging lamp
  558.     glTranslatef(2.95, 0.6, -2.95);
  559.     glScalef(0.14, 0.14, 0.14);
  560.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  561.     glBindTexture(GL_TEXTURE_2D, TexNone);
  562.     Program.SendUniform("matrixModelView", matrix);
  563.     Program.SendUniform("lightSpot.matrix", matrix);
  564.     Program.SendUniform("materialAmbient", 0.102f * 9, 0.051f * 9, 0.0f);
  565.     glutSolidSphere(5, 32, 32);
  566.     glPopMatrix(); //2
  567.     //-----------------------//
  568. }
  569.  
  570. void prepareCubeMapAnimated(float x, float y, float z, float theta, float theta2, float theta3)
  571. {
  572.     // Store the current viewport in a safe place
  573.     GLint viewport[4];
  574.     glGetIntegerv(GL_VIEWPORT, viewport);
  575.     int w = viewport[2];
  576.     int h = viewport[3];
  577.  
  578.     // setup the viewport to 256x256, 90 degrees FoV (Field of View)
  579.     glViewport(0, 0, 256, 256);
  580.     glMatrixMode(GL_PROJECTION);
  581.     glLoadIdentity();
  582.     gluPerspective(90, 1, 0.02f, 1000.0f);
  583.  
  584.     // send the projection matrix to the shader
  585.     glGetFloatv(GL_PROJECTION_MATRIX, matrix);
  586.     Program.SendUniform("matrixProjection", matrix);
  587.  
  588.     // render environment 6 times
  589.     glMatrixMode(GL_MODELVIEW);
  590.     Program.SendUniform("reflectionPower", 0.0);
  591.     for (int i = 0; i < 6; ++i)
  592.     {
  593.         // clear background
  594.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  595.  
  596.         // setup the camera
  597.         const GLfloat ROTATION[6][6] =
  598.         {    // at              up
  599.             { 1.0, 0.0, 0.0,   0.0, -1.0, 0.0 },  // pos x
  600.             { -1.0, 0.0, 0.0,  0.0, -1.0, 0.0 },  // neg x
  601.             { 0.0, 1.0, 0.0,   0.0, 0.0, 1.0 },   // pos y
  602.             { 0.0, -1.0, 0.0,  0.0, 0.0, -1.0 },  // neg y
  603.             { 0.0, 0.0, 1.0,   0.0, -1.0, 0.0 },  // poz z
  604.             { 0.0, 0.0, -1.0,  0.0, -1.0, 0.0 }   // neg z
  605.         };
  606.         glLoadIdentity();
  607.         gluLookAt(x, y, z,
  608.             x + ROTATION[i][0], y + ROTATION[i][1], z + ROTATION[i][2],
  609.             ROTATION[i][3], ROTATION[i][4], ROTATION[i][5]);
  610.  
  611.         // send the View Matrix
  612.         glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  613.         Program.SendUniform("matrixView", matrix);
  614.  
  615.         // render scene objects -all but the reflective one
  616.         glActiveTexture(GL_TEXTURE0);
  617.         renderObjects(theta, theta2, theta3);
  618.         renderLamps();
  619.  
  620.         // send the image to the cube texture
  621.         glActiveTexture(GL_TEXTURE1);
  622.         glBindTexture(GL_TEXTURE_CUBE_MAP, TexCubeMapAnimated);
  623.         glCopyTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB8, 0, 0, 256, 256, 0);
  624.     }
  625.  
  626.     // restore the viewport and projection
  627.     reshape(w, h);
  628. }
  629.  
  630. void prepareShadowMatrix()
  631. {
  632.     // This is matrix transform every coordinate x,y,z
  633.     // x = x* 0.5 + 0.5
  634.     // y = y* 0.5 + 0.5
  635.     // z = z* 0.5 + 0.5
  636.     // Moving from unit cube [-1,1] to [0,1]
  637.     const float bias[16] = {
  638.         0.5, 0.0, 0.0, 0.0,
  639.         0.0, 0.5, 0.0, 0.0,
  640.         0.0, 0.0, 0.5, 0.0,
  641.         0.5, 0.5, 0.5, 1.0 };
  642.  
  643.     // Grab modelview and transformation matrices
  644.     static float modelView[16], projection[16];
  645.     glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
  646.     glGetFloatv(GL_PROJECTION_MATRIX, projection);
  647.  
  648.     // concatenating all matrices into one
  649.     glPushMatrix();
  650.     glLoadIdentity();
  651.     glLoadMatrixf(bias);
  652.     glMultMatrixf(projection);
  653.     glMultMatrixf(modelView);
  654.  
  655.     // Store and send as uniform variable
  656.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  657.     Program.SendUniform("matrixShadow", matrix);
  658.     glPopMatrix();
  659. }
  660.  
  661. void prepareShadowMap(float x, float y, float z, float centerx, float centery, float centerz, float upx, float upy, float upz, float theta, float theta2, float theta3)
  662. {
  663.     glEnable(GL_CULL_FACE);
  664.     glCullFace(GL_FRONT);
  665.  
  666.     // Store the current viewport in a safe place
  667.     GLint viewport[4];
  668.     glGetIntegerv(GL_VIEWPORT, viewport);
  669.     int w = viewport[2];
  670.     int h = viewport[3];
  671.    
  672.     // setup the viewport to 2x2 the originalandwide (120 degrees)FoV (Field of View)
  673.     glViewport(0, 0, w * 2, h * 2);
  674.     glMatrixMode(GL_PROJECTION);
  675.     glLoadIdentity();
  676.     gluPerspective(120, (float)w / (float)h, 0.1, 80);
  677.    
  678.     // send the projection matrix to the shader
  679.     glGetFloatv(GL_PROJECTION_MATRIX, matrix);
  680.     Program.SendUniform("matrixProjection", matrix);
  681.    
  682.     // prepare the camera
  683.     glMatrixMode(GL_MODELVIEW);
  684.     glLoadIdentity();
  685.     gluLookAt(x, y, z, centerx, centery, centerz, upx, upy, upz);
  686.    
  687.     // send the View Matrix
  688.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  689.     Program.SendUniform("matrixView", matrix);
  690.    
  691.     // Bind the Framebuffer
  692.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, ShadowMapFBO);
  693.    
  694.     // OFF-SCREEN RENDERING FROM NOW!
  695.     // Clear previous frame values -depth buffer only!
  696.     glClear(GL_DEPTH_BUFFER_BIT);
  697.    
  698.     // Disable color rendering, we only want to write to the Z-Buffer (this is to speed-up)
  699.     glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  700.    
  701.     // Prepare and send the Shadow Matrix
  702.     prepareShadowMatrix();
  703.  
  704.     // Render all objects in the scene (except the lamps)
  705.     renderObjects(theta, theta2, theta3);
  706.     renderReflectiveObjects();
  707.    
  708.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  709.     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  710.     glDisable(GL_CULL_FACE);
  711.     reshape(w, h);
  712. }
  713.  
  714. void render()
  715. {
  716.     // control model animations
  717.     static float theta = 0.0f;
  718.     static float theta2 = 0.0f;
  719.     static float theta3 = 0.0f;
  720.  
  721.     if (theta3 > 2) theta3 = 0;
  722.  
  723.     static GLint prev_time = 0;
  724.     int time = glutGet(GLUT_ELAPSED_TIME);
  725.     theta += (time - prev_time) * 0.01f;
  726.     prev_time = time;
  727.     glutPostRedisplay();
  728.  
  729.     static GLint prev_time2 = 0;
  730.     int time2 = glutGet(GLUT_ELAPSED_TIME);
  731.     theta2 += (time2 - prev_time2) * 0.15f;
  732.     prev_time2 = time2;
  733.  
  734.     static GLint prev_time3 = 0;
  735.     int time3 = glutGet(GLUT_ELAPSED_TIME);
  736.     theta3 += (time3 - prev_time3) * 0.004f;
  737.     prev_time3 = time3;
  738.  
  739.     // creating shadow map from a position
  740.     if (B1) prepareShadowMap(7.0f, 19.7f, 3.4f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, theta, theta2, theta3);
  741.     if (B2) prepareShadowMap(-7.15f, 19.7f, -5.85f, -7.15f, 0.0f, -5.85f, 1.0f, 0.0f, 0.0f, theta, theta2, theta3);
  742.  
  743.     // prepare animated cube map
  744.     // NEEDS to be called before anything else (not shadowmapping) in the render() -
  745.     // renders the scene 6 times to capture the image of reflection
  746.     prepareCubeMapAnimated(3.0f, 20.2f, 0.0f, theta, theta2, theta3);
  747.  
  748.     // clear screen and buffers
  749.     glClearColor(0.18f, 0.25f, 0.22f, 1.0f);   // deep grey background
  750.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  751.  
  752.     // setup the View Matrix (camera)
  753.     glMatrixMode(GL_MODELVIEW);
  754.     glLoadIdentity();
  755.     glRotatef(angleTilt, 1, 0, 0);                  // switch tilt off
  756.     glTranslatef(deltaX, deltaY, deltaZ);           // animate camera motion (controlled by WASD keys)
  757.     glRotatef(-angleTilt, 1, 0, 0);                 // switch tilt on
  758.     glMultMatrixf(matrixView);
  759.     glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  760.  
  761.     // setup View Matrix
  762.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  763.     Program.SendUniform("matrixView", matrix);
  764.  
  765.     // MAIN RENDER OBJECT FUNCTIONS
  766.     renderReflectiveObjects();
  767.     renderObjects(theta, theta2, theta3);
  768.     renderLamps();
  769.     // turn on shadow maps for certain bulbs
  770.     if (B1)
  771.     {
  772.         Program.SendUniform("lightPoint.on", 1);
  773.         Program.SendUniform("B1", 1);
  774.     }
  775.     if (!B1)
  776.     {
  777.         Program.SendUniform("lightPoint.on", 0);
  778.         Program.SendUniform("B1", 0);
  779.     }
  780.     if (B2)
  781.     {
  782.         Program.SendUniform("lightPoint2.on", 1);
  783.         Program.SendUniform("B2", 1);
  784.     }
  785.     if (!B2)
  786.     {
  787.         Program.SendUniform("lightPoint2.on", 0);
  788.         Program.SendUniform("B2", 0);
  789.     }
  790.  
  791.     // essential for double-buffering technique
  792.     glutSwapBuffers();
  793. }
  794.  
  795. // Handle WASDQE keys
  796. void onKeyDown(unsigned char key, int x, int y)
  797. {
  798.     switch (tolower(key))
  799.     {
  800.     case 'w': deltaZ = max(deltaZ * 1.05f, 0.01f); break;
  801.     case 's': deltaZ = min(deltaZ * 1.05f, -0.01f); break;
  802.     case 'a': deltaX = max(deltaX * 1.05f, 0.01f); angleRot = 0.1f; break;
  803.     case 'd': deltaX = min(deltaX * 1.05f, -0.01f); angleRot = -0.1f; break;
  804.     case 'e': deltaY = max(deltaY * 1.05f, 0.01f); break;
  805.     case 'q': deltaY = min(deltaY * 1.05f, -0.01f); break;
  806.     case '1': B1 = !B1; break;
  807.     case '2': B2 = !B2; break;
  808.     case '3': break;
  809.     case '4': break;
  810.     case '5': Program.SendUniform("lightSpot.on", 1); break;
  811.     case '6': Program.SendUniform("lightSpot.on", 0); break;
  812.     }
  813.     // speed limit
  814.     deltaX = max(-0.15f, min(0.15f, deltaX));
  815.     deltaY = max(-0.15f, min(0.15f, deltaY));
  816.     deltaZ = max(-0.15f, min(0.15f, deltaZ));
  817.     // stop orbiting
  818.     if ((glutGetModifiers() & GLUT_ACTIVE_SHIFT) == 0) angleRot = 0;
  819. }
  820.  
  821. // Handle WASDQE keys (key up)
  822. void onKeyUp(unsigned char key, int x, int y)
  823. {
  824.     switch (tolower(key))
  825.     {
  826.     case 'w': getCamPos(); break;
  827.     case 's': deltaZ = 0; break;
  828.     case 'a':
  829.     case 'd': deltaX = 0; break;
  830.     case 'q':
  831.     case 'e': deltaY = 0; break;
  832.     case ' ': deltaY = 0; break;
  833.     }
  834. }
  835.  
  836. // Handle arrow keys and Alt+F4
  837. void onSpecDown(int key, int x, int y)
  838. {
  839.     switch (key)
  840.     {
  841.     case GLUT_KEY_F4:       if ((glutGetModifiers() & GLUT_ACTIVE_ALT) != 0) exit(0); break;
  842.     case GLUT_KEY_UP:       onKeyDown('w', x, y); break;
  843.     case GLUT_KEY_DOWN:     onKeyDown('s', x, y); break;
  844.     case GLUT_KEY_LEFT:     onKeyDown('a', x, y); break;
  845.     case GLUT_KEY_RIGHT:    onKeyDown('d', x, y); break;
  846.     case GLUT_KEY_PAGE_UP:  onKeyDown('q', x, y); break;
  847.     case GLUT_KEY_PAGE_DOWN:onKeyDown('e', x, y); break;
  848.     case GLUT_KEY_F11:      glutFullScreenToggle();
  849.     }
  850. }
  851.  
  852. // Handle arrow keys (key up)
  853. void onSpecUp(int key, int x, int y)
  854. {
  855.     switch (key)
  856.     {
  857.     case GLUT_KEY_UP:       onKeyUp('w', x, y); break;
  858.     case GLUT_KEY_DOWN:     onKeyUp('s', x, y); break;
  859.     case GLUT_KEY_LEFT:     onKeyUp('a', x, y); break;
  860.     case GLUT_KEY_RIGHT:    onKeyUp('d', x, y); break;
  861.     case GLUT_KEY_PAGE_UP:  onKeyUp('q', x, y); break;
  862.     case GLUT_KEY_PAGE_DOWN:onKeyUp('e', x, y); break;
  863.     }
  864. }
  865.  
  866. // Handle mouse click
  867. void onMouse(int button, int state, int x, int y)
  868. {
  869.     int cx = glutGet(GLUT_WINDOW_WIDTH) / 2;
  870.     int cy = glutGet(GLUT_WINDOW_HEIGHT) / 2;
  871.  
  872.     if (state == GLUT_DOWN)
  873.     {
  874.         glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  875.         glutWarpPointer(cx, cy);
  876.     }
  877.     else
  878.         glutSetCursor(GLUT_CURSOR_INHERIT);
  879. }
  880.  
  881. // handle mouse move
  882. void onMotion(int x, int y)
  883. {
  884.     int cx = glutGet(GLUT_WINDOW_WIDTH) / 2;
  885.     int cy = glutGet(GLUT_WINDOW_HEIGHT) / 2;
  886.     if (x == cx && y == cy)
  887.         return; // caused by glutWarpPointer
  888.  
  889.     float amp = 0.25;
  890.     float deltaTilt = amp * (y - cy);
  891.     float deltaPan  = amp * (x - cx);
  892.  
  893.     glutWarpPointer(cx, cy);
  894.  
  895.     // handle camera tilt (mouse move up & down)
  896.     glMatrixMode(GL_MODELVIEW);
  897.     glLoadIdentity();
  898.     glRotatef(deltaTilt, 1, 0, 0);
  899.     glMultMatrixf(matrixView);
  900.     glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  901.  
  902.     angleTilt += deltaTilt;
  903.  
  904.     // handle camera pan (mouse move left & right)
  905.     glMatrixMode(GL_MODELVIEW);
  906.     glLoadIdentity();
  907.     glRotatef(angleTilt, 1, 0, 0);
  908.     glRotatef(deltaPan, 0, 1, 0);
  909.     glRotatef(-angleTilt, 1, 0, 0);
  910.     glMultMatrixf(matrixView);
  911.     glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
  912. }
  913.  
  914. int main(int argc, char **argv)
  915. {
  916.     // init GLUT and create Window
  917.     glutInit(&argc, argv);
  918.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  919.     glutInitWindowPosition(100, 100);
  920.     glutInitWindowSize(ScreenWidth, ScreenHeight);
  921.     glutCreateWindow("CI5520 3D Graphics Programming");
  922.  
  923.     // init glew
  924.     GLenum err = glewInit();
  925.     if (GLEW_OK != err)
  926.     {
  927.         cerr << "GLEW Error: " << glewGetErrorString(err) << endl;
  928.         return 0;
  929.     }
  930.     cout << "Using GLEW " << glewGetString(GLEW_VERSION) << endl;
  931.  
  932.     // register callbacks
  933.     glutDisplayFunc(render);
  934.     glutReshapeFunc(reshape);
  935.     glutKeyboardFunc(onKeyDown);
  936.     glutSpecialFunc(onSpecDown);
  937.     glutKeyboardUpFunc(onKeyUp);
  938.     glutSpecialUpFunc(onSpecUp);
  939.     glutMouseFunc(onMouse);
  940.     glutMotionFunc(onMotion);
  941.  
  942.     cout << "Vendor: " << glGetString(GL_VENDOR) << endl;
  943.     cout << "Renderer: " << glGetString(GL_RENDERER) << endl;
  944.     cout << "Version: " << glGetString(GL_VERSION) << endl;
  945.  
  946.     // init light and everything – not a GLUT or callback function!
  947.     if (!init())
  948.     {
  949.         cerr << "Application failed to initialise" << endl;
  950.         return 0;
  951.     }
  952.  
  953.     // enter GLUT event processing cycle
  954.     glutMainLoop();
  955.  
  956.     done();
  957.  
  958.     return 1;
  959. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top