summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Turner <mattst88@gmail.com>2012-10-24 21:29:35 -0700
committerMatt Turner <mattst88@gmail.com>2012-10-24 21:40:43 -0700
commitaf06cc8002215c50cc07f4a40b643d3b1978450f (patch)
treef5730cf56cb4cd4b403180116124c8f80b9bce3f
parent7ed93eee2116d7d38b1281e339c8d4c183dcb775 (diff)
Use instanced renderingHEADmaster
Note: calling glDrawElementsInstanced (non-ARB) causes a segfault.
-rw-r--r--data/instanced.vert13
-rw-r--r--src/main.cpp49
2 files changed, 53 insertions, 9 deletions
diff --git a/data/instanced.vert b/data/instanced.vert
new file mode 100644
index 0000000..52a9713
--- /dev/null
+++ b/data/instanced.vert
@@ -0,0 +1,13 @@
+#extension GL_ARB_draw_instanced : enable
+
+uniform mat4 mvp[5];
+
+attribute vec4 vert_color;
+
+varying vec4 color;
+
+void main(void)
+{
+ gl_Position = mvp[gl_InstanceID] * gl_Vertex;
+ color = abs(vert_color);
+}
diff --git a/src/main.cpp b/src/main.cpp
index 7ead051..200cd89 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -35,6 +35,7 @@ static SDL_Surface *my_surf = NULL;
static bool anim = true;
static bool done = false;
+static bool instanced = false;
static float y_angle = 0.0f;
static float hinge_angle = 0.0f;
@@ -237,20 +238,36 @@ Redisplay(void)
if (timer_queries[0])
glBeginQuery(GL_TIME_ELAPSED, timer_queries[0]);
- for (unsigned i = 0; i < ARRAY_SIZE(transformations); i++) {
+ if (instanced) {
/* Set the modelview-projection matrix for rendering this
* object.
*/
- glUniformMatrix4fv(cube_program->mvp_uniform, 1, false,
- (float *) &transformations[i]);
+ glUniformMatrix4fv(cube_program->mvp_uniform, ARRAY_SIZE(transformations), false,
+ (float *) &transformations[0]);
/* Draw the object.
*/
- glDrawElements(cube_sink->mode,
- cube_sink->elt_count,
- GL_UNSIGNED_SHORT,
- BUFFER_OFFSET(cube_sink->elt_offset));
+ glDrawElementsInstancedARB(cube_sink->mode,
+ cube_sink->elt_count,
+ GL_UNSIGNED_SHORT,
+ BUFFER_OFFSET(cube_sink->elt_offset),
+ ARRAY_SIZE(transformations));
+ } else {
+ for (unsigned i = 0; i < ARRAY_SIZE(transformations); i++) {
+ /* Set the modelview-projection matrix for rendering this
+ * object.
+ */
+ glUniformMatrix4fv(cube_program->mvp_uniform, 1, false,
+ (float *) &transformations[i]);
+
+ /* Draw the object.
+ */
+ glDrawElements(cube_sink->mode,
+ cube_sink->elt_count,
+ GL_UNSIGNED_SHORT,
+ BUFFER_OFFSET(cube_sink->elt_offset));
+ }
}
if (timer_queries[0])
@@ -368,8 +385,14 @@ build_all_shaders(void)
/* Compile all of the shaders
*/
- GLuint vs = compile_shader_from_file(GL_VERTEX_SHADER,
- "simple.vert");
+ GLuint vs;
+ if (instanced) {
+ vs = compile_shader_from_file(GL_VERTEX_SHADER,
+ "instanced.vert");
+ } else {
+ vs = compile_shader_from_file(GL_VERTEX_SHADER,
+ "simple.vert");
+ }
GLuint fs = compile_shader_from_file(GL_FRAGMENT_SHADER,
"simple.frag");
@@ -421,6 +444,7 @@ Init(void)
" f: Toggle fullscreen.\n"
" a: Toggle animation of object.\n"
" c: Re-load and compile shader program code.\n"
+ " i: Toggle instanced rendering.\n"
" ESC: Exit program.\n");
}
@@ -469,6 +493,13 @@ Key(SDLKey sym, bool key_down)
build_all_shaders();
break;
+ case 'i':
+ instanced = !instanced;
+ build_all_shaders();
+ printf("Using %sinstanced rendering.\n",
+ instanced ? "" : "non-");
+ break;
+
default:
break;
}