graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit d0034d74211e0a2cd5a674d323614fcc3adb51ac
parent 6e434b962edf76c4f5dce0b3d9a6176dfa8fda4e
Author: Brian Swetland <swetland@frotz.net>
Date:   Tue, 10 Sep 2013 04:57:02 -0700

textgrid: use shared scene UBO, change how grid is specified

Diffstat:
Mcommon/assets/textgrid.glsl | 44+++++++++++++++++++-------------------------
Mcommon/textgrid.cc | 43++++++++++++++++---------------------------
Mcommon/textgrid.h | 10+++++-----
Mhello/hello.cc | 7+++++--
4 files changed, 45 insertions(+), 59 deletions(-)

diff --git a/common/assets/textgrid.glsl b/common/assets/textgrid.glsl @@ -3,41 +3,35 @@ -- vertex -layout(std140) uniform block3 { - mat4 MVP; - int cw; - int ch; -}; +layout (location = 0) in vec4 aPosition; +layout (location = 1) in vec2 aTexCoord; +layout (location = 2) in uint aCharacter; -layout (location = 0) in vec4 POSITION; -layout (location = 1) in vec2 TEXCOORD; -layout (location = 2) in uint CHARACTER; - -out vec2 vTEXCOORD; +out vec2 vTexCoord; void main() { - vec4 pos = POSITION; + ivec2 cell = uTextGrid.xy; + ivec2 dims = uTextGrid.zw; + vec4 pos = aPosition; int id = gl_InstanceID; - // shift unit rectangle to character cell rectangle - pos.xy += vec2(id % cw, (ch-1) - id / cw); + // translate cell to destination + pos.x += (id % dims.x) * cell.x; + pos.y += uOrthoSize.y - ((id / dims.x) + 1) * cell.y; // adjust unit texture coord to font cell rectangle - float tx = (CHARACTER % uint(16)); - float ty = (CHARACTER / uint(16)); + float tx = (aCharacter % uint(16)); + float ty = (aCharacter / uint(16)); - vTEXCOORD = - // scale to size of character in fonttexture - TEXCOORD * vec2(1.0/16.0,1.0/16.0) - // move to correct character + // translate texture coordinates to character position + vTexCoord = aTexCoord + vec2(tx/16.0,ty/16.0) - // offset to avoid cruft - + vec2(1.0/256.0,1.0/256.0); + + vec2(1.0/256.0,1.0/256.0); + pos = uOrtho * pos; - pos = MVP * pos; // discard via clipping - if (CHARACTER == uint(0)) pos.z = -1.1; + if (aCharacter == uint(0)) pos.z = -1.1; gl_Position = pos; } @@ -46,8 +40,8 @@ void main() { uniform sampler2D uTexture0; -in vec2 vTEXCOORD; +in vec2 vTexCoord; void main() { - gl_FragColor = texture2D(uTexture0, vTEXCOORD); + gl_FragColor = texture2D(uTexture0, vTexCoord); } diff --git a/common/textgrid.cc b/common/textgrid.cc @@ -38,61 +38,50 @@ static float unit_box_2d[] = { 0, 1, 0, 0, }; -int TextGrid::init(App *a, int w, int h) { - struct { - mat4 proj; - unsigned cw; - unsigned ch; - unsigned pad0; - unsigned pad1; - } cb; +int TextGrid::init(int cellw, int cellh, int columns, int rows) { VertexBuffer *data[] = { &vtx, &vtx, &cbuf, }; - width = w; - height = h; + float box_2d[4 * 6]; + width = columns; + height = rows; dirty = 0; - grid = (unsigned char*) malloc(w * h); + grid = (unsigned char*) malloc(width * height); if (!grid) return -1; clear(); if (texture.load("font-vincent-8x8.png", 0)) return -1; - - if (ps.load("textgrid.fragment")) - return -1; - if (vs.load("textgrid.vertex")) - return -1; - if (pgm.link(&vs, &ps)) + if (!(effect = Effect::load("textgrid"))) return -1; + // scale quad to character cell and texture cell size + for (int n = 0; n < (4 * 6); n += 4) { + box_2d[n + 0] = unit_box_2d[n + 0] * float(cellw); + box_2d[n + 1] = unit_box_2d[n + 1] * float(cellh); + box_2d[n + 2] = unit_box_2d[n + 2] * (1.0f / 16.0f); + box_2d[n + 3] = unit_box_2d[n + 3] * (1.0f / 16.0f); + } - cb.proj.setOrtho(0, w, 0, h, -1, 1); - cb.cw = width; - cb.ch = height; - - vtx.load(unit_box_2d, sizeof(unit_box_2d)); + vtx.load(box_2d, sizeof(box_2d)); cbuf.load(grid, width * height); - ubuf.load(&cb, sizeof(cb)); - attr.init(layout, data, sizeof(layout) / sizeof(layout[0])); return 0; } -void TextGrid::render(App *a) { +void TextGrid::render(void) { if (dirty) { dirty = 0; cbuf.load(grid, width * height); } - pgm.use(); attr.use(); - ubuf.use(3); texture.use(0); + effect->apply(); glDrawArraysInstanced(GL_TRIANGLES, 0, 6, width * height); } diff --git a/common/textgrid.h b/common/textgrid.h @@ -16,10 +16,12 @@ #ifndef _TEXTGRID_H_ #define _TEXTGRID_H_ +#include "Effect.h" + class TextGrid { public: - int init(App *a, int w, int h); - void render(App *a); + int init(int cellw, int cellh, int columns, int rows); + void render(void); void clear(void); void printf(int x, int y, const char *fmt, ...); @@ -31,9 +33,7 @@ private: UniformBuffer ubuf; VertexBuffer vtx; VertexBuffer cbuf; - PixelShader ps; - VertexShader vs; - Program pgm; + Effect *effect; Texture2D texture; VertexAttributes attr; diff --git a/hello/hello.cc b/hello/hello.cc @@ -131,7 +131,7 @@ int TestApp::init(void) { attr.init(layout, data, sizeof(layout) / sizeof(layout[0])); ibuf.use(); - if (text.init(this, width/16, height/16)) + if (text.init(16, 16, width/16, height/16)) return -1; onResize(); @@ -201,6 +201,9 @@ 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.OrthoSize.set(width, height, 0, 0); + scene.TextGrid.set(16, 16, width / 16, height / 16); scene.LightColor.set(1.0, 1.0, 1.0); scene.LightPosition = view * vec4(0, 1, 0, 0); scene.LightPosition.w = 0; @@ -230,7 +233,7 @@ void TestApp::render(void) { glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); - text.render(this); + text.render(); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); }