commit 894e81ee82f6c2dde64031459b7a081ffe5c2cf3
parent cb8c5db06aa27df1be5394c08917204c80108864
Author: Brian Swetland <swetland@frotz.net>
Date: Tue, 10 Sep 2013 05:49:22 -0700
textgrid/texturefont: uniform ortho mapping
- adjust textgrid to match texturefont
- 0,0 at top left, w-1,h-1 at bottom right
Diffstat:
7 files changed, 26 insertions(+), 40 deletions(-)
diff --git a/common/assets/textgrid.glsl b/common/assets/textgrid.glsl
@@ -16,17 +16,13 @@ void main() {
int id = gl_InstanceID;
// translate cell to destination
- pos.x += (id % dims.x) * cell.x;
- pos.y += uOrthoSize.y - ((id / dims.x) + 1) * cell.y;
+ pos.xy += vec2(id % dims.x, id / dims.x) * cell;
// adjust unit texture coord to font cell rectangle
- float tx = (aCharacter % uint(16));
- float ty = (aCharacter / uint(16));
+ vec2 tadj = vec2(aCharacter % uint(16), aCharacter / uint(16)) / 16.0;
// translate texture coordinates to character position
- vTexCoord = aTexCoord
- + vec2(tx/16.0,ty/16.0)
- + vec2(1.0/256.0,1.0/256.0);
+ vTexCoord = aTexCoord + tadj + vec2(1.0/256.0,1.0/256.0);
pos = uOrtho * pos;
diff --git a/common/assets/texturefont.glsl b/common/assets/texturefont.glsl
@@ -4,11 +4,7 @@
uniform samplerBuffer sampler0; // character data
uniform sampler2D sampler1; // glyph texture
-layout(std140) uniform block3 {
- mat4 xMVP;
-};
-
--- vs
+-- vertex
layout(location = 0) in ivec4 aData; // X, Y, ID, RGBA
@@ -25,10 +21,10 @@ void main() {
1.0
);
vTexCoord = vec2(cdata.zw);
- gl_Position = xMVP * pos;
+ gl_Position = uOrtho * pos;
}
--- fs
+-- fragment
in vec2 vTexCoord;
in vec4 vColor;
diff --git a/common/textgrid.cc b/common/textgrid.cc
@@ -30,12 +30,12 @@ static VertexAttrDesc layout[] = {
};
static float unit_box_2d[] = {
- 0, 1, 0, 0,
- 0, 0, 0, 1,
- 1, 0, 1, 1,
- 1, 0, 1, 1,
- 1, 1, 1, 0,
- 0, 1, 0, 0,
+ 0, 0, 0, 0,
+ 0, 1, 0, 1,
+ 1, 1, 1, 1,
+ 1, 1, 1, 1,
+ 1, 0, 1, 0,
+ 0, 0, 0, 0,
};
void TextGrid::resize(int columns, int rows) {
diff --git a/common/texturefont.cc b/common/texturefont.cc
@@ -34,7 +34,7 @@ static VertexAttrDesc layout[] = {
{ 0, SRC_INT32, DST_INTEGER, 4, 0, 16, 6 },
};
-int TextureFont::init(App *app, const char *fontname) {
+int TextureFont::init(const char *fontname) {
VertexBuffer *vdata[] = { &vtx };
char tmp[256];
float *cdata, *cp;
@@ -70,7 +70,7 @@ int TextureFont::init(App *app, const char *fontname) {
first = header->first;
last = first + header->count - 1;
- if (pgm.load("texturefont.vs", "texturefont.fs"))
+ if (!(effect = Effect::load("texturefont")))
goto fail;
cp = cdata = (float*) malloc(sizeof(float) * 4 * 6 * header->count);
@@ -114,9 +114,6 @@ int TextureFont::init(App *app, const char *fontname) {
cbuf.load(cdata, sizeof(float) * 4 * 6 * header->count);
vtx.load(data, sizeof(CharData) * max);
- mvp.setOrtho(0, app->getWidth(), app->getHeight(), 0, -1, 1);
- ubuf.load(&mvp, sizeof(mvp));
-
attr.init(layout, vdata, sizeof(layout) / sizeof(layout[0]));
dirty = 0;
@@ -149,7 +146,7 @@ void TextureFont::setColor(unsigned rgba) {
color = rgba;
}
-void TextureFont::render(App *app) {
+void TextureFont::render(void) {
if (count == 0)
return;
@@ -158,8 +155,7 @@ void TextureFont::render(App *app) {
dirty = 0;
}
- pgm.use();
- ubuf.use(3);
+ effect->apply();
attr.use();
glyphs.use(1);
glActiveTexture(GL_TEXTURE0 + 0);
diff --git a/common/texturefont.h b/common/texturefont.h
@@ -19,6 +19,8 @@
#include "app.h"
#include "matrix.h"
+#include "Effect.h"
+
struct CharInfo {
// location in texture
u16 x;
@@ -52,8 +54,8 @@ struct CharData {
class TextureFont {
public:
- int init(App *app, const char *fontname);
- void render(App *app);
+ int init(const char *fontname);
+ void render(void);
void clear(void);
void printf(int x, int y, const char *fmt, ...);
void puts(int x, int y, const char *s);
@@ -65,16 +67,11 @@ private:
unsigned first;
unsigned last;
- mat4 mvp;
- UniformBuffer ubuf;
+ Effect *effect;
VertexBuffer vtx;
VertexBuffer cbuf;
- PixelShader ps;
- GeometryShader gs;
- VertexShader vs;
- Program pgm;
- Texture2D glyphs;
VertexAttributes attr;
+ Texture2D glyphs;
unsigned tbid;
diff --git a/hello/hello.cc b/hello/hello.cc
@@ -202,7 +202,7 @@ void TestApp::render(void) {
object.mvp = model * view * proj;
object.mv = model * view;
- scene.Ortho.setOrtho(0, width, 0, height, -1.0, 1.0);
+ scene.Ortho.setOrtho(0, width, height, 0, -1.0, 1.0);
scene.OrthoSize.set(width, height, 0, 0);
scene.TextGrid.set(16, 16, width / 16, height / 16);
scene.LightColor.set(1.0, 1.0, 1.0);
diff --git a/test/object.cc b/test/object.cc
@@ -100,7 +100,7 @@ int TestApp::init(void) {
proj.setPerspective(D2R(90.0), width / (float) height, 0.1f, 250.0f);
- font.init(this, "orbitron-bold-72");
+ font.init("orbitron-bold-72");
return 0;
}
@@ -126,6 +126,7 @@ void TestApp::render(void) {
ge->apply();
g->render();
+ scene.Ortho.setOrtho(0, width, height, 0, -1.0, 1.0);
scene.LightColor.set(1.0, 1.0, 1.0);
scene.LightPosition = view * vec4(0, 1, 0, 0);
scene.LightPosition.w = 0;
@@ -165,7 +166,7 @@ void TestApp::render(void) {
font.clear();
font.puts(100, 100, "Hello World!");
- font.render(this);
+ font.render();
}
App *createApp(void) {