Text component added and Text render queue

This commit is contained in:
viktorljung
2015-12-14 14:48:24 +01:00
parent 9bd3da409d
commit 123c198e19
12 changed files with 115 additions and 30 deletions
+17
View File
@@ -8,6 +8,7 @@
#include "../GLM.h"
#include "../Core/Util/Rectangle.h"
#include "../Core/Entity.h"
#include "Font.h"
class Model;
class Skeleton;
@@ -80,6 +81,19 @@ struct SpriteJob : RenderJob
}
};
struct TextJob : RenderJob
{
glm::mat4 ModelMatrix;
glm::vec4 Color;
std::string Content;
Font* Resource;
void CalculateHash() override
{
Hash = 0;
}
};
struct PointLightJob : RenderJob
{
glm::vec3 Position;
@@ -137,17 +151,20 @@ struct RenderQueueCollection
{
RenderQueue Forward;
RenderQueue Lights;
RenderQueue Text;
void Clear()
{
Forward.Clear();
Lights.Clear();
Text.Clear();
}
void Sort()
{
Forward.Sort();
Lights.Sort();
Text.Sort();
}
};
@@ -20,6 +20,7 @@ private:
void FillModels(World* world, RenderQueue* renderQueue);
void FillLights(World* world, RenderQueue* renderQueue);
void FillText(World* world, RenderQueue* renderQueue);
glm::mat4 ModelMatrix(World* world, EntityID entity);
+3 -2
View File
@@ -9,6 +9,7 @@
#include "ShaderProgram.h"
#include "Font.h"
#include "../Core/ResourceManager.h"
#include "RenderQueue.h"
class TextRenderer
{
@@ -16,14 +17,14 @@ public:
TextRenderer();
void Initialize();
void Update();
void Draw(glm::mat4 projection, glm::mat4 view);
void Draw(RenderQueue &rq, glm::mat4 projection, glm::mat4 view);
private:
Font* font;
GLuint VAO, VBO;
void RenderText(std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color, glm::mat4 projection, glm::mat4 view);
void RenderText(std::string text, Font* font, GLfloat scale, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix);
ShaderProgram m_TextProgram;
+1
View File
@@ -5,4 +5,5 @@
<xs:include schemaLocation="Components/Model.xsd"/>
<xs:include schemaLocation="Components/Test.xsd"/>
<xs:include schemaLocation="Components/RaptorCopter.xsd"/>
<xs:include schemaLocation="Components/Text.xsd"/>
</xs:schema>
+6
View File
@@ -0,0 +1,6 @@
<c:Text>
<Content></Content>
<Resource></Resource>
<Color R="1" G="1" B="1" A="1"/>
<Visible>true</Visible>
</c:Text>
+27
View File
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
<xs:element name="Text">
<xs:annotation>
<xs:documentation>A visible font loaded from disk</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:all>
<xs:element name="Content" type="t:string" minOccurs="0">
<xs:annotation><xs:documentation>the content of the string printed</xs:documentation></xs:annotation>
</xs:element>
<xs:element name="Resource" type="t:string" minOccurs="0">
<xs:annotation><xs:documentation>font file</xs:documentation></xs:annotation>
</xs:element>
<xs:element name="Color" type="t:Color" minOccurs="0">
<xs:annotation><xs:documentation>Color</xs:documentation></xs:annotation>
</xs:element>
<xs:element name="Visible" type="t:bool" minOccurs="0">
<xs:annotation><xs:documentation>Wether the text is visible or not</xs:documentation></xs:annotation>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
+11
View File
@@ -21,6 +21,17 @@
</c:Model>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
<Scale X="1" Y="1" Z="1"/>
</c:Transform>
<c:Text>
<Resource>Fonts/arial.ttf</Resource>
<Content>Hej I am component text</Content>
</c:Text>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
+1
View File
@@ -14,6 +14,7 @@
<xs:element ref="c:Model" minOccurs="0"/>
<xs:element ref="c:Test" minOccurs="0"/>
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
<xs:element ref="c:Text" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
+1 -1
View File
@@ -27,7 +27,7 @@ Font::Font(std::string path)
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
continue;
}
printf("Char: %c\n", c);
//Generate texture
GLuint texture;
glGenTextures(1, &texture);
@@ -11,6 +11,7 @@ void RenderQueueFactory::Update(World* world)
m_RenderQueues.Clear();
FillModels(world, &m_RenderQueues.Forward);
FillLights(world, &m_RenderQueues.Lights);
FillText(world, &m_RenderQueues.Text);
}
glm::mat4 RenderQueueFactory::ModelMatrix(World* world, EntityID entity)
@@ -71,6 +72,9 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue)
}
for (auto& modelC : *models) {
std::string resource = modelC["Resource"];
if (resource.empty()) {
continue;
@@ -103,3 +107,32 @@ void RenderQueueFactory::FillLights(World* world, RenderQueue* renderQueue)
}
void RenderQueueFactory::FillText(World* world, RenderQueue* renderQueue)
{
auto texts = world->GetComponents("Text");
if (texts == nullptr) {
return;
}
for (auto& textC : *texts) {
std::string resource = textC["Resource"];
if (resource.empty()) {
continue;
}
Font* font = ResourceManager::Load<Font>(resource);
glm::vec4 color = textC["Color"];
std::string content = textC["Content"];
TextJob job;
job.Color = color;
job.Content = content;
job.Resource = font;
job.ModelMatrix = ModelMatrix(world, textC.EntityID);
renderQueue->Add(job);
}
}
+2 -1
View File
@@ -156,7 +156,7 @@ void Renderer::Draw(RenderQueueCollection& rq)
// DrawScreenQuad(m_PickingTexture);
DrawScene(rq);
m_TextRenderer->Draw(m_Camera->ProjectionMatrix(), m_Camera->ViewMatrix());
m_TextRenderer->Draw(rq.Text,m_Camera->ProjectionMatrix(), m_Camera->ViewMatrix());
glfwSwapBuffers(m_Window);
}
@@ -272,6 +272,7 @@ void Renderer::PickingPass(RenderQueueCollection& rq)
m_EventBroker->Publish(pickEvent);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
+12 -26
View File
@@ -7,11 +7,6 @@ TextRenderer::TextRenderer()
void TextRenderer::Initialize()
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
@@ -22,8 +17,6 @@ void TextRenderer::Initialize()
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
m_TextProgram.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Text.vert.glsl")));
m_TextProgram.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Text.frag.glsl")));
m_TextProgram.Compile();
@@ -35,40 +28,33 @@ void TextRenderer::Update()
}
void TextRenderer::Draw(glm::mat4 projection, glm::mat4 view)
void TextRenderer::Draw(RenderQueue &rq, glm::mat4 projection, glm::mat4 view)
{
if(counter > 2) {
if (text == "") {
text = text + "~wub ";
} else if (text == "~wub ") {
text = text + "wub ";
} else if (text == "~wub wub ") {
text = "";
for (auto &job : rq) {
auto textJob = std::dynamic_pointer_cast<TextJob>(job);
if (textJob) {
RenderText(textJob->Content, textJob->Resource, 0.01f, textJob->Color, textJob->ModelMatrix, projection, view);
}
counter = 0;
} else {
counter++;
}
RenderText(text, 0.f, 0.f, 1.0f, glm::vec3(1.f, 1.f, 1.f), projection, view);
}
void TextRenderer::RenderText(std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color, glm::mat4 projection, glm::mat4 view)
void TextRenderer::RenderText(std::string text, Font* font, GLfloat scale, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix)
{
font = ResourceManager::Load<Font>("fonts/arial.ttf");
GLfloat x = 0;
GLfloat y = 0;
glm::mat4 modelMatrix = glm::translate(glm::mat4(), glm::vec3(0.5f, 0.3f, 0.f)) * glm::toMat4(glm::quat()) * glm::scale(glm::vec3(0.01f));
// Activate corresponding render state
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_TextProgram.Bind();
glUniform3f(glGetUniformLocation(m_TextProgram.GetHandle(), "textColor"), color.x, color.y, color.z);
glUniformMatrix4fv(glGetUniformLocation(m_TextProgram.GetHandle(), "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(m_TextProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(m_TextProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(m_TextProgram.GetHandle(), "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(glGetUniformLocation(m_TextProgram.GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);