ADD: Render Basic UI Textures

This commit is contained in:
sebastian 2026-08-06 16:18:23 +02:00
parent 4ab4ea5262
commit 82b499e65c
24 changed files with 414 additions and 6 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "external/NetworkCore"]
path = external/NetworkCore
url = git@git.fawkes100.de:sebastian/JsonTCPServer.git

View File

@ -118,6 +118,8 @@ CPMAddPackage(
message(STATUS "dr_libs_SOURCE_DIR = ${dr_libs_SOURCE_DIR}") message(STATUS "dr_libs_SOURCE_DIR = ${dr_libs_SOURCE_DIR}")
add_subdirectory(external/NetworkCore)
# Engine als statische Lib # Engine als statische Lib
add_library(Engine STATIC add_library(Engine STATIC
engine/src/core/Window.h engine/src/core/Window.h
@ -247,9 +249,25 @@ add_library(Engine STATIC
engine/src/core/events/EventBus.h engine/src/core/events/EventBus.h
engine/src/core/audio/ecs/PlaySoundComponent.h engine/src/core/audio/ecs/PlaySoundComponent.h
engine/src/core/animation/events/AnimationMarkerEvent.h engine/src/core/animation/events/AnimationMarkerEvent.h
engine/src/core/animation/components/BlockingAnimationComponent.h
engine/src/core/audio/ecs/AudioSystem.cpp
engine/src/core/audio/ecs/AudioSystem.h
engine/src/core/audio/ecs/BlockingSoundComponent.h
engine/src/renderer/shader/GuiShader.cpp
engine/src/renderer/shader/GuiShader.h
engine/src/renderer/primitives/QuadFactory.cpp
engine/src/renderer/primitives/QuadFactory.h
engine/src/loader/models/SimpleTexturedMesh.cpp
engine/src/loader/models/SimpleTexturedMesh.h
engine/src/loader/models/Mesh.cpp
engine/src/loader/models/Mesh.h
engine/src/renderer/GuiRenderer.cpp
engine/src/renderer/GuiRenderer.h
engine/src/renderer/ui/GUITexture.cpp
engine/src/renderer/ui/GUITexture.h
) )
target_include_directories(Engine PUBLIC engine/src ${dr_libs_SOURCE_DIR}) target_include_directories(Engine PUBLIC engine/src ${dr_libs_SOURCE_DIR})
target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui spdlog::spdlog tinyobjloader stb_image OpenAL::OpenAL) target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui spdlog::spdlog tinyobjloader stb_image OpenAL::OpenAL NetworkCore)
# --- GameLib: Spiel-Logik, unabhängig von main.cpp testbar --- # --- GameLib: Spiel-Logik, unabhängig von main.cpp testbar ---
add_library(GameLib STATIC add_library(GameLib STATIC
@ -292,6 +310,8 @@ add_library(GameLib STATIC
game/src/ludo/gameMode/PiecePathBuilder.cpp game/src/ludo/gameMode/PiecePathBuilder.cpp
game/src/ludo/gameMode/PiecePathBuilder.h game/src/ludo/gameMode/PiecePathBuilder.h
game/src/ludo/gameMode/PathNode.h game/src/ludo/gameMode/PathNode.h
game/src/mainMenu/MainMenuScene.cpp
game/src/mainMenu/MainMenuScene.h
) )
target_include_directories(GameLib PUBLIC game/src) target_include_directories(GameLib PUBLIC game/src)
target_link_libraries(GameLib PUBLIC Engine) target_link_libraries(GameLib PUBLIC Engine)
@ -299,10 +319,8 @@ target_link_libraries(GameLib PUBLIC Engine)
# --- Executable --- # --- Executable ---
add_executable(ColorRace add_executable(ColorRace
game/src/main.cpp game/src/main.cpp
engine/src/core/animation/components/BlockingAnimationComponent.h game/src/mainMenu/layer/MainMenuUiLayer.cpp
engine/src/core/audio/ecs/AudioSystem.cpp game/src/mainMenu/layer/MainMenuUiLayer.h
engine/src/core/audio/ecs/AudioSystem.h
engine/src/core/audio/ecs/BlockingSoundComponent.h
) )
target_link_libraries(ColorRace PRIVATE target_link_libraries(ColorRace PRIVATE

11
assets/shaders/gui.frag Normal file
View File

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

12
assets/shaders/gui.vert Normal file
View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

View File

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

View File

@ -0,0 +1,20 @@
//
// Created by sebastian on 06.08.26.
//
#ifndef COLORRACE_MESH_H
#define COLORRACE_MESH_H
namespace engine {
class Mesh {
public:
virtual ~Mesh() = default;
virtual void bind() const = 0;
virtual void unbind() const = 0;
virtual void draw() const = 0;
};
}
#endif //COLORRACE_MESH_H

View File

@ -0,0 +1,17 @@
//
// Created by sebastian on 06.08.26.
//
#include "SimpleTexturedMesh.h"
void engine::SimpleTexturedMesh::bind() const {
m_vao->bind();
}
void engine::SimpleTexturedMesh::unbind() const {
m_vao->unbind();
}
void engine::SimpleTexturedMesh::draw() const {
glDrawArrays(GL_TRIANGLES, 0, 6);
}

View File

@ -0,0 +1,25 @@
//
// Created by sebastian on 06.08.26.
//
#ifndef COLORRACE_SIMPLETEXTUREDMESH_H
#define COLORRACE_SIMPLETEXTUREDMESH_H
#include "Mesh.h"
#include "utils/openglWrapper/openglObjects/Vao.h"
namespace engine {
class SimpleTexturedMesh : public engine::Mesh{
public:
SimpleTexturedMesh(std::unique_ptr<Vao> vao, int vertexCount) : m_vao(std::move(vao)), vertexCount(vertexCount) {}
void bind() const override;
void unbind() const override;
void draw() const override;
private:
std::unique_ptr<Vao> m_vao;
int vertexCount;
};
}
#endif //COLORRACE_SIMPLETEXTUREDMESH_H

View File

@ -0,0 +1,19 @@
//
// Created by sebastian on 06.08.26.
//
#include "GuiRenderer.h"
void engine::renderer::GuiRenderer::render(const std::vector<GUITexture> &guiElements) const {
m_shader->start();
m_renderState.apply();
m_fullscreenQuad->bind();
for (auto& guiElement : guiElements) {
m_shader->loadTransformationMatrix(guiElement.getTransform());
guiElement.getTexture()->bind();
m_fullscreenQuad->draw();
guiElement.getTexture()->unbind();
}
m_fullscreenQuad->unbind();
}

View File

@ -0,0 +1,36 @@
//
// Created by sebastian on 06.08.26.
//
#ifndef COLORRACE_GUIRENDERER_H
#define COLORRACE_GUIRENDERER_H
#include <memory>
#include "loader/models/SimpleTexturedMesh.h"
#include "primitives/QuadFactory.h"
#include "shader/GuiShader.h"
#include "ui/GUITexture.h"
#include "utils/openglWrapper/GLRenderState.h"
namespace engine::renderer {
class GuiRenderer {
public:
GuiRenderer() : m_shader(std::make_unique<shader::GuiShader>()) {
m_renderState.depthTesting = false;
m_renderState.depthWriting = false;
m_renderState.alphaBlending = true;
m_renderState.backfaceCulling = false;
m_renderState.frontFaceCulling = false;
m_fullscreenQuad = engine::QuadFactory::createFullScreenQuad();
}
void render(const std::vector<GUITexture> &guiElements) const;
private:
std::shared_ptr<SimpleTexturedMesh> m_fullscreenQuad;
std::unique_ptr<shader::GuiShader> m_shader;
GLRenderState m_renderState;
};
}
#endif //COLORRACE_GUIRENDERER_H

View File

@ -0,0 +1,28 @@
//
// Created by sebastian on 06.08.26.
//
#include "QuadFactory.h"
std::shared_ptr<engine::SimpleTexturedMesh> engine::QuadFactory::createFullScreenQuad() {
// pos.xy in NDC, uv.xy
std::vector<float> vertices = {
-1,-1,
1,-1,
1, 1,
1, 1,
-1, 1,
-1,-1
};
std::vector<unsigned int> indices = { 0, 1, 2, 2, 3, 0 };
auto vao = Vao::create();
std::vector<std::unique_ptr<Attribute>> attribs;
attribs.push_back(std::make_unique<Vec2Attribute>(0)); // position
vao->initDataFeed(vertices.data(), vertices.size() * sizeof(float), GL_STATIC_DRAW, std::move(attribs));
vao->createIndexBuffer(indices.data(), indices.size());
vao->unbind();
return std::make_shared<engine::SimpleTexturedMesh>(std::move(vao), 4);
}

View File

@ -0,0 +1,17 @@
//
// Created by sebastian on 06.08.26.
//
#ifndef COLORRACE_QUADFACTORY_H
#define COLORRACE_QUADFACTORY_H
#include <memory>
#include "loader/models/SimpleTexturedMesh.h"
namespace engine::QuadFactory {
std::shared_ptr<engine::SimpleTexturedMesh> createFullScreenQuad();
}
#endif //COLORRACE_QUADFACTORY_H

View File

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

View File

@ -0,0 +1,30 @@
//
// Created by sebastian on 06.08.26.
//
#ifndef COLORRACE_GUISHADER_H
#define COLORRACE_GUISHADER_H
#include <functional>
#include "glm/fwd.hpp"
#include "utils/openglWrapper/shader/ShaderProgram.h"
#include "utils/openglWrapper/shader/UniformValue.h"
namespace engine::shader {
class GuiShader : public ShaderProgram {
public:
GuiShader() : ShaderProgram("assets/shaders/gui.vert", "assets/shaders/gui.frag") {
storeAllUniformLocations({
std::ref(transformationMatrix)
});
}
void loadTransformationMatrix(const glm::mat4& matrix) {
transformationMatrix.load(matrix);
}
private:
UniformValue<glm::mat4> transformationMatrix{"transformationMatrix"};
};
}
#endif //COLORRACE_GUISHADER_H

View File

@ -0,0 +1,28 @@
//
// Created by sebastian on 06.08.26.
//
#include "GUITexture.h"
#include "glm/glm.hpp"
#include "glm/ext/matrix_transform.hpp"
namespace engine {
void GUITexture::bind() {
texture->bind();
}
void GUITexture::unbind() {
texture->unbind();
}
glm::mat4 GUITexture::getTransform() const {
glm::vec2 translationNDC;
translationNDC.x = position.x * 2.0f - 1.0f + size.x;
translationNDC.y = 1.0f - position.y * 2.0f - size.y;
auto matrix = glm::identity<glm::mat4>();
matrix = glm::translate(matrix, glm::vec3( translationNDC, 0.0f));
matrix = glm::scale(matrix, glm::vec3(size.x, size.y, 1.0f));
return matrix;
}
} // engine

View File

@ -0,0 +1,33 @@
//
// Created by sebastian on 06.08.26.
//
#ifndef COLORRACE_GUITEXTURE_H
#define COLORRACE_GUITEXTURE_H
#include <memory>
#include "glm/fwd.hpp"
#include "glm/vec2.hpp"
#include "loader/textures/Texture2D.h"
namespace engine {
class GUITexture {
public:
GUITexture(std::shared_ptr<Texture2D> texture, glm::vec2 position, glm::vec2 size) : position(position), size(size), texture(std::move(texture)) {}
[[nodiscard]] const glm::vec2& getPosition() const { return position; }
[[nodiscard]] const glm::vec2& getSize() const { return size; }
[[nodiscard]] const std::shared_ptr<Texture2D>& getTexture() const { return texture; }
void bind();
void unbind();
glm::mat4 getTransform() const;
private:
const glm::vec2 position;
const glm::vec2 size;
std::shared_ptr<Texture2D> texture;
};
} // engine
#endif //COLORRACE_GUITEXTURE_H

1
external/NetworkCore vendored Submodule

@ -0,0 +1 @@
Subproject commit f67ce801f799aacb9d10b35423417953e954548b

View File

@ -11,6 +11,7 @@
#include "ecs/standardComponents/MeshComponent.h" #include "ecs/standardComponents/MeshComponent.h"
#include "ecs/standardComponents/TransformComponent.h" #include "ecs/standardComponents/TransformComponent.h"
#include "layer/SceneManager.h" #include "layer/SceneManager.h"
#include "mainMenu/MainMenuScene.h"
#include "renderer/MeshRenderer.h" #include "renderer/MeshRenderer.h"
#include "renderer/entities/Camera.h" #include "renderer/entities/Camera.h"
#include "renderer/shader/StaticShader.h" #include "renderer/shader/StaticShader.h"
@ -33,7 +34,7 @@ public:
return config; return config;
} }
ColorRaceApp() : Application(makeConfig()) { ColorRaceApp() : Application(makeConfig()) {
getSceneManager().switchTo(std::make_unique<GameScene>(getInputContextStack(), getKeyboard(), getMouse())); getSceneManager().switchTo(std::make_unique<MainMenuScene>(getInputContextStack(), getKeyboard(), getMouse()));
} }
protected: protected:
void onUpdate(float deltaTime) override; void onUpdate(float deltaTime) override;

View File

@ -0,0 +1,18 @@
//
// Created by sebastian on 06.08.26.
//
#include "MainMenuScene.h"
#include "layer/MainMenuUiLayer.h"
void MainMenuScene::onEnter() {
Scene::onEnter();
addLayer(std::make_unique<MainMenuUiLayer>(m_keyboard, m_mouse, getAnimationEventBus(), *assetManager));
}
std::vector<engine::AssetRequest> MainMenuScene::getRequiredAssets() const {
std::vector<engine::AssetRequest> requests;
requests.emplace_back(engine::TextureRequest("main_menu_background", "assets/textures/main_menu_background.png"));
return requests;
}

View File

@ -0,0 +1,18 @@
//
// Created by sebastian on 06.08.26.
//
#ifndef COLORRACE_MAINMENUSCENE_H
#define COLORRACE_MAINMENUSCENE_H
#include "layer/Scene.h"
class MainMenuScene : public engine::Scene {
public:
MainMenuScene(engine::InputContextStack& inputContext, engine::Keyboard& keyboard, engine::Mouse& mouse) : Scene(inputContext, keyboard, mouse) {}
void onEnter() override;
std::vector<engine::AssetRequest> getRequiredAssets() const override;
};
#endif //COLORRACE_MAINMENUSCENE_H

View File

@ -0,0 +1,29 @@
//
// Created by sebastian on 06.08.26.
//
#include "MainMenuUiLayer.h"
void MainMenuUiLayer::onRender() {
Layer::onRender();
std::vector<engine::GUITexture> guiElements;
guiElements.emplace_back(*backgroundImage);
m_guiRenderer.render(guiElements);
}
void MainMenuUiLayer::onAttachImpl() {
Layer::onAttachImpl();
auto backgroundTexture = m_assetManager.getTexture("main_menu_background");
backgroundImage = std::make_unique<engine::GUITexture>(backgroundTexture, glm::vec2(0,0), glm::vec2(1,1));
}
void MainMenuUiLayer::onDetachImpl() {
Layer::onDetachImpl();
}
void MainMenuUiLayer::onUpdate(float deltaTime) {
Layer::onUpdate(deltaTime);
}

View File

@ -0,0 +1,34 @@
//
// Created by sebastian on 06.08.26.
//
#ifndef COLORRACE_MAINMENUUILAYER_H
#define COLORRACE_MAINMENUUILAYER_H
#include "layer/Layer.h"
#include "loader/assets/AssetManager.h"
#include "renderer/GuiRenderer.h"
#include "renderer/ui/GUITexture.h"
class MainMenuUiLayer : public engine::Layer {
public:
MainMenuUiLayer(engine::Keyboard &keyboard, engine::Mouse &mouse, engine::EventBus<engine::animation::AnimationMarkerEvent> &animationEventBus, engine::AssetManager& assetManager)
: Layer(keyboard, mouse, animationEventBus), m_assetManager(assetManager) {
}
void onRender() override;
void onUpdate(float deltaTime) override;
protected:
void onAttachImpl() override;
void onDetachImpl() override;
private:
engine::AssetManager& m_assetManager;
std::unique_ptr<engine::GUITexture> backgroundImage;
engine::renderer::GuiRenderer m_guiRenderer;
};
#endif //COLORRACE_MAINMENUUILAYER_H