From af06cc8002215c50cc07f4a40b643d3b1978450f Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Wed, 24 Oct 2012 21:29:35 -0700 Subject: Use instanced rendering Note: calling glDrawElementsInstanced (non-ARB) causes a segfault. --- data/instanced.vert | 13 +++++++++++++ src/main.cpp | 49 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 data/instanced.vert 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; } -- cgit v1.2.3