ADD: Diffuse Lightning
This commit is contained in:
parent
0163373a9e
commit
9991a43b4d
@ -53,7 +53,9 @@ add_executable(Dicewars_Siedler src/main.cpp
|
||||
src/engine/platform/glfw/InputManager.cpp
|
||||
src/engine/platform/glfw/InputManager.h
|
||||
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
|
||||
lib/glfw/include
|
||||
|
||||
@ -1,11 +1,22 @@
|
||||
#version 400 core
|
||||
|
||||
in vec2 pass_textureCoords;
|
||||
in vec3 surfaceNormal;
|
||||
in vec3 toLightVector;
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
uniform sampler2D textureSampler;
|
||||
uniform vec3 lightColor;
|
||||
|
||||
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);
|
||||
}
|
||||
@ -2,15 +2,22 @@
|
||||
|
||||
in vec3 position;
|
||||
in vec2 textureCoords;
|
||||
in vec3 normal;
|
||||
|
||||
out vec2 pass_textureCoords;
|
||||
out vec3 surfaceNormal;
|
||||
out vec3 toLightVector;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform vec3 lightPosition;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0f);
|
||||
void main() {
|
||||
vec4 worldPosition = transformationMatrix * vec4(position, 1.0f);
|
||||
gl_Position = projectionMatrix * viewMatrix * worldPosition;
|
||||
pass_textureCoords = textureCoords;
|
||||
|
||||
surfaceNormal = (transformationMatrix * vec4(normal, 0.0f)).xyz;
|
||||
toLightVector = lightPosition - worldPosition.xyz;
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
#include "../renderer/loader/OBJLoader.h"
|
||||
#include "../renderer/model/TexturedModel.h"
|
||||
#include "../renderer/textures/ModelTexture.h"
|
||||
#include "entities/Light.h"
|
||||
|
||||
GameLayer::GameLayer() :texturedModel(0,0) //Platzhalter, echtes Model kommt in onAttach
|
||||
{
|
||||
@ -35,9 +36,10 @@ void GameLayer::onAttach()
|
||||
1,0 //v3
|
||||
};
|
||||
|
||||
texturedModel = *OBJLoader::loadModel("assets/stall/stall.obj", "assets/stall/stallTexture.png", loader);
|
||||
entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-50), 0,0,0, 1.f));
|
||||
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,-25), 0,0,0, 1.f));
|
||||
camera = std::make_unique<Camera>();
|
||||
light = std::make_unique<Light>(glm::vec3(0,0,-20), glm::vec3(1,1,1));
|
||||
}
|
||||
|
||||
void GameLayer::onDetach()
|
||||
@ -54,8 +56,8 @@ void GameLayer::onUpdate()
|
||||
if (InputManager::isKeyPressed(GLFW_KEY_D)) moveDir.x += 1.0f;
|
||||
|
||||
entity->increaseRotation(0,1,0);
|
||||
camera->move(moveDir, 0.02f);
|
||||
renderer.renderFrame(*entity, *camera);
|
||||
camera->move(moveDir, 0.5f);
|
||||
renderer.renderFrame(*entity, *camera, *light);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -25,6 +25,7 @@ private:
|
||||
TexturedModel texturedModel;
|
||||
std::unique_ptr<Entity> entity;
|
||||
std::unique_ptr<Camera> camera;
|
||||
std::unique_ptr<Light> light;
|
||||
Renderer renderer;
|
||||
};
|
||||
|
||||
|
||||
5
src/engine/layer/entities/Light.cpp
Normal file
5
src/engine/layer/entities/Light.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by sebastian on 07.02.26.
|
||||
//
|
||||
|
||||
#include "Light.h"
|
||||
26
src/engine/layer/entities/Light.h
Normal file
26
src/engine/layer/entities/Light.h
Normal 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
|
||||
@ -5,24 +5,28 @@
|
||||
#include "Renderer.h"
|
||||
|
||||
#include "../core/Application.h"
|
||||
#include "../layer/entities/Light.h"
|
||||
#include "../toolbox/MathUtils.h"
|
||||
#include "glad/glad.h"
|
||||
#include "glm/ext/matrix_clip_space.hpp"
|
||||
#include "model/TexturedModel.h"
|
||||
|
||||
void Renderer::prepare(const Camera& camera) {
|
||||
|
||||
|
||||
void Renderer::prepare(const Camera& camera, const Light& light) {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
|
||||
staticShader.start();
|
||||
staticShader.loadLight(light.getPosition(), light.getColor());
|
||||
staticShader.loadViewMatrix(viewMatrix);
|
||||
staticShader.stop();
|
||||
}
|
||||
|
||||
void Renderer::renderFrame(const Entity &entity, const Camera& camera) {
|
||||
prepare(camera);
|
||||
void Renderer::renderFrame(const Entity &entity, const Camera& camera, const Light& light) {
|
||||
prepare(camera, light);
|
||||
staticShader.start();
|
||||
renderRawModel(entity);
|
||||
staticShader.stop();
|
||||
@ -34,6 +38,7 @@ void Renderer::renderRawModel(const Entity& entity) {
|
||||
glBindVertexArray(model->vaoID);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
|
||||
staticShader.loadTransformationMatrix(transformationMatrix);
|
||||
@ -43,6 +48,7 @@ void Renderer::renderRawModel(const Entity& entity) {
|
||||
glDrawElements(GL_TRIANGLES , model->vertexCount, GL_UNSIGNED_INT, 0);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(2);
|
||||
glBindVertexArray(0);
|
||||
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
#include "shaders/StaticShader.h"
|
||||
|
||||
|
||||
class Light;
|
||||
class Camera;
|
||||
|
||||
class Renderer {
|
||||
@ -19,8 +20,8 @@ public:
|
||||
staticShader.loadProjectionMatrix(projectionMatrix);
|
||||
staticShader.stop();
|
||||
};
|
||||
void prepare(const Camera& camera);
|
||||
void renderFrame(const ::Entity &entity, const Camera& camera);
|
||||
void prepare(const ::Camera &camera, const Light& light);
|
||||
void renderFrame(const ::Entity &entity, const ::Camera &camera, const Light& light);
|
||||
private:
|
||||
void renderRawModel(const Entity &entity);
|
||||
StaticShader staticShader;
|
||||
|
||||
@ -7,10 +7,11 @@
|
||||
#include "Texture2D.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();
|
||||
storeDataInAttributeList(0, 3, vertices);
|
||||
storeDataInAttributeList(1, 2, textureCoords);
|
||||
storeDataInAttributeList(2, 3, normals);
|
||||
bindIndicesBuffer(indices);
|
||||
unbindVAO();
|
||||
return {vaoID, static_cast<int>(indices.size())};
|
||||
|
||||
@ -15,7 +15,8 @@
|
||||
class Loader
|
||||
{
|
||||
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();
|
||||
void cleanUp();
|
||||
|
||||
|
||||
@ -5,6 +5,8 @@
|
||||
#include "OBJLoader.h"
|
||||
|
||||
#define TINYOBJLOADER_IMPLEMENTATION
|
||||
#include <complex>
|
||||
|
||||
#include "tiny_obj_loader.h"
|
||||
|
||||
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> normals;
|
||||
std::vector<float> uvs;
|
||||
std::vector<int> indices;
|
||||
|
||||
@ -41,13 +44,17 @@ std::shared_ptr<TexturedModel> OBJLoader::loadModel(const std::string &modelPath
|
||||
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
|
||||
indices.push_back(indexOffset);
|
||||
indexOffset++;
|
||||
}
|
||||
}
|
||||
|
||||
RawModel rawModel = loader.loadToVAO(vertices, uvs, indices);
|
||||
RawModel rawModel = loader.loadToVAO(vertices, normals, uvs, indices);
|
||||
ModelTexture modelTexture = loader.loadTextureFromFile(texturePath);
|
||||
TexturedModel texturedModel = TexturedModel(std::make_shared<RawModel>(rawModel), std::make_shared<ModelTexture>(modelTexture));
|
||||
return std::make_shared<TexturedModel>(texturedModel);
|
||||
|
||||
@ -21,15 +21,23 @@ void StaticShader::loadViewMatrix(glm::mat4 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 {
|
||||
bindAttribute(0, "position");
|
||||
bindAttribute(1, "textureCoords");
|
||||
bindAttribute(2, "normal");
|
||||
}
|
||||
|
||||
void StaticShader::getAllUniformLocations() {
|
||||
location_transformationMatrix = getUniformLocation("transformationMatrix");
|
||||
location_projectionMatrix = getUniformLocation("projectionMatrix");
|
||||
location_viewMatrix = getUniformLocation("viewMatrix");
|
||||
location_lightPosition = getUniformLocation("lightPosition");
|
||||
location_lightColor = getUniformLocation("lightColor");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ public:
|
||||
void loadTransformationMatrix(glm::mat4 matrix);
|
||||
void loadProjectionMatrix(glm::mat4 matrix);
|
||||
void loadViewMatrix(glm::mat4 matrix);
|
||||
void loadLight(glm::vec3 position, glm::vec3 color);
|
||||
private:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/vertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/fragmentShader.glsl";
|
||||
@ -21,6 +22,8 @@ private:
|
||||
int location_transformationMatrix;
|
||||
int location_projectionMatrix;
|
||||
int location_viewMatrix;
|
||||
int location_lightPosition;
|
||||
int location_lightColor;
|
||||
|
||||
protected:
|
||||
void bindAttributes() const override;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user