From 893c3555119689cf689d5e007306bd84ccd480c7 Mon Sep 17 00:00:00 2001 From: Hugo Willaume Date: Wed, 3 Apr 2019 11:44:35 +0900 Subject: [PATCH] Displaying a skybox --- BaseGLProject/BaseGLProject.vcxproj | 6 ++- BaseGLProject/BaseGLProject.vcxproj.filters | 12 +++++ BaseGLProject/MyGLWindow.cpp | 4 +- BaseGLProject/MyGLWindow.h | 4 +- BaseGLProject/SceneContext.h | 1 + BaseGLProject/Skybox.cpp | 58 ++++++++++++++++----- BaseGLProject/Skybox.h | 8 ++- BaseGLProject/skybox.frag | 21 ++++++++ BaseGLProject/skybox.vert | 25 +++++++++ 9 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 BaseGLProject/skybox.frag create mode 100644 BaseGLProject/skybox.vert diff --git a/BaseGLProject/BaseGLProject.vcxproj b/BaseGLProject/BaseGLProject.vcxproj index dd3d73b..0b45579 100644 --- a/BaseGLProject/BaseGLProject.vcxproj +++ b/BaseGLProject/BaseGLProject.vcxproj @@ -1,4 +1,4 @@ - + @@ -157,6 +157,7 @@ + @@ -179,6 +180,7 @@ + @@ -189,6 +191,8 @@ + + diff --git a/BaseGLProject/BaseGLProject.vcxproj.filters b/BaseGLProject/BaseGLProject.vcxproj.filters index 65d1efa..a3dfe79 100644 --- a/BaseGLProject/BaseGLProject.vcxproj.filters +++ b/BaseGLProject/BaseGLProject.vcxproj.filters @@ -66,6 +66,9 @@ Source Files + + Source Files + @@ -125,6 +128,9 @@ Header Files + + Header Files + @@ -178,6 +184,12 @@ Shaders + + Shaders + + + Shaders + diff --git a/BaseGLProject/MyGLWindow.cpp b/BaseGLProject/MyGLWindow.cpp index 637717e..eaf4858 100644 --- a/BaseGLProject/MyGLWindow.cpp +++ b/BaseGLProject/MyGLWindow.cpp @@ -129,6 +129,7 @@ void MyGlWindow::setup() glEnable(GL_DEPTH_BUFFER); glEnable(GL_TEXTURE_2D); + skybox.initialize("Models/Skybox/"); textureSetup(); shaderSetup(); lightSetup(); @@ -160,7 +161,7 @@ void MyGlWindow::setup() //meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"])); - meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"])); + //meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"])); //meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1)); @@ -206,6 +207,7 @@ void MyGlWindow::draw() //_scnctx.adjustSpots(); for (auto it = meshes.begin(); it != meshes.end(); it++) (*it).second->draw(_scnctx); + skybox.draw(_scnctx); } void MyGlWindow::resize(int w, int h) diff --git a/BaseGLProject/MyGLWindow.h b/BaseGLProject/MyGLWindow.h index 9b22d13..30865ff 100644 --- a/BaseGLProject/MyGLWindow.h +++ b/BaseGLProject/MyGLWindow.h @@ -4,6 +4,7 @@ #include "GL/glew.h" #include #include +#include "Skybox.h" #include "Shader.h" #include "ModelView.h" #include "Viewer.h" @@ -22,7 +23,7 @@ public: MyGlWindow(int w, int h); ~MyGlWindow(); - void draw(); + void draw(); void setBgColor(float bgColor[3]); std::map shaders; @@ -38,6 +39,7 @@ private: float _bgColor[3]; + Skybox skybox; GLuint _vaoHandle; GLuint _iboHandle; diff --git a/BaseGLProject/SceneContext.h b/BaseGLProject/SceneContext.h index 399c75a..bf09fd3 100644 --- a/BaseGLProject/SceneContext.h +++ b/BaseGLProject/SceneContext.h @@ -4,6 +4,7 @@ #include #include #include "Texture.h" +#include "Light.h" struct SceneContext { diff --git a/BaseGLProject/Skybox.cpp b/BaseGLProject/Skybox.cpp index c0f1a30..bf6ec74 100644 --- a/BaseGLProject/Skybox.cpp +++ b/BaseGLProject/Skybox.cpp @@ -1,4 +1,5 @@ #include "Skybox.h" +#include "imgui/stb_image.h" Skybox::Skybox() { @@ -57,40 +58,71 @@ Skybox::Skybox() glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), el, GL_STATIC_DRAW); - glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, NULL); - glEnableVertexAttribArray(4); + //glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, NULL); + //glEnableVertexAttribArray(4); + shader.initFromFiles("skybox.vert", "skybox.frag"); +} + +void Skybox::initialize(std::string skybox_dir) +{ glActiveTexture(GL_TEXTURE0); glGenTextures(1, &texID); //set the texID as a member variable glBindTexture(GL_TEXTURE_CUBE_MAP, texID); - const char * suffixes[] = { "left", "right", "top", "down", "back", "front" }; + + 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); + + const char * suffixes[] = { "left", "right", "top", "down", "back", "front"}; GLuint targets[] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - }; + }; - for (int i = 0; i < 6; i++) { - int channel; - int width, height; + for (int i = 0; i < 6; i++) { + int channel, width, height; + unsigned char *image; std::string filename; - filename = std::string("Models/skybox/" + suffixes[i]) + string(".jpg"); - unsigned char * image = SOIL_load_image(filename.c_str(), &width, &height, &channel, SOIL_LOAD_RGB); + filename = std::string(skybox_dir + suffixes[i]) + std::string(".JPG"); + image = stbi_load(filename.c_str(), &width, &height, &channel, 0); glTexImage2D(targets[i], 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glGenerateMipmap(GL_TEXTURE_2D); - SOIL_free_image_data(image); + stbi_image_free(image); } - + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } - Skybox::~Skybox() { + glDeleteBuffers(1, &vector_vbo); + glDeleteBuffers(1, &ibo); + glDeleteVertexArrays(1, &vao); } -void Skybox::draw() +void Skybox::draw(SceneContext &ctx) { + shader.use(); + + glm::mat4x4 mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * glm::mat4(1.0); + glUniformMatrix4fv(shader.addUniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix)); + glUniform1i(shader.addUniform("DrawSkyBox"), GL_TRUE); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_CUBE_MAP, texID); + glUniform1i(shader.addUniform("CubeMapTex"), 0); + glBindVertexArray(vao); + + int size; + glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); + glDrawElements(GL_TRIANGLES, size / sizeof(GLuint), GL_UNSIGNED_INT, 0); + + glBindVertexArray(0); + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + shader.disable(); } diff --git a/BaseGLProject/Skybox.h b/BaseGLProject/Skybox.h index a92f8fc..f4dd5eb 100644 --- a/BaseGLProject/Skybox.h +++ b/BaseGLProject/Skybox.h @@ -8,18 +8,22 @@ #include #include #include +#include #include "Loader.h" +#include "SceneContext.h" class Skybox { private: unsigned int vao; unsigned int vector_vbo; - unsigned int ibo; + unsigned int ibo; GLuint texID; + ShaderProgram shader; public: Skybox(); + void initialize(std::string skybox_dir); ~Skybox(); - void draw(); + void draw(SceneContext &ctx); }; diff --git a/BaseGLProject/skybox.frag b/BaseGLProject/skybox.frag new file mode 100644 index 0000000..2747e0b --- /dev/null +++ b/BaseGLProject/skybox.frag @@ -0,0 +1,21 @@ +#version 440 + +out vec4 FragColors; + +in vec3 reflectDir; + +uniform bool DrawSkyBox; +uniform samplerCube CubeMapTex; + +float rand(vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} + +void main() +{ + vec4 texColor = texture(CubeMapTex, reflectDir); + if (DrawSkyBox == true) + FragColors = texColor; + else + FragColors = vec4(1, 1, rand(reflectDir.xy), 1); +} diff --git a/BaseGLProject/skybox.vert b/BaseGLProject/skybox.vert new file mode 100644 index 0000000..982b1e8 --- /dev/null +++ b/BaseGLProject/skybox.vert @@ -0,0 +1,25 @@ +#version 440 + +layout(location=0) in vec3 coord3d; +layout(location=1) in vec3 v_normal; +layout(location=2) in vec3 v_color; +layout(location=3) in vec2 v_texmap; + +out vec3 reflectDir; + +uniform mat4 mvp; +uniform bool DrawSkyBox; + +void main(void) +{ + + +//For drawing a mesh with reflected skybox +//P = (vec3(ModelMAtrix * vec4(Vertexpos, 1) +// N = normalize(ModelMatrix * VertexNormals); +//V = normalize(WorldCamPos - worldPos); +//ReflectDir = reflect(-worldview, worldNorm); + if (DrawSkyBox == true) + reflectDir = coord3d; + gl_Position = mvp * vec4(coord3d, 1.0f); +}