Fix lighting: Normalmatrix was never applied

This commit is contained in:
4VRDriver
2020-09-17 12:39:59 +02:00
parent 56190d3cb6
commit 065ec8d7ae
10 changed files with 45 additions and 31 deletions

View File

@@ -5,7 +5,7 @@ project(Fall-Fever)
if(WIN32)
set(GLFW3_ROOT "${PROJECT_SOURCE_DIR}/glfw")
include_directories(${PROJECT_SOURCE_DIR}/glfw/include)
endif(WIN32)
endif()
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
@@ -23,6 +23,6 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
if(!WIN32)
if(NOT WIN32)
add_subdirectory(${PROJECT_SOURCE_DIR}/tools)
endif(!WIN32)
endif()

View File

@@ -4,7 +4,7 @@ Size=400,400
Collapsed=0
[Window][Object Modifier]
Pos=60,60
Pos=182,52
Size=925,127
Collapsed=0

View File

@@ -7,4 +7,4 @@ target_include_directories(glad PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
if(UNIX)
target_link_libraries(glad dl)
endif(UNIX)
endif()

View File

@@ -13,6 +13,8 @@ struct Material {
sampler2D texture_specular1;
sampler2D texture_normal0;
sampler2D texture_normal1;
sampler2D texture_height0;
sampler2D texture_height1;
sampler2D texture_gloss0;
sampler2D texture_gloss1;
float shininess;
@@ -73,7 +75,7 @@ void main() {
vec3 fragmentColor = vec3(0.0f);
vec3 normal = normalize(v_normal);
vec3 normal = normalize(u_normalMatrix * v_normal);
vec3 viewDir = normalize(u_viewPosition - v_fragmentPosition);
fragmentColor += directionalLightContribution(u_directionalLight, normal, viewDir);

View File

@@ -4,13 +4,8 @@ layout(location = 0) in vec3 a_position;
layout(location = 1) in vec2 a_texCoord;
layout(location = 2) in vec4 a_color;
out vec4 v_color;
out vec2 v_texCoord;
uniform mat4 u_modelViewProjMatrix;
void main() {
gl_Position = u_modelViewProjMatrix * vec4(a_position, 1.0f);
v_texCoord = a_texCoord;
v_color = a_color;
}

View File

@@ -82,29 +82,27 @@ void Controller::run() {
//Model model_plant("res/models/plant.ffo");
Model model_container("res/models/container.ffo");
Model model_cube("res/models/cube.ffo");
//Model model_dragon("res/models/dragon.ffo");
Model model_hut("res/models/hut.ffo");
Model model_dragon("res/models/dragon.ffo");
//Model model_hut("res/models/hut.ffo");
//Model model_sphere("res/models/sphere.ffo");
//Entity backpack(&model_backpack, &shaderProgram);
//Entity sphere(&model_sphere, &shaderProgram);
//Entity container(&model_container, &shaderProgram);
Entity hut(&model_hut, &shaderProgram);
//Entity dragon(&model_dragon, &shaderProgram);
//Entity hut(&model_hut, &shaderProgram);
Entity dragon(&model_dragon, &shaderProgram);
//Entity plant(&model_plant, &shaderProgram);
Entity lightSource(&model_cube, &lightProgram);
lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f));
lightSource.setScale(0.2f);
//plant.setScale(5.0f);
//dragon.setScale(0.2f);
dragon.setScale(0.2f);
World world(&shaderProgram);
world.addEntity(hut);
world.addEntity(dragon);
world.addEntity(lightSource);
world.updatePointLight(0, true, lightSource.getPosition(), glm::vec3(1.0f));
camera->translate(glm::vec3(0.0f, 0.0f, 7.5f));
// This is the game loop
@@ -114,6 +112,11 @@ void Controller::run() {
// Update game
// ...
//world.getEntities()->operator[](0).rotate(glm::vec3(0.f,1.0f,0.f), deltaTime*0.2);
float radius = 4.0;
glm::vec3 newPos = glm::vec3(cos(glfwGetTime()*0.2), 0.f, sin(glfwGetTime()*0.2)) * radius;
world.getEntities()->operator[](1).setPosition(newPos);
world.updatePointLight(0, true, world.getEntities()->operator[](1).getPosition(), glm::vec3(1.0f));
// Render and buffer swap
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -194,6 +197,7 @@ void Controller::renderImGui(std::vector<Entity> *entites) {
static float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
entites->operator[](0).setPosition(glm::vec3(translation[0], 0.0f, translation[1]));
entites->operator[](0).setRotation(glm::vec3(0.f,1.0f,0.f) * rotation);
//entity->setRotation()
// color picker

View File

@@ -19,7 +19,8 @@ void Entity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) {
shaderProgram->setUniform("u_modelMatrix", modelMatrix);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(modelMatrix)));
glm::mat3 normalMatrix = glm::mat3(modelMatrix);
normalMatrix = glm::transpose(glm::inverse(normalMatrix));
shaderProgram->setUniform("u_normalMatrix", normalMatrix);
shaderProgram->setUniform("u_viewPosition", viewPosition);
@@ -37,11 +38,7 @@ void Entity::translate(glm::vec3 vector) {
}
void Entity::rotate(glm::vec3 axis, float radians) {
glm::mat4 rotationMatrix(1.0f);
rotationMatrix = glm::rotate(rotationMatrix, radians, axis);
// Rotate orientation vector
orientation = glm::normalize(glm::vec3(rotationMatrix * glm::vec4(orientation, 1.0)));
orientation += axis * radians;
updateModelMatrix();
}
@@ -50,6 +47,11 @@ void Entity::setPosition(glm::vec3 position) {
updateModelMatrix();
}
void Entity::setRotation(glm::vec3 orientation) {
this->orientation = orientation;
updateModelMatrix();
}
void Entity::setScale(float scaleFactor) {
modelScale = scaleFactor;
updateModelMatrix();
@@ -66,8 +68,9 @@ void Entity::updateModelMatrix() {
newModelMatrix = glm::translate(newModelMatrix, position);
// Rotate
glm::vec3 const up(0.0f, 0.0f, 1.0f);
//newModelMatrix = glm::rotate(newModelMatrix, , )
newModelMatrix = glm::rotate(newModelMatrix, orientation.x, glm::vec3(1.f, 0.f, 0.f));
newModelMatrix = glm::rotate(newModelMatrix, orientation.y, glm::vec3(0.f, 1.f, 0.f));
newModelMatrix = glm::rotate(newModelMatrix, orientation.z, glm::vec3(0.f, 0.f, 1.f));
// Scale
newModelMatrix = glm::scale(newModelMatrix, glm::vec3(modelScale, modelScale, modelScale));

View File

@@ -17,7 +17,7 @@ public:
void rotate(glm::vec3 axis, float radians);
void setPosition(glm::vec3 position);
void setOrientiation(glm::vec3 orientation);
void setRotation(glm::vec3 orientation);
void setScale(float scaleFactor);
void setId(uint32_t id) { this->id = id; }

View File

@@ -54,11 +54,14 @@ void Texture::bind(uint8_t textureUnit, ShaderProgram* shaderProgram, uint8_t te
case texture_specular:
uniformName += "specular" + std::to_string(textureTypeNum);
break;
case texture_normal:
uniformName += "normal" + std::to_string(textureTypeNum);
break;
case texture_height:
uniformName += "height" + std::to_string(textureTypeNum);
break;
case texture_normal:
uniformName += "normal" + std::to_string(textureTypeNum);
case texture_gloss:
uniformName += "gloss" + std::to_string(textureTypeNum);
break;
}

View File

@@ -5,7 +5,14 @@
#define INIT_WINDOW_WIDTH 960
#define INIT_WINDOW_HEIGHT 720
enum textureType{texture_diffuse, texture_specular, texture_normal, texture_height, TEXTURE_TYPE_NUM_ITEMS};
enum textureType{
texture_diffuse,
texture_specular,
texture_normal,
texture_height,
texture_gloss,
TEXTURE_TYPE_NUM_ITEMS
};
struct Vertex {
// Postition