ADD: Diffuse Lightning

This commit is contained in:
sebastian 2026-02-07 19:44:07 +01:00
parent 0163373a9e
commit 9991a43b4d
14 changed files with 98 additions and 17 deletions

View File

@ -53,7 +53,9 @@ add_executable(Dicewars_Siedler src/main.cpp
src/engine/platform/glfw/InputManager.cpp src/engine/platform/glfw/InputManager.cpp
src/engine/platform/glfw/InputManager.h src/engine/platform/glfw/InputManager.h
src/engine/renderer/loader/OBJLoader.cpp src/engine/renderer/loader/OBJLoader.cpp
src/engine/renderer/loader/OBJLoader.h) src/engine/renderer/loader/OBJLoader.h
src/engine/layer/entities/Light.cpp
src/engine/layer/entities/Light.h)
target_include_directories(Dicewars_Siedler PRIVATE target_include_directories(Dicewars_Siedler PRIVATE
lib/glfw/include lib/glfw/include

View File

@ -1,11 +1,22 @@
#version 400 core #version 400 core
in vec2 pass_textureCoords; in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
out vec4 outColor; out vec4 outColor;
uniform sampler2D textureSampler; uniform sampler2D textureSampler;
uniform vec3 lightColor;
void main(void) { void main(void) {
outColor = texture(textureSampler, pass_textureCoords);
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitToLightDir = normalize(toLightVector);
float cosTheta = dot(unitNormal, unitToLightDir);
float brightness = max(cosTheta, 0.0);
vec3 diffuse = brightness * lightColor;
outColor = vec4(diffuse, 1.0f) * texture(textureSampler, pass_textureCoords);
} }

View File

@ -2,15 +2,22 @@
in vec3 position; in vec3 position;
in vec2 textureCoords; in vec2 textureCoords;
in vec3 normal;
out vec2 pass_textureCoords; out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
uniform mat4 transformationMatrix; uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix; uniform mat4 projectionMatrix;
uniform mat4 viewMatrix; uniform mat4 viewMatrix;
uniform vec3 lightPosition;
void main() void main() {
{ vec4 worldPosition = transformationMatrix * vec4(position, 1.0f);
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0f); gl_Position = projectionMatrix * viewMatrix * worldPosition;
pass_textureCoords = textureCoords; pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal, 0.0f)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
} }

View File

@ -9,6 +9,7 @@
#include "../renderer/loader/OBJLoader.h" #include "../renderer/loader/OBJLoader.h"
#include "../renderer/model/TexturedModel.h" #include "../renderer/model/TexturedModel.h"
#include "../renderer/textures/ModelTexture.h" #include "../renderer/textures/ModelTexture.h"
#include "entities/Light.h"
GameLayer::GameLayer() :texturedModel(0,0) //Platzhalter, echtes Model kommt in onAttach GameLayer::GameLayer() :texturedModel(0,0) //Platzhalter, echtes Model kommt in onAttach
{ {
@ -35,9 +36,10 @@ void GameLayer::onAttach()
1,0 //v3 1,0 //v3
}; };
texturedModel = *OBJLoader::loadModel("assets/stall/stall.obj", "assets/stall/stallTexture.png", loader); texturedModel = *OBJLoader::loadModel("assets/dragon/dragon.obj", "assets/dragon/dragon.png", loader);
entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-50), 0,0,0, 1.f)); entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-25), 0,0,0, 1.f));
camera = std::make_unique<Camera>(); camera = std::make_unique<Camera>();
light = std::make_unique<Light>(glm::vec3(0,0,-20), glm::vec3(1,1,1));
} }
void GameLayer::onDetach() void GameLayer::onDetach()
@ -54,8 +56,8 @@ void GameLayer::onUpdate()
if (InputManager::isKeyPressed(GLFW_KEY_D)) moveDir.x += 1.0f; if (InputManager::isKeyPressed(GLFW_KEY_D)) moveDir.x += 1.0f;
entity->increaseRotation(0,1,0); entity->increaseRotation(0,1,0);
camera->move(moveDir, 0.02f); camera->move(moveDir, 0.5f);
renderer.renderFrame(*entity, *camera); renderer.renderFrame(*entity, *camera, *light);
} }

View File

@ -25,6 +25,7 @@ private:
TexturedModel texturedModel; TexturedModel texturedModel;
std::unique_ptr<Entity> entity; std::unique_ptr<Entity> entity;
std::unique_ptr<Camera> camera; std::unique_ptr<Camera> camera;
std::unique_ptr<Light> light;
Renderer renderer; Renderer renderer;
}; };

View File

@ -0,0 +1,5 @@
//
// Created by sebastian on 07.02.26.
//
#include "Light.h"

View File

