diff --git a/src/Primitives/Scene.h b/src/Primitives/Scene.h index 1ac3cbf..c81f63a 100644 --- a/src/Primitives/Scene.h +++ b/src/Primitives/Scene.h @@ -2,11 +2,13 @@ #include #include "../model.h" #include "camera.h" +#include "Skybox.h" class Scene { public: - Camera cameras; + Camera MainCamera; + Skybox skybox; std::vector entities; // std::vector materials; diff --git a/src/Primitives/Skybox.cpp b/src/Primitives/Skybox.cpp index 28afe0c..bf53c03 100644 --- a/src/Primitives/Skybox.cpp +++ b/src/Primitives/Skybox.cpp @@ -1,38 +1,5 @@ #include "Skybox.h" - - -Skybox::Skybox() -{ - glGenTextures(1, &id); - - // Create SkyboxVAO - - glGenVertexArrays(1, &m_skyboxVAO); - glBindVertexArray(m_skyboxVAO); - - glGenBuffers(1, &m_skyboxVBO); - glBindBuffer(GL_ARRAY_BUFFER, m_skyboxVBO); - - glBufferData(GL_ARRAY_BUFFER, m_skyboxVertices.size() * sizeof(float), &m_skyboxVertices[0], GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0,3, GL_FLOAT,GL_FALSE, 3 * sizeof(float),(void*) 0); - - glBindVertexArray(0); - - std::cout << "Skybox Created" << std::endl; - -} - -Skybox::~Skybox() -{ - glDeleteTextures(1, &id); - std::cout << "Skybox destroyed!" << std::endl; -} - - - void Skybox::Bind() { glBindVertexArray(m_skyboxVAO); @@ -45,8 +12,33 @@ void Skybox::Unbind() glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } +void Skybox::Destroy() { + glDeleteTextures(1, &id); + std::cout << "Skybox destroyed!" << std::endl; +} + void Skybox::loadCubeTextures(const std::vector& texture_faces) { + glGenTextures(1, &id); + + // Create SkyboxVAO + + glGenVertexArrays(1, &m_skyboxVAO); + glBindVertexArray(m_skyboxVAO); + + glGenBuffers(1, &m_skyboxVBO); + glBindBuffer(GL_ARRAY_BUFFER, m_skyboxVBO); + + glBufferData(GL_ARRAY_BUFFER, m_skyboxVertices.size() * sizeof(float), &m_skyboxVertices[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + + glBindVertexArray(0); + + std::cout << "Skybox Created" << std::endl; + + Bind(); int width, height, nrChannels; unsigned char* data; diff --git a/src/Primitives/Skybox.h b/src/Primitives/Skybox.h index 0498e48..c533c52 100644 --- a/src/Primitives/Skybox.h +++ b/src/Primitives/Skybox.h @@ -9,14 +9,14 @@ class Skybox { public: - Skybox (); - ~Skybox (); + Skybox () = default ; GLuint m_skyboxVAO; void Bind(); void Unbind(); + void Destroy(); void loadCubeTextures(const std::vector& texture_faces); private: diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp index cd09987..b67617c 100644 --- a/src/Renderer/Renderer.cpp +++ b/src/Renderer/Renderer.cpp @@ -68,13 +68,11 @@ void Renderer::Render(Scene& scene) glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - glm::mat4 projection = glm::perspective(glm::radians(scene.cameras.Zoom), (float)800 / (float)600, 0.1f, 100.0f); - auto view = scene.cameras.GetViewMatrix(); + glm::mat4 projection = glm::perspective(glm::radians(scene.MainCamera.Zoom), (float)800 / (float)600, 0.1f, 100.0f); + auto view = scene.MainCamera.GetViewMatrix(); auto model = glm::mat4(1.0f); - // 1. Skybox pass - /* Shader cubemap; cubemap.Load("../Shaders/skybox.vs", "../Shaders/Cubemap.fs"); @@ -83,17 +81,16 @@ void Renderer::Render(Scene& scene) cubemap.use(); cubemap.setMat4("projection", projection); - auto centeredView = glm::mat4(glm::mat3(scene.cameras.GetViewMatrix())); + auto centeredView = glm::mat4(glm::mat3(scene.MainCamera.GetViewMatrix())); cubemap.setMat4("view", centeredView); - skybox.Bind(); + scene.skybox.Bind(); glDrawArrays(GL_TRIANGLES, 0, 36); - skybox.Unbind(); + scene.skybox.Unbind(); glDepthMask(GL_TRUE); - */ // 2. Scene pass @@ -101,14 +98,7 @@ void Renderer::Render(Scene& scene) blinnPhong.Load("../Shaders/shader.vs", "../Shaders/shader.fs"); blinnPhong.use(); - blinnPhong.setVec3("cameraPos", scene.cameras.Position); - - /* - glActiveTexture(GL_TEXTURE11); - m_skybox.Bind(); // Segmentation Fault HERE!! - glActiveTexture(GL_TEXTURE0); - - */ + blinnPhong.setVec3("cameraPos", scene.MainCamera.Position); blinnPhong.setInt("skybox", 11); @@ -120,8 +110,6 @@ void Renderer::Render(Scene& scene) entity.Draw(blinnPhong); } - // m_skybox.Unbind(); - // 3. outline pass //Shader OutlineShader; //OutlineShader.Load("../Shaders/shader.vs", "../Shaders/outlineshader.fs"); @@ -161,17 +149,3 @@ void Renderer::Shutdown() { framebuffer.Unbind(); } - -Renderer::Renderer() { - - - - // auto outlinepass = OutlinePass(scene , model, projection, camera); - - -} - -Renderer::~Renderer() -{ - -} \ No newline at end of file diff --git a/src/Renderer/Renderer.h b/src/Renderer/Renderer.h index c19db48..f6e9531 100644 --- a/src/Renderer/Renderer.h +++ b/src/Renderer/Renderer.h @@ -10,8 +10,7 @@ class Renderer { public: - Renderer(); - ~Renderer(); + Renderer() = default ; void Setup(); void resize(int width, int height); void Render(Scene& scene); diff --git a/src/main.cpp b/src/main.cpp index 7ed9f1c..3262c23 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,8 +26,7 @@ float deltaTime = 0.0f; ; // Time between current frame and last frame float lastFrame = 0.0f; // Time of last frame bool firstMouse = true; float lastx = 400, lasty = 300; - -Camera camera(glm::vec3(0.0f,0.0f,8.0f)); +Scene scene; class LearnOpenGL : Application { @@ -50,10 +49,8 @@ public: stbi_set_flip_vertically_on_load(true); Model backpack("../Models/backpack.obj"); - Scene scene; scene.entities.push_back(backpack); - - scene.cameras = camera; + scene.MainCamera = Camera(glm::vec3(0.0f, 0.0f, 8.0f)); std::vector faces = { @@ -64,8 +61,7 @@ public: "../Textures/skybox/front.jpg", "../Textures/skybox/back.jpg" }; - Skybox skybox = Skybox(); - skybox.loadCubeTextures(faces); + scene.skybox.loadCubeTextures(faces); Renderer renderer = Renderer(); renderer.resize(WIDTH, HEIGHT); @@ -97,6 +93,8 @@ public: window.SwapBuffers(); } + scene.skybox.Destroy(); + ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); @@ -111,13 +109,13 @@ void processInput( GLFWwindow* window) const float CameraSpeed = 0.5f * deltaTime; if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) - camera.ProcessKeyboard(FORWARD, CameraSpeed); + scene.MainCamera.ProcessKeyboard(FORWARD, CameraSpeed); if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) - camera.ProcessKeyboard(BACKWARD,CameraSpeed); + scene.MainCamera.ProcessKeyboard(BACKWARD,CameraSpeed); if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) - camera.ProcessKeyboard(LEFT, CameraSpeed); + scene.MainCamera.ProcessKeyboard(LEFT, CameraSpeed); if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) - camera.ProcessKeyboard(RIGHT,CameraSpeed); + scene.MainCamera.ProcessKeyboard(RIGHT,CameraSpeed); } void mouse_callback(GLFWwindow* window, double xpos, double ypos) @@ -134,17 +132,19 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos) lastx = xpos; lasty - ypos; - camera.ProcessMouseMovement(xoffset, yoffset); + scene.MainCamera.ProcessMouseMovement(xoffset, yoffset); } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0,0, width,height); + + } void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { - camera.ProcessMouseScroll(yoffset); + scene.MainCamera.ProcessMouseScroll(yoffset); } int main()