From e7a119f0ddd7a1c77dd3f8961be8b9b990938faa Mon Sep 17 00:00:00 2001 From: Nigel Date: Sun, 16 Oct 2022 20:14:06 +0200 Subject: [PATCH] Cubemap added, Skybox pass implemented --- Cubemap.fs | 11 +++ Framebuffers.fs | 6 +- Textures/skybox/back.jpg | 3 + Textures/skybox/bottom.jpg | 3 + Textures/skybox/front.jpg | 3 + Textures/skybox/left.jpg | 3 + Textures/skybox/right.jpg | 3 + Textures/skybox/top.jpg | 3 + shader.fs | 4 +- skybox.vs | 13 ++++ src/Cubemap.cpp | 44 ++++++++++++ src/Cubemap.h | 25 +++++++ src/main.cpp | 133 +++++++++++++++++++++++++++++++++---- 13 files changed, 236 insertions(+), 18 deletions(-) create mode 100644 Cubemap.fs create mode 100644 Textures/skybox/back.jpg create mode 100644 Textures/skybox/bottom.jpg create mode 100644 Textures/skybox/front.jpg create mode 100644 Textures/skybox/left.jpg create mode 100644 Textures/skybox/right.jpg create mode 100644 Textures/skybox/top.jpg create mode 100644 skybox.vs create mode 100644 src/Cubemap.cpp create mode 100644 src/Cubemap.h diff --git a/Cubemap.fs b/Cubemap.fs new file mode 100644 index 0000000..d61749e --- /dev/null +++ b/Cubemap.fs @@ -0,0 +1,11 @@ +#version 460 core +out vec4 FragColor; + +in vec3 TexCoords; +uniform samplerCube cubemap; + + +void main() +{ + FragColor = texture(cubemap, TexCoords); +} \ No newline at end of file diff --git a/Framebuffers.fs b/Framebuffers.fs index e0fe2c8..39c06ef 100644 --- a/Framebuffers.fs +++ b/Framebuffers.fs @@ -21,9 +21,9 @@ void main(){ */ // 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 diff --git a/Textures/skybox/back.jpg b/Textures/skybox/back.jpg new file mode 100644 index 0000000..0166e68 --- /dev/null +++ b/Textures/skybox/back.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:da4938039f6a536c7db3fba35ffdd905861ce36608ceadf4412479c5fcacd138 +size 740068 diff --git a/Textures/skybox/bottom.jpg b/Textures/skybox/bottom.jpg new file mode 100644 index 0000000..47253e1 --- /dev/null +++ b/Textures/skybox/bottom.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:088f074272f731abdc9da601cf925aeb80060d048ef33d2a60dd1b215d41feec +size 280589 diff --git a/Textures/skybox/front.jpg b/Textures/skybox/front.jpg new file mode 100644 index 0000000..2b99520 --- /dev/null +++ b/Textures/skybox/front.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dd6f9d8bb04466c06387e8dc736eb453bcb92a09750be520b8d5e00e8bd484fe +size 473329 diff --git a/Textures/skybox/left.jpg b/Textures/skybox/left.jpg new file mode 100644 index 0000000..62ce923 --- /dev/null +++ b/Textures/skybox/left.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:087df0cdc2d28b7e9f5f6b2d82795aa6b10684ce51d3104e8dd3c128b485a960 +size 601885 diff --git a/Textures/skybox/right.jpg b/Textures/skybox/right.jpg new file mode 100644 index 0000000..804ed5c --- /dev/null +++ b/Textures/skybox/right.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4a1b6fd9daffdfb6c42d0dc169cffba7129a8d7da3fab52531e4df4a203191d +size 538038 diff --git a/Textures/skybox/top.jpg b/Textures/skybox/top.jpg new file mode 100644 index 0000000..9ea52e6 --- /dev/null +++ b/Textures/skybox/top.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ff0aa54d15b7c69fc6fae738f943854950a67afd7b1a4e297809668b2380e055 +size 346139 diff --git a/shader.fs b/shader.fs index 7e5fe62..d8cafaf 100644 --- a/shader.fs +++ b/shader.fs @@ -1,8 +1,10 @@ #version 460 core out vec4 FragColor; -in vec2 TexCoords; + + +in vec2 TexCoords; uniform sampler2D texture_diffuse1; void main() diff --git a/skybox.vs b/skybox.vs new file mode 100644 index 0000000..bf8c1c1 --- /dev/null +++ b/skybox.vs @@ -0,0 +1,13 @@ +#version 460 core +layout (location = 0) in vec3 aPos; + +out vec3 TexCoords; + +uniform mat4 projection; +uniform mat4 view; + +void main() +{ + TexCoords = aPos; + gl_Position = projection * view * vec4(aPos ,1.0); +} \ No newline at end of file diff --git a/src/Cubemap.cpp b/src/Cubemap.cpp new file mode 100644 index 0000000..5c8c34d --- /dev/null +++ b/src/Cubemap.cpp @@ -0,0 +1,44 @@ +#include "Cubemap.h" + + +Cubemap::Cubemap(){ + glGenTextures(1,&id); +} + +Cubemap::~Cubemap(){ + // glDeleteTextures(id); +} + +void Cubemap::Bind(){ + glBindTexture(GL_TEXTURE_CUBE_MAP, id); +}; + +void Cubemap::Unbind(){ + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); +} + +void Cubemap::loadCubeTextures(const std::vector& texture_faces){ + int width, height, nrChannels; + unsigned char* data; + stbi_set_flip_vertically_on_load(false); + + for (unsigned int i = 0; i < texture_faces.size(); i++){ + data = stbi_load(texture_faces[i].c_str(), &width, &height, &nrChannels,0); + if (data){ + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0 ,GL_RGB, GL_UNSIGNED_BYTE,data); + } else { + std::cout << "Cubemap tex failed to load at path: " << texture_faces[i] << std::endl; + } + + } + + 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); + + + stbi_set_flip_vertically_on_load(true); + +} \ No newline at end of file diff --git a/src/Cubemap.h b/src/Cubemap.h new file mode 100644 index 0000000..b480a30 --- /dev/null +++ b/src/Cubemap.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include "stb_image.h" +#include +#include +#include + + +class Cubemap { +public: + + unsigned int id; + + Cubemap(); + ~Cubemap(); + + void Bind(); + void Unbind(); + + void loadCubeTextures(const std::vector& texture_faces); + + +}; + + diff --git a/src/main.cpp b/src/main.cpp index 34ab34d..7e40305 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -13,6 +14,7 @@ #include "model.h" #include "FrameBuffer.hpp" #include "RenderBuffer.hpp" +#include "Cubemap.h" // https://learnopengl.com/Advanced-OpenGL/Framebuffers @@ -40,6 +42,60 @@ std::vector ScreenVertices = { 1.0f, 1.0f, 1.0f, 1.0f, } ; +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" +}; + +std::vector skyboxVertices = { + // positions + -1.0f, 1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + + -1.0f, 1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, 1.0f +}; + void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0,0, width, height); } @@ -111,13 +167,32 @@ GLFWwindow* CreateWindowWithOpenGLContext() } +void Skybox_pass (GLuint& SkyboxVAO, Shader& skyboxShader, Cubemap& skycubemap) +{ + //glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + //glClear(GL_COLOR_BUFFER_BIT ); + + glDepthMask(GL_FALSE); + + skyboxShader.use(); + skyboxShader.setMat4("projection", projection); + + glm::mat4 centeredView = glm::mat4(glm::mat3(camera.GetViewMatrix())); + skyboxShader.setMat4("view", centeredView); + + glBindVertexArray(SkyboxVAO); + skycubemap.Bind(); + glDrawArrays(GL_TRIANGLES, 0, 36); + glDepthMask(GL_TRUE); +} + void drawScene_pass(Shader& shader, Model& Object) { - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glStencilFunc(GL_ALWAYS, 1, 0xff); - glStencilMask(0xFF); - + //glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + + shader.use(); shader.setMat4("model", model); shader.setMat4("view", view); @@ -128,6 +203,8 @@ void drawScene_pass(Shader& shader, Model& Object) void outline_pass( Shader& shader, Model& Object) { + + glStencilFunc(GL_NOTEQUAL, 1, 0xFF); glStencilMask(0x00); glDisable(GL_DEPTH_TEST); @@ -142,6 +219,9 @@ void outline_pass( Shader& shader, Model& Object) shader.setMat4("model", model); Object.Draw(shader); + + glStencilFunc(GL_ALWAYS, 1, 0xff); + glStencilMask(0xFF); } void DrawToScreen( GLuint& VAO, GLuint& ScreenTexture, Shader& shader) @@ -179,6 +259,7 @@ int main() { Shader shader ("shader.vs", "shader.fs"); Shader outlineShader("shader.vs","outlineshader.fs"); Shader framebufferShader("Framebuffers.vs", "Framebuffers.fs" ); + Shader skyboxShader("skybox.vs", "Cubemap.fs"); Model backpack("Models/backpack.obj"); @@ -223,7 +304,12 @@ int main() { framebuffer.Unbind(); + Cubemap skybox= Cubemap (); + skybox.Bind(); + skybox.loadCubeTextures(faces); + // Create ScreenVAO + #pragma region ScreenVAO GLuint ScreenVAO, VBO; glGenVertexArrays(1, &ScreenVAO); glBindVertexArray(ScreenVAO); @@ -240,7 +326,25 @@ int main() { glBindVertexArray(0); +#pragma endregion ScreenVAO + + + // Create SkyboxVAO + GLuint SkyboxVAO, SkyboxVBO; + glGenVertexArrays(1, &SkyboxVAO); + glBindVertexArray(SkyboxVAO); + glGenBuffers(1, &SkyboxVBO); + glBindBuffer(GL_ARRAY_BUFFER, SkyboxVBO); + + glBufferData(GL_ARRAY_BUFFER, skyboxVertices.size() * sizeof(float), &skyboxVertices[0], GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0,3, GL_FLOAT,GL_FALSE, 3 * sizeof(float),(void*) 0); + + glBindVertexArray(0); + + glEnable(GL_DEPTH_TEST); glEnable(GL_STENCIL_TEST); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); @@ -264,22 +368,23 @@ while(!glfwWindowShouldClose(window)) framebuffer.Bind(); - drawScene_pass(shader, backpack); - //outline_pass(outlineShader, backpack); - - + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + Skybox_pass(SkyboxVAO, skyboxShader, skybox); + + drawScene_pass(shader, backpack); + + outline_pass(outlineShader, backpack); DrawToScreen(ScreenVAO, ColourBuffer->id, framebufferShader); - - // Reset stencil + + // Reset stencil glStencilMask(0xFF); glStencilFunc(GL_ALWAYS, 1, 0xFF); glEnable(GL_DEPTH_TEST); - - - glfwSwapBuffers(window); glfwPollEvents(); }