graphics

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

commit 12ddab860c05b4102639963fc7d17532263ecbe8
parent a7b3be0f56d702fe0289b05dc946950f21f824b2
Author: Brian Swetland <swetland@frotz.net>
Date:   Sat,  9 Feb 2013 03:36:06 -0800

textgrid and hello: update for new API and provide glsl shaders

Diffstat:
Acommon/TextPS.glsl | 8++++++++
Acommon/TextVS.glsl | 42++++++++++++++++++++++++++++++++++++++++++
Mcommon/textgrid.cc | 27+++++++++++++--------------
Mcommon/textgrid.h | 4++--
Ahello/SimplePS.glsl | 8++++++++
Ahello/SimpleVS.glsl | 30++++++++++++++++++++++++++++++
Mhello/hello.cc | 37++++++++++++++++++++++---------------
7 files changed, 125 insertions(+), 31 deletions(-)

diff --git a/common/TextPS.glsl b/common/TextPS.glsl @@ -0,0 +1,8 @@ + +uniform sampler2D uTexture0; + +in vec2 vTEXCOORD; + +void main() { + gl_FragColor = texture2D(uTexture0, vTEXCOORD); +} diff --git a/common/TextVS.glsl b/common/TextVS.glsl @@ -0,0 +1,42 @@ +#version 330 + +layout(std140) uniform cb0 { + mat4 MVP; + int cw; + int ch; +}; + + +in vec4 POSITION; +in vec2 TEXCOORD; +in uint CHARACTER; + +out vec2 vTEXCOORD; + + +void main() { + vec4 pos = POSITION; + int id = gl_InstanceID; + + // shift unit rectangle to character cell rectangle + pos.xy += vec2(id % cw, (ch-1) - id / ch); + + // adjust unit texture coord to font cell rectangle + float tx = (CHARACTER % uint(16)); + float ty = (CHARACTER / uint(16)); + + vTEXCOORD = + // scale to size of character in fonttexture + TEXCOORD * vec2(1.0/16.0,1.0/16.0) + // move to correct character + + vec2(tx/16.0,ty/16.0) + // offset to avoid cruft + + vec2(1.0/256.0,1.0/256.0); + + + pos = MVP * pos; + // discard via clipping + if (CHARACTER == uint(0)) pos.z = -1.1; + + gl_Position = pos; +} diff --git a/common/textgrid.cc b/common/textgrid.cc @@ -22,7 +22,7 @@ #include "matrix.h" #include "textgrid.h" -#define BUILTIN 1 +#define BUILTIN 0 #if BUILTIN #include "TextVS.h" @@ -45,10 +45,6 @@ static float unit_box_2d[] = { }; int TextGrid::init(App *a, int w, int h) { - void *data; - unsigned int dw, dh; - HRESULT hr; - struct { mat4 proj; unsigned cw; @@ -83,9 +79,9 @@ int TextGrid::init(App *a, int w, int h) { text_layout_desc, sizeof(text_layout_desc)/sizeof(text_layout_desc[0]))) return -1; #else - if (a->loadShader(&ps, "../common/TextPS.hlsl")) + if (a->loadShader(&ps, "../common/TextPS.glsl")) return -1; - if (a->loadShader(&vs, "../common/TextVS.hlsl", + if (a->loadShader(&vs, "../common/TextVS.glsl", text_layout_desc, sizeof(text_layout_desc)/sizeof(text_layout_desc[0]))) return -1; #endif @@ -95,6 +91,14 @@ int TextGrid::init(App *a, int w, int h) { cb.ch = height; a->updateBuffer(&ubuf, &cb); + a->initConfig(&cfg, &vs, &ps); + + a->useConfig(&cfg); + a->useBuffer(&ubuf, 0); + a->useTexture(&texture, 0); + a->useBuffer(&vtx, 0, 16, 0); + a->useBuffer(&cbuf, 1, 1, 0); + return 0; } @@ -103,11 +107,7 @@ void TextGrid::render(App *a) { dirty = 0; a->updateBuffer(&cbuf, grid); } - a->useShaders(&ps, &vs); - a->useBuffer(&ubuf, 0); - a->useTexture(&texture, 0); - a->useBuffer(&vtx, 0, 16, 0); - a->useBuffer(&cbuf, 1, 1, 0); + a->useConfig(&cfg); a->drawInstanced(6, width * height); } @@ -127,4 +127,4 @@ void TextGrid::printf(int x, int y, const char *fmt, ...) { if (y < 0) y = height + y; memcpy(grid + y * width + x, buf, len); // TODO rangecheck dirty = 1; -} -\ No newline at end of file +} diff --git a/common/textgrid.h b/common/textgrid.h @@ -34,8 +34,9 @@ private: PixelShader ps; VertexShader vs; Texture2D texture; + InputConfiguration cfg; unsigned char *grid; }; -#endif -\ No newline at end of file +#endif diff --git a/hello/SimplePS.glsl b/hello/SimplePS.glsl @@ -0,0 +1,8 @@ + +varying vec2 vTEXCOORD; +varying float vDIFFUSE; + +void main() { + vec4 c = vec4(1.0, 0.0, 0.0, 1.0); + gl_FragColor = c * 0.25 + c * vDIFFUSE; +} diff --git a/hello/SimpleVS.glsl b/hello/SimpleVS.glsl @@ -0,0 +1,30 @@ +#version 330 + +layout(std140) uniform cb0 { + mat4 MVP; + mat4 MV; +}; + +in vec4 POSITION; +in vec4 NORMAL; +in vec2 TEXCOORD; +in vec4 LOCATION; + +out vec2 vTEXCOORD; +out float vDIFFUSE; + +void main() { + vec4 pos = POSITION; + + pos.xyz += LOCATION.xyz * vec3(127,127,127); + + vec3 mvPosition = (MV * pos).xyz; + vec3 mvNormal = (MV * vec4(NORMAL.xyz,0.0)).xyz; + + vec3 lightVec = normalize(vec3(10,20,25) - mvPosition); + float diffuse = max(dot(mvNormal, lightVec), 0.0); + + gl_Position = MVP * pos; //POSITION; + vTEXCOORD = TEXCOORD; + vDIFFUSE = diffuse; +} diff --git a/hello/hello.cc b/hello/hello.cc @@ -36,7 +36,7 @@ static float locationx[] = { 4, 0, 0, }; -#define SZ 16 +#define SZ 32 #define SZh (SZ / 2) #define SZe (SZ * SZ * SZ) #define SZb (SZe * 4) @@ -54,7 +54,6 @@ public: private: float t; - DWORD timeStart; PixelShader ps; VertexShader vs; @@ -62,6 +61,7 @@ private: VertexBuffer vbuf; UniformBuffer ubuf; VertexBuffer lbuf; + InputConfiguration cfg; TextGrid text; float nx,ny; @@ -71,7 +71,9 @@ private: struct model *m; }; -TestApp::TestApp() : App(), t(0.0f), timeStart(0), zoom(0), rx(0), ry(0), nx(0), ny(0) { +TestApp::TestApp() : App(), t(0.0f), zoom(0), rx(0), ry(0), nx(0), ny(0) { + width = 1280; + height = 1024; } void TestApp::release(void) { @@ -112,9 +114,9 @@ void TestApp::build(void) { } int TestApp::init(void) { - if (loadShader(&ps, "SimplePS.hlsl")) + if (loadShader(&ps, "SimplePS."SL )) return -1; - if (loadShader(&vs, "SimpleVS.hlsl", obj_layout, sizeof(obj_layout) / sizeof(obj_layout[0]))) + if (loadShader(&vs, "SimpleVS."SL, obj_layout, sizeof(obj_layout) / sizeof(obj_layout[0]))) return -1; if (!(m = load_wavefront_obj("unitcubeoid.obj"))) @@ -128,11 +130,19 @@ int TestApp::init(void) { if (initBuffer(&ubuf, NULL, 32 * 4)) return -1; + if (initConfig(&cfg, &vs, &ps)) + return -1; proj.setPerspective(D2R(90.0), width / (float) height, 0.1f, 250.0f); build(); zoom = SZ; + useConfig(&cfg); + useBuffer(&ubuf, 0); + useBuffer(&vbuf, 0, 32, 0); + useBuffer(&lbuf, 1, 4, 0); + useBuffer(&ibuf); + if (text.init(this, 64, 64)) return -1; @@ -142,7 +152,7 @@ int TestApp::init(void) { static float rate = 90.0; void TestApp::render(void) { - UINT stride, offset; + unsigned stride, offset; int update = 0; if (mouseBTN & 1) { @@ -162,25 +172,23 @@ void TestApp::render(void) { if (zoom > 100.0) zoom = 100.0; } +#if _WIN32 if (keystate[DIK_A]) { nx -= 0.01; update = 1; } if (keystate[DIK_D]) { nx += 0.01; update = 1; } if (keystate[DIK_W]) { ny -= 0.01; update = 1; } if (keystate[DIK_S]) { ny += 0.01; update = 1; } if (keystate[DIK_P]) { - loadShader(&ps, "HelloPS.hlsl"); - loadShader(&vs, "SimpleVS.hlsl", obj_layout, + loadShader(&ps, "HelloPS."SL); + loadShader(&vs, "SimpleVS."SL, obj_layout, sizeof(obj_layout) / sizeof(obj_layout[0])); } oops: if (update) build(); +#endif - useShaders(&ps, &vs); - useBuffer(&ubuf, 0); - useBuffer(&vbuf, 0, 32, 0); - useBuffer(&lbuf, 1, 4, 0); - useBuffer(&ibuf); + useConfig(&cfg); struct { mat4 mvp; @@ -207,4 +215,4 @@ oops: App *createApp(void) { return new TestApp(); -} -\ No newline at end of file +}