commit e5a6dacd1ee9ae3d7774a154546a5f48dbfd3acd
parent 8a5148e5771b02df1e2759a3ee5072148b7622fb
Author: Brian Swetland <swetland@frotz.net>
Date: Thu, 5 Sep 2013 21:43:04 -0700
hello: update to using common uniforms and Effect class
Diffstat:
3 files changed, 84 insertions(+), 66 deletions(-)
diff --git a/common/shared.h b/common/shared.h
@@ -40,9 +40,10 @@ struct ubObject {
};
struct ubMaterial {
- vec4 Ambient;
- vec4 Diffuse;
- vec4 Specular;
+ vec4 Ambient; /* ambient factor (of Color) */
+ vec4 Diffuse; /* diffuse factor (of Color) */
+ vec4 Specular; /* specular factor (of LightColor) */
+ vec4 Color; /* used if non-textured */
float Shininess;
};
@@ -65,6 +66,7 @@ static const char *shader_globals =
" vec4 uAmbient;\n"
" vec4 uDiffuse;\n"
" vec4 uSpecular;\n"
+" vec4 uColor;\n"
" float uShininess;\n"
"};\n"
;
diff --git a/hello/assets/simple.glsl b/hello/assets/simple.glsl
@@ -3,42 +3,52 @@
-- vertex
-layout(std140) uniform block3 {
- mat4 MVP;
- mat4 MV;
-};
-
-layout(location = A_POSITION) in vec4 POSITION;
-layout(location = A_NORMAL) in vec4 NORMAL;
-layout(location = A_TEXCOORD) in vec2 TEXCOORD;
+layout(location = A_POSITION) in vec4 aPosition;
+layout(location = A_NORMAL) in vec4 aNormal;
+layout(location = A_TEXCOORD) in vec2 aTexCoord;
in vec4 LOCATION;
-out vec2 vTEXCOORD;
-out float vDIFFUSE;
+out vec2 vTexCoord;
+out vec3 vPosition;
+out vec3 vNormal;
void main() {
- vec4 pos = POSITION;
-
- pos.xyz += LOCATION.xyz * vec3(127,127,127);
+ vec4 pos = aPosition;
- vec3 mvPosition = (MV * pos).xyz;
- vec3 mvNormal = (MV * vec4(NORMAL.xyz,0.0)).xyz;
+ pos.xyz += LOCATION.xyz * vec3(127.0, 127.0, 127.0);
- 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;
+ vPosition = (uMV * pos).xyz;
+ vNormal = (uMV * vec4(aNormal.xyz,0.0)).xyz;
+ vTexCoord = aTexCoord;
+ gl_Position = uMVP * pos;
}
-- fragment
-varying vec2 vTEXCOORD;
-varying float vDIFFUSE;
+in vec2 vTexCoord;
+in vec3 vPosition;
+in vec3 vNormal;
void main() {
- vec4 c = vec4(1.0, 0.0, 0.0, 1.0);
- gl_FragColor = c * 0.25 + c * vDIFFUSE;
+ vec4 c = uColor;
+ vec3 n = normalize(vNormal);
+ vec3 s;
+ if (uLightPosition.w > 0) {
+ /* positional light, compute direction */
+ s = normalize(uLightPosition.xyz - vPosition);
+ } else {
+ /* directional light - light position is actually a vector */
+ s = uLightPosition.xyz;
+ }
+ vec3 v = normalize(-vPosition);
+ vec3 h = normalize(v + s);
+
+ gl_FragColor = uAmbient * c
+ + uDiffuse * c * max( dot(s, n), 0.0)
+#ifdef SPECULAR
+ + uSpecular * uLightColor * pow( max( dot(h,n), 0.0), uShininess)
+#endif
+ ;
+
}
diff --git a/hello/hello.cc b/hello/hello.cc
@@ -20,6 +20,8 @@
#include "matrix.h"
#include "util.h"
#include "textgrid.h"
+#include "shared.h"
+#include "Effect.h"
// idx, src, dst, count, offset, stride, divisor
static VertexAttrDesc layout[] = {
@@ -29,14 +31,6 @@ static VertexAttrDesc layout[] = {
{ 3, SRC_INT8, DST_NORMALIZED, 4, 0, 4, 1 },
};
-static float locationx[] = {
- 0, 0, 0,
- -2, 0, 0,
- -4, 0, 0,
- 2, 0, 0,
- 4, 0, 0,
-};
-
#define SZ 32
#define SZh (SZ / 2)
#define SZe (SZ * SZ * SZ)
@@ -57,15 +51,17 @@ public:
private:
float t;
- PixelShader ps;
- VertexShader vs;
- Program pgm;
+ Effect *e;
+
IndexBuffer ibuf;
VertexBuffer vbuf;
- UniformBuffer ubuf;
VertexBuffer lbuf;
VertexAttributes attr;
+ UniformBuffer scn;
+ UniformBuffer mat;
+ UniformBuffer obj;
+
TextGrid text;
mat4 proj;
@@ -120,20 +116,15 @@ int TestApp::init(void) {
VertexBuffer *data[] = {
&vbuf, &vbuf, &vbuf, &lbuf,
};
- if (ps.load("simple.fragment"))
- return -1;
- if (vs.load("simple.vertex"))
- return -1;
- if (pgm.link(&vs, &ps))
- return -1;
- if (!(m = load_wavefront_obj("unitcubeoid.obj")))
+ if (!(e = Effect::load("simple")))
+ return -1;
+ if (!(m = load_wavefront_obj("unitcube.obj")))
return error("cannot load model");
printx("Object Loaded. %d vertices, %d indices.\n", m->vcount, m->icount);
vbuf.load(m->vdata, 32 * m->vcount);
ibuf.load(m->idx, 2 * m->icount);
- ubuf.load(NULL, 32 * 4);
proj.setPerspective(D2R(90.0), width / (float) height, 0.1f, 250.0f);
@@ -154,15 +145,16 @@ static float rate = 90.0;
void TestApp::onKeyUp(unsigned code) {
switch (code) {
case SDL_SCANCODE_P:
- ps.load("simple.fragment");
- vs.load("simple.vertex");
- pgm.link(&vs, &ps);
+ Effect *e2 = Effect::load("simple");
+ if (e2) {
+ delete e;
+ e = e2;
+ }
break;
}
}
void TestApp::render(void) {
- unsigned stride, offset;
int update = 0;
if (mouseBTN & 1) {
@@ -186,26 +178,40 @@ void TestApp::render(void) {
if (keydown(SDL_SCANCODE_D)) { nx += 0.01; update = 1; }
if (keydown(SDL_SCANCODE_W)) { ny -= 0.01; update = 1; }
if (keydown(SDL_SCANCODE_S)) { ny += 0.01; update = 1; }
-oops:
if (update)
build();
- struct {
- mat4 mvp;
- mat4 mv;
- } cb0;
- mat4 world, view, tmp;
+
+ struct ubScene scene;
+ struct ubObject object;
+ struct ubMaterial material;
+
+ mat4 model, view, tmp;
view.identity().rotateX(D2R(10)).rotateY(t).translate(0, 0, -10);
view.identity().rotateY(D2R(ry)).rotateX(D2R(rx)).translate(0, 0, -zoom);
-// world.identity().translate(0.5, 0.5, 0.5).rotateY(D2R(ry)).rotateX(D2R(rx));
- world.identity().translate(0.5, 0.5, 0.5);
- cb0.mvp = world * view * proj;
- cb0.mv = world * view;
-
- pgm.use();
- ubuf.load(&cb0, sizeof(cb0));
- ubuf.use(3);
+ model.identity().translate(0.5, 0.5, 0.5);
+ object.mvp = model * view * proj;
+ object.mv = model * view;
+
+ scene.LightColor.set(1.0, 1.0, 1.0);
+ scene.LightPosition.set(0.0, 1.0, 0.0, 0.0);
+
+ material.Ambient.set(0.325,0.325,0.325,1.0);
+ material.Diffuse.set(1.0,1.0,1.0,1.0);
+ material.Specular.set(1.0,1.0,1.0,1.0);
+ material.Specular.set(0, 0, 0, 0);
+ material.Color.set(0,1,0,1);
+ material.Shininess = 50.0f;
+
+ scn.load(&scene, sizeof(scene));
+ mat.load(&material, sizeof(material));
+ obj.load(&object, sizeof(object));
+ scn.use(U_SCENE);
+ mat.use(U_MATERIAL);
+ obj.use(U_OBJECT);
+
+ e->apply();
attr.use();
glDrawElementsInstanced(GL_TRIANGLES, m->icount, GL_UNSIGNED_SHORT, NULL, lcount);