ADD: Apply Transformation matrices
This commit is contained in:
parent
cbeddfdbe3
commit
6f0d8852fa
@ -44,7 +44,10 @@ add_executable(Dicewars_Siedler src/main.cpp
|
||||
src/engine/renderer/textures/ModelTexture.cpp
|
||||
src/engine/renderer/textures/ModelTexture.h
|
||||
src/engine/renderer/model/TexturedModel.cpp
|
||||
src/engine/renderer/model/TexturedModel.h)
|
||||
src/engine/renderer/model/TexturedModel.h
|
||||
src/engine/toolbox/MathUtils.h
|
||||
src/engine/layer/entities/Entity.cpp
|
||||
src/engine/layer/entities/Entity.h)
|
||||
|
||||
target_include_directories(Dicewars_Siedler PRIVATE
|
||||
lib/glfw/include
|
||||
|
||||
@ -5,8 +5,10 @@ in vec2 textureCoords;
|
||||
|
||||
out vec2 pass_textureCoords;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(position, 1.0);
|
||||
gl_Position = transformationMatrix * vec4(position, 1.0f);
|
||||
pass_textureCoords = textureCoords;
|
||||
}
|
||||
|
||||
@ -36,6 +36,7 @@ void GameLayer::onAttach()
|
||||
RawModel model = loader.loadToVAO(vertices, textureCoords, indices);
|
||||
ModelTexture texture = ModelTexture(loader.loadTexture("assets/textures/texture.png"));
|
||||
texturedModel = TexturedModel(std::make_shared<RawModel>(model), std::make_shared<ModelTexture>(texture));
|
||||
entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(-1,0,0), 0,0,0, 1.f));
|
||||
}
|
||||
|
||||
void GameLayer::onDetach()
|
||||
@ -45,7 +46,7 @@ void GameLayer::onDetach()
|
||||
|
||||
void GameLayer::onUpdate()
|
||||
{
|
||||
renderer.renderFrame(texturedModel);
|
||||
renderer.renderFrame(*entity);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ public:
|
||||
private:
|
||||
Loader loader;
|
||||
TexturedModel texturedModel;
|
||||
std::unique_ptr<Entity> entity;
|
||||
Renderer renderer;
|
||||
};
|
||||
|
||||
|
||||
5
src/engine/layer/entities/Entity.cpp
Normal file
5
src/engine/layer/entities/Entity.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by sebastian on 07.02.26.
|
||||
//
|
||||
|
||||
#include "Entity.h"
|
||||
41
src/engine/layer/entities/Entity.h
Normal file
41
src/engine/layer/entities/Entity.h
Normal file
@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by sebastian on 07.02.26.
|
||||
//
|
||||
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
#include <memory>
|
||||
|
||||
#include "../../renderer/model/TexturedModel.h"
|
||||
#include "glm/vec3.hpp"
|
||||
|
||||
|
||||
class Entity {
|
||||
private:
|
||||
std::shared_ptr<TexturedModel> model;
|
||||
glm::vec3 position;
|
||||
float rotX, rotY, rotZ;
|
||||
float scale;
|
||||
|
||||
public:
|
||||
Entity(std::shared_ptr<TexturedModel> model, const glm::vec3 position, const float rotX, const float rotY, const float rotZ, const float scale) : model(std::move(model)), position(position), rotX(rotX), rotY(rotY), rotZ(rotZ), scale(scale) {};
|
||||
|
||||
void increasePosition(const float dx, const float dy, const float dz) {position += glm::vec3(dx, dy, dz);} //( dx, dy, dz
|
||||
void increaseRotation(const float dx, const float dy, const float dz) {rotX += dx; rotY += dy; rotZ += dz;}
|
||||
|
||||
[[nodiscard]] const std::shared_ptr<TexturedModel> getModel() const {return model;}
|
||||
[[nodiscard]] const glm::vec3 getPosition() const {return position;}
|
||||
[[nodiscard]] float getRotX() const {return rotX;}
|
||||
[[nodiscard]] float getRotY() const {return rotY;}
|
||||
[[nodiscard]] float getRotZ() const {return rotZ;}
|
||||
[[nodiscard]] float getScale() const {return scale;}
|
||||
void setPosition(glm::vec3 position) {this->position = position;}
|
||||
void setRotX(float rotX) {this->rotX = rotX;}
|
||||
void setRotY(float rotY) {this->rotY = rotY;}
|
||||
void setRotZ(float rotZ) {this->rotZ = rotZ;}
|
||||
void setScale(float scale) {this->scale = scale;}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //ENTITY_H
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
#include "Renderer.h"
|
||||
|
||||
#include "../toolbox/MathUtils.h"
|
||||
#include "glad/glad.h"
|
||||
#include "model/TexturedModel.h"
|
||||
|
||||
@ -12,20 +13,25 @@ void Renderer::prepare() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void Renderer::renderFrame(const TexturedModel& model) {
|
||||
void Renderer::renderFrame(const Entity &entity) {
|
||||
prepare();
|
||||
staticShader.start();
|
||||
renderRawModel(model);
|
||||
renderRawModel(entity);
|
||||
staticShader.stop();
|
||||
}
|
||||
|
||||
void Renderer::renderRawModel(const TexturedModel& texturedModel) {
|
||||
auto model = texturedModel.getRawModel();
|
||||
void Renderer::renderRawModel(const Entity& entity) {
|
||||
auto texturedModel = entity.getModel();
|
||||
auto model = texturedModel->getRawModel();
|
||||
glBindVertexArray(model->vaoID);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
|
||||
staticShader.loadTransformationMatrix(transformationMatrix);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texturedModel.getTexture()->getTextureID());
|
||||
glBindTexture(GL_TEXTURE_2D, texturedModel->getTexture()->getTextureID());
|
||||
glDrawElements(GL_TRIANGLES , model->vertexCount, GL_UNSIGNED_INT, 0);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
#ifndef DICEWARS_SIEDLER_RENDERER_H
|
||||
#define DICEWARS_SIEDLER_RENDERER_H
|
||||
#include "../layer/entities/Entity.h"
|
||||
#include "model/RawModel.h"
|
||||
#include "model/TexturedModel.h"
|
||||
#include "shaders/StaticShader.h"
|
||||
@ -12,9 +13,9 @@
|
||||
class Renderer {
|
||||
public:
|
||||
void prepare();
|
||||
void renderFrame(const TexturedModel &model);
|
||||
void renderFrame(const Entity &entity);
|
||||
private:
|
||||
void renderRawModel(const TexturedModel &model);
|
||||
void renderRawModel(const Entity &entity);
|
||||
StaticShader staticShader;
|
||||
};
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
#include <sstream>
|
||||
|
||||
#include "glad/glad.h"
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
ShaderProgram::ShaderProgram(std::string vert, std::string frag) {
|
||||
vertexShaderID = loadShader(vert, GL_VERTEX_SHADER);
|
||||
@ -43,6 +44,27 @@ void ShaderProgram::bindAttribute(int attribute, const std::string& variableName
|
||||
glBindAttribLocation(programID, attribute, variableName.c_str());
|
||||
}
|
||||
|
||||
int ShaderProgram::getUniformLocation(const std::string &uniformName) const {
|
||||
return glGetUniformLocation(programID, uniformName.c_str());
|
||||
}
|
||||
|
||||
void ShaderProgram::loadFloat(int location, float value) {
|
||||
glUniform1f(location, value);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadVector(const int location, const glm::vec3 vector) {
|
||||
glUniform3f(location, vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadBoolean(int location, bool value) {
|
||||
glUniform1f(location, value ? 1.f: 0.f);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadMatrix(int location, glm::mat4 matrix) {
|
||||
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
|
||||
}
|
||||
|
||||
|
||||
int ShaderProgram::loadShader(const std::string& file, int type) {
|
||||
std::ifstream shaderFile(file);
|
||||
if (!shaderFile.is_open())
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "../../../../lib/glfw/src/internal.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class ShaderProgram {
|
||||
public:
|
||||
@ -24,6 +24,13 @@ protected:
|
||||
|
||||
virtual void bindAttributes() const = 0;
|
||||
|
||||
[[nodiscard]] int getUniformLocation(const std::string& uniformName) const;
|
||||
virtual void getAllUniformLocations() = 0;
|
||||
|
||||
void loadFloat(int location, float value);
|
||||
void loadVector(int location, glm::vec3 vector);
|
||||
void loadBoolean(int location, bool value);
|
||||
void loadMatrix(int location, glm::mat4 matrix);
|
||||
private:
|
||||
static int loadShader(const std::string& file, int type);
|
||||
};
|
||||
|
||||
@ -6,9 +6,20 @@
|
||||
|
||||
StaticShader::StaticShader() : ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
|
||||
StaticShader::bindAttributes();
|
||||
StaticShader::getAllUniformLocations();
|
||||
}
|
||||
|
||||
void StaticShader::loadTransformationMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_transformationMatrix, matrix);
|
||||
}
|
||||
|
||||
void StaticShader::bindAttributes() const {
|
||||
bindAttribute(0, "position");
|
||||
bindAttribute(1, "textureCoords");
|
||||
}
|
||||
|
||||
void StaticShader::getAllUniformLocations() {
|
||||
location_transformationMatrix = getUniformLocation("transformationMatrix");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -11,12 +11,16 @@ class StaticShader: public ShaderProgram
|
||||
{
|
||||
public:
|
||||
StaticShader();
|
||||
void loadTransformationMatrix(glm::mat4 matrix);
|
||||
private:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/vertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/fragmentShader.glsl";
|
||||
|
||||
int location_transformationMatrix;
|
||||
|
||||
protected:
|
||||
void bindAttributes() const override;
|
||||
void getAllUniformLocations() override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
23
src/engine/toolbox/MathUtils.h
Normal file
23
src/engine/toolbox/MathUtils.h
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by sebastian on 07.02.26.
|
||||
//
|
||||
|
||||
#ifndef MATHUTILS_H
|
||||
#define MATHUTILS_H
|
||||
#include "glm/mat4x4.hpp"
|
||||
#include "glm/vec3.hpp"
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
|
||||
namespace MathUtils {
|
||||
inline static glm::mat4 createTransformationMatrix(const glm::vec3 translation, const float rx, const float ry, const float rz, const float scale) {
|
||||
auto matrix = glm::mat4(1.0f);
|
||||
matrix = glm::translate(matrix, translation);
|
||||
matrix = glm::rotate(matrix, glm::radians(rx), glm::vec3(1, 0, 0));
|
||||
matrix = glm::rotate(matrix, glm::radians(ry), glm::vec3(0, 1, 0));
|
||||
matrix = glm::rotate(matrix, glm::radians(rz), glm::vec3(0, 0, 1));
|
||||
matrix = glm::scale(matrix, glm::vec3(scale));
|
||||
return matrix;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //MATHUTILS_H
|
||||
Loading…
Reference in New Issue
Block a user