ADD: Implementing Basic for UI System

This commit is contained in:
sebastian 2026-02-09 20:35:54 +01:00
parent 0f9655b742
commit 0a384345bb
18 changed files with 265 additions and 1 deletions

View File

@ -109,6 +109,14 @@ add_executable(Dicewars_Siedler src/main.cpp
src/game/player/Player.h
src/game/hexWorld/ecs/components/OwnerComponent.cpp
src/game/hexWorld/ecs/components/OwnerComponent.h
src/engine/renderer/model/GUITexture.cpp
src/engine/renderer/model/GUITexture.h
src/engine/renderer/GUIRenderer.cpp
src/engine/renderer/GUIRenderer.h
src/game/UILayer.cpp
src/game/UILayer.h
src/engine/renderer/shaders/GUIShader.cpp
src/engine/renderer/shaders/GUIShader.h
)
target_include_directories(Dicewars_Siedler PRIVATE

View File

@ -0,0 +1,11 @@
#version 400 core
in vec2 textureCoords;
out vec4 color;
uniform sampler2D guiTexture;
void main(void) {
color = texture(guiTexture, textureCoords);
}

View File

@ -0,0 +1,12 @@
#version 400 core
in vec2 position;
out vec2 textureCoords;
uniform mat4 transformationMatrix;
void main(void) {
gl_Position = transformationMatrix * vec4(position, 0.0, 1.0);
textureCoords = vec2((position.x + 1.0)/ 2.0, 1 - (position.y + 1.0)/2.0);
}

View File

@ -0,0 +1,38 @@
//
// Created by sebastian on 09.02.26.
//
#include "GUIRenderer.h"
#include <vector>
#include "../toolbox/MathUtils.h"
#include "loader/Loader.h"
GUIRenderer::GUIRenderer(Loader &loader) {
std::vector<float> positions = {-1, 1, -1,-1,1,1,1,-1};
auto model = loader.loadToVAO(positions);
rawModel = std::make_unique<RawModel>(model);
}
void GUIRenderer::render(const std::vector<GUITexture>& guiTextures) {
guiShader.start();
glBindVertexArray(rawModel->vaoID);
glEnableVertexAttribArray(0);
for (GUITexture texture : guiTextures) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(texture.getPosition(), texture.getScale());
guiShader.loadTransformationMatrix(transformationMatrix);
glDrawArrays(GL_TRIANGLE_STRIP, 0, rawModel->vertexCount);
}
//render
glDisableVertexAttribArray(0);
glBindVertexArray(0);
guiShader.stop();
}
void GUIRenderer::cleanUp() {
guiShader.cleanUp();
}

View File

@ -0,0 +1,31 @@
//
// Created by sebastian on 09.02.26.
//
#ifndef GUIRENDERER_H
#define GUIRENDERER_H
#include <memory>
#include <vector>
#include "model/GUITexture.h"
#include "model/RawModel.h"
#include "shaders/GUIShader.h"
class Loader;
class GUIRenderer {
private:
std::unique_ptr<RawModel> rawModel;
public:
GUIShader guiShader;
explicit GUIRenderer(Loader& loader);
void render(const std::vector<GUITexture> &guiTextures);
void cleanUp();
};
#endif //GUIRENDERER_H

View File

@ -7,6 +7,11 @@
#include "Texture2D.h"
#include "TextureLoader.h"
Loader & Loader::instance() {
static Loader loader;
return loader;
}
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);
@ -17,6 +22,13 @@ RawModel Loader::loadToVAO(const std::vector<float> &vertices, const std::vector
return {vaoID, static_cast<int>(indices.size())};
}
RawModel Loader::loadToVAO(const std::vector<float> &vertices) {
GLuint vaoID = createVAO();
storeDataInAttributeList(0, 2, vertices);
unbindVAO();
return {vaoID, static_cast<int>(vertices.size() / 2)};
}
Loader::~Loader() {
cleanUp();
}

View File

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

View File

@ -0,0 +1,8 @@
//
// Created by sebastian on 09.02.26.
//
#include "GUITexture.h"
GUITexture::GUITexture(GLuint textureID, glm::vec2 position, glm::vec2 scale) : textureID(textureID), position(position), scale(scale){
}

View File

@ -0,0 +1,28 @@
//
// Created by sebastian on 09.02.26.
//
#ifndef GUITEXTURE_H
#define GUITEXTURE_H
#include "glad/glad.h"
#include "glm/vec2.hpp"
class GUITexture {
private:
const GLuint textureID;
const glm::vec2 position;
const glm::vec2 scale;
public:
GUITexture(GLuint textureID, glm::vec2 position, glm::vec2 scale);
[[nodiscard]] GLuint getTextureID() const {return textureID;}
[[nodiscard]] glm::vec2 getPosition() const {return position;}
[[nodiscard]] glm::vec2 getScale() const {return scale;}
};
#endif //GUITEXTURE_H

View File

@ -0,0 +1,17 @@
//
// Created by sebastian on 09.02.26.
//
#include "GUIShader.h"
void GUIShader::loadTransformationMatrix(glm::mat4 matrix) {
ShaderProgram::loadMatrix(location_transformationMatrix, matrix);
}
void GUIShader::getAllUniformLocations() {
location_transformationMatrix = ShaderProgram::getUniformLocation("transformationMatrix");
}
void GUIShader::bindAttributes() const {
ShaderProgram::bindAttribute(0, "position");
}

View File

@ -0,0 +1,30 @@
//
// Created by sebastian on 09.02.26.
//
#ifndef GUISHADER_H
#define GUISHADER_H
#include "ShaderProgram.h"
class GUIShader: public ShaderProgram {
public:
GUIShader() : ShaderProgram(VERTEX_FILE, FRAGMENT_FILE) {
GUIShader::bindAttributes();
GUIShader::getAllUniformLocations();
};
void loadTransformationMatrix(glm::mat4 matrix);
private:
inline static const std::string VERTEX_FILE = "assets/shaders/guiVertexShader.glsl";
inline static const std::string FRAGMENT_FILE = "assets/shaders/guiFragmentShader.glsl";
int location_transformationMatrix;
protected:
void getAllUniformLocations() override;
void bindAttributes() const override;
};
#endif //GUISHADER_H

View File

@ -11,6 +11,8 @@
class ShaderProgram {
public:
virtual ~ShaderProgram() = default;
ShaderProgram(std::string vert, std::string frag);
void start();
void stop();

View File

@ -23,6 +23,13 @@ namespace MathUtils {
return matrix;
}
inline static glm::mat4 createTransformationMatrix(const glm::vec2 translation, const glm::vec2 scale) {
auto matrix = glm::identity<glm::mat4>();
matrix = glm::translate(matrix, glm::vec3( translation.x, translation.y, 0.0f));
matrix = glm::scale(matrix, glm::vec3(scale.x, scale.y, 1.0f));
return matrix;
}
inline static glm::mat4 createViewMatrix(const Camera& camera) {
const glm::vec3& pos = camera.getPosition();

View File

@ -5,10 +5,12 @@
#include "DicewarsApp.h"
#include "GameLayer.h"
#include "UILayer.h"
DicewarsApp::DicewarsApp() {
gameMode = std::make_shared<GameMode>();
GameLayer* gamelayer = new GameLayer();
gamelayer->setGameMode(gameMode);
pushLayer(gamelayer);
pushLayer(new UILayer());
}

View File

@ -21,6 +21,7 @@ GameLayer::GameLayer() :texturedModel(0,0) //Platzhalter, echtes Model kommt in
void GameLayer::onAttach()
{
Loader& loader = Loader::instance();
texturedModel = *OBJLoader::loadModel("assets/dragon/dragon.obj", "assets/dragon/dragon.png", loader);
entities.push_back(std::make_shared<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-25), 0,0,0, 1.f)));
camera = std::make_unique<Camera>();

View File

@ -31,7 +31,6 @@ public:
private:
std::shared_ptr<GameMode> gameMode;
Loader loader;
TexturedModel texturedModel;
std::unique_ptr<Camera> camera;
std::unique_ptr<Light> light;

30
src/game/UILayer.cpp Normal file
View File

@ -0,0 +1,30 @@
//
// Created by sebastian on 09.02.26.
//
#include "UILayer.h"
#include "../engine/renderer/loader/Loader.h"
#include "../engine/renderer/model/GUITexture.h"
UILayer::UILayer() {
Loader& loader = Loader::instance();
guiRenderer = std::make_unique<GUIRenderer>(loader);
}
void UILayer::onAttach() {
Layer::onAttach();
ModelTexture modelTexture = Loader::instance().loadTextureFromFile("assets/textures/texture.png");
guiTextures.emplace_back(modelTexture.getTextureID(), glm::vec2(0.5f, 0.5f), glm::vec2(0.5f));
}
void UILayer::onUpdate() {
Layer::onUpdate();
guiRenderer->render(guiTextures);
}
void UILayer::onDetach() {
Layer::onDetach();
}

26
src/game/UILayer.h Normal file
View File

@ -0,0 +1,26 @@
//
// Created by sebastian on 09.02.26.
//
#ifndef UILAYER_H
#define UILAYER_H
#include <memory>
#include "../engine/layer/Layer.h"
#include "../engine/renderer/GUIRenderer.h"
class UILayer : public Layer{
private:
std::unique_ptr<GUIRenderer> guiRenderer;
std::vector<GUITexture> guiTextures;
public:
UILayer();
void onAttach() override;
void onUpdate() override;
void onDetach() override;
};
#endif //UILAYER_H