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
+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);