Skip to content

Commit

Permalink
GRAPHICS: Shader based debug console rendering
Browse files Browse the repository at this point in the history
Fix console and ttfont to use shader based rendering if the
experimental renderer is enabled.
  • Loading branch information
mirv-sillyfish authored and DrMcCoy committed Feb 18, 2019
1 parent 2f7666b commit 3e2d262
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/engines/aurora/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include <boost/bind.hpp>

#include "glm/gtc/matrix_transform.hpp"

#include "src/common/util.h"
#include "src/common/strutil.h"
#include "src/common/filepath.h"
Expand Down Expand Up @@ -57,6 +59,10 @@
#include "src/engines/aurora/console.h"
#include "src/engines/aurora/util.h"

#include "src/graphics/mesh/meshman.h"
#include "src/graphics/shader/surfaceman.h"
#include "src/graphics/shader/materialman.h"


static const uint32 kDoubleClickTime = 500;

Expand Down Expand Up @@ -113,6 +119,22 @@ ConsoleWindow::ConsoleWindow(const Common::UString &font, size_t lines, size_t h

calculateDistance();

_shaderRenderableBackdrop.setMesh(MeshMan.getMesh("defaultMeshQuad"));
_shaderRenderableBackdrop.setSurface(SurfaceMan.getSurface("defaultSurface"), false);
_shaderRenderableBackdrop.setMaterial(MaterialMan.getMaterial("defaultBlack75"));

_shaderRenderableBottomEdge.setMesh(MeshMan.getMesh("defaultMeshQuad"));
_shaderRenderableBottomEdge.setSurface(SurfaceMan.getSurface("defaultSurface"), false);
_shaderRenderableBottomEdge.setMaterial(MaterialMan.getMaterial("defaultBlack"));

_shaderRenderableScrollBackground.setMesh(MeshMan.getMesh("defaultMeshQuad"));
_shaderRenderableScrollBackground.setSurface(SurfaceMan.getSurface("defaultSurface"), false);
_shaderRenderableScrollBackground.setMaterial(MaterialMan.getMaterial("defaultBlack"));

_shaderRenderableScrollbar.setMesh(MeshMan.getMesh("defaultMeshQuad"));
_shaderRenderableScrollbar.setSurface(SurfaceMan.getSurface("defaultSurface"), false);
_shaderRenderableScrollbar.setMaterial(MaterialMan.getMaterial("defaultGrey50"));

openLogFile();
}

