summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cwd/assets/altcraft/shaders/frag/rml.fs12
-rw-r--r--cwd/assets/altcraft/shaders/frag/rmltex.fs14
-rw-r--r--cwd/assets/altcraft/shaders/rml.json7
-rw-r--r--cwd/assets/altcraft/shaders/rmltex.json8
-rw-r--r--cwd/assets/altcraft/shaders/vert/rml.vs21
-rw-r--r--cwd/test.rcss6
-rw-r--r--cwd/test.rml10
-rw-r--r--src/AssetManager.cpp12
-rw-r--r--src/Render.cpp40
-rw-r--r--src/Render.hpp11
-rw-r--r--src/Rml.cpp154
-rw-r--r--src/Rml.hpp56
-rw-r--r--src/Shader.hpp8
13 files changed, 355 insertions, 4 deletions
diff --git a/cwd/assets/altcraft/shaders/frag/rml.fs b/cwd/assets/altcraft/shaders/frag/rml.fs
new file mode 100644
index 0000000..54c3f36
--- /dev/null
+++ b/cwd/assets/altcraft/shaders/frag/rml.fs
@@ -0,0 +1,12 @@
+#version 330 core
+
+in VS_OUT {
+ vec4 color;
+ vec2 tex_coord;
+} fs_in;
+
+out vec4 fragColor;
+
+void main() {
+ fragColor = fs_in.color;
+}
diff --git a/cwd/assets/altcraft/shaders/frag/rmltex.fs b/cwd/assets/altcraft/shaders/frag/rmltex.fs
new file mode 100644
index 0000000..d885b3b
--- /dev/null
+++ b/cwd/assets/altcraft/shaders/frag/rmltex.fs
@@ -0,0 +1,14 @@
+#version 330 core
+
+uniform sampler2D fontTexture;
+
+in VS_OUT {
+ vec4 color;
+ vec2 tex_coord;
+} fs_in;
+
+out vec4 fragColor;
+
+void main() {
+ fragColor = fs_in.color * texture(fontTexture, fs_in.tex_coord);
+}
diff --git a/cwd/assets/altcraft/shaders/rml.json b/cwd/assets/altcraft/shaders/rml.json
new file mode 100644
index 0000000..c62ff25
--- /dev/null
+++ b/cwd/assets/altcraft/shaders/rml.json
@@ -0,0 +1,7 @@
+{
+ "vert": "/altcraft/shaders/vert/rml",
+ "frag": "/altcraft/shaders/frag/rml",
+ "uniforms": [
+ "viewportSize"
+ ]
+} \ No newline at end of file
diff --git a/cwd/assets/altcraft/shaders/rmltex.json b/cwd/assets/altcraft/shaders/rmltex.json
new file mode 100644
index 0000000..3fef969
--- /dev/null
+++ b/cwd/assets/altcraft/shaders/rmltex.json
@@ -0,0 +1,8 @@
+{
+ "vert": "/altcraft/shaders/vert/rml",
+ "frag": "/altcraft/shaders/frag/rmltex",
+ "uniforms": [
+ "viewportSize",
+ "fontTexture"
+ ]
+} \ No newline at end of file
diff --git a/cwd/assets/altcraft/shaders/vert/rml.vs b/cwd/assets/altcraft/shaders/vert/rml.vs
new file mode 100644
index 0000000..bfc50fb
--- /dev/null
+++ b/cwd/assets/altcraft/shaders/vert/rml.vs
@@ -0,0 +1,21 @@
+#version 330 core
+
+uniform uvec2 viewportSize;
+uniform mat4 rotationMat;
+
+layout (location = 0) in vec2 pos;
+layout (location = 1) in uvec4 color;
+layout (location = 2) in vec2 tex_coord;
+
+out VS_OUT {
+ vec4 color;
+ vec2 tex_coord;
+} vs_out;
+
+void main() {
+ float x = (pos.x - viewportSize.x) / viewportSize.x;
+ float y = ((pos.y - viewportSize.y) / viewportSize.y) * -1;
+ gl_Position = vec4(x, y, -1.0f, 1.0f);
+ vs_out.color = vec4(float(color.x) / 255.0f, float(color.y) / 255.0f, float(color.z) / 255.0f, float(color.w) / 255.0f);
+ vs_out.tex_coord = tex_coord;
+}
diff --git a/cwd/test.rcss b/cwd/test.rcss
new file mode 100644
index 0000000..47441bb
--- /dev/null
+++ b/cwd/test.rcss
@@ -0,0 +1,6 @@
+body {
+ background-color: red;
+ color: blue;
+ font-size: 100px;
+ font-family: "open sans";
+}
diff --git a/cwd/test.rml b/cwd/test.rml
new file mode 100644
index 0000000..ee72ece
--- /dev/null
+++ b/cwd/test.rml
@@ -0,0 +1,10 @@
+<rml>
+ <head>
+ <link type="text/rcss" href="test.rcss" />
+ </head>
+ <body>
+ <div style="background-color:green;">
+ Test RmlUi
+ </div>
+ </body>
+</rmL>
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 514d008..0db495a 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -402,22 +402,26 @@ void ParseAssetShader(AssetTreeNode &node) {
std::string vertPath = j["vert"].get<std::string>();
std::string fragPath = j["frag"].get<std::string>();
- AssetTreeNode *vertAsset = AssetManager::GetAssetByAssetName(vertPath);
- AssetTreeNode *fragAsset = AssetManager::GetAssetByAssetName(fragPath);
+ AssetTreeNode* vertAsset = AssetManager::GetAssetByAssetName(vertPath);
+ AssetTreeNode* fragAsset = AssetManager::GetAssetByAssetName(fragPath);
std::string vertSource((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size());
std::string fragSource((char*)fragAsset->data.data(), (char*)fragAsset->data.data() + fragAsset->data.size());
std::vector<std::string> uniforms;
- for (auto &it : j["uniforms"]) {
+ for (auto& it : j["uniforms"]) {
uniforms.push_back(it.get<std::string>());
}
node.asset = std::make_unique<AssetShader>();
- AssetShader *asset = dynamic_cast<AssetShader*>(node.asset.get());
+ AssetShader* asset = dynamic_cast<AssetShader*>(node.asset.get());
asset->shader = std::make_unique<Shader>(vertSource, fragSource, uniforms);
+ } catch (std::exception &e) {
+ glCheckError();
+ LOG(ERROR) << "Shader asset parsing failed: " << e.what();
} catch (...) {
glCheckError();
+ LOG(ERROR) << "Shader asset parsing failed with unknown reason";
return;
}
}
diff --git a/src/Render.cpp b/src/Render.cpp
index d583205..7835b98 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -3,6 +3,7 @@
#include <imgui.h>
#include <easylogging++.h>
#include <optick.h>
+#include <RmlUi/Core.h>
#include "imgui_impl_sdl_gl3.h"
#include "Shader.hpp"
@@ -16,6 +17,7 @@
#include "Settings.hpp"
#include "Framebuffer.hpp"
#include "Plugin.hpp"
+#include "Rml.hpp"
Render::Render(unsigned int windowWidth, unsigned int windowHeight,
std::string windowTitle) {
@@ -31,6 +33,8 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight,
glCheckError();
PrepareToRendering();
glCheckError();
+ InitRml();
+ glCheckError();
//Read settings
strcpy(fieldUsername, Settings::Read("username", "HelloOne").c_str());
@@ -73,6 +77,10 @@ Render::~Render() {
Settings::WriteDouble("resolutionScale", fieldResolutionScale);
Settings::Save();
+ Rml::RemoveContext("default");
+ rmlRender.reset();
+ rmlSystem.reset();
+
PluginSystem::Init();
framebuffer.reset();
@@ -125,6 +133,7 @@ void Render::InitGlew() {
glViewport(0, 0, width, height);
glClearColor(0.8,0.8,0.8, 1.0f);
glEnable(GL_DEPTH_TEST);
+ glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
@@ -220,6 +229,8 @@ void Render::HandleEvents() {
SDL_GL_GetDrawableSize(window, &width, &height);
renderState.WindowWidth = width;
renderState.WindowHeight = height;
+ rmlRender->Update(width, height);
+ rmlContext->SetDimensions(Rml::Vector2i(width, height));
framebuffer->Resize(width * fieldResolutionScale, height * fieldResolutionScale);
Framebuffer::GetDefault().Resize(width, height);
break;
@@ -331,6 +342,7 @@ void Render::HandleEvents() {
break;
}
}
+ rmlContext->Update();
}
void Render::HandleMouseCapture() {
@@ -366,6 +378,9 @@ void Render::Update() {
void Render::RenderGui() {
OPTICK_EVENT();
+
+ rmlContext->Render();
+
ImGui_ImplSdlGL3_NewFrame(window);
if (isMouseCaptured) {
@@ -750,3 +765,28 @@ void Render::InitEvents() {
}
});
}
+
+void Render::InitRml() {
+ LOG(INFO) << "Initializing Rml";
+
+ rmlSystem = std::make_unique<RmlSystemInterface>();
+ Rml::SetSystemInterface(rmlSystem.get());
+
+ rmlRender = std::make_unique<RmlRenderInterface>(renderState);
+ Rml::SetRenderInterface(rmlRender.get());
+ rmlRender->Update(renderState.WindowWidth, renderState.WindowHeight);
+
+ if (!Rml::Initialise())
+ LOG(WARNING) << "Rml not initialized";
+
+ rmlContext = Rml::CreateContext("default", Rml::Vector2i(renderState.WindowWidth, renderState.WindowHeight));
+
+ if (!Rml::LoadFontFace("OpenSans-Regular.ttf"))
+ LOG(WARNING) << "Rml font not loaded";
+
+ Rml::ElementDocument* document = rmlContext->LoadDocument("test.rml");
+ if (document)
+ document->Show();
+ else
+ LOG(WARNING) << "Rml document not loaded";
+}
diff --git a/src/Render.hpp b/src/Render.hpp
index 8e2e233..a8c437c 100644
--- a/src/Render.hpp
+++ b/src/Render.hpp
@@ -13,6 +13,12 @@
class RendererWorld;
class Framebuffer;
+class RmlRenderInterface;
+class RmlSystemInterface;
+namespace Rml
+{
+ class Context;
+}
class Render {
SDL_Window *window;
@@ -43,6 +49,9 @@ class Render {
bool fieldFlight;
float fieldBrightness;
float fieldResolutionScale;
+ std::unique_ptr<RmlRenderInterface> rmlRender;
+ std::unique_ptr<RmlSystemInterface> rmlSystem;
+ Rml::Context* rmlContext;
void SetMouseCapture(bool IsCaptured);
@@ -64,6 +73,8 @@ class Render {
void InitEvents();
+ void InitRml();
+
public:
Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle);
~Render();
diff --git a/src/Rml.cpp b/src/Rml.cpp
new file mode 100644
index 0000000..7987121
--- /dev/null
+++ b/src/Rml.cpp
@@ -0,0 +1,154 @@
+#include "Rml.hpp"
+
+#include <easylogging++.h>
+
+#include "AssetManager.hpp"
+#include "Shader.hpp"
+#include "Utility.hpp"
+
+double RmlSystemInterface::GetElapsedTime() {
+ return totalTime;
+}
+
+bool RmlSystemInterface::LogMessage(Rml::Log::Type type, const Rml::String& message) {
+ switch (type) {
+ case Rml::Log::Type::LT_ALWAYS:
+ LOG(ERROR) << message;
+ break;
+ case Rml::Log::Type::LT_ERROR:
+ LOG(ERROR) << message;
+ break;
+ case Rml::Log::Type::LT_ASSERT:
+ LOG(ERROR) << message;
+ break;
+ case Rml::Log::Type::LT_WARNING:
+ LOG(WARNING) << message;
+ break;
+ case Rml::Log::Type::LT_INFO:
+ LOG(INFO) << message;
+ break;
+ case Rml::Log::Type::LT_DEBUG:
+ LOG(DEBUG) << message;
+ break;
+ case Rml::Log::Type::LT_MAX:
+ LOG(DEBUG) << message;
+ break;
+ }
+ return true;
+}
+
+RmlRenderInterface::RmlRenderInterface(RenderState& renderState) : State(&renderState) {
+ glGenVertexArrays(1, &Vao);
+ glBindVertexArray(Vao);
+ glCheckError();
+
+ glGenBuffers(1, &Ebo);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Ebo);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_STREAM_DRAW);
+ glCheckError();
+
+ glGenBuffers(1, &Vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo);
+ glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STREAM_DRAW);
+ glCheckError();
+
+ {
+ //Vertex position (2 float)
+ GLuint PosAttribPos = 0;
+ glVertexAttribPointer(PosAttribPos, 2, GL_FLOAT, GL_FALSE, 20, (void*)0);
+ glEnableVertexAttribArray(PosAttribPos);
+
+ //Vertex colour (4 uint8 RGBA)
+ GLuint ColAttribPos = 1;
+ glVertexAttribIPointer(ColAttribPos, 4, GL_UNSIGNED_BYTE, 20, (void*)8);
+ glEnableVertexAttribArray(ColAttribPos);
+
+ //Vertex tex_coord (2 float)
+ GLuint TexAttribPos = 2;
+ glVertexAttribPointer(TexAttribPos, 2, GL_FLOAT, GL_FALSE, 20, (void*)12);
+ glEnableVertexAttribArray(TexAttribPos);
+ }
+ glBindVertexArray(0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+ glCheckError();
+}
+
+RmlRenderInterface::~RmlRenderInterface() {
+ glDeleteVertexArrays(1, &Vao);
+ glDeleteBuffers(1, &Vbo);
+ glDeleteBuffers(1, &Ebo);
+ glCheckError();
+}
+
+void RmlRenderInterface::RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture, const Rml::Vector2f& translation) {
+ if (texture) {
+ AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->Activate();
+ glBindTexture(GL_TEXTURE_2D, texture);
+ }
+ else
+ AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rml")->shader->Activate();
+ glCheckError();
+
+ glBindVertexArray(Vao);
+ glCheckError();
+
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo);
+ glBufferData(GL_ARRAY_BUFFER, num_vertices * sizeof(Rml::Vertex), vertices, GL_STREAM_DRAW);
+ glCheckError();
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Ebo);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_indices * sizeof(int), indices, GL_STREAM_DRAW);
+ glCheckError();
+
+ glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, 0);
+ glCheckError();
+ glBindVertexArray(0);
+}
+
+void RmlRenderInterface::EnableScissorRegion(bool enable) {
+
+}
+
+void RmlRenderInterface::SetScissorRegion(int x, int y, int width, int height) {
+
+}
+
+bool RmlRenderInterface::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) {
+ return false;
+}
+
+bool RmlRenderInterface::GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions) {
+ int mipLevelCount = 1;
+ glActiveTexture(GL_TEXTURE0);
+ GLuint texture;
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glCheckError();
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, source);
+ glCheckError();
+
+ texture_handle = texture;
+ return true;
+}
+
+void RmlRenderInterface::ReleaseTexture(Rml::TextureHandle texture) {
+ GLuint textures = texture;
+ glDeleteTextures(1, &textures);
+ glCheckError();
+}
+
+void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHeight) {
+ AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rml")->shader->Activate();
+ AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rml")->shader->SetUniform("viewportSize", windowWidth, windowHeight);
+ glCheckError();
+ AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->Activate();
+ AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->SetUniform("viewportSize", windowWidth, windowHeight);
+ AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->SetUniform("fontTexture", 0);
+ glCheckError();
+}
diff --git a/src/Rml.hpp b/src/Rml.hpp
new file mode 100644
index 0000000..5815c3e
--- /dev/null
+++ b/src/Rml.hpp
@@ -0,0 +1,56 @@
+#pragma once
+
+#include <RmlUi/Core/SystemInterface.h>
+#include <RmlUi/Core/RenderInterface.h>
+
+#include "Renderer.hpp"
+
+class RmlSystemInterface : public Rml::SystemInterface {
+ double totalTime;
+
+public:
+
+ virtual double GetElapsedTime() override;
+
+ virtual bool LogMessage(Rml::Log::Type type, const Rml::String& message) override;
+
+ inline void Update(double timeToUpdate) {
+ totalTime += timeToUpdate;
+ }
+
+};
+
+class RmlRenderInterface : public Rml::RenderInterface {
+ RenderState* State;
+
+ GLuint Vao, Vbo, Ebo;
+
+public:
+
+ RmlRenderInterface(RenderState &renderState);
+
+ RmlRenderInterface(const RmlRenderInterface&) = delete;
+
+ RmlRenderInterface(RmlRenderInterface&&) = delete;
+
+ RmlRenderInterface& operator=(const RmlRenderInterface&) = delete;
+
+ RmlRenderInterface& operator=(RmlRenderInterface&&) = delete;
+
+ ~RmlRenderInterface();
+
+ virtual void RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture, const Rml::Vector2f& translation) override;
+
+ virtual void EnableScissorRegion(bool enable) override;
+
+ virtual void SetScissorRegion(int x, int y, int width, int height) override;
+
+ virtual bool LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) override;
+
+ virtual bool GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions) override;
+
+ virtual void ReleaseTexture(Rml::TextureHandle texture) override;
+
+ void Update(unsigned int windowWidth, unsigned int windowHeight);
+
+};
diff --git a/src/Shader.hpp b/src/Shader.hpp
index 8e1ce9e..6b3220d 100644
--- a/src/Shader.hpp
+++ b/src/Shader.hpp
@@ -26,10 +26,18 @@ public:
void Activate();
+ inline void SetUniform(const std::string& name, unsigned int val, unsigned int val2) {
+ glUniform2ui(GetUniformLocation(name), val, val2);
+ }
+
inline void SetUniform(const std::string &name, int val) {
glUniform1i(GetUniformLocation(name), val);
}
+ inline void SetUniform(const std::string& name, int val, int val2) {
+ glUniform2i(GetUniformLocation(name), val, val2);
+ }
+
inline void SetUniform(const std::string &name, float val) {
glUniform1f(GetUniformLocation(name), val);
}