Merge remote-tracking branch 'origin/TextRendering' into Networking

# Conflicts:
#	include/Engine/Rendering/RenderSystem.h
#	src/Engine/Rendering/RenderSystem.cpp
This commit is contained in:
2016-01-22 22:08:56 +01:00
23 changed files with 741 additions and 96 deletions
+36
View File
@@ -0,0 +1,36 @@
#ifndef Font_h__
#define Font_h__
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
#include "../OpenGL.h"
#include "../GLM.h"
#include "../Core/ResourceManager.h"
class Font : public Resource
{
friend class ResourceManager;
private:
Font(std::string path);
public:
struct Character {
GLuint TextureID; // ID handle of the glyph texture
glm::ivec2 Size; // Size of glyph
glm::ivec2 Bearing; // Offset from baseline to left/top of glyph
GLuint Advance; // Offset to advance to next glyph
};
int FontSize = 16;
~Font();
std::map<GLchar, Character> m_Characters;
};
#endif
+3 -36
View File
@@ -11,49 +11,16 @@
#include "Camera.h"
#include "RenderJob.h"
#include "ModelJob.h"
#include "TextJob.h"
#include "PointLightJob.h"
#include "DirectionalLightJob.h"
/*
struct SpriteJob : RenderJob
{
unsigned int ShaderID = 0;
unsigned int TextureID = 0;
glm::mat4 ModelMatrix;
const Texture* DiffuseTexture = nullptr;
const Texture* NormalTexture = nullptr;
const Texture* SpecularTexture = nullptr;
glm::vec4 Color;
void CalculateHash() override
{
Hash = TextureID;
}
};
struct PointLightJob : RenderJob
{
glm::vec4 Position;
glm::vec4 Color;
float Radius;
float Intensity;
float Falloff;
float padding = 123;
void CalculateHash() override
{
Hash = 0;
}
};
*/
struct RenderScene
{
::Camera* Camera = nullptr;
std::list<std::shared_ptr<RenderJob>> ForwardJobs;
std::list<std::shared_ptr<RenderJob>> PointLightJobs;
std::list<std::shared_ptr<RenderJob>> TextJobs;
std::list<std::shared_ptr<RenderJob>> DirectionalLightJobs;
Rectangle Viewport;
bool ClearDepth = false;
@@ -62,6 +29,7 @@ struct RenderScene
{
ForwardJobs.clear();
PointLightJobs.clear();
TextJobs.clear();
DirectionalLightJobs.clear();
}
};
@@ -98,5 +66,4 @@ public:
private:
int m_Size = 0;
};
#endif
+1
View File
@@ -32,6 +32,7 @@ private:
EventRelay<RenderSystem, Events::SetCamera> m_ESetCamera;
bool OnSetCamera(Events::SetCamera &event);
void fillText(std::list<std::shared_ptr<RenderJob>>& jobs, World* world);
void fillPointLights(std::list<std::shared_ptr<RenderJob>>& jobs, World* world);
void fillDirectionalLights(std::list<std::shared_ptr<RenderJob>>& jobs, World* world);
EventRelay<RenderSystem, Events::InputCommand> m_EInputCommand;
+3
View File
@@ -18,6 +18,8 @@
#include "Camera.h"
#include "../Core/Transform.h"
#include "TextRenderer.h"
class Renderer : public IRenderer
{
public:
@@ -34,6 +36,7 @@ public:
private:
//----------------------Variables----------------------//
EventBroker* m_EventBroker;
TextRenderer* m_TextRenderer;
Texture* m_ErrorTexture;
Texture* m_WhiteTexture;
+58
View File
@@ -0,0 +1,58 @@
#ifndef TextJob_h__
#define TextJob_h__
#include <cstdint>
#include "../Common.h"
#include "../GLM.h"
#include "../Core/ComponentWrapper.h"
#include "Texture.h"
#include "RenderJob.h"
#include "../Core/ResourceManager.h"
#include "Font.h"
struct TextJob : RenderJob
{
TextJob(glm::mat4 matrix, Font* font, ComponentWrapper textComponent)
: RenderJob()
{
Matrix = matrix;
Color = (glm::vec4)textComponent["Color"];
Content = (std::string)textComponent["Content"];
Resource = font;
if ((int)textComponent["Alignment"] == textComponent["Alignment"].Enum("Left")) {
Alignment = AlignmentEnum::Left;
} else if ((int)textComponent["Alignment"] == textComponent["Alignment"].Enum("Right")) {
Alignment = AlignmentEnum::Right;
} else if ((int)textComponent["Alignment"] == textComponent["Alignment"].Enum("Center")) {
Alignment = AlignmentEnum::Center;
} else {
LOG_ERROR("Text alignment invalid");
Alignment = AlignmentEnum::Left;
}
};
enum class AlignmentEnum
{
Left,
Right,
Center
};
glm::mat4 Matrix;
glm::vec4 Color;
std::string Content;
Font* Resource;
AlignmentEnum Alignment;
void CalculateHash() override
{
Hash = 0;
}
};
#endif
+31
View File
@@ -0,0 +1,31 @@
#ifndef TextRenderer_h__
#define TextRenderer_h__
#include <ft2build.h>
#include FT_FREETYPE_H
#include "../OpenGL.h"
#include "../GLM.h"
#include "ShaderProgram.h"
#include "Font.h"
#include "../Core/ResourceManager.h"
#include "RenderQueue.h"
class TextRenderer
{
public:
TextRenderer();
void Initialize();
void Update();
void Draw(RenderScene& scene);
private:
void renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix);
Font* font;
GLuint VAO, VBO;
ShaderProgram* m_TextProgram;
};
#endif
+1
View File
@@ -18,6 +18,7 @@ public:
void Bind(GLenum textureUnit = GL_TEXTURE0);
GLuint m_Texture = 0;
};
#endif
+1
View File
@@ -19,6 +19,7 @@
#include "Rendering/RenderSystem.h"
#include "Core/EntityFileParser.h"
#include "Core/Octree.h"
#include "Rendering/Font.h"
#include "Systems/InterpolationSystem.h"
// Network
#include <boost/thread.hpp>
+1
View File
@@ -6,6 +6,7 @@
<xs:include schemaLocation="Components/Model.xsd"/>
<xs:include schemaLocation="Components/RaptorCopter.xsd"/>
<xs:include schemaLocation="Components/Player.xsd"/>
<xs:include schemaLocation="Components/Text.xsd"/>
<xs:include schemaLocation="Components/Camera.xsd"/>
<xs:include schemaLocation="Components/AABB.xsd"/>
<xs:include schemaLocation="Components/PointLight.xsd"/>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Text xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Text.xsd">
<Content>Text</Content>
<Resource></Resource>
<Color R="1" G="1" B="1" A="1"/>
<Visible>true</Visible>
<Alignment><Center/></Alignment>
</Text>
+42
View File
@@ -0,0 +1,42 @@
<?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:complexType name="AlignmentEnum" mixed="true">
<xs:complexContent>
<xs:extension base="t:enum">
<xs:choice>
<xs:element name="Right" type="xs:integer" fixed="0" minOccurs="0"/>
<xs:element name="Left" type="xs:integer" fixed="1" minOccurs="0"/>
<xs:element name="Center" type="xs:integer" fixed="2" minOccurs="0"/>
</xs:choice>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<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>The asset file, font resolution, example: Fonts/font.ttf,16</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:element name="Alignment" type="AlignmentEnum" minOccurs="0">
<xs:annotation><xs:documentation>Text alignment</xs:documentation></xs:annotation>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
+260 -56
View File
@@ -1,58 +1,262 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Entity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd" xmlns:c="components">
<Entity xmlns:c="components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd">
<Components>
<c:Transform>
<Position X="0" Y="0" Z="0"/>
<Orientation X="0" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="1" Z="10"/>
</c:Transform>
<c:Model>
<Resource>Models/Camera.obj</Resource>
</c:Model>
<c:Camera>
<Name>MainCamera</Name>
</c:Camera>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
<Position X="5" Y="1" Z="5"/>
</c:Transform>
<c:Model>
<Resource>Models/Camera.obj</Resource>
</c:Model>
<c:Camera>
<Name>ActionCamera</Name>
</c:Camera>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="-1" Z="0"/>
<Scale X="100" Y="1" Z="100"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitPlane.obj</Resource>
</c:Model>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="0" Z="0"/>
</c:Transform>
<c:Model>
<Resource>An error</Resource>
</c:Model>
</Components>
</Entity>
</Children>
</Entity>
<c:Transform/>
</Components>
<Children>
<Entity>
<Components>
<c:Camera>
<Name>ActionCamera</Name>
</c:Camera>
<c:Model>
<Resource>Models/Camera.obj</Resource>
</c:Model>
<c:Transform>
<Position X="-0.504807115" Y="6.00582743" Z="9.68687439"/>
<Orientation X="-0.733036518" Y="0.000105090476" Z="5.12391125e-07"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:Text>
<Content>Camera #2</Content>
<Resource>Fonts/DroidSans.ttf,64</Resource>
<Alignment>0</Alignment>
</c:Text>
<c:Transform>
<Position X="0.700063765" Y="0.310270816" Z="-1"/>
<Scale X="0.100000001" Y="0.100000001" Z="0.100000001"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity>
<Components>
<c:Model>
<Resource>Models/Core/UnitPlane.obj</Resource>
</c:Model>
<c:Transform>
<Position X="0" Y="-1" Z="0.600000024"/>
<Scale X="100" Y="1" Z="100"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Model>
<Resource>Models/Assault.obj</Resource>
</c:Model>
<c:Transform>
<Position X="-2.5" Y="0" Z="1"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Text>
<Content>Welcome!</Content>
<Resource>Fonts/DroidSans.ttf,1280</Resource>
</c:Text>
<c:Transform>
<Position X="0" Y="0" Z="0.700000048"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:RaptorCopter>
<Speed>2</Speed>
<Axis X="0" Y="1" Z="0"/>
</c:RaptorCopter>
<c:Transform>
<Position X="0" Y="2.37320328" Z="0"/>
<Orientation X="0" Y="8252.15918" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="1" G="0" R="1"/>
<Radius>6</Radius>
<Intensity>0.5</Intensity>
</c:PointLight>
<c:Transform>
<Position X="-2.5" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="0" G="0" R="1"/>
<Radius>8</Radius>
</c:PointLight>
<c:Transform>
<Position X="-5" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="0" G="1" R="1"/>
<Radius>6</Radius>
<Intensity>0.5</Intensity>
</c:PointLight>
<c:Transform>
<Position X="0" Y="0" Z="-2.5"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="0" G="1" R="0"/>
<Radius>8</Radius>
</c:PointLight>
<c:Transform>
<Position X="0" Y="0" Z="-5"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="1" G="1" R="0"/>
<Radius>6</Radius>
<Intensity>0.5</Intensity>
</c:PointLight>
<c:Transform>
<Position X="0" Y="0" Z="2.5"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:PointLight>
<Color A="1" B="1" G="0.00784313772" R="0"/>
<Radius>8</Radius>
</c:PointLight>
<c:Transform>
<Position X="0" Y="0" Z="5"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity>
<Components>
<c:PointLight>
<Radius>6</Radius>
<Intensity>0.5</Intensity>
</c:PointLight>
<c:Transform>
<Position X="2.5" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:PointLight>
<Radius>8</Radius>
</c:PointLight>
<c:Transform>
<Position X="5" Y="0" Z="0"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
</Children>
</Entity>
<Entity>
<Components>
<c:Model>
<Resource>Models/Assault.obj</Resource>
</c:Model>
<c:Transform>
<Position X="2.5" Y="0" Z="1"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Camera>
<Name>MainCamera</Name>
</c:Camera>
<c:Listener/>
<c:Model>
<Resource>Models/Camera.obj</Resource>
<Visible>false</Visible>
</c:Model>
<c:Transform>
<Position X="-1.27354908" Y="5.18422079" Z="10.6223917"/>
<Orientation X="-0.436328411" Y="-0.0523675606" Z="1.67869363e-08"/>
</c:Transform>
</Components>
<Children>
<Entity>
<Components>
<c:Text>
<Content>Taiwan #1</Content>
<Resource>Fonts/DroidSans.ttf,64</Resource>
<Alignment>0</Alignment>
</c:Text>
<c:Transform>
<Position X="0.716896772" Y="0.296712071" Z="-1"/>
<Scale X="0.100000001" Y="0.100000001" Z="0.100000001"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
<Entity>
<Components>
<c:DirectionalLight>
<Color A="1" B="0.996078432" G="1" R="1"/>
<Intensity>0.80000001192092896</Intensity>
</c:DirectionalLight>
<c:Model>
<Resource>Models/DirectionalLightWidget.obj</Resource>
</c:Model>
<c:Transform>
<Position X="-0.0013256073" Y="1.89540339" Z="6.53467274"/>
<Orientation X="-0.859663308" Y="-0.100310192" Z="-0.404723018"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
</c:Model>
<c:Transform>
<Position X="-5.4082036" Y="0" Z="-1.84950542"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
+1
View File
@@ -16,6 +16,7 @@
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
<xs:element ref="c:Player" minOccurs="0"/>
<xs:element ref="c:Camera" minOccurs="0"/>
<xs:element ref="c:Text" minOccurs="0"/>
<xs:element ref="c:AABB" minOccurs="0"/>
<xs:element ref="c:Trigger" minOccurs="0"/>
<xs:element ref="c:Health" minOccurs="0"/>
+12
View File
@@ -0,0 +1,12 @@
#version 430
in vec2 TexCoords;
out vec4 color;
uniform sampler2D text;
uniform vec3 textColor;
void main()
{
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);
color = vec4(textColor, 1.0) * sampled;
}
+13
View File
@@ -0,0 +1,13 @@
#version 430
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>
out vec2 TexCoords;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
void main()
{
gl_Position = P * V * M * vec4(vertex.xy, 0.0, 1.0);
TexCoords = vertex.zw;
}
+3
View File
@@ -8,6 +8,7 @@ find_package(assimp REQUIRED)
find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED)
find_package(Xerces REQUIRED)
find_package(Freetype REQUIRED)
# Because FindOpenAL is retarded
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${CMAKE_SOURCE_DIR}/deps/include/OpenAL")
find_package(OpenAL REQUIRED)
@@ -25,6 +26,7 @@ include_directories(
${assimp_INCLUDE_DIRS}
${PNG_INCLUDE_DIRS}
${Xerces_INCLUDE_DIRS}
${FREETYPE_INCLUDE_DIRS}
${OPENAL_INCLUDE_DIR}
${X11_INCLUDE_DIRS}
)
@@ -126,6 +128,7 @@ set(LIBRARIES
${assimp_LIBRARIES}
${PNG_LIBRARIES}
${Xerces_LIBRARIES}
${FREETYPE_LIBRARIES}
${OPENAL_LIBRARY}
${X11_LIBRARIES}
)
+108
View File
@@ -0,0 +1,108 @@
#include "Rendering/Font.h"
Font::Font(std::string path)
{
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
boost::char_separator<char> sep(",");
tokenizer tok(path, sep);
tokenizer::iterator it = tok.begin();
std::string filePath = "";
if (it != tok.end()) {
filePath = (*it).c_str();
it++;
if (it != tok.end()) {
if((*it).c_str() == "") {
throw std::runtime_error("");
}
try {
FontSize = boost::lexical_cast<int>((*it).c_str());
} catch (boost::bad_lexical_cast const&) {
LOG_ERROR("input string did not have a valid font resolution");
throw std::runtime_error("");
}
}
} else {
throw std::runtime_error("");;
}
FT_Library library;
FT_Face face;
if (FT_Init_FreeType(&library)) {
LOG_ERROR("FreeType error: init failed");
throw std::runtime_error("");;
}
if (FT_New_Face(library, filePath.c_str(), 0, &face)) {
LOG_ERROR("FreeType error: loading font");
throw std::runtime_error("");;
}
FT_Set_Char_Size(face, 0, FontSize*64, 300, 300); // temp
FT_Set_Pixel_Sizes(face, 0, FontSize); //
if (FT_Load_Char(face, 'X', FT_LOAD_RENDER)) {
LOG_ERROR("FreeType error: loading char");
throw std::runtime_error("");;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (GLubyte c = 0; c < 128; c++) {
//Load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
continue;
}
//Generate texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Now store character for later use
Character character = {
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x
};
m_Characters.insert(std::pair<GLchar, Character>(c, character));
}
FT_Done_Face(face);
FT_Done_FreeType(library);
GLERROR("Font Load");
}
Font::~Font()
{
for (auto c : m_Characters) {
glDeleteTextures(1, &c.second.TextureID);
}
}
+2 -4
View File
@@ -46,10 +46,8 @@ void PickingPass::InitializeShaderPrograms()
void PickingPass::Draw(RenderScene& scene)
{
PickingPassState* state = new PickingPassState(m_PickingBuffer.GetHandle());
//TODO: Render: Add code for more jobs than modeljobs.
GLuint ShaderHandle = m_PickingProgram->GetHandle();
m_PickingProgram->Bind();
@@ -78,9 +76,9 @@ void PickingPass::Draw(RenderScene& scene)
m_EntityColors[std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera)] = glm::ivec2(pickColor[0], pickColor[1]);
if (m_ColorCounter[0] > 255) {
m_ColorCounter[0] = 0;
m_ColorCounter[1]++;;
m_ColorCounter[1]++;
} else {
m_ColorCounter[0]++;;
m_ColorCounter[0]++;
}
}
@@ -8,6 +8,7 @@ PickingPassState::PickingPassState(GLuint frameBuffer)
GLERROR("---3");
Enable(GL_DEPTH_TEST);
Enable(GL_CULL_FACE);
Disable(GL_BLEND);
glm::vec4 clearColor = glm::vec4(0.f);
//ClearColor(clearColor);
+37
View File
@@ -110,6 +110,42 @@ void RenderSystem::fillDirectionalLights(std::list<std::shared_ptr<RenderJob>>&
}
}
void RenderSystem::fillText(std::list<std::shared_ptr<RenderJob>>& jobs, World* world)
{
auto texts = world->GetComponents("Text");
if (texts == nullptr) {
return;
}
for (auto& textComponent : *texts) {
bool visible = textComponent["Visible"];
if (!visible) {
continue;
}
std::string resource = textComponent["Resource"];
if (resource.empty()) {
continue;
}
Font* font;
try {
font = ResourceManager::Load<Font>(resource);
} catch (const std::exception&) {
try {
font = ResourceManager::Load<Font>("Fonts/DroidSans.ttf,16");
} catch (const std::exception&) {
continue;
}
}
glm::mat4 modelMatrix = Transform::ModelMatrix(textComponent.EntityID, world);
std::shared_ptr<TextJob> modelJob = std::shared_ptr<TextJob>(new TextJob(modelMatrix, font, textComponent));
jobs.push_back(modelJob);
}
}
bool RenderSystem::OnInputCommand(const Events::InputCommand& e)
{
return false;
@@ -133,6 +169,7 @@ void RenderSystem::Update(double dt)
fillModels(scene.ForwardJobs);
fillPointLights(scene.PointLightJobs, m_World);
fillDirectionalLights(scene.DirectionalLightJobs, m_World);
fillText(scene.TextJobs, world);
m_RenderFrame->Add(scene);
}
+8
View File
@@ -9,6 +9,9 @@ void Renderer::Initialize()
glfwSwapInterval(m_VSYNC);
InitializeShaders();
InitializeTextures();
m_TextRenderer = new TextRenderer();
m_TextRenderer->Initialize();
m_ScreenQuad = ResourceManager::Load<Model>("Models/Core/ScreenQuad.obj");
m_UnitQuad = ResourceManager::Load<Model>("Models/Core/UnitQuad.obj");
@@ -77,6 +80,7 @@ void Renderer::Update(double dt)
{
m_EventBroker->Process<Renderer>();
InputUpdate(dt);
m_TextRenderer->Update();
m_ImGuiRenderPass->Update(dt);
}
@@ -97,7 +101,11 @@ void Renderer::Draw(RenderFrame& frame)
//m_DrawScenePass->Draw(rq);
GLERROR("Renderer::Draw m_DrawScenePass->Draw");
m_TextRenderer->Draw(*scene);
}
m_ImGuiRenderPass->Draw();
glfwSwapBuffers(m_Window);
+110
View File
@@ -0,0 +1,110 @@
#include "Rendering/TextRenderer.h"
TextRenderer::TextRenderer()
{
}
void TextRenderer::Initialize()
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
m_TextProgram = ResourceManager::Load<ShaderProgram>("#TextProgram");
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();
m_TextProgram->Link();
}
void TextRenderer::Update()
{
}
void TextRenderer::Draw(RenderScene& scene)
{
for (auto &job : scene.TextJobs) {
auto textJob = std::dynamic_pointer_cast<TextJob>(job);
if (textJob) {
renderText(textJob->Content, textJob->Resource, textJob->Alignment, textJob->Color, textJob->Matrix, scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix());
}
}
}
void TextRenderer::renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix)
{
GLfloat penX = 0;
GLfloat penY = 0;
float scale = 1.0/font->FontSize;
GLfloat stringWidth = 0.f;
for (std::string::const_iterator c = text.begin(); c != text.end(); c++) {
Font::Character ch = font->m_Characters[*c];
stringWidth += (ch.Advance >> 6) * scale;
}
if(alignment == TextJob::AlignmentEnum::Center) {
penX = -stringWidth/2.f;
} else if (alignment == TextJob::AlignmentEnum::Right) {
penX = -stringWidth;
} else {
penX = 0;
}
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
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(viewMatrix));
glUniformMatrix4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);
for (std::string::const_iterator c = text.begin(); c != text.end(); c++) {
Font::Character ch = font->m_Characters[*c];
GLfloat xpos = penX + ch.Bearing.x * scale;
GLfloat ypos = penY - (ch.Size.y - ch.Bearing.y) * scale;
GLfloat w = ch.Size.x * scale;
GLfloat h = ch.Size.y * scale;
GLfloat vertices[6][4] = {
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos, ypos, 0.0, 1.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 0.0 }
};
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
penX += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64)
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
GLERROR("Text rendering Error");
}
+1
View File
@@ -20,6 +20,7 @@ Game::Game(int argc, char* argv[])
ResourceManager::RegisterType<Texture>("Texture");
ResourceManager::RegisterType<ShaderProgram>("ShaderProgram");
ResourceManager::RegisterType<EntityFile>("EntityFile");
ResourceManager::RegisterType<Font>("FontFile");
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
ResourceManager::UseThreading = m_Config->Get<bool>("Multithreading.ResourceLoading", true);