@ -0,0 +1,26 @@
//
// Created by sebastian on 07.02.26.
//
#ifndef LIGHT_H
#define LIGHT_H
#include "glm/vec3.hpp"
class Light {
private:
glm::vec3 position;
glm::vec3 color;
public:
Light(glm::vec3 position, glm::vec3 color) : position(position), color(color) {};
[[nodiscard]] glm::vec3 getPosition() const {return position;}
[[nodiscard]] glm::vec3 getColor() const {return color;}
void setPosition(glm::vec3 position) {this->position = position;}
void setColor(glm::vec3 color) {this->color = color;}
};
#endif //LIGHT_H

View File

@ -5,24 +5,28 @@
#include "Renderer.h" #include "Renderer.h"
#include "../core/Application.h" #include "../core/Application.h"
#include "../layer/entities/Light.h"
#include "../toolbox/MathUtils.h" #include "../toolbox/MathUtils.h"
#include "glad/glad.h" #include "glad/glad.h"
#include "glm/ext/matrix_clip_space.hpp" #include "glm/ext/matrix_clip_space.hpp"
#include "model/TexturedModel.h" #include "model/TexturedModel.h"
void Renderer::prepare(const Camera& camera) {
void Renderer::prepare(const Camera& camera, const Light& light) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera); const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
staticShader.start(); staticShader.start();
staticShader.loadLight(light.getPosition(), light.getColor());
staticShader.loadViewMatrix(viewMatrix); staticShader.loadViewMatrix(viewMatrix);
staticShader.stop(); staticShader.stop();
} }
void Renderer::renderFrame(const Entity &entity, const Camera& camera) { void Renderer::renderFrame(const Entity &entity, const Camera& camera, const Light& light) {
prepare(camera); prepare(camera, light);
staticShader.start(); staticShader.start();
renderRawModel(entity); renderRawModel(entity);
staticShader.stop(); staticShader.stop();
@ -34,6 +38,7 @@ void Renderer::renderRawModel(const Entity& entity) {
glBindVertexArray(model->vaoID); glBindVertexArray(model->vaoID);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale()); glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
staticShader.loadTransformationMatrix(transformationMatrix); staticShader.loadTransformationMatrix(transformationMatrix);
@ -43,6 +48,7 @@ void Renderer::renderRawModel(const Entity& entity) {
glDrawElements(GL_TRIANGLES , model->vertexCount, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES , model->vertexCount, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1); glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0); glBindVertexArray(0);
} }

View File

@ -10,6 +10,7 @@
#include "shaders/StaticShader.h" #include "shaders/StaticShader.h"
class Light;
class Camera; class Camera;
class Renderer { class Renderer {
@ -19,8 +20,8 @@ public:
staticShader.loadProjectionMatrix(projectionMatrix); staticShader.loadProjectionMatrix(projectionMatrix);
staticShader.stop(); staticShader.stop();
}; };
void prepare(const Camera& camera); void prepare(const ::Camera &camera, const Light& light);
void renderFrame(const ::Entity &entity, const Camera& camera); void renderFrame(const ::Entity &entity, const ::Camera &camera, const Light& light);
private: private:
void renderRawModel(const Entity &entity); void renderRawModel(const Entity &entity);
StaticShader staticShader; StaticShader staticShader;

View File

@ -7,10 +7,11 @@
#include "Texture2D.h" #include "Texture2D.h"
#include "TextureLoader.h" #include "TextureLoader.h"
RawModel Loader::loadToVAO(const std::vector<float> &vertices, const std::vector<float> &textureCoords, const std::vector<int> &indices) { RawModel Loader::loadToVAO(const std::vector<float> &vertices, const std::vector<float>& normals, const std::vector<float> &textureCoords, const std::vector<int> &indices) {
GLuint vaoID = createVAO(); GLuint vaoID = createVAO();
storeDataInAttributeList(0, 3, vertices); storeDataInAttributeList(0, 3, vertices);
storeDataInAttributeList(1, 2, textureCoords); storeDataInAttributeList(1, 2, textureCoords);
storeDataInAttributeList(2, 3, normals);
bindIndicesBuffer(indices); bindIndicesBuffer(indices);
unbindVAO(); unbindVAO();
return {vaoID, static_cast<int>(indices.size())}; return {vaoID, static_cast<int>(indices.size())};

View File

@ -15,7 +15,8 @@
class Loader class Loader
{ {
public: public:
RawModel loadToVAO(const std::vector<float>& vertices, const std::vector<float>& textureCoords, const std::vector<int>& indices); RawModel loadToVAO(const std::vector<float> &vertices, const std::vector<float> &normals, const std::vector<float> &textureCoords, const
std::vector<int> &indices);
~Loader(); ~Loader();
void cleanUp(); void cleanUp();

View File

@ -5,6 +5,8 @@
#include "OBJLoader.h" #include "OBJLoader.h"
#define TINYOBJLOADER_IMPLEMENTATION #define TINYOBJLOADER_IMPLEMENTATION
#include <complex>
#include "tiny_obj_loader.h" #include "tiny_obj_loader.h"
std::shared_ptr<TexturedModel> OBJLoader::loadModel(const std::string &modelPath, const std::string& texturePath, Loader &loader) { std::shared_ptr<TexturedModel> OBJLoader::loadModel(const std::string &modelPath, const std::string& texturePath, Loader &loader) {
@ -21,6 +23,7 @@ std::shared_ptr<TexturedModel> OBJLoader::loadModel(const std::string &modelPath
} }
std::vector<float> vertices; std::vector<float> vertices;
std::vector<float> normals;
std::vector<float> uvs; std::vector<float> uvs;
std::vector<int> indices; std::vector<int> indices;
@ -41,13 +44,17 @@ std::shared_ptr<TexturedModel> OBJLoader::loadModel(const std::string &modelPath
uvs.push_back(0.0f); uvs.push_back(0.0f);
} }
normals.push_back(attrib.normals[3*index.normal_index + 0]);
normals.push_back(attrib.normals[3*index.normal_index + 1]);
normals.push_back(attrib.normals[3*index.normal_index + 2]);
// Index // Index
indices.push_back(indexOffset); indices.push_back(indexOffset);
indexOffset++; indexOffset++;
} }
} }
RawModel rawModel = loader.loadToVAO(vertices, uvs, indices); RawModel rawModel = loader.loadToVAO(vertices, normals, uvs, indices);
ModelTexture modelTexture = loader.loadTextureFromFile(texturePath); ModelTexture modelTexture = loader.loadTextureFromFile(texturePath);
TexturedModel texturedModel = TexturedModel(std::make_shared<RawModel>(rawModel), std::make_shared<ModelTexture>(modelTexture)); TexturedModel texturedModel = TexturedModel(std::make_shared<RawModel>(rawModel), std::make_shared<ModelTexture>(modelTexture));
return std::make_shared<TexturedModel>(texturedModel); return std::make_shared<TexturedModel>(texturedModel);

View File

@ -21,15 +21,23 @@ void StaticShader::loadViewMatrix(glm::mat4 matrix) {
loadMatrix(location_viewMatrix, matrix); loadMatrix(location_viewMatrix, matrix);
} }
void StaticShader::loadLight(glm::vec3 position, glm::vec3 color) {
loadVector(location_lightPosition, position);
loadVector(location_lightColor, color);
}
void StaticShader::bindAttributes() const { void StaticShader::bindAttributes() const {
bindAttribute(0, "position"); bindAttribute(0, "position");
bindAttribute(1, "textureCoords"); bindAttribute(1, "textureCoords");
bindAttribute(2, "normal");
} }
void StaticShader::getAllUniformLocations() { void StaticShader::getAllUniformLocations() {
location_transformationMatrix = getUniformLocation("transformationMatrix"); location_transformationMatrix = getUniformLocation("transformationMatrix");
location_projectionMatrix = getUniformLocation("projectionMatrix"); location_projectionMatrix = getUniformLocation("projectionMatrix");
location_viewMatrix = getUniformLocation("viewMatrix"); location_viewMatrix = getUniformLocation("viewMatrix");
location_lightPosition = getUniformLocation("lightPosition");
location_lightColor = getUniformLocation("lightColor");
} }

View File

@ -14,6 +14,7 @@ public:
void loadTransformationMatrix(glm::mat4 matrix); void loadTransformationMatrix(glm::mat4 matrix);
void loadProjectionMatrix(glm::mat4 matrix); void loadProjectionMatrix(glm::mat4 matrix);
void loadViewMatrix(glm::mat4 matrix); void loadViewMatrix(glm::mat4 matrix);
void loadLight(glm::vec3 position, glm::vec3 color);
private: private:
inline static const std::string VERTEX_FILE = "assets/shaders/vertexShader.glsl"; inline static const std::string VERTEX_FILE = "assets/shaders/vertexShader.glsl";
inline static const std::string FRAGMENT_FILE = "assets/shaders/fragmentShader.glsl"; inline static const std::string FRAGMENT_FILE = "assets/shaders/fragmentShader.glsl";
@ -21,6 +22,8 @@ private:
int location_transformationMatrix; int location_transformationMatrix;
int location_projectionMatrix; int location_projectionMatrix;
int location_viewMatrix; int location_viewMatrix;
int location_lightPosition;
int location_lightColor;
protected: protected:
void bindAttributes() const override; void bindAttributes() const override;