#include "MyGlWindow.h" //Getting the projection matrix glm::mat4x4 perspective(float fovy, float aspect, float near, float far) { float fovy2 = glm::tan(fovy / 2); glm::mat4x4 pmat{ { 1 / (aspect * fovy2), 0, 0, 0}, { 0, 1 / fovy2, 0, 0}, { 0, 0, -((far + near) / (far - near)), -1}, { 0, 0, -((2 * far * near) / (far - near)), 0} }; return pmat; } // Getting the view matrix glm::mat4x4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) { glm::vec3 ZCam(glm::normalize(campos - look)); glm::vec3 XCam(glm::normalize(glm::cross(up, ZCam))); glm::vec3 YCam(glm::normalize(glm::cross(ZCam, XCam))); glm::mat4x4 cam_mat{ {XCam.x, YCam.x, ZCam.x, 0}, {XCam.y, YCam.y, ZCam.y, 0}, {XCam.z, YCam.z, ZCam.z, 0}, {0, 0, 0 ,1} }; glm::mat4x4 norm_mat{ {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {-campos.x, -campos.y, -campos.z, 1} }; return cam_mat * norm_mat; } MyGlWindow::MyGlWindow(int w, int h) : viewer(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0), 45.0f, (w / (float)h)) { m_width = w; m_height = h; _scnctx.height = m_height; _scnctx.width = m_width; setup(); } MyGlWindow::~MyGlWindow() { shaders.clear(); } void MyGlWindow::setBgColor(float bgColor[3]) { _scnctx.bg = glm::vec4(bgColor[0], bgColor[1], bgColor[2], 1); } void MyGlWindow::textureSetup() { glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); _scnctx.textures.emplace("BrickTex", Texture("brick1.jpg")); _scnctx.textures.emplace("MossTex", Texture("moss.png")); _scnctx.textures.emplace("EarthTex", Texture("earth.jpg")); _scnctx.textures.emplace("OgreTex", Texture("Models/ogre/ogre_diffuse.png")); _scnctx.textures["OgreTex"].mat.shininess = 3.0f; _scnctx.textures["OgreTex"].mat.ks = glm::vec3(0.1f, 0.1f, 0.1f); _scnctx.textures["OgreTex"].mat.ka = glm::vec3(0.3f, 0.3f, 0.3f); _scnctx.textures["OgreTex"].mat.enabled = true; _scnctx.textures.emplace("OgreNmap", Texture("Models/ogre/ogre_normalmap.png")); _scnctx.textures["OgreNmap"].isNmap = true; _scnctx.textures.emplace("CubeTex", Texture("Models/cube/color_map.jpg")); _scnctx.textures["CubeTex"].mat.shininess = 3.0f; _scnctx.textures["CubeTex"].mat.ks = glm::vec3(0.1f, 0.1f, 0.1f); _scnctx.textures["CubeTex"].mat.ka = glm::vec3(0.3f, 0.3f, 0.3f); _scnctx.textures["CubeTex"].mat.enabled = true; _scnctx.textures.emplace("CubeNmap", Texture("Models/cube/normal_map.jpg")); _scnctx.textures["CubeNmap"].isNmap = true; } void MyGlWindow::lightSetup() { _scnctx.lights.emplace("Spotlight1", Light(glm::vec3(0.8f), glm::vec4(10, 10, 10, 1))); } void MyGlWindow::multipassSetup() { _multipassManager.addTexture("position_buffer", GL_NEAREST, GL_RGB16F, GL_RGB, GL_FLOAT, false, _scnctx); _multipassManager.addTexture("normal_buffer", GL_NEAREST, GL_RGB16F, GL_RGB, GL_UNSIGNED_BYTE, false, _scnctx); _multipassManager.addTexture("color_buffer", GL_NEAREST, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, false, _scnctx); _multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, "position_buffer"); _multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, "normal_buffer"); _multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, "color_buffer"); _multipassManager.setDrawBuffers(); } void MyGlWindow::setup() { glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_BUFFER); glEnable(GL_TEXTURE_2D); _scnctx.bg = glm::vec4(0.7, 0.7, 0.9, 1); textureSetup(); lightSetup(); multipassSetup(); Dataset moddata; //Scene for GBuffer Testing /*Mesh *nanosuit_A = new Mesh("nanosuit/nanosuit.obj", shaders["DSGeometryPass"]); Mesh *nanosuit_B = new Mesh("nanosuit/nanosuit.obj", shaders["DSGeometryPass"]); Mesh *nanosuit_C = new Mesh("nanosuit/nanosuit.obj", shaders["DSGeometryPass"]); nanosuit_A->addStartScaling(glm::vec4(0.2, 0.2, 0.2, 1)); nanosuit_B->addStartScaling(glm::vec4(0.2, 0.2, 0.2, 1)); nanosuit_C->addStartScaling(glm::vec4(0.2, 0.2, 0.2, 1)); nanosuit_A->addStartTranslation(glm::vec4(-10, 0, 0, 1)); nanosuit_C->addStartTranslation(glm::vec4(10, 0, 0, 1)); meshes.emplace("Nanosuit_A", nanosuit_A); meshes.emplace("Nanosuit_B", nanosuit_B); meshes.emplace("Nanosuit_C", nanosuit_C); */ //Scene for light testing moddata.simpleCube(); //Hardcoded seed for easy scene replication std::srand(18); int zob = std::rand(); for (int i = 0; i < 1; i++) { std::string cube_name = "Cube" + std::to_string(i); meshes.emplace(cube_name, new Mesh(moddata, "DSGeometryPass.vert", "DSGeometryPass.frag")); /*meshes[cube_name]->textures["cube_tex"] = Texture("./Models/cube/color_map.png"); float pos_x = std::rand() % 100 + 50; float pos_z = std::rand() % 100 + 50; meshes[cube_name]->addStartTranslation(glm::vec4(0, 1, 0, 0)); meshes[cube_name]->addStartTranslation(glm::vec4(pos_x, 0, 0, 0)); meshes[cube_name]->addStartTranslation(glm::vec4(0, 0, pos_z, 0)); meshes[cube_name]->addStartRotation(glm::vec4(1, 0, 0, std::rand() % 360)); meshes[cube_name]->addStartRotation(glm::vec4(0, 1, 0, std::rand() % 360)); meshes[cube_name]->addStartRotation(glm::vec4(0, 0, 1, std::rand() % 360));*/ //float light_r = (40 + std::rand() % 60) / 100.f; //float light_g = (40 + std::rand() % 60) / 100.f; //float light_b = (40 + std::rand() % 60) / 100.f; //_scnctx.lights.emplace("RandLight" + i, // Light(glm::vec3(light_r, light_g, light_b), glm::vec4(pos_x, 2, pos_z, 1))); } } void MyGlWindow::draw() { _scnctx.height = m_height; _scnctx.width = m_width; glm::vec3 eye(viewer.getViewPoint().x, viewer.getViewPoint().y, viewer.getViewPoint().z); glm::vec3 look(viewer.getViewCenter().x, viewer.getViewCenter().y, viewer.getViewCenter().z); glm::vec3 up(viewer.getUpVector().x, viewer.getUpVector().y, viewer.getUpVector().z); glm::mat4 view = lookAt(eye, look, up); //Calculate view matrix from parameters of m_viewer glm::mat4 projection = perspective(45.0f, (float)_scnctx.width / (float)_scnctx.height, 0.1f, 1000.0f); _scnctx.viewMatrix = view; _scnctx.projectionMatrix = projection; glClearColor(_scnctx.bg.r, _scnctx.bg.g, _scnctx.bg.b, _scnctx.bg.a); glViewport(0, 0, _scnctx.width, _scnctx.height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //_multipassManager.enableFrameBufferTexture("position_buffer"); for (auto it = meshes.begin(); it != meshes.end(); it++) (*it).second->draw(_scnctx); //_multipassManager.drawResultToScreen(_scnctx); } void MyGlWindow::resize(int w, int h) { m_width = w; m_height = h; viewer.setAspectRatio(w / float(h)); }