Skip to content

Commit

Permalink
GRAPHICS: Implement shader rendering methods in TextureFont
Browse files Browse the repository at this point in the history
  • Loading branch information
mirv-sillyfish authored and DrMcCoy committed Nov 17, 2018
1 parent 177f11a commit 11dd56d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/graphics/aurora/texturefont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include "src/graphics/aurora/textureman.h"
#include "src/graphics/aurora/texture.h"

#include "src/graphics/mesh/meshman.h"

#include "src/graphics/shader/surfaceman.h"

namespace Graphics {

namespace Aurora {
Expand All @@ -61,9 +65,18 @@ TextureFont::TextureFont(const Common::UString &name) : _height(1.0f), _spaceR(0
_texture = TextureMan.get(name);

load();

_mesh = static_cast<Mesh::MeshFont *>(MeshMan.getMesh("defaultMeshFont"));
_material = new Shader::ShaderMaterial(ShaderMan.getShaderObject("default/text.frag", Shader::SHADER_FRAGMENT), "text");
Shader::ShaderSampler *sampler;
sampler = (Shader::ShaderSampler *)(_material->getVariableData("sampler_0_id"));
sampler->handle = _texture;
_renderable = new Shader::ShaderRenderable(SurfaceMan.getSurface("textSurface"), _material, _mesh);
}

TextureFont::~TextureFont() {
delete _renderable;
delete _material;
}

float TextureFont::getWidth(uint32 c) const {
Expand Down Expand Up @@ -120,6 +133,58 @@ void TextureFont::draw(uint32 c) const {
glTranslatef(cC->second.width + _spaceR, 0.0f, 0.0f);
}

void TextureFont::renderBind(const glm::mat4 &transform) const {
glUseProgram(_renderable->getProgram()->glid);
_material->bindProgram(_renderable->getProgram(), 1.0f);
_material->bindGLState();
_renderable->getSurface()->bindProgram(_renderable->getProgram(), &transform);
_renderable->getSurface()->bindGLState();
_mesh->renderBind();

/**
* Mesh data will be dynamically updated for each character drawn to the screen
* (at least for now), so make sure the mesh VBO is bound. This isn't required
* for GL2.1, but is for GL3.2 because using the VAO doesn't automatically bind
* the VBO for data updates.
*/
glBindBuffer(GL_ARRAY_BUFFER, _mesh->getVertexBuffer()->getVBO());
}

void TextureFont::render(uint32 c, float &x, float &y, float *rgba) const {
std::map<uint32, Char>::const_iterator cC = _chars.find(c);

if (cC == _chars.end()) {
//drawMissing();
return;
}

float v_pos[12];
float v_uv[8];
float v_rgba[4*4];

for (int i = 0; i < 4; ++i) {
v_uv[i*2] = cC->second.tX[i];
v_uv[i*2 +1] = cC->second.tY[i];
v_pos[i*3] = x + cC->second.vX[i];
v_pos[i*3 +1] = y + cC->second.vY[i];
v_pos[i*3 +2] = 0.0f;
v_rgba[i*4] = rgba[0];
v_rgba[i*4 +1] = rgba[1];
v_rgba[i*4 +2] = rgba[2];
v_rgba[i*4 +3] = rgba[3];
}
_mesh->render(v_pos, v_uv, v_rgba);
x += cC->second.width + _spaceR;
}

void TextureFont::renderUnbind() const {
_mesh->renderUnbind();

_renderable->getSurface()->unbindGLState();
_material->unbindGLState();
glUseProgram(0);
}

void TextureFont::load() {
const Texture &texture = _texture.getTexture();
const TXI::Features &txiFeatures = texture.getTXI().getFeatures();
Expand Down
17 changes: 17 additions & 0 deletions src/graphics/aurora/texturefont.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

#include "src/graphics/aurora/texturehandle.h"

#include "src/graphics/mesh/meshfont.h"

#include "src/graphics/shader/shaderrenderable.h"

namespace Common {
class UString;
}
Expand All @@ -56,6 +60,15 @@ class TextureFont : public Graphics::Font {

void draw(uint32 c) const;

/**
* @brief Bind the font for rendering. Must be performed before render is called.
* @param transform Base modelview transform. Under most circumstances this is expected to be the identity matrix.
*/
virtual void renderBind(const glm::mat4 &transform) const;
virtual void render(uint32 c, float &x, float &y, float *rgba) const;
virtual void renderUnbind() const;


private:
/** A font character. */
struct Char {
Expand All @@ -72,6 +85,10 @@ class TextureFont : public Graphics::Font {
float _spaceR;
float _spaceB;

Mesh::MeshFont *_mesh;
Shader::ShaderMaterial *_material;
Shader::ShaderRenderable *_renderable;

void load();

void drawMissing() const;
Expand Down

0 comments on commit 11dd56d

Please sign in to comment.