From 8890b4d973712a9e89a93a41eee95bb3f4552c68 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Fri, 19 May 2023 20:48:24 +0200 Subject: [PATCH] Back to basic - Just a backpack rendering --- Shaders/Framebuffers.fs | 69 +++++++------ Shaders/Framebuffers.vs | 1 - premake5.lua | 1 - src/Primitives/Material.h | 9 ++ src/Primitives/Scene.h | 14 +++ src/Primitives/shader.cpp | 27 +++--- src/Primitives/shader.h | 4 +- src/Renderer/OutlinePass.cpp | 84 ---------------- src/Renderer/OutlinePass.h | 30 ------ src/Renderer/RenderBuffer.cpp | 4 +- src/Renderer/RenderBuffer.h | 2 +- src/Renderer/RenderPass.h | 19 ---- src/Renderer/Renderer.cpp | 176 ++++++++++++++++++++-------------- src/Renderer/Renderer.h | 31 ++---- src/Renderer/ScenePass.cpp | 34 ------- src/Renderer/ScenePass.h | 33 ------- src/Renderer/SkyboxPass.cpp | 32 ------- src/Renderer/SkyboxPass.h | 20 ---- src/main.cpp | 29 ++++-- 19 files changed, 210 insertions(+), 409 deletions(-) create mode 100644 src/Primitives/Material.h create mode 100644 src/Primitives/Scene.h delete mode 100644 src/Renderer/OutlinePass.cpp delete mode 100644 src/Renderer/OutlinePass.h delete mode 100644 src/Renderer/RenderPass.h delete mode 100644 src/Renderer/ScenePass.cpp delete mode 100644 src/Renderer/ScenePass.h delete mode 100644 src/Renderer/SkyboxPass.cpp delete mode 100644 src/Renderer/SkyboxPass.h diff --git a/Shaders/Framebuffers.fs b/Shaders/Framebuffers.fs index 39c06ef..e2601be 100644 --- a/Shaders/Framebuffers.fs +++ b/Shaders/Framebuffers.fs @@ -12,64 +12,59 @@ void main(){ // Inversion filter //FragColor = vec4( vec3 (1.0 - texture(screenTexure, TexCoords) ), 1.0) ; - - //grayscale - /* - FragColor = texture(screenTexure, TexCoords); - float average = (FragColor.r + FragColor.g + FragColor.b) / 3.0; - FragColor = vec4(average, average, average, 1.0); - */ - + // Simple Grayscale + //FragColor = texture(screenTexure, TexCoords); + //float average = (FragColor.r + FragColor.g + FragColor.b) / 3.0; + //FragColor = vec4(average, average, average, 1.0); + // physically accurate grayscale - /* FragColor = texture(screenTexure, TexCoords); - float average = 0.2126 * FragColor.r + 0.7152 * FragColor.g + 0.0722 * FragColor.b; - FragColor = vec4(average,average,average, 1.0);*/ + //FragColor = texture(screenTexure, TexCoords); + //float average = 0.2126 * FragColor.r + 0.7152 * FragColor.g + 0.0722 * FragColor.b; + //FragColor = vec4(average,average,average, 1.0);*/ vec2 offset[9] = vec2[] ( - vec2(-offset, offset), // top-left - vec2(0.0f, offset), // top-center - vec2(offset, offset), // top-right - vec2(-offset, 0.0f), // center-left - vec2(0.0f, 0.0f), // center-center - vec2(offset, 0.0f), // center-right - vec2(-offset, -offset), // bottom-left - vec2(0.0f, -offset), // bottom-center - vec2(offset, -offset) // bottom-right + vec2(-offset, offset), // top-left + vec2(0.0f, offset), // top-center + vec2(offset, offset), // top-right + vec2(-offset, 0.0f), // center-left + vec2(0.0f, 0.0f), // center-center + vec2(offset, 0.0f), // center-right + vec2(-offset, -offset), // bottom-left + vec2(0.0f, -offset), // bottom-center + vec2(offset, -offset) // bottom-right ); // sharpen - /*float kernel[9] = float[]( + float sharpen_kernel[9] = float[]( -1, -1, -1, -1, 9, -1, -1, -1, -1 - );*/ + ); // blur - /* float kernel[9] = float[]( + float blur_kernel[9] = float[]( 1.0 /16, 2.0 /16, 1.0 /16, 2.0 /16, 4.0 /16, 2.0 /16, 1.0 /16, 2.0 /16, 1.0 /16 - );*/ + ); // Edge-detection - /* float kernel[9] = float[] ( + float edgeDetection_kernel[9] = float[] ( 1, 1, 1, 1, -8, 1, 1, 1, 1 ); - vec3 sampleTex[9]; - for (int i= 0; i < 9; i++) { - sampleTex[i] = vec3(texture(screenTexure, TexCoords.st + offset[i])); - } - - vec3 col = vec3(0.0); - for (int i = 0; i < 9; i++) { - col += sampleTex[i] * kernel[i]; - } - - FragColor = vec4(col, 1.0); -*/ + //vec3 sampleTex[9]; + //for (int i= 0; i < 9; i++) { + // sampleTex[i] = vec3(texture(screenTexure, TexCoords.st + offset[i])); + //} + //vec3 col = vec3(0.0); + //for (int i = 0; i < 9; i++) { + // col += sampleTex[i] * kernel[i]; + //} + //FragColor = vec4(col, 1.0); + } \ No newline at end of file diff --git a/Shaders/Framebuffers.vs b/Shaders/Framebuffers.vs index d8773ab..d3e78fc 100644 --- a/Shaders/Framebuffers.vs +++ b/Shaders/Framebuffers.vs @@ -2,7 +2,6 @@ layout (location = 0) in vec2 aPos; layout (location = 1) in vec2 aTexCoords; - out vec2 TexCoords; void main() diff --git a/premake5.lua b/premake5.lua index abc99fe..27bb62c 100644 --- a/premake5.lua +++ b/premake5.lua @@ -60,4 +60,3 @@ project "LearnOpenGL" filter "configurations:Release" defines{"NDEBUG"} optimize "On" - diff --git a/src/Primitives/Material.h b/src/Primitives/Material.h new file mode 100644 index 0000000..71b0fd8 --- /dev/null +++ b/src/Primitives/Material.h @@ -0,0 +1,9 @@ +#pragma once +#include "glm/glm.hpp" + +struct Material { + glm::vec3 ambient; + glm::vec3 diffuse; + glm::vec3 specular; + float shininess; +}; \ No newline at end of file diff --git a/src/Primitives/Scene.h b/src/Primitives/Scene.h new file mode 100644 index 0000000..1ac3cbf --- /dev/null +++ b/src/Primitives/Scene.h @@ -0,0 +1,14 @@ +#pragma once +#include +#include "../model.h" +#include "camera.h" + +class Scene { +public: + + Camera cameras; + std::vector entities; +// std::vector materials; + + +}; \ No newline at end of file diff --git a/src/Primitives/shader.cpp b/src/Primitives/shader.cpp index 1d0597d..d2f3be3 100644 --- a/src/Primitives/shader.cpp +++ b/src/Primitives/shader.cpp @@ -1,7 +1,7 @@ #include "shader.h" -Shader::Shader(const char* vertextPath, const char* fragmentPath) -{ + +void Shader::Load(const char* vertexPath, const char* fragmentPath) { // retrieve the vertex / fragment source code from filepath std::string vertexCode; std::string fragmentCode; @@ -11,15 +11,15 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath) vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); - try{ + try { - std::cout << "Opening VertexShader.. " << vertextPath << std::endl; - vShaderFile.open(vertextPath); + std::cout << "Opening VertexShader.. " << vertexPath << std::endl; + vShaderFile.open(vertexPath); std::cout << "Opening FragmentShader.. " << fragmentPath << std::endl; fShaderFile.open(fragmentPath); - std::stringstream vShaderStream,fShaderStream; + std::stringstream vShaderStream, fShaderStream; vShaderStream << vShaderFile.rdbuf(); fShaderStream << fShaderFile.rdbuf(); @@ -31,11 +31,11 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath) vShaderFile.close(); fShaderFile.close(); - + } - catch(std::ifstream::failure e){ + catch (std::ifstream::failure e) { std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl; - std::cout << e.what() << "| IO error code: " << e.code() << std::endl ; + std::cout << e.what() << "| IO error code: " << e.code() << std::endl; } const char* vShaderCode = vertexCode.c_str(); @@ -53,8 +53,8 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath) glCompileShader(vertex); // print any compile errors if there are any - glGetShaderiv(vertex,GL_COMPILE_STATUS, &success); - if(!success) + glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); + if (!success) { glGetShaderInfoLog(vertex, 512, NULL, infoLog); std::cout << "ERROR::SHADER:::VERTEX::COMPILATION_FAILED" << std::endl << infoLog << std::endl; @@ -67,7 +67,7 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath) glCompileShader(fragment); glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); - if(!success){ + if (!success) { glGetShaderInfoLog(fragment, 512, NULL, infoLog); std::cout << "ERROR::SHADER:::VERTEX::COMPILATION_FAILED" << std::endl << infoLog << std::endl; } @@ -80,7 +80,7 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath) glLinkProgram(ID); glGetProgramiv(ID, GL_LINK_STATUS, &success); - if(!success){ + if (!success) { glGetProgramInfoLog(ID, 512, NULL, infoLog); std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED" << std::endl << infoLog << std::endl; } @@ -89,7 +89,6 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath) glDeleteShader(vertex); glDeleteShader(fragment); - } void Shader::use(){ diff --git a/src/Primitives/shader.h b/src/Primitives/shader.h index e936833..d5d1441 100644 --- a/src/Primitives/shader.h +++ b/src/Primitives/shader.h @@ -13,8 +13,8 @@ class Shader unsigned int ID;// Program ID // Read and build the shader upon construction - Shader(const char* vertexPath, const char* fragmentPath); - + Shader() = default; + void Load(const char* vertexPath, const char* fragmentPath); // Activate the shader void use(); diff --git a/src/Renderer/OutlinePass.cpp b/src/Renderer/OutlinePass.cpp deleted file mode 100644 index e7ef6dc..0000000 --- a/src/Renderer/OutlinePass.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "OutlinePass.h" -#include "Renderer.h" -#include "../../CheckErrors.h" - -OutlinePass::OutlinePass(Model& sceneObject, glm::mat4& model, glm::mat4& projection, Camera& camera) -: RenderPass("Renderpass - Outline", "../Shaders/shader.vs", "../Shaders/outlineshader.fs" ) , m_model(sceneObject), camera(camera), projection(projection), model(model) -{ -} - -void OutlinePass::Create() { - // Framebuffer outline effect - std::cout << "Create Framebuffer" << std::endl; - - m_outlineFrameBuffer = new FrameBuffer(); - glCheckError(); - - m_outlineFrameBuffer->Bind(); - glCheckError(); - - // Create a texture attachment (colour attachment) - std::cout << "Create Texture" << std::endl; - m_colourTexture = CreateTexture(800, 600); - glCheckError(); - - // attach texture to the frame buffer as a colour attachment - m_outlineFrameBuffer->Attach(*m_colourTexture); - glCheckError(); - - // Add depth buffer attachment - std::cout << "Create RenderBuffer" << std::endl; - m_outlineRenderBuffer = new RenderBuffer(); - glCheckError(); - - m_outlineRenderBuffer->Bind(); - glCheckError(); - - m_outlineRenderBuffer->UseDepthAndStencil(); - glCheckError(); - - m_outlineRenderBuffer->Unbind(); - glCheckError(); - - // attach depth buffer to our framebuffer - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_outlineRenderBuffer->id); - glCheckError(); - - m_outlineFrameBuffer->Unbind(); - glCheckError(); - - std::cout << "Success!" << std::endl; -} - - -OutlinePass::~OutlinePass(){ - delete m_colourTexture; - delete m_outlineRenderBuffer; -} - -void OutlinePass::Render() -{ - std::cout << "Start outline render" << std::endl; - glStencilFunc(GL_NOTEQUAL, 1, 0xFF); - glStencilMask(0x00); - glDisable(GL_DEPTH_TEST); - - m_shader.use(); - m_shader.setMat4("model", model); - m_shader.setMat4("view", camera.GetViewMatrix()); - m_shader.setMat4("projection", projection); - m_shader.setVec3("outlineColor", glm::vec3(0.28, 0.10, 0.26)); - - model = glm::scale(model, glm::vec3(1.05f,1.05f, 1.05f)); - m_shader.setMat4("model", model); - - std::cout << "Drawing model" << std::endl; - m_model.Draw(m_shader); - - - glStencilFunc(GL_ALWAYS, 1, 0xff); - glStencilMask(0xFF); - - std::cout << "Finished outline render" << std::endl; - -} \ No newline at end of file diff --git a/src/Renderer/OutlinePass.h b/src/Renderer/OutlinePass.h deleted file mode 100644 index 4f4c68c..0000000 --- a/src/Renderer/OutlinePass.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include -#include "RenderPass.h" -#include "FrameBuffer.h" -#include "RenderBuffer.h" -#include "../model.h" -#include "../Primitives/texture.h" -#include "../Primitives/camera.h" - -class OutlinePass : RenderPass { - -public: - void Render() override; - - OutlinePass (Model& sceneObject, glm::mat4& model , glm::mat4& projection, Camera& camera) ; - void Create(); - ~OutlinePass(); - -private: - FrameBuffer* m_outlineFrameBuffer; - RenderBuffer* m_outlineRenderBuffer; - Texture* m_colourTexture; - - glm::mat4 model; - glm::mat4 projection; - - Model& m_model; - Camera& camera; - -}; \ No newline at end of file diff --git a/src/Renderer/RenderBuffer.cpp b/src/Renderer/RenderBuffer.cpp index 4e4320e..e642fbd 100644 --- a/src/Renderer/RenderBuffer.cpp +++ b/src/Renderer/RenderBuffer.cpp @@ -1,7 +1,7 @@ #include "RenderBuffer.h" -void RenderBuffer::UseDepthAndStencil (){ - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600); /// NOTE: should be part of creating but kept seperate for now! +void RenderBuffer::UseDepthAndStencil (int width, int height){ + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); /// NOTE: should be part of creating but kept seperate for now! } void RenderBuffer::Bind() diff --git a/src/Renderer/RenderBuffer.h b/src/Renderer/RenderBuffer.h index 808ad1d..8067660 100644 --- a/src/Renderer/RenderBuffer.h +++ b/src/Renderer/RenderBuffer.h @@ -10,7 +10,7 @@ public: void Bind(); void Unbind(); - void UseDepthAndStencil(); + void UseDepthAndStencil(int width, int height); RenderBuffer(); ~RenderBuffer(); diff --git a/src/Renderer/RenderPass.h b/src/Renderer/RenderPass.h deleted file mode 100644 index 6c4c0cc..0000000 --- a/src/Renderer/RenderPass.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include -#include "../Primitives/shader.h" - -class RenderPass { -public : - virtual void Render() = 0; - - RenderPass(const std::string& name,const std::string& VertexShaderFile, const std::string& FragmentShaderFile) : m_shader(VertexShaderFile.c_str(), FragmentShaderFile.c_str()) - { - std::cout << "Created " << name << std::endl; - } - - ~RenderPass(){} - -protected: - Shader m_shader; - -}; \ No newline at end of file diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp index 2c650db..cd09987 100644 --- a/src/Renderer/Renderer.cpp +++ b/src/Renderer/Renderer.cpp @@ -9,60 +9,131 @@ #include #include #include +#include "../Primitives/Scene.h" void Renderer::Setup() { + shader.Load("../Shaders/Framebuffers.vs", "../Shaders/Framebuffers.fs"); + + // Create ScreenVAO + glGenVertexArrays(1, &ScreenVAO); + glBindVertexArray(ScreenVAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + + glBufferData(GL_ARRAY_BUFFER, ScreenVertices.size() * sizeof(float), &ScreenVertices[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float))); + + glBindVertexArray(0); + + // Enable features + glEnable(GL_DEPTH_TEST); + glEnable(GL_STENCIL_TEST); + glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); + glEnable(GL_MULTISAMPLE); + +} +void Renderer::resize(int width, int height ) { + framebuffer = FrameBuffer(); + framebuffer.Bind(); - - ColourBuffer = CreateTexture(800,600); - + ColourBuffer = CreateTexture(width, height); framebuffer.Attach(*ColourBuffer); + renderbufferObject = RenderBuffer(); + renderbufferObject.Bind(); - renderbufferObject.UseDepthAndStencil(); - - renderbufferObject.Unbind(); - + renderbufferObject.UseDepthAndStencil(width, height); framebuffer.Attach(renderbufferObject); - if(framebuffer.IsComplete() == false ){ - std::cout << "ERROR::FRAMEBUFFER::Framebuffer is not complete! " << std::endl; - } + if (framebuffer.IsComplete() == false) { + std::cout << "ERROR::FRAMEBUFFER::Framebuffer is not complete! " << std::endl; + } + + renderbufferObject.Unbind(); - framebuffer.Unbind(); } -void Renderer::Render( ) + +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(); + auto model = glm::mat4(1.0f); - // Recalculate the Camera's view matrix - view = camera.GetViewMatrix(); - // Reset the model matrix to an identity matrix - model = glm::mat4(1.0f); - - framebuffer.Bind(); // 1. Skybox pass - skyboxpass.Render(); + /* + Shader cubemap; + cubemap.Load("../Shaders/skybox.vs", "../Shaders/Cubemap.fs"); + + glDepthMask(GL_FALSE); + + cubemap.use(); + + cubemap.setMat4("projection", projection); + auto centeredView = glm::mat4(glm::mat3(scene.cameras.GetViewMatrix())); + cubemap.setMat4("view", centeredView); + + skybox.Bind(); + + glDrawArrays(GL_TRIANGLES, 0, 36); + + skybox.Unbind(); + + glDepthMask(GL_TRUE); + */ + + // 2. Scene pass - scenepass.Render(); + Shader blinnPhong; + 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.setInt("skybox", 11); + + blinnPhong.setMat4("model", model); + blinnPhong.setMat4("view", view); + blinnPhong.setMat4("projection", projection); + + for (auto entity : scene.entities) { + entity.Draw(blinnPhong); + } + + // m_skybox.Unbind(); // 3. outline pass - - outlinepass.Render(); + //Shader OutlineShader; + //OutlineShader.Load("../Shaders/shader.vs", "../Shaders/outlineshader.fs"); + + /* glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + */ // 4. draw result to screen - glBindFramebuffer(GL_FRAMEBUFFER, 0); + /*glBindFramebuffer(GL_FRAMEBUFFER, 0); glDisable(GL_DEPTH_TEST); @@ -70,8 +141,8 @@ void Renderer::Render( ) glClear(GL_COLOR_BUFFER_BIT); - m_shader.use(); - m_shader.setInt("screenTexture",0); + shader.use(); + shader.setInt("screenTexture",0); glBindVertexArray(ScreenVAO); glBindTexture(GL_TEXTURE_2D, ColourBuffer->id); @@ -83,57 +154,20 @@ void Renderer::Render( ) glStencilMask(0xFF); glStencilFunc(GL_ALWAYS, 1, 0xFF); glEnable(GL_DEPTH_TEST); - + */ } -Renderer::Renderer(Camera& camera, Model& scene) : - camera(camera), - renderbufferObject(), - framebuffer(), - skybox(), - skyboxpass(skybox, camera, projection), - scenepass(skybox, scene,camera,projection, model), - outlinepass(scene, model, projection, camera), - m_shader("../Shaders/Framebuffers.vs", "../Shaders/Framebuffers.fs") -{ +void Renderer::Shutdown() { + framebuffer.Unbind(); +} - outlinepass.Create(); - - - std::vector faces = { - "../Textures/skybox/right.jpg", - "../Textures/skybox/left.jpg", - "../Textures/skybox/top.jpg", - "../Textures/skybox/bottom.jpg", - "../Textures/skybox/front.jpg", - "../Textures/skybox/back.jpg" - }; - skybox.loadCubeTextures(faces); +Renderer::Renderer() { + // auto outlinepass = OutlinePass(scene , model, projection, camera); - // Create ScreenVAO - glGenVertexArrays(1, &ScreenVAO); - glBindVertexArray(ScreenVAO); - - glGenBuffers(1, &VBO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - - glBufferData(GL_ARRAY_BUFFER, ScreenVertices.size() * sizeof(float), &ScreenVertices[0], GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0,2, GL_FLOAT,GL_FALSE, 4 * sizeof(float),(void*) 0 ); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1,2,GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2*sizeof(float))); - - glBindVertexArray(0); - - // Enable features - glEnable(GL_DEPTH_TEST); - glEnable(GL_STENCIL_TEST); - glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); - glEnable(GL_MULTISAMPLE); + } diff --git a/src/Renderer/Renderer.h b/src/Renderer/Renderer.h index 4fe388a..c19db48 100644 --- a/src/Renderer/Renderer.h +++ b/src/Renderer/Renderer.h @@ -5,39 +5,26 @@ #include "../Primitives/shader.h" #include "FrameBuffer.h" #include "RenderBuffer.h" -#include "ScenePass.h" -#include "OutlinePass.h" -#include "SkyboxPass.h" - +#include "../Primitives/Scene.h" class Renderer { public: - void Render(); - void Setup(); - - Renderer(Camera& camera,Model& scene); + Renderer(); ~Renderer(); - - - private: - ScenePass scenepass; - SkyboxPass skyboxpass; - OutlinePass outlinepass; + void Setup(); + void resize(int width, int height); + void Render(Scene& scene); + void Shutdown(); - Camera& camera; + + private: GLuint ScreenVAO, VBO; FrameBuffer framebuffer; RenderBuffer renderbufferObject; Texture* ColourBuffer; - Skybox skybox; + Shader shader; - Shader m_shader; - - glm::mat4 model = glm::mat4(1.0f); - glm::mat4 view; - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)800 / (float)600, 0.1f, 100.0f); - glm::vec3 lightpos = glm::vec3(-0.2f, -1.0f, -0.3f); std::vector ScreenVertices = { diff --git a/src/Renderer/ScenePass.cpp b/src/Renderer/ScenePass.cpp deleted file mode 100644 index 4ec4486..0000000 --- a/src/Renderer/ScenePass.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "ScenePass.h" - -void ScenePass::Render() -{ - m_shader.use(); - m_shader.setVec3("cameraPos", camera.Position); - - - glActiveTexture(GL_TEXTURE11); - m_skybox.Bind(); // Segmentation Fault HERE!! - glActiveTexture(GL_TEXTURE0); - - m_shader.setInt("skybox",11); - - - m_shader.setMat4("model", model); - m_shader.setMat4("view", camera.GetViewMatrix()); - m_shader.setMat4("projection", projection); - - - m_scene.Draw(m_shader); - - - m_skybox.Unbind(); - - - - -} - -void ScenePass::SetSkybox(Skybox& skybox) -{ - m_skybox = skybox; -} \ No newline at end of file diff --git a/src/Renderer/ScenePass.h b/src/Renderer/ScenePass.h deleted file mode 100644 index 1de7b3d..0000000 --- a/src/Renderer/ScenePass.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once -#include "RenderPass.h" -#include "../Primitives/Skybox.h" -#include "../Primitives/shader.h" -#include "../Primitives/camera.h" -#include "../model.h" - -class ScenePass : public RenderPass { - - public: - void Render() override; - - ScenePass(Skybox& skybox, Model& scene, Camera& camera, glm::mat4 projection , glm::mat4 model) - : RenderPass("RenderPass - Scene", "../Shaders/shader.vs", "../Shaders/shader.fs" ), - m_scene(scene), camera(camera), projection(projection), model(model), m_skybox(skybox) - { - - - } - ~ScenePass() = default; - - void SetSkybox(Skybox& skybox); - - private: - Skybox& m_skybox; - Model& m_scene; - Camera& camera; - - glm::mat4 projection; - glm::mat4 model; - - -}; \ No newline at end of file diff --git a/src/Renderer/SkyboxPass.cpp b/src/Renderer/SkyboxPass.cpp deleted file mode 100644 index d24757f..0000000 --- a/src/Renderer/SkyboxPass.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "SkyboxPass.h" -SkyboxPass::SkyboxPass(Skybox& skybox, Camera& camera, glm::mat4& projection) : RenderPass("Render Pass - Skybox", "../Shaders/skybox.vs", "../Shaders/Cubemap.fs"), -skybox(skybox), -camera(camera), -projection(projection) -{ - -} - - - -void SkyboxPass::Render() -{ - glDepthMask(GL_FALSE); - - m_shader.use(); - - m_shader.setMat4("projection", projection); - glm::mat4 centeredView = glm::mat4(glm::mat3(camera.GetViewMatrix())); - m_shader.setMat4("view", centeredView); - - - - skybox.Bind(); - - - glDrawArrays(GL_TRIANGLES, 0, 36); - - skybox.Unbind(); - - glDepthMask(GL_TRUE); -} \ No newline at end of file diff --git a/src/Renderer/SkyboxPass.h b/src/Renderer/SkyboxPass.h deleted file mode 100644 index 7237e81..0000000 --- a/src/Renderer/SkyboxPass.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "RenderPass.h" -#include "../Primitives/Skybox.h" -#include "../Primitives/shader.h" -#include "../Primitives/camera.h" - -class SkyboxPass : public RenderPass { - public: - void Render() override; - - SkyboxPass( Skybox& skybox, Camera& camera, glm::mat4& projection); - ~SkyboxPass() = default; - - private: - Skybox& skybox; - Camera& camera; - glm::mat4& projection; - - -}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e34651f..7ed9f1c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,14 +14,14 @@ #include #include "Application.h" - +#include "Primitives/Scene.h" void processInput( GLFWwindow* window); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void framebuffer_size_callback(GLFWwindow* window, int width, int height); void mouse_callback(GLFWwindow* window, double xpos, double ypos); - +const int WIDTH = 800, HEIGHT = 600; float deltaTime = 0.0f; ; // Time between current frame and last frame float lastFrame = 0.0f; // Time of last frame bool firstMouse = true; @@ -35,7 +35,7 @@ public: void Run () override { // Create a window - Window window(800,600, "LearnOpenGL"); + Window window(WIDTH,HEIGHT, "LearnOpenGL"); IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -48,10 +48,27 @@ public: ImGui_ImplOpenGL3_Init("#version 460"); stbi_set_flip_vertically_on_load(true); - Model backpack("../Models/backpack.obj"); - Renderer renderer = Renderer(camera, backpack); + Scene scene; + scene.entities.push_back(backpack); + + scene.cameras = camera; + + + std::vector faces = { + "../Textures/skybox/right.jpg", + "../Textures/skybox/left.jpg", + "../Textures/skybox/top.jpg", + "../Textures/skybox/bottom.jpg", + "../Textures/skybox/front.jpg", + "../Textures/skybox/back.jpg" + }; + Skybox skybox = Skybox(); + skybox.loadCubeTextures(faces); + + Renderer renderer = Renderer(); + renderer.resize(WIDTH, HEIGHT); renderer.Setup(); @@ -65,7 +82,7 @@ public: glfwPollEvents(); processInput(window.ptr()); - renderer.Render(); + renderer.Render(scene); ImGui_ImplOpenGL3_NewFrame();