commit e34b2278b34843043afc2e0b2d62e56712e4f58c
parent 1f002760dfacbf27a20b56a4535416289864e87a
Author: Brian Swetland <swetland@frotz.net>
Date: Mon, 2 Sep 2013 00:29:41 -0700
start on higher level APIs, with Renderable, Model, and Effect classes
- an Effect() wraps a shader program and its configuration, setup with apply()
- a Renderable() is anything capable of sending geometry to the GPU via render()
- a Model() is a Renderable() that can load and render a textured wavefront .obj
- updated test/object.cc (which became much simpler)
Diffstat:
10 files changed, 345 insertions(+), 44 deletions(-)
diff --git a/common/Effect.cc b/common/Effect.cc
@@ -0,0 +1,46 @@
+/* Copyright 2013 Brian Swetland <swetland@frotz.net>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+
+#include "Effect.h"
+
+void Effect::apply(void) {
+ pgm.use();
+}
+
+int Effect::init(const char *name) {
+ std::string vname(name);
+ std::string fname(name);
+ vname.append(".vertex");
+ fname.append(".fragment");
+ if (vs.load(vname.c_str()))
+ return -1;
+ if (ps.load(fname.c_str()))
+ return -1;
+ if (pgm.link(&vs, &ps))
+ return -1;
+ return 0;
+}
+
+Effect *Effect::load(const char *name) {
+ Effect *e = new Effect();
+ if (e->init(name)) {
+ delete e;
+ return nullptr;
+ } else {
+ return e;
+ }
+}
diff --git a/common/Effect.h b/common/Effect.h
@@ -0,0 +1,35 @@
+/* Copyright 2013 Brian Swetland <swetland@frotz.net>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _EFFECT_H_
+#define _EFFECT_H_
+
+#include "core.h"
+
+class Effect {
+public:
+ virtual void apply(void);
+ static Effect *load(const char *name);
+private:
+ int init(const char *name);
+ Effect(void) {};
+ DISALLOW_COPY_AND_ASSIGN(Effect);
+
+ PixelShader ps;
+ VertexShader vs;
+ Program pgm;
+};
+
+#endif
diff --git a/common/Model.cc b/common/Model.cc
@@ -0,0 +1,78 @@
+/* Copyright 2013 Brian Swetland <swetland@frotz.net>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+
+#include "Model.h"
+
+#include "util.h"
+
+// idx, src, dst, count, offset, stride, divisor
+static VertexAttrDesc layout[] = {
+ { 0, SRC_FLOAT, DST_FLOAT, 3, 0, 32, 0 },
+ { 1, SRC_FLOAT, DST_FLOAT, 3, 12, 32, 0 },
+ { 2, SRC_FLOAT, DST_FLOAT, 2, 24, 32, 0 },
+};
+
+void Model::render(void) {
+ attr.use();
+ texture.use(0);
+ glDrawElements(GL_TRIANGLES, icount, GL_UNSIGNED_SHORT, NULL);
+}
+
+int Model::init(const char *name) {
+ struct model *m;
+ VertexBuffer *data[] = {
+ &vbuf, &vbuf, &vbuf,
+ };
+
+ std::string mname(name);
+ std::string tname(name);
+ mname.append(".obj");
+ tname.append(".png");
+
+ if (!(m = load_wavefront_obj(mname.c_str())))
+ return error("cannot load model");
+
+ printx("Object '%s' loaded. %d vertices, %d indices.\n",
+ name, m->vcount, m->icount);
+
+ texture.load(tname.c_str(), 0);
+
+ vbuf.load(m->vdata, 32 * m->vcount);
+ ibuf.load(m->idx, 2 * m->icount);
+
+ icount = m->icount;
+
+ attr.init(layout, data, sizeof(layout)/sizeof(layout[0]));
+
+ /* this will persist because it is part of the VAO state */
+ ibuf.use();
+
+ delete_wavefront_obj(m);
+
+ return 0;
+}
+
+Model *Model::load(const char *name) {
+ Model *m = new Model();
+ if (m->init(name)) {
+ delete m;
+ return nullptr;
+ } else {
+ return m;
+ }
+}
+
diff --git a/common/Model.h b/common/Model.h
@@ -0,0 +1,37 @@
+/* Copyright 2013 Brian Swetland <swetland@frotz.net>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _MODEL_H_
+#define _MODEL_H_
+
+#include "Renderable.h"
+
+class Model : public Renderable {
+public:
+ virtual void render(void);
+ static Model *load(const char *name);
+private:
+ int init(const char *name);
+ Model() : Renderable() {};
+ //DISALLOW_COPY_AND_ASSIGN(Model);
+
+ IndexBuffer ibuf;
+ VertexBuffer vbuf;
+ VertexAttributes attr;
+ Texture2D texture;
+ int icount;
+};
+
+#endif
diff --git a/common/Renderable.h b/common/Renderable.h
@@ -0,0 +1,29 @@
+/* Copyright 2013 Brian Swetland <swetland@frotz.net>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _RENDERABLE_H_
+#define _RENDERABLE_H_
+
+#include "core.h"
+
+class Renderable {
+public:
+ Renderable(void) {};
+ virtual void render(void) = 0;
+private:
+ DISALLOW_COPY_AND_ASSIGN(Renderable);
+};
+
+#endif
diff --git a/common/module.mk b/common/module.mk
@@ -13,6 +13,8 @@ M_OBJS += savepng.o
M_OBJS += simplexnoise.o
M_OBJS += textgrid.o
M_OBJS += texturefont.o
+M_OBJS += Model.o
+M_OBJS += Effect.o
M_LIBS := common
include build/lib.mk
diff --git a/test/assets/cube.obj b/test/assets/cube.obj
@@ -0,0 +1,95 @@
+mtllib cube.mtl
+g g0
+v -1 1 -1
+v 1 1 -1
+v 1 1 1
+v -1 1 1
+v -1 -1 1
+v 1 -1 1
+v 1 -1 -1
+v -1 -1 -1
+vn 0 -1 0
+vn 0 -1 0
+vn 0 -1 0
+vn 0 -1 0
+vn 0 -1 0
+vn 0 -1 0
+vn 0 1 0
+vn 0 1 0
+vn 0 1 0
+vn 0 1 0
+vn 0 1 0
+vn 0 1 0
+vn 0 0 1
+vn 0 0 1
+vn 0 0 1
+vn 0 0 1
+vn 0 0 1
+vn 0 0 1
+vn 0 0 -1
+vn 0 0 -1
+vn 0 0 -1
+vn 0 0 -1
+vn 0 0 -1
+vn 0 0 -1
+vn -1 0 0
+vn -1 0 0
+vn -1 0 0
+vn -1 0 0
+vn -1 0 0
+vn -1 0 0
+vn 1 0 0
+vn 1 0 0
+vn 1 0 0
+vn 1 0 0
+vn 1 0 0
+vn 1 0 0
+vt 0.25 0.25
+vt 0.5 0.5
+vt 0.25 0.5
+vt 0.5 0.5
+vt 0.25 0.25
+vt 0.5 0.25
+vt 0.5 1
+vt 0.25 0.75
+vt 0.5 0.75
+vt 0.25 0.75
+vt 0.5 1
+vt 0.25 1
+vt 0.25 0.75
+vt 0.5 0.5
+vt 0.5 0.75
+vt 0.5 0.5
+vt 0.25 0.75
+vt 0.25 0.5
+vt 1 0.5
+vt 0.75 0.75
+vt 0.75 0.5
+vt 0.75 0.75
+vt 1 0.5
+vt 1 0.75
+vt 0 0.5
+vt 0.25 0.75
+vt 0 0.75
+vt 0.25 0.75
+vt 0 0.5
+vt 0.25 0.5
+vt 0.5 0.5
+vt 0.75 0.75
+vt 0.5 0.75
+vt 0.75 0.75
+vt 0.5 0.5
+vt 0.75 0.5
+usemtl mat0
+f 8/1/1 6/2/2 5/3/3
+f 6/4/4 8/5/5 7/6/6
+f 2/7/7 4/8/8 3/9/9
+f 4/10/10 2/11/11 1/12/12
+f 4/13/13 6/14/14 3/15/15
+f 6/16/16 4/17/17 5/18/18
+f 8/19/19 2/20/20 7/21/21
+f 2/22/22 8/23/23 1/24/24
+f 8/25/25 4/26/26 1/27/27
+f 4/28/28 8/29/29 5/30/30
+f 6/31/31 2/32/32 3/33/33
+f 2/34/34 6/35/35 7/36/36
diff --git a/test/assets/cube.png b/test/assets/cube.png
Binary files differ.
diff --git a/test/assets/simple.glsl b/test/assets/simple.glsl
@@ -1,9 +1,11 @@
#version 140
#extension GL_ARB_explicit_attrib_location : enable
+#define TEXTURED 1
+
-- vertex
-layout(std140) uniform cb0 {
+layout(std140) uniform block0 {
mat4 MVP;
mat4 MV;
};
@@ -14,6 +16,7 @@ layout (location = 2) in vec2 TEXCOORD;
out vec2 vTEXCOORD;
out float vDIFFUSE;
+out vec4 vCOLOR;
void main() {
vec4 pos = POSITION;
@@ -34,8 +37,14 @@ void main() {
in vec2 vTEXCOORD;
in float vDIFFUSE;
+uniform sampler2D sampler0;
+
void main() {
+#if TEXTURED
+ gl_FragColor = texture2D(sampler0, vTEXCOORD);
+#else
vec4 c = vec4(1.0, 0.0, 0.0, 1.0);
gl_FragColor = c * 0.25 + c * vDIFFUSE;
+#endif
}
diff --git a/test/object.cc b/test/object.cc
@@ -13,19 +13,11 @@
* limitations under the License.
*/
-#include <stdio.h>
-#include <stdlib.h>
-
#include "app.h"
#include "matrix.h"
-#include "util.h"
-// idx, src, dst, count, offset, stride, divisor
-static VertexAttrDesc layout[] = {
- { 0, SRC_FLOAT, DST_FLOAT, 3, 0, 32, 0 },
- { 1, SRC_FLOAT, DST_FLOAT, 3, 12, 32, 0 },
- { 2, SRC_FLOAT, DST_FLOAT, 2, 24, 32, 0 },
-};
+#include "Model.h"
+#include "Effect.h"
class TestApp : public App {
public:
@@ -38,16 +30,11 @@ public:
private:
float r;
- PixelShader ps;
- VertexShader vs;
- Program pgm;
- IndexBuffer ibuf;
- VertexBuffer vbuf;
- UniformBuffer ubuf;
- VertexAttributes attr;
+ Model *m;
+ Effect *e;
+ UniformBuffer ubuf;
mat4 proj;
- struct model *m;
};
TestApp::TestApp() : App(), r(0.0) {
@@ -57,29 +44,13 @@ void TestApp::release(void) {
}
int TestApp::init(void) {
- VertexBuffer *data[] = {
- &vbuf, &vbuf, &vbuf,
- };
-
- if (!(m = load_wavefront_obj("unitcubeoid.obj")))
- return error("cannot load model");
- printx("Object Loaded. %d vertices, %d indices.\n", m->vcount, m->icount);
+ if (!(m = Model::load("cube")))
+ return error("cannot load cube object");
+ if (!(e = Effect::load("simple")))
+ return error("could not load simple effect");
+
proj.setPerspective(D2R(90.0), width / (float) height, 0.1f, 250.0f);
-
- ps.load("simple.fragment");
- vs.load("simple.vertex");
- pgm.link(&vs, &ps);
-
- vbuf.load(m->vdata, 32 * m->vcount);
- ibuf.load(m->idx, 2 * m->icount);
- ubuf.load(NULL, 32 * 4);
-
- attr.init(layout, data, sizeof(layout)/sizeof(layout[0]));
-
- /* this will persist because it is part of the VAO state */
- ibuf.use();
-
return 0;
}
@@ -100,11 +71,10 @@ void TestApp::render(void) {
cb0.mv = world * view;
ubuf.load(&cb0, 32 * 4);
-
- pgm.use();
ubuf.use(0);
- attr.use();
- glDrawElements(GL_TRIANGLES, m->icount, GL_UNSIGNED_SHORT, NULL);
+
+ e->apply();
+ m->render();
}
App *createApp(void) {