diff --git a/grk/project/grk-project.vcxproj b/grk/project/grk-project.vcxproj index e58f79e..94f6e28 100644 --- a/grk/project/grk-project.vcxproj +++ b/grk/project/grk-project.vcxproj @@ -48,6 +48,8 @@ + + diff --git a/grk/project/grk-project.vcxproj.filters b/grk/project/grk-project.vcxproj.filters index 778df94..94a14a2 100644 --- a/grk/project/grk-project.vcxproj.filters +++ b/grk/project/grk-project.vcxproj.filters @@ -130,5 +130,11 @@ Shader Files + + Shader Files + + + Shader Files + \ No newline at end of file diff --git a/grk/project/shaders/shader_skybox.frag b/grk/project/shaders/shader_skybox.frag new file mode 100644 index 0000000..2253ffc --- /dev/null +++ b/grk/project/shaders/shader_skybox.frag @@ -0,0 +1,12 @@ +#version 430 core + +uniform samplerCube skybox; + +in vec3 texCoord; + +out vec4 out_color; + +void main() +{ + out_color = texture(skybox,texCoord); +} \ No newline at end of file diff --git a/grk/project/shaders/shader_skybox.vert b/grk/project/shaders/shader_skybox.vert new file mode 100644 index 0000000..90d66bd --- /dev/null +++ b/grk/project/shaders/shader_skybox.vert @@ -0,0 +1,13 @@ +#version 430 core + +layout(location = 0) in vec3 vertexPosition; + +uniform mat4 transformation; + +out vec3 texCoord; + +void main() +{ + texCoord = vertexPosition; + gl_Position = transformation * vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/grk/project/src/Render_Utils.cpp b/grk/project/src/Render_Utils.cpp index d725cd4..b1cc537 100644 --- a/grk/project/src/Render_Utils.cpp +++ b/grk/project/src/Render_Utils.cpp @@ -188,4 +188,17 @@ void Core::drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, gl glUniform3f(glGetUniformLocation(program, "spotlightColor"), spaceship->spotlightColor.x, spaceship->spotlightColor.y, spaceship->spotlightColor.z); glUniform1f(glGetUniformLocation(program, "spotlightPhi"), spaceship->spotlightPhi); Core::DrawContext(context); +} +void Core::drawSkybox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint program) { + Spaceship* spaceship = Spaceship::getInstance(); + glDisable(GL_DEPTH_TEST); + glUseProgram(program); + glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix(); + glm::mat4 transformation = viewProjectionMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation); + + glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); + Core::DrawContext(context); + glEnable(GL_DEPTH_TEST); + } \ No newline at end of file diff --git a/grk/project/src/Render_Utils.h b/grk/project/src/Render_Utils.h index d49c5a8..5c876c6 100644 --- a/grk/project/src/Render_Utils.h +++ b/grk/project/src/Render_Utils.h @@ -69,6 +69,7 @@ namespace Core void DrawVertexArray(const VertexData & data); void DrawContext(RenderContext& context); + void drawSkybox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint program); glm::mat4 createPerspectiveMatrix(); void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program); diff --git a/grk/project/src/Texture.cpp b/grk/project/src/Texture.cpp index 2548b13..0e1a352 100644 --- a/grk/project/src/Texture.cpp +++ b/grk/project/src/Texture.cpp @@ -28,6 +28,43 @@ GLuint Core::LoadTexture( const char * filepath ) return id; } +GLuint Core::LoadCubemap(const std::vector& faces) +{ + GLuint textureID; + glGenTextures(1, &textureID); + glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); + + int width, height; + unsigned char* data; + + for (unsigned int i = 0; i < faces.size(); i++) + { + data = SOIL_load_image(("textures/skybox/" + faces[i]).c_str(), &width, &height, 0, SOIL_LOAD_RGBA); + + if (data) + { + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + SOIL_free_image_data(data); + } + else + { + std::cerr << "Cubemap tex failed to load at path:" << faces[i] << std::endl; + SOIL_free_image_data(data); + glDeleteTextures(1, &textureID); + return 0; + } + } + + 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); + + return textureID; +} + + void Core::SetActiveTexture(GLuint textureID, const char * shaderVariableName, GLuint programID, int textureUnit) { diff --git a/grk/project/src/Texture.h b/grk/project/src/Texture.h index 910228e..b2c9924 100644 --- a/grk/project/src/Texture.h +++ b/grk/project/src/Texture.h @@ -2,10 +2,13 @@ #include "glew.h" #include "freeglut.h" +#include +#include namespace Core { GLuint LoadTexture(const char * filepath); + GLuint LoadCubemap(const std::vector& faces); // textureID - identyfikator tekstury otrzymany z funkcji LoadTexture // shaderVariableName - nazwa zmiennej typu 'sampler2D' w shaderze, z ktora ma zostac powiazana tekstura diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index ea9fefb..4c3a850 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -5,10 +5,11 @@ #include #include #include +#include #include "Shader_Loader.h" #include "Render_Utils.h" -//#include "Texture.h" +#include "texture.h" #include "Box.cpp" #include @@ -29,6 +30,10 @@ namespace models { Core::RenderContext marbleBustContext; Core::RenderContext spaceshipContext; Core::RenderContext sphereContext; + Core::RenderContext cubeContext; +} +namespace texture { + GLuint cubemapTexture; } GLuint depthMapFBO; @@ -38,6 +43,7 @@ GLuint program; GLuint programSun; GLuint programTest; GLuint programTex; +GLuint programCubemap; Core::Shader_Loader shaderLoader; @@ -75,7 +81,6 @@ void renderShadowapSun() { glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, WIDTH, HEIGHT); } - void renderScene(GLFWwindow* window) { glClearColor(0.4f, 0.4f, 0.8f, 1.0f); @@ -84,6 +89,9 @@ void renderScene(GLFWwindow* window) updateDeltaTime(time); renderShadowapSun(); + drawSkybox(models::cubeContext, glm::translate(glm::mat4(), spaceship->cameraPos), texture::cubemapTexture, programCubemap); + + //space lamp glUseProgram(programSun); @@ -165,7 +173,6 @@ void createSuns() { suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(200, 150, -100), glm::vec3(0.03633f, 0.151106, 0.103226f), glm::vec3(0.6f, 0.2f, 0.9f), 3.9)); suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-150, -100, -50), glm::vec3(0.83633f, -0.251106, -0.123226f), glm::vec3(0.7f, 0.5f, 0.8f), 4.1));*/ } - void init(GLFWwindow* window) { glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); @@ -174,15 +181,33 @@ void init(GLFWwindow* window) program = shaderLoader.CreateProgram("shaders/shader_9_1.vert", "shaders/shader_9_1.frag"); programTest = shaderLoader.CreateProgram("shaders/test.vert", "shaders/test.frag"); programSun = shaderLoader.CreateProgram("shaders/shader_8_sun.vert", "shaders/shader_8_sun.frag"); - - //loadModelToContext("./models/sphere.obj", sphereContext); - //loadModelToContext("./models/spaceship.obj", spaceship.context); - - + programCubemap = shaderLoader.CreateProgram("shaders/shader_skybox.vert", "shaders/shader_skybox.frag"); loadModelToContext("./models/marbleBust.obj", models::marbleBustContext); loadModelToContext("./models/spaceship.obj", models::spaceshipContext); loadModelToContext("./models/sphere.obj", models::sphereContext); + loadModelToContext("./models/cube.obj", models::cubeContext); + + /*std::vector cubeFaces = { + "space_rt.png", + "space_lf.png", + "space_up.png", + "space_dn.png", + "space_bk.png", + "space_ft.png" + };*/ + + std::vector cubeFaces = { + "bkg2_right1.png", + "bkg2_left2.png", + "bkg2_top3.png", + "bkg2_bottom4.png", + "bkg2_front5.png", + "bkg2_back6.png" + }; + + texture::cubemapTexture = Core::LoadCubemap(cubeFaces); + createSuns(); }