Skip to content

Commit

Permalink
[World] add debug texts
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthou committed Aug 16, 2024
1 parent bc52384 commit 25bdaef
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 9 deletions.
20 changes: 20 additions & 0 deletions include/overworld/Engine/Common/Debug/DebugText.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef OWDS_DEBUGTEXT_H
#define OWDS_DEBUGTEXT_H

#include <glm/vec3.hpp>
#include <string>

namespace owds {

struct DebugText_t
{
std::string text;
bool centered = false;
glm::vec3 position;
glm::vec3 color;
float height = 0.2;
};

} // namespace owds

#endif // OWDS_DEBUGTEXT_H
10 changes: 10 additions & 0 deletions include/overworld/Engine/Common/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <unordered_map>
#include <vector>

#include "overworld/Engine/Common/Debug/DebugText.h"
#include "overworld/Engine/Common/Lights/AmbientLight.h"
#include "overworld/Engine/Common/Lights/PointLights.h"
#include "overworld/Engine/Common/Shapes/Shape.h"
Expand Down Expand Up @@ -208,6 +209,14 @@ namespace owds {
void setPointLightPosition(std::size_t id, const glm::vec3& position);
void setPointLightAmbientStrength(std::size_t id, float strength);

int addDebugText(const std::string& text,
const std::array<float, 3>& position,
float height,
const std::array<float, 3>& color = {1.0, 1.0, 1.0},
bool centered = true,
int replace_id = -1);
void removeDebugText(int id);

/**
* @param gravity Self-explanatory.
*/
Expand All @@ -234,6 +243,7 @@ namespace owds {

AmbientLight ambient_light_;
PointLights point_lights_;
std::vector<DebugText_t> debug_texts_;
};
} // namespace owds

Expand Down
3 changes: 3 additions & 0 deletions include/overworld/Engine/Graphics/OpenGL/TextRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <map>
#include <string>

#include "overworld/Engine/Common/Debug/DebugText.h"

namespace owds {

class Shader;
Expand All @@ -27,6 +29,7 @@ namespace owds {
bool load(const std::string& font, unsigned int font_size);

void renderText(Shader& shader, const glm::mat4& view_matrix, const std::string& text, const glm::vec3& position, float height, const glm::vec3& color, bool center = false);
void renderText(Shader& shader, const glm::mat4& view_matrix, const DebugText_t& text);

private:
unsigned int vao_;
Expand Down
53 changes: 53 additions & 0 deletions src/Engine/Common/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,57 @@ namespace owds {
point_lights_.setAmbientStrength(id, strength);
}

int World::addDebugText(const std::string& text,
const std::array<float, 3>& position,
float height,
const std::array<float, 3>& color,
bool centered,
int replace_id)
{
DebugText_t debug{
text,
centered,
glm::vec3(position[0], position[1], position[2]),
glm::vec3(color[0], color[1], color[2]),
height};

if(replace_id >= 0)
{
if(replace_id < (int)debug_texts_.size())
{
debug_texts_[replace_id] = debug;
return replace_id;
}
else
return -1;
}
else
{
int id = -1;
for(size_t i = 0; i < debug_texts_.size(); i++)
{
if(debug_texts_[i].text.empty())
{
id = i;
break;
}
}

if(id >= 0)
debug_texts_[id] = debug;
else
{
id = debug_texts_.size();
debug_texts_.emplace_back(debug);
}
return id;
}
}

void World::removeDebugText(int id)
{
if(id >= 0 && id < (int)debug_texts_.size())
debug_texts_[id].text = "";
}

} // namespace owds
7 changes: 5 additions & 2 deletions src/Engine/Graphics/OpenGL/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,11 @@ namespace owds {

text_shader.use();
text_shader.setMat4("projection", render_camera_.getProjectionMatrix());

text_renderer_.renderText(text_shader, render_camera_.getViewMatrix(), "tesH", glm::vec3(0, 0, 5), 1, glm::vec3(1.f, 0.f, 1.f), true);
for(auto& debug : world_->debug_texts_)
{
if(debug.text.empty() == false)
text_renderer_.renderText(text_shader, render_camera_.getViewMatrix(), debug);
}

// 2. now blit multisampled buffer(s) to normal colorbuffer of intermediate FBO. Image is stored in screenTexture
glDisable(GL_BLEND);
Expand Down
10 changes: 10 additions & 0 deletions src/Engine/Graphics/OpenGL/TextRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ namespace owds {
return true;
}

void TextRenderer::renderText(Shader& shader, const glm::mat4& view_matrix, const DebugText_t& text)
{
renderText(shader, view_matrix,
text.text,
text.position,
text.height,
text.color,
text.centered);
}

void TextRenderer::renderText(Shader& shader, const glm::mat4& view_matrix, const std::string& text, const glm::vec3& position, float height, const glm::vec3& color, bool center)
{
shader.use();
Expand Down
21 changes: 14 additions & 7 deletions src/Nodes/TestOpengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,26 @@ int main()

std::cout << "================== WORLD CREATED ! ================" << std::endl;

world.setAmbientLight({-0.2f, -0.5f, -0.3f},
world.setAmbientLight({0.5f, 0.2f, -0.3f},
{1.0f, 0.976f, 0.898f},
0.3, 0.5, 0.9);
0.25, 0.4, 0.8);

world.addPointLight({1.5f, 2.0f, 2.9f},
world.addPointLight({5.f, 7.0f, 2.9f},
{1.0f, 1.0f, 1.0f},
0.4, 0.5, 1.0,
5.f);
6.f);

world.addPointLight({1.5f, 10.0f, 2.9f},
{0.0f, 1.0f, 1.0f},
world.addPointLight({8.5f, 7.0f, 2.9f},
{1.0f, 1.0f, 1.0f},
0.5, 0.6, 1.0,
8.0f);
6.0f);

world.addPointLight({5.f, 12.0f, 2.9f},
{1.0f, 1.0f, 1.0f},
0.4, 0.5, 1.0,
6.f);

world.addDebugText("overworld", {5, 5, 5}, 0.5, {0., 0.5, 1.0});

renderer.attachWorld(&world);

Expand Down

0 comments on commit 25bdaef

Please sign in to comment.