normal mapping - sth wrong with the ship

This commit is contained in:
Pawel Felcyn 2024-02-04 18:06:58 +01:00
parent 3b506455c6
commit 2c13cde602
6 changed files with 21 additions and 13 deletions

View File

@ -38,9 +38,9 @@ public:
return; return;
} }
float rotationAngle = glm::radians(time * rotationSpeed); float rotationAngle = 0.f; //glm::radians(time * rotationSpeed);
positionMatrix = center->getPositionMatrix() * glm::eulerAngleY(time * rotationSpeed) * glm::translate(glm::vec3(distanceFromCenter, 0, 0)); positionMatrix = center->getPositionMatrix() /** glm::eulerAngleY(time * rotationSpeed)*/ * glm::translate(glm::vec3(distanceFromCenter, 0, 0));
glm::mat4 modelMatrix = positionMatrix * glm::scale(glm::vec3(scale)); 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);
} }
}; };

View File

@ -9,6 +9,7 @@ uniform vec3 cameraPos;
uniform sampler2D textureSampler; uniform sampler2D textureSampler;
uniform sampler2D normalSampler;
uniform vec3 lightPositions[100]; uniform vec3 lightPositions[100];
uniform vec3 lightColors[100]; uniform vec3 lightColors[100];
@ -26,6 +27,7 @@ uniform float exposition;
in vec3 vecNormal; in vec3 vecNormal;
in vec3 worldPos; in vec3 worldPos;
in vec2 TexCoords; in vec2 TexCoords;
in mat3 TBN;
out vec4 outColor; out vec4 outColor;
@ -100,8 +102,8 @@ void main()
vec4 texColor = texture(textureSampler, TexCoords); 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 viewDir = normalize(cameraPos-worldPos);
vec3 lightDirs[4]; vec3 lightDirs[4];

View File

@ -20,6 +20,7 @@ uniform vec3 cameraPos;
out vec3 viewDirTS; out vec3 viewDirTS;
out vec3 lightDirTS[10]; out vec3 lightDirTS[10];
out vec3 spotlightDirTS; out vec3 spotlightDirTS;
out mat3 TBN;
void main() void main()
{ {
@ -31,7 +32,7 @@ void main()
vec3 w_tangent = normalize(mat3(modelMatrix)*vertexTangent); vec3 w_tangent = normalize(mat3(modelMatrix)*vertexTangent);
vec3 w_bitangent = normalize(mat3(modelMatrix)*vertexBitangent); 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); vec3 V = normalize(cameraPos-worldPos);
viewDirTS = TBN*V; viewDirTS = TBN*V;

View File

@ -190,7 +190,7 @@ void Core::drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, gl
Core::DrawContext(context); 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(); Spaceship* spaceship = Spaceship::getInstance();
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix(); glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix; 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, "roughness"), roughness);
glUniform1f(glGetUniformLocation(program, "metallic"), metallic); glUniform1f(glGetUniformLocation(program, "metallic"), metallic);
;
//glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
Core::SetActiveTexture(textureID, "textureSampler", program, 0); Core::SetActiveTexture(textureID, "textureSampler", program, 0);
glUniform1i(glGetUniformLocation(program, "textureSampler"), 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); glUniform3f(glGetUniformLocation(program, "cameraPos"), spaceship->cameraPos.x, spaceship->cameraPos.y, spaceship->cameraPos.z);

View File

@ -74,5 +74,5 @@ namespace Core
glm::mat4 createPerspectiveMatrix(); glm::mat4 createPerspectiveMatrix();
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program); 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 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);
} }

View File

@ -39,6 +39,7 @@ namespace models {
namespace texture { namespace texture {
GLuint cubemapTexture; GLuint cubemapTexture;
GLuint spaceshipTexture; GLuint spaceshipTexture;
GLuint spaceshipNormal;
GLuint spriteTexture; GLuint spriteTexture;
GLuint earthTexture; GLuint earthTexture;
} }
@ -196,14 +197,15 @@ void renderScene(GLFWwindow* window)
drawObjectPBRTexture(models::spaceshipContext, drawObjectPBRTexture(models::spaceshipContext,
spaceship->calculateModelMatrix(), spaceship->calculateModelMatrix(),
texture::spaceshipTexture, texture::spaceshipTexture,
texture::spaceshipNormal,
spaceship->roughness, spaceship->metallic, programTex spaceship->roughness, spaceship->metallic, programTex
); );
//Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0); //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)) * //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); //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); //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); //glUseProgram(programSprite);
//for (const auto& enemy : enemies) { //for (const auto& enemy : enemies) {
@ -345,6 +347,7 @@ void init(GLFWwindow* window)
loadPlanetsTextures(); loadPlanetsTextures();
texture::cubemapTexture = Core::LoadCubemap(cubeFaces); texture::cubemapTexture = Core::LoadCubemap(cubeFaces);
texture::spaceshipTexture = Core::LoadTexture("./textures/spaceship/Material.001_Base_color.jpg"); 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"); texture::earthTexture = Core::LoadTexture("./textures/planets/8k_earth_daymap.jpg");
spaceship->createParticles(); spaceship->createParticles();