Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "GL/glew.h"
- #include "GL/3dgl.h"
- #include "GL/glut.h"
- #include "GL/freeglut_ext.h"
- #pragma comment (lib, "glew32.lib")
- using namespace std;
- using namespace _3dgl;
- // THE matrix
- float matrix[16];
- // window size (also used for shadowmaps)
- int ScreenWidth = 800;
- int ScreenHeight = 600;
- // 3D models
- C3dglModel camera, lamp, LivingRoom, table, vase, utah_teapot_hires, apple, Lamp2, Lamp22, hanginglamp;
- // textures
- GLuint TexOakWood, TexOakWoodNormals, TexChairCloth, TexShadowMap, ShadowMapFBO, TexCubeMapStatic, TexCubeMapAnimated, TexNone;
- // 3D GLProgram
- C3dglProgram Program;
- // declaring vertex and fragment shader
- C3dglShader VertexShader;
- C3dglShader FragmentShader;
- // camera position (for first person type camera navigation)
- float matrixView[16]; // The View Matrix
- float angleTilt = 0; // Tilt Angle
- float angleRot = 0.1f; // Camera orbiting angle
- float deltaX = 0, deltaY = 0, deltaZ = 0; // Camera movement values
- float cameraVector[3];
- // on or offs for bulbs
- bool B1, B2;
- // helper function: quick material setter (deprecated!)
- inline void glMaterial3f(GLenum face, GLenum pname, GLfloat r, GLfloat g, GLfloat b) { GLfloat params[] = { r, g, b, 1.0f }; glMaterialfv(face, pname, params); }
- //---- PYRAMID VBO ----//
- // buffers names
- unsigned vertexBuffer = 0;
- unsigned normalBuffer = 0;
- unsigned indexBuffer = 0;
- 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 };
- 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 };
- unsigned indices[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 13, 14, 15 };
- //---------------------//
- bool init()
- {
- // rendering states
- glEnable(GL_DEPTH_TEST); // depth test is necessary for most 3D scenes
- glEnable(GL_NORMALIZE); // normalization is needed by AssImp library models
- glShadeModel(GL_SMOOTH); // smooth shading mode is the default one; try GL_FLAT here!
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // this is the default one; try GL_LINE!
- // Initialise Shaders
- if (!VertexShader.Create(GL_VERTEX_SHADER)) return false;
- if (!VertexShader.LoadFromFile("shaders\\basic.vert")) return false;
- if (!VertexShader.Compile()) return false;
- if (!FragmentShader.Create(GL_FRAGMENT_SHADER)) return false;
- if (!FragmentShader.LoadFromFile("shaders\\basic.frag")) return false;
- if (!FragmentShader.Compile()) return false;
- if (!Program.Create()) return false;
- if (!Program.Attach(VertexShader)) return false;
- if (!Program.Attach(FragmentShader)) return false;
- if (!Program.Link()) return false;
- if (!Program.Use(true)) return false;
- // glut additional setup
- glutSetVertexAttribCoord3(Program.GetAttribLocation("aVertex"));
- glutSetVertexAttribNormal(Program.GetAttribLocation("aNormal"));
- // loading 3D models
- if (!table.load("models\\table.obj")) return false;
- if (!vase.load("models\\vase.obj")) return false;
- //if (!LivingRoom.load("models\\LivingRoom.obj")) return false;
- if (!apple.load("models\\apple.3ds")) return false;
- if (!Lamp2.load("models\\Lamp2.obj")) return false;
- if (!Lamp22.load("models\\Lamp2.obj")) return false;
- if (!hanginglamp.load("models\\hanginglamp.obj")) return false;
- if (!LivingRoom.load("models\\LivingRoomObj\\LivingRoom.obj")) return false;
- //LivingRoom.loadMaterials("models\\LivingRoomObj\\");
- //---- initializing pyramid VBO details ----//
- // prepare vertex data
- glGenBuffers(1, &vertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- // prepare normal data
- glGenBuffers(1, &normalBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
- // prepare indices array
- glGenBuffers(1, &indexBuffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- //-------------------------------------//
- //---- loading and binding textures ----//
- C3dglBitmap bm, bm2, bm3, bm4;
- bm.Load("models/oakwood.png", GL_RGBA);
- if (!bm.GetBits()) return false;
- bm2.Load("models/chaircloth.png", GL_RGBA);
- if (!bm2.GetBits()) return false;
- bm4.Load("models/oakwoodnormals.png", GL_RGBA);
- if (!bm4.GetBits()) return false;
- // oakwood
- glActiveTexture(GL_TEXTURE0);
- glGenTextures(1, &TexOakWood);
- glBindTexture(GL_TEXTURE_2D, TexOakWood);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm.GetWidth(), bm.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm.GetBits());
- // chaircloth
- glActiveTexture(GL_TEXTURE0);
- glGenTextures(1, &TexChairCloth);
- glBindTexture(GL_TEXTURE_2D, TexChairCloth);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm2.GetWidth(), bm2.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm2.GetBits());
- // null
- glActiveTexture(GL_TEXTURE0);
- glGenTextures(1, &TexNone);
- glBindTexture(GL_TEXTURE_2D, TexNone);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- BYTE bytes[] = { 255, 255, 255 };
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_BGR, GL_UNSIGNED_BYTE, &bytes);
- // vase cube map static
- //glActiveTexture(GL_TEXTURE1);
- //glGenTextures(1, &TexCubeMapStatic);
- //glBindTexture(GL_TEXTURE_CUBE_MAP, TexCubeMapStatic);
- //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
- //bm3.Load("models/right.png", GL_RGBA);
- //glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
- //bm3.Load("models/left.png", GL_RGBA);
- //glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
- //bm3.Load("models/down.png", GL_RGBA);
- //glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
- //bm3.Load("models/up.png", GL_RGBA);
- //glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
- //bm3.Load("models/back.png", GL_RGBA);
- //glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
- //bm3.Load("models/forward.png", GL_RGBA);
- //glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, bm3.GetWidth(), abs(bm3.GetHeight()), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm3.GetBits());
- //if (!bm3.GetBits()) return false;
- // vase cube map animated
- glActiveTexture(GL_TEXTURE1);
- glGenTextures(1, &TexCubeMapAnimated);
- glBindTexture(GL_TEXTURE_CUBE_MAP, TexCubeMapAnimated);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
- // shadow map texture setup
- glActiveTexture(GL_TEXTURE7);
- glGenTextures(1, &TexShadowMap);
- glBindTexture(GL_TEXTURE_2D, TexShadowMap);
- // shadow map parameters - to get nice filtering & avoid artefact on the edges of the shadowmap
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
- // This will associate the texture with the depth component in the Z-buffer
- glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, ScreenWidth * 2, ScreenHeight * 2, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
- Program.SendUniform("shadowMap", 7); // tex channel 7
- glActiveTexture(GL_TEXTURE0);
- // Shadow mapping
- // Create a framebuffer object (FBO)
- glGenFramebuffers(1, &ShadowMapFBO);
- glBindFramebuffer(GL_FRAMEBUFFER_EXT, ShadowMapFBO);
- // Instruct openGL that we won't bind a color texture with the currently binded FBO
- glDrawBuffer(GL_NONE);
- glReadBuffer(GL_NONE);
- // attach the texture to FBO depth attachment point
- glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,GL_TEXTURE_2D, TexShadowMap, 0);
- // switch back to window-system-provided framebuffer
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- // oakwood normal map
- glActiveTexture(GL_TEXTURE2);
- glGenTextures(1, &TexOakWoodNormals);
- glBindTexture(GL_TEXTURE_2D, TexOakWoodNormals);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm4.GetWidth(), bm4.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bm4.GetBits());
- // Lighting and effects declarations
- Program.SendUniform("texture0", 0); // tex channel 0
- Program.SendUniform("textureCubeMap", 1); // tex channel 1
- Program.SendUniform("reflectionPower", 0.0); // turn off cubemap reflections, initially
- Program.SendUniform("textureNormal", 2); // tex channel 2
- Program.SendUniform("NormalMap", 0); // turn off normal maps, initially
- Program.SendUniform("shadowMap", 7); // tex channel 7
- // ambient light
- Program.SendUniform("lightAmbient.on", 1); // turn off ambient if you want other lights to light the scene
- Program.SendUniform("lightAmbient.color", 0.25, 0.25, 0.25);
- //Program.SendUniform("materialAmbient", 0.5, 0.5, 0.5);
- // directional light
- Program.SendUniform("lightDir.on", 1); // same as above
- Program.SendUniform("lightDir.direction", 1.0, 0.5, 1.0);
- Program.SendUniform("lightDir.diffuse", 0.2, 0.2, 0.2);
- Program.SendUniform("materialDiffuse", 0.1, 0.1, 0.1);
- // point light (bulb 1)
- //Program.SendUniform("lightPoint.on", 1); set by key input
- Program.SendUniform("lightPoint.position", 7.0, 19.7, 3.4); // position needs to be identical to the model of the source
- Program.SendUniform("lightPoint.diffuse", 0.5, 0.5, 0.5);
- Program.SendUniform("lightPoint.specular", 1.0, 1.0, 1.0);
- // point light (bulb 2)
- //Program.SendUniform("lightPoint2.on", 1); set by key input
- Program.SendUniform("lightPoint2.position", -7.15, 19.7, -5.85); // position needs to be identical to the model of the source
- Program.SendUniform("lightPoint2.diffuse", 0.5, 0.5, 0.5);
- Program.SendUniform("lightPoint2.specular", 0.7, 0.7, 0.7);
- // use this for each model if you want shininess
- // at the moment (setting it here) will make everything this shiny
- Program.SendUniform("shininess", 5.0); // strength of the shininess, higher value is a smaller shine
- // enable spot light
- //Program.SendUniform("lightSpot.on", 1); set by key input
- Program.SendUniform("lightSpot.diffuse", 0.102f * 9, 0.051f * 9, 0.0f);
- Program.SendUniform("lightSpot.specular", 0.102f * 9, 0.051f * 9, 0.0f);
- Program.SendUniform("lightSpot.direction", 0.0, -1.0, 0.0);
- Program.SendUniform("lightSpot.cutoff", 90.0);
- Program.SendUniform("lightSpot.strength", 0.006); // keep between 0.1 (very weak) and 0.001 (very strong)
- Program.SendUniform("lightSpot.attenuation", 2.0); // higher value is higher fade
- // fog
- //Program.SendUniform("fogColor", 0.05f, 0.05f, 0.05f);
- //Program.SendUniform("fogThickness", 0.05);
- // Initialise the View Matrix (initial position of the camera)
- glMatrixMode(GL_MODELVIEW);
- angleTilt = 15;
- glLoadIdentity();
- glRotatef(angleTilt, 1, 0, 0);
- gluLookAt(25.0, 25.0, -25.0,
- 0.0, 25.0, 0.0,
- 0.0, 1.0, 0.0);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
- cout << endl;
- cout << "Use:" << endl;
- cout << " WASD or arrow key to navigate" << endl;
- cout << " QE or PgUp/Dn to move the camera up and down" << endl;
- cout << " Shift+AD or arrow key to auto-orbit" << endl;
- cout << " Drag the mouse to look around" << endl;
- cout << endl;
- //glEnable(GL_CULL_FACE);
- return true;
- }
- void done()
- {
- }
- void getCamPos()
- {
- // getting position of camera in 'world space'
- GLfloat cameraMatrix[16];
- glGetFloatv(GL_MODELVIEW_MATRIX, cameraMatrix);
- cameraVector[0] = -(cameraMatrix[0] * cameraMatrix[12] + cameraMatrix[1] * cameraMatrix[13] + cameraMatrix[2] * cameraMatrix[14]); // x
- cameraVector[1] = -(cameraMatrix[4] * cameraMatrix[12] + cameraMatrix[5] * cameraMatrix[13] + cameraMatrix[6] * cameraMatrix[14]); // y
- cameraVector[2] = -(cameraMatrix[8] * cameraMatrix[12] + cameraMatrix[9] * cameraMatrix[13] + cameraMatrix[10] * cameraMatrix[14]); // z
- cout << "x, y, z \n";
- cout << cameraVector[0]; cout << "f, ";
- cout << cameraVector[1]; cout << "f, ";
- cout << cameraVector[2]; cout << "f, ";
- cout << "\n";
- }
- // called before window opened or resized - to setup the Projection Matrix
- void reshape(int w, int h)
- {
- // find screen aspect ratio
- float ratio = w * 1.0f / h; // we hope that h is not zero
- // --- setup the projection matrix
- glMatrixMode(GL_PROJECTION); // projection matrix using fixed-pipeline vertex shader
- glLoadIdentity();
- glViewport(0, 0, w, h);
- gluPerspective(60.0f, ratio, 0.02f, 1000.0f);
- float matrix[16]; // projection matrix using "basic.vert" vertex shader
- glGetFloatv(GL_PROJECTION_MATRIX, matrix);
- Program.SendUniform("matrixProjection", matrix);
- }
- void renderReflectiveObjects()
- {
- // vase
- Program.SendUniform("reflectionPower", 0.5);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_CUBE_MAP, TexCubeMapAnimated);
- glPushMatrix();
- glTranslatef(3.0f, 15.2f, 0.0f);
- glScalef(0.4f, 0.4f, 0.4f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.1f, 0.2f, 0.3f);
- Program.SendUniform("materialSpecular", 2.2, 2.2, 2.2);
- vase.render();
- glPopMatrix();
- Program.SendUniform("reflectionPower", 0.0);
- }
- void renderLamps()
- {
- // lamp (with B1)
- glPushMatrix();
- glTranslatef(7.0f, 15.2f, 6.0f);
- glRotatef(90, 0.0f, 1.0f, 0.0f);
- glScalef(0.03f, 0.03f, 0.03f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.7f, 0.7f, 0.7f);
- if (B1) Program.SendUniform("materialSpecular", 1.2f, 1.2f, 1.2f);
- else Program.SendUniform("materialSpecular", 0.0f, 0.0f, 0.0f);
- Lamp2.render();
- glPopMatrix();
- // lamp (with B2)
- glPushMatrix();
- glTranslatef(-9.0f, 15.2f, -4.0f);
- glRotatef(45, 0.0f, 1.0f, 0.0f);
- glScalef(0.03f, 0.03f, 0.03f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.1f, 0.1f, 0.1f);
- if (B2) Program.SendUniform("materialSpecular", 1.2f, 1.2f, 1.2f);
- else Program.SendUniform("materialSpecular", 0.0f, 0.0f, 0.0f);
- Lamp22.render();
- glPopMatrix();
- // light bulb (B1)
- glPushMatrix();
- glTranslatef(7.0f, 19.7f, 3.4f);
- glScalef(0.08f, 0.08f, 0.08f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- if (B1) Program.SendUniform("materialAmbient", 50.0f, 50.0f, 50.0f);
- else Program.SendUniform("materialAmbient", 0.3f, 0.3f, 0.3f);
- glutSolidSphere(5, 32, 32);
- glPopMatrix();
- // light bulb (B2)
- glPushMatrix();
- glTranslatef(-7.15f, 19.7f, -5.85f);
- glScalef(0.08f, 0.08f, 0.08f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- if (B2) Program.SendUniform("materialAmbient", 50.0f, 50.0f, 50.0f);
- else Program.SendUniform("materialAmbient", 0.3f, 0.3f, 0.3f);
- glutSolidSphere(5, 32, 32);
- glPopMatrix();
- }
- void renderObjects(float theta, float theta2, float theta3)
- {
- //---- RENDERING PYRAMID VBO ----//
- // Get / Setting Attribute Locations
- glPushMatrix();
- glTranslatef(8.0f, 16.6f, -3.0f);
- glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
- glRotatef(theta2, 0.0f, 1.0f, 0.0f);
- glScalef(0.2f, 0.2f, 0.2f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- glBindTexture(GL_TEXTURE_2D, TexNone);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.0f, 0.338f, 0.1f);
- Program.SendUniform("materialSpecular", 0.4f, 0.4f, 0.4f);
- GLuint attribVertex = Program.GetAttribLocation("aVertex");
- GLuint attribNormal = Program.GetAttribLocation("aNormal");
- // Enable vertex attribute arrays
- glEnableVertexAttribArray(attribVertex);
- glEnableVertexAttribArray(attribNormal);
- // Bind (activate) the vertex buffer and set the pointer to it
- glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- glVertexAttribPointer(attribVertex, 3, GL_FLOAT, GL_FALSE, 0, 0);
- // Bind (activate) the normal buffer and set the pointer to it
- glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
- glVertexAttribPointer(attribNormal, 3, GL_FLOAT, GL_FALSE, 0, 0);
- // Draw triangles–using index buffer
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
- glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, 0);
- // Disable arrays
- glDisableVertexAttribArray(attribVertex);
- glDisableVertexAttribArray(attribNormal);
- glPopMatrix();
- //-------------------------------//
- // table
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, TexOakWood);
- glActiveTexture(GL_TEXTURE2);
- glBindTexture(GL_TEXTURE_2D, TexOakWoodNormals);
- Program.SendUniform("NormalMap", 1);
- glPushMatrix();
- glTranslatef(0.0f, 0, 0.0f);
- glRotatef(180, 0.0f, 1.0f, 0.0f);
- glScalef(0.02f, 0.02f, 0.02f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.278f, 0.138f, 0.038f);
- Program.SendUniform("materialSpecular", 1.8, 1.8, 1.8);
- table.render(1);
- glPopMatrix();
- Program.SendUniform("NormalMap", 0);
- glActiveTexture(GL_TEXTURE2);
- glBindTexture(GL_TEXTURE_2D, TexNone);
- // chair
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, TexChairCloth);
- glPushMatrix();
- glTranslatef(0.0f, 0, 0.0f);
- glRotatef(180, 0.0f, 1.0f, 0.0f);
- glScalef(0.02f, 0.02f, 0.02f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.222f * 3, 0.184f * 3, 0.135f * 3);
- Program.SendUniform("materialSpecular", 0.2, 0.2, 0.2);
- table.render(0);
- glPopMatrix();
- // chair
- glPushMatrix();
- glTranslatef(0.0f, 0, 0.0f);
- glRotatef(0, 0.0f, 1.0f, 0.0f);
- glScalef(0.02f, 0.02f, 0.02f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.222f * 3, 0.184f * 3, 0.135f * 3);
- Program.SendUniform("materialSpecular", 0.2, 0.2, 0.2);
- table.render(0);
- glPopMatrix();
- // chair
- glPushMatrix();
- glTranslatef(4.0f, 0, 0.0f);
- glRotatef(90, 0.0f, 1.0f, 0.0f);
- glScalef(0.02f, 0.02f, 0.02f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.222f * 3, 0.184f * 3, 0.135f * 3);
- Program.SendUniform("materialSpecular", 0.2, 0.2, 0.2);
- table.render(0);
- glPopMatrix();
- // chair
- glPushMatrix();
- glTranslatef(-4.0f, 0, 0.0f);
- glRotatef(-90, 0.0f, 1.0f, 0.0f);
- glScalef(0.02f, 0.02f, 0.02f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.222f * 3, 0.184f * 3, 0.135f * 3);
- Program.SendUniform("materialSpecular", 0.2, 0.2, 0.2);
- table.render(0);
- glPopMatrix();
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, TexNone);
- // teapot
- glPushMatrix();
- glTranslatef(-3.0f, 16.7f, 0.0f);
- glRotatef(120, 0.0f, 1.0f, 0.0f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.288f, 0.143f, 0.143f);
- Program.SendUniform("materialSpecular", 0.4f, 0.4f, 0.4f);
- glutSolidTeapot(2.0);
- glPopMatrix();
- // --- bouncing apple --- //
- glPushMatrix();
- if (theta3 <= 1) glTranslatef(7.0f, 16.5f + theta3, -3.0f);
- if (theta3 >= 1) glTranslatef(7.0f, 18.5f - theta3, -3.0f);
- glRotatef(90, 1.0f, 0.0f, 0.0f);
- glScalef(0.02f, 0.02f, 0.02f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.6f, 0.6f, 0.0f);
- Program.SendUniform("materialSpecular", 0.4f, 0.4f, 0.4f);
- apple.render();
- glPopMatrix();
- // ------------------------//
- // living room
- glPushMatrix();
- glTranslatef(-16.0f, 0.0f, -10.0f);
- glRotatef(90, 0.0f, 1.0f, 0.0f);
- glScalef(0.13f, 0.13f, 0.13f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.4f, 0.4f, 0.4f);
- Program.SendUniform("materialSpecular", 0.2f, 0.2f, 0.2f);
- LivingRoom.render();
- glPopMatrix();
- //---- hanging lamp ----//
- // Pendulum mechanics
- static float alpha4 = 0.0;
- static float delta4 = 0.04;
- delta4 -= alpha4 / 102400;
- alpha4 += delta4;
- //
- glPushMatrix(); //1
- glTranslatef(-1.0, 50.0, 1.0);
- glRotatef(alpha4, 0.5, 0, 1);
- glTranslatef(-1.0, -25.0, 1.0);
- glPushMatrix(); //2
- glRotatef(-90, 1.0f, 0.0f, 0.0f);
- glScalef(0.015f, 0.015f, 0.015f);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- glBindTexture(GL_TEXTURE_2D, TexNone);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("materialAmbient", 0.0f, 0.0f, 0.0f);
- hanginglamp.render();
- glPopMatrix(); //1
- // bulb placement within hanging lamp
- glTranslatef(2.95, 0.6, -2.95);
- glScalef(0.14, 0.14, 0.14);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- glBindTexture(GL_TEXTURE_2D, TexNone);
- Program.SendUniform("matrixModelView", matrix);
- Program.SendUniform("lightSpot.matrix", matrix);
- Program.SendUniform("materialAmbient", 0.102f * 9, 0.051f * 9, 0.0f);
- glutSolidSphere(5, 32, 32);
- glPopMatrix(); //2
- //-----------------------//
- }
- void prepareCubeMapAnimated(float x, float y, float z, float theta, float theta2, float theta3)
- {
- // Store the current viewport in a safe place
- GLint viewport[4];
- glGetIntegerv(GL_VIEWPORT, viewport);
- int w = viewport[2];
- int h = viewport[3];
- // setup the viewport to 256x256, 90 degrees FoV (Field of View)
- glViewport(0, 0, 256, 256);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(90, 1, 0.02f, 1000.0f);
- // send the projection matrix to the shader
- glGetFloatv(GL_PROJECTION_MATRIX, matrix);
- Program.SendUniform("matrixProjection", matrix);
- // render environment 6 times
- glMatrixMode(GL_MODELVIEW);
- Program.SendUniform("reflectionPower", 0.0);
- for (int i = 0; i < 6; ++i)
- {
- // clear background
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // setup the camera
- const GLfloat ROTATION[6][6] =
- { // at up
- { 1.0, 0.0, 0.0, 0.0, -1.0, 0.0 }, // pos x
- { -1.0, 0.0, 0.0, 0.0, -1.0, 0.0 }, // neg x
- { 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 }, // pos y
- { 0.0, -1.0, 0.0, 0.0, 0.0, -1.0 }, // neg y
- { 0.0, 0.0, 1.0, 0.0, -1.0, 0.0 }, // poz z
- { 0.0, 0.0, -1.0, 0.0, -1.0, 0.0 } // neg z
- };
- glLoadIdentity();
- gluLookAt(x, y, z,
- x + ROTATION[i][0], y + ROTATION[i][1], z + ROTATION[i][2],
- ROTATION[i][3], ROTATION[i][4], ROTATION[i][5]);
- // send the View Matrix
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixView", matrix);
- // render scene objects -all but the reflective one
- glActiveTexture(GL_TEXTURE0);
- renderObjects(theta, theta2, theta3);
- renderLamps();
- // send the image to the cube texture
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_CUBE_MAP, TexCubeMapAnimated);
- glCopyTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB8, 0, 0, 256, 256, 0);
- }
- // restore the viewport and projection
- reshape(w, h);
- }
- void prepareShadowMatrix()
- {
- // This is matrix transform every coordinate x,y,z
- // x = x* 0.5 + 0.5
- // y = y* 0.5 + 0.5
- // z = z* 0.5 + 0.5
- // Moving from unit cube [-1,1] to [0,1]
- const float bias[16] = {
- 0.5, 0.0, 0.0, 0.0,
- 0.0, 0.5, 0.0, 0.0,
- 0.0, 0.0, 0.5, 0.0,
- 0.5, 0.5, 0.5, 1.0 };
- // Grab modelview and transformation matrices
- static float modelView[16], projection[16];
- glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
- glGetFloatv(GL_PROJECTION_MATRIX, projection);
- // concatenating all matrices into one
- glPushMatrix();
- glLoadIdentity();
- glLoadMatrixf(bias);
- glMultMatrixf(projection);
- glMultMatrixf(modelView);
- // Store and send as uniform variable
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixShadow", matrix);
- glPopMatrix();
- }
- 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)
- {
- glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);
- // Store the current viewport in a safe place
- GLint viewport[4];
- glGetIntegerv(GL_VIEWPORT, viewport);
- int w = viewport[2];
- int h = viewport[3];
- // setup the viewport to 2x2 the originalandwide (120 degrees)FoV (Field of View)
- glViewport(0, 0, w * 2, h * 2);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(120, (float)w / (float)h, 0.1, 80);
- // send the projection matrix to the shader
- glGetFloatv(GL_PROJECTION_MATRIX, matrix);
- Program.SendUniform("matrixProjection", matrix);
- // prepare the camera
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(x, y, z, centerx, centery, centerz, upx, upy, upz);
- // send the View Matrix
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixView", matrix);
- // Bind the Framebuffer
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, ShadowMapFBO);
- // OFF-SCREEN RENDERING FROM NOW!
- // Clear previous frame values -depth buffer only!
- glClear(GL_DEPTH_BUFFER_BIT);
- // Disable color rendering, we only want to write to the Z-Buffer (this is to speed-up)
- glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
- // Prepare and send the Shadow Matrix
- prepareShadowMatrix();
- // Render all objects in the scene (except the lamps)
- renderObjects(theta, theta2, theta3);
- renderReflectiveObjects();
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
- glDisable(GL_CULL_FACE);
- reshape(w, h);
- }
- void render()
- {
- // control model animations
- static float theta = 0.0f;
- static float theta2 = 0.0f;
- static float theta3 = 0.0f;
- if (theta3 > 2) theta3 = 0;
- static GLint prev_time = 0;
- int time = glutGet(GLUT_ELAPSED_TIME);
- theta += (time - prev_time) * 0.01f;
- prev_time = time;
- glutPostRedisplay();
- static GLint prev_time2 = 0;
- int time2 = glutGet(GLUT_ELAPSED_TIME);
- theta2 += (time2 - prev_time2) * 0.15f;
- prev_time2 = time2;
- static GLint prev_time3 = 0;
- int time3 = glutGet(GLUT_ELAPSED_TIME);
- theta3 += (time3 - prev_time3) * 0.004f;
- prev_time3 = time3;
- // creating shadow map from a position
- if (B1) prepareShadowMap(7.0f, 19.7f, 3.4f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, theta, theta2, theta3);
- if (B2) prepareShadowMap(-7.15f, 19.7f, -5.85f, -7.15f, 0.0f, -5.85f, 1.0f, 0.0f, 0.0f, theta, theta2, theta3);
- // prepare animated cube map
- // NEEDS to be called before anything else (not shadowmapping) in the render() -
- // renders the scene 6 times to capture the image of reflection
- prepareCubeMapAnimated(3.0f, 20.2f, 0.0f, theta, theta2, theta3);
- // clear screen and buffers
- glClearColor(0.18f, 0.25f, 0.22f, 1.0f); // deep grey background
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // setup the View Matrix (camera)
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(angleTilt, 1, 0, 0); // switch tilt off
- glTranslatef(deltaX, deltaY, deltaZ); // animate camera motion (controlled by WASD keys)
- glRotatef(-angleTilt, 1, 0, 0); // switch tilt on
- glMultMatrixf(matrixView);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
- // setup View Matrix
- glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
- Program.SendUniform("matrixView", matrix);
- // MAIN RENDER OBJECT FUNCTIONS
- renderReflectiveObjects();
- renderObjects(theta, theta2, theta3);
- renderLamps();
- // turn on shadow maps for certain bulbs
- if (B1)
- {
- Program.SendUniform("lightPoint.on", 1);
- Program.SendUniform("B1", 1);
- }
- if (!B1)
- {
- Program.SendUniform("lightPoint.on", 0);
- Program.SendUniform("B1", 0);
- }
- if (B2)
- {
- Program.SendUniform("lightPoint2.on", 1);
- Program.SendUniform("B2", 1);
- }
- if (!B2)
- {
- Program.SendUniform("lightPoint2.on", 0);
- Program.SendUniform("B2", 0);
- }
- // essential for double-buffering technique
- glutSwapBuffers();
- }
- // Handle WASDQE keys
- void onKeyDown(unsigned char key, int x, int y)
- {
- switch (tolower(key))
- {
- case 'w': deltaZ = max(deltaZ * 1.05f, 0.01f); break;
- case 's': deltaZ = min(deltaZ * 1.05f, -0.01f); break;
- case 'a': deltaX = max(deltaX * 1.05f, 0.01f); angleRot = 0.1f; break;
- case 'd': deltaX = min(deltaX * 1.05f, -0.01f); angleRot = -0.1f; break;
- case 'e': deltaY = max(deltaY * 1.05f, 0.01f); break;
- case 'q': deltaY = min(deltaY * 1.05f, -0.01f); break;
- case '1': B1 = !B1; break;
- case '2': B2 = !B2; break;
- case '3': break;
- case '4': break;
- case '5': Program.SendUniform("lightSpot.on", 1); break;
- case '6': Program.SendUniform("lightSpot.on", 0); break;
- }
- // speed limit
- deltaX = max(-0.15f, min(0.15f, deltaX));
- deltaY = max(-0.15f, min(0.15f, deltaY));
- deltaZ = max(-0.15f, min(0.15f, deltaZ));
- // stop orbiting
- if ((glutGetModifiers() & GLUT_ACTIVE_SHIFT) == 0) angleRot = 0;
- }
- // Handle WASDQE keys (key up)
- void onKeyUp(unsigned char key, int x, int y)
- {
- switch (tolower(key))
- {
- case 'w': getCamPos(); break;
- case 's': deltaZ = 0; break;
- case 'a':
- case 'd': deltaX = 0; break;
- case 'q':
- case 'e': deltaY = 0; break;
- case ' ': deltaY = 0; break;
- }
- }
- // Handle arrow keys and Alt+F4
- void onSpecDown(int key, int x, int y)
- {
- switch (key)
- {
- case GLUT_KEY_F4: if ((glutGetModifiers() & GLUT_ACTIVE_ALT) != 0) exit(0); break;
- case GLUT_KEY_UP: onKeyDown('w', x, y); break;
- case GLUT_KEY_DOWN: onKeyDown('s', x, y); break;
- case GLUT_KEY_LEFT: onKeyDown('a', x, y); break;
- case GLUT_KEY_RIGHT: onKeyDown('d', x, y); break;
- case GLUT_KEY_PAGE_UP: onKeyDown('q', x, y); break;
- case GLUT_KEY_PAGE_DOWN:onKeyDown('e', x, y); break;
- case GLUT_KEY_F11: glutFullScreenToggle();
- }
- }
- // Handle arrow keys (key up)
- void onSpecUp(int key, int x, int y)
- {
- switch (key)
- {
- case GLUT_KEY_UP: onKeyUp('w', x, y); break;
- case GLUT_KEY_DOWN: onKeyUp('s', x, y); break;
- case GLUT_KEY_LEFT: onKeyUp('a', x, y); break;
- case GLUT_KEY_RIGHT: onKeyUp('d', x, y); break;
- case GLUT_KEY_PAGE_UP: onKeyUp('q', x, y); break;
- case GLUT_KEY_PAGE_DOWN:onKeyUp('e', x, y); break;
- }
- }
- // Handle mouse click
- void onMouse(int button, int state, int x, int y)
- {
- int cx = glutGet(GLUT_WINDOW_WIDTH) / 2;
- int cy = glutGet(GLUT_WINDOW_HEIGHT) / 2;
- if (state == GLUT_DOWN)
- {
- glutSetCursor(GLUT_CURSOR_CROSSHAIR);
- glutWarpPointer(cx, cy);
- }
- else
- glutSetCursor(GLUT_CURSOR_INHERIT);
- }
- // handle mouse move
- void onMotion(int x, int y)
- {
- int cx = glutGet(GLUT_WINDOW_WIDTH) / 2;
- int cy = glutGet(GLUT_WINDOW_HEIGHT) / 2;
- if (x == cx && y == cy)
- return; // caused by glutWarpPointer
- float amp = 0.25;
- float deltaTilt = amp * (y - cy);
- float deltaPan = amp * (x - cx);
- glutWarpPointer(cx, cy);
- // handle camera tilt (mouse move up & down)
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(deltaTilt, 1, 0, 0);
- glMultMatrixf(matrixView);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
- angleTilt += deltaTilt;
- // handle camera pan (mouse move left & right)
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(angleTilt, 1, 0, 0);
- glRotatef(deltaPan, 0, 1, 0);
- glRotatef(-angleTilt, 1, 0, 0);
- glMultMatrixf(matrixView);
- glGetFloatv(GL_MODELVIEW_MATRIX, matrixView);
- }
- int main(int argc, char **argv)
- {
- // init GLUT and create Window
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(ScreenWidth, ScreenHeight);
- glutCreateWindow("CI5520 3D Graphics Programming");
- // init glew
- GLenum err = glewInit();
- if (GLEW_OK != err)
- {
- cerr << "GLEW Error: " << glewGetErrorString(err) << endl;
- return 0;
- }
- cout << "Using GLEW " << glewGetString(GLEW_VERSION) << endl;
- // register callbacks
- glutDisplayFunc(render);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(onKeyDown);
- glutSpecialFunc(onSpecDown);
- glutKeyboardUpFunc(onKeyUp);
- glutSpecialUpFunc(onSpecUp);
- glutMouseFunc(onMouse);
- glutMotionFunc(onMotion);
- cout << "Vendor: " << glGetString(GL_VENDOR) << endl;
- cout << "Renderer: " << glGetString(GL_RENDERER) << endl;
- cout << "Version: " << glGetString(GL_VERSION) << endl;
- // init light and everything – not a GLUT or callback function!
- if (!init())
- {
- cerr << "Application failed to initialise" << endl;
- return 0;
- }
- // enter GLUT event processing cycle
- glutMainLoop();
- done();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement