From 2c13cde60225df00deb6c579bf7c310354a43433 Mon Sep 17 00:00:00 2001 From: Pawel Felcyn Date: Sun, 4 Feb 2024 18:06:58 +0100 Subject: [PATCH] normal mapping - sth wrong with the ship --- grk/project/Planet.h | 6 +++--- grk/project/shaders/shader_tex.frag | 6 ++++-- grk/project/shaders/shader_tex.vert | 3 ++- grk/project/src/Render_Utils.cpp | 8 +++++--- grk/project/src/Render_Utils.h | 2 +- grk/project/src/ex_9_1.hpp | 9 ++++++--- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/grk/project/Planet.h b/grk/project/Planet.h index 07cf131..3e5e1ec 100644 --- a/grk/project/Planet.h +++ b/grk/project/Planet.h @@ -38,9 +38,9 @@ public: return; } - float rotationAngle = glm::radians(time * rotationSpeed); - positionMatrix = center->getPositionMatrix() * glm::eulerAngleY(time * rotationSpeed) * glm::translate(glm::vec3(distanceFromCenter, 0, 0)); + float rotationAngle = 0.f; //glm::radians(time * rotationSpeed); + positionMatrix = center->getPositionMatrix() /** glm::eulerAngleY(time * rotationSpeed)*/ * glm::translate(glm::vec3(distanceFromCenter, 0, 0)); glm::mat4 modelMatrix = positionMatrix * glm::scale(glm::vec3(scale)); - Core::drawObjectPBRTexture(sphereContext, modelMatrix, textureID, 0.7, 0.0, program); + Core::drawObjectPBRTexture(sphereContext, modelMatrix, textureID, normalMapID, 0.7, 0.0, program); } }; \ No newline at end of file diff --git a/grk/project/shaders/shader_tex.frag b/grk/project/shaders/shader_tex.frag index b69e463..682f124 100644 --- a/grk/project/shaders/shader_tex.frag +++ b/grk/project/shaders/shader_tex.frag @@ -9,6 +9,7 @@ uniform vec3 cameraPos; uniform sampler2D textureSampler; +uniform sampler2D normalSampler; uniform vec3 lightPositions[100]; uniform vec3 lightColors[100]; @@ -26,6 +27,7 @@ uniform float exposition; in vec3 vecNormal; in vec3 worldPos; in vec2 TexCoords; +in mat3 TBN; out vec4 outColor; @@ -100,8 +102,8 @@ void main() vec4 texColor = texture(textureSampler, TexCoords); - vec3 normal = normalize(vecNormal); - + vec3 normal = normalize(texture(normalSampler, TexCoords).xyz * TBN); + //vec3 normal = normalize(vecNormal); vec3 viewDir = normalize(cameraPos-worldPos); vec3 lightDirs[4]; diff --git a/grk/project/shaders/shader_tex.vert b/grk/project/shaders/shader_tex.vert index 429816f..4cd0898 100644 --- a/grk/project/shaders/shader_tex.vert +++ b/grk/project/shaders/shader_tex.vert @@ -20,6 +20,7 @@ uniform vec3 cameraPos; out vec3 viewDirTS; out vec3 lightDirTS[10]; out vec3 spotlightDirTS; +out mat3 TBN; void main() { @@ -31,7 +32,7 @@ void main() vec3 w_tangent = normalize(mat3(modelMatrix)*vertexTangent); vec3 w_bitangent = normalize(mat3(modelMatrix)*vertexBitangent); - mat3 TBN = transpose(mat3(w_tangent, w_bitangent, vecNormal)); + TBN = transpose(mat3(w_tangent, w_bitangent, vecNormal)); vec3 V = normalize(cameraPos-worldPos); viewDirTS = TBN*V; diff --git a/grk/project/src/Render_Utils.cpp b/grk/project/src/Render_Utils.cpp index 2cef9ad..d37c2d2 100644 --- a/grk/project/src/Render_Utils.cpp +++ b/grk/project/src/Render_Utils.cpp @@ -190,7 +190,7 @@ void Core::drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, gl Core::DrawContext(context); } -void Core::drawObjectPBRTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, float roughness, float metallic, GLuint program) { +void Core::drawObjectPBRTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint normalID, float roughness, float metallic, GLuint program) { Spaceship* spaceship = Spaceship::getInstance(); glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix(); glm::mat4 transformation = viewProjectionMatrix * modelMatrix; @@ -201,10 +201,12 @@ void Core::drawObjectPBRTexture(Core::RenderContext& context, glm::mat4 modelMat glUniform1f(glGetUniformLocation(program, "roughness"), roughness); glUniform1f(glGetUniformLocation(program, "metallic"), metallic); - ; - //glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z); + Core::SetActiveTexture(textureID, "textureSampler", program, 0); glUniform1i(glGetUniformLocation(program, "textureSampler"), 0); + + Core::SetActiveTexture(normalID, "normalSampler", program, 1); + glUniform1i(glGetUniformLocation(program, "normalSampler"), 1); glUniform3f(glGetUniformLocation(program, "cameraPos"), spaceship->cameraPos.x, spaceship->cameraPos.y, spaceship->cameraPos.z); diff --git a/grk/project/src/Render_Utils.h b/grk/project/src/Render_Utils.h index 9302b72..c032cf6 100644 --- a/grk/project/src/Render_Utils.h +++ b/grk/project/src/Render_Utils.h @@ -74,5 +74,5 @@ namespace Core glm::mat4 createPerspectiveMatrix(); void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program); void loadModelToContext(std::string path, Core::RenderContext& context); - void drawObjectPBRTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, float roughness, float metallic, GLuint program); + void drawObjectPBRTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint normalID, float roughness, float metallic, GLuint program); } \ No newline at end of file diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index 346e6a9..a0a4e74 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -39,6 +39,7 @@ namespace models { namespace texture { GLuint cubemapTexture; GLuint spaceshipTexture; + GLuint spaceshipNormal; GLuint spriteTexture; GLuint earthTexture; } @@ -196,14 +197,15 @@ void renderScene(GLFWwindow* window) drawObjectPBRTexture(models::spaceshipContext, spaceship->calculateModelMatrix(), texture::spaceshipTexture, + texture::spaceshipNormal, spaceship->roughness, spaceship->metallic, programTex ); //Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); - drawObjectPBRTexture(models::sphereContext, glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(1.0f)), texture::earthTexture, 0.3, 0.0, programTex); + //drawObjectPBRTexture(models::sphereContext, glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(1.0f)), texture::earthTexture, 0.3, 0.0, programTex); //glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * - Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); - drawObjectPBRTexture(models::sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), texture::earthTexture, 0.3, 0.0, programTex); + //Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); + //drawObjectPBRTexture(models::sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), texture::earthTexture, 0.3, 0.0, programTex); //glUseProgram(programSprite); //for (const auto& enemy : enemies) { @@ -345,6 +347,7 @@ void init(GLFWwindow* window) loadPlanetsTextures(); texture::cubemapTexture = Core::LoadCubemap(cubeFaces); texture::spaceshipTexture = Core::LoadTexture("./textures/spaceship/Material.001_Base_color.jpg"); + texture::spaceshipNormal = Core::LoadTexture("./textures/spaceship/Material.001_Normal_DirectX.jpg"); texture::earthTexture = Core::LoadTexture("./textures/planets/8k_earth_daymap.jpg"); spaceship->createParticles();