Expand Down Expand Up @@ -612,6 +634,37 @@ void ConsoleWindow::render(Graphics::RenderPass pass) {
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

void ConsoleWindow::renderImmediate(const glm::mat4 &parentTransform) {
uint32 now = EventMan.getTimestamp();
if ((now - _lastCursorBlink) > 500) {
_cursorBlinkState = !_cursorBlinkState;
_lastCursorBlink = now;

_cursor->setColor(1.0f, 1.0f, 1.0f, _cursorBlinkState ? 1.0f : 0.0f);
}

glm::mat4 backdropTransform = parentTransform;
backdropTransform = glm::translate(backdropTransform, glm::vec3(_x, _y, 0.0f));
backdropTransform = glm::scale(backdropTransform, glm::vec3(_width, _height, 1.0f));

glm::mat4 bottomEdgeTransform = parentTransform;
bottomEdgeTransform = glm::translate(bottomEdgeTransform, glm::vec3(_x, _y - 3.0f, 0.0f));
bottomEdgeTransform = glm::scale(bottomEdgeTransform, glm::vec3(_width, 3.0f, 1.0f));

glm::mat4 scrollBackgroundTransform = parentTransform;
scrollBackgroundTransform = glm::translate(scrollBackgroundTransform, glm::vec3(_x + _width - 12.0f, _y, 0.0f));
scrollBackgroundTransform = glm::scale(scrollBackgroundTransform, glm::vec3(12.0f, _height, 1.0f));

glm::mat4 scrollbarTransform = parentTransform;
scrollbarTransform = glm::translate(scrollbarTransform, glm::vec3(_x + _width - 10.0f, _y + 2.0f + _scrollbarPosition, 0.0f));
scrollbarTransform = glm::scale(scrollbarTransform, glm::vec3(8.0f, _scrollbarLength, 1.0f));

_shaderRenderableBackdrop.renderImmediate(backdropTransform);
_shaderRenderableBottomEdge.renderImmediate(bottomEdgeTransform);
_shaderRenderableScrollBackground.renderImmediate(scrollBackgroundTransform);
_shaderRenderableScrollbar.renderImmediate(scrollbarTransform);
}

void ConsoleWindow::notifyResized(int UNUSED(oldWidth), int UNUSED(oldHeight),
int newWidth, int newHeight) {

Expand Down
7 changes: 7 additions & 0 deletions src/engines/aurora/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include "src/graphics/aurora/types.h"
#include "src/graphics/aurora/fonthandle.h"

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

namespace Common {
class ReadLine;
}
Expand Down Expand Up @@ -121,6 +123,7 @@ class ConsoleWindow : public Graphics::GUIElement, public Events::Notifyable {
void calculateDistance();
void render(Graphics::RenderPass pass);

void renderImmediate(const glm::mat4 &parentTransform);

private:
Graphics::Aurora::FontHandle _font;
Expand Down Expand Up @@ -164,6 +167,10 @@ class ConsoleWindow : public Graphics::GUIElement, public Events::Notifyable {
Common::WriteFile _logFile;
Common::WriteFile _redirect;

Graphics::Shader::ShaderRenderable _shaderRenderableBackdrop;
Graphics::Shader::ShaderRenderable _shaderRenderableBottomEdge;
Graphics::Shader::ShaderRenderable _shaderRenderableScrollBackground;
Graphics::Shader::ShaderRenderable _shaderRenderableScrollbar;

void recalcCursor();
void redrawLines();
Expand Down
82 changes: 82 additions & 0 deletions src/graphics/aurora/ttffont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include "src/graphics/aurora/textureman.h"
#include "src/graphics/aurora/texture.h"

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

static const uint32 kPageWidth = 256;
static const uint32 kPageHeight = 256;

Expand Down Expand Up @@ -77,6 +80,8 @@ TTFFont::TTFFont(const Common::UString &name, int height) {
}

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

void TTFFont::load(Common::SeekableReadStream *ttf, int height) {
Expand Down Expand Up @@ -111,6 +116,16 @@ void TTFFont::load(Common::SeekableReadStream *ttf, int height) {
_missingWidth = _missingChar->second.width;

rebuildPages();

_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"));
if (_pages.size() > 0) {
// At the moment this doesn't matter too much - just need a valid texture of some kind.
sampler->handle = _pages[0]->texture;
}
_renderable = new Shader::ShaderRenderable(SurfaceMan.getSurface("textSurface"), _material, _mesh);
}

float TTFFont::getWidth(uint32 c) const {
Expand Down Expand Up @@ -173,6 +188,73 @@ void TTFFont::buildChars(const Common::UString &str) {
rebuildPages();
}

void TTFFont::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 TTFFont::render(uint32 c, float &x, float &y, float *rgba) const {
std::map<uint32, Char>::const_iterator cC = _chars.find(c);

if (cC == _chars.end()) {
// Nothing is rendered for missing characters. Maybe one day use a placeholder instead.
x += _missingWidth - 1.0f;
return;
}


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

size_t page = cC->second.page;
assert(page < _pages.size());

/* The assumption here is that the material will have been bound, and either only
* constains the single sampler, or the sampler was bound last. In either case, the
* active texture unit is properly set and the appropriate texture can be easily
* bound on the fly.
* todo: It would be better if the textures were place entirely into one large texture
* and the UV coordinates offset as appropriate. It's also likely better if instead of
* multiple render calls here, if instead everything can be batched together into a
* single render call.
*/
TextureMan.set(_pages[page]->texture);

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;
}

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

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

void TTFFont::rebuildPages() {
for (std::vector<Page *>::iterator p = _pages.begin(); p != _pages.end(); ++p)
(*p)->rebuild();
Expand Down
18 changes: 18 additions & 0 deletions src/graphics/aurora/ttffont.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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 @@ -60,6 +64,15 @@ class TTFFont : public Graphics::Font {

void buildChars(const Common::UString &str);

/** 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 texture page filled with characters. */
struct Page {
Expand Down Expand Up @@ -100,6 +113,11 @@ class TTFFont : public Graphics::Font {

uint32 _height;

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


void load(Common::SeekableReadStream *ttf, int height);

void rebuildPages();
Expand Down
21 changes: 21 additions & 0 deletions src/graphics/shader/materialman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ void MaterialManager::init() {
color[3] = 1.0f;
}
_resourceMap[material->getName()] = material;

material = new ShaderMaterial(ShaderMan.getShaderObject("default/colour.frag", SHADER_FRAGMENT), "defaultBlack75");
color = (float *)(material->getVariableData("_colour"));
if (color) {
color[0] = 0.0f;
color[1] = 0.0f;
color[2] = 0.0f;
color[3] = 0.75f;
}
_resourceMap[material->getName()] = material;

material = new ShaderMaterial(ShaderMan.getShaderObject("default/colour.frag", SHADER_FRAGMENT), "defaultGrey50");
color = (float *)(material->getVariableData("_colour"));
if (color) {
color[0] = 0.5f;
color[1] = 0.5f;
color[2] = 0.5f;
color[3] = 0.5f;
}
_resourceMap[material->getName()] = material;

}

void MaterialManager::deinit() {
Expand Down

0 comments on commit 3e2d262

Please sign in to comment.