commit 76e4d4bd0c357d4ffe37ae615a8f2c5d03d55c89 parent 9a6e5b2439b28d5a0d5c18349704968660f2b3f3 Author: Michael Savage <mikejsavage@gmail.com> Date: Tue Aug 18 20:39:50 +0200 Make the immediate API colourful Diffstat:
hm.cc | | | 23 | +++++++++++++++++------ |
immediate.cc | | | 31 | ++++++++++++++++++++++--------- |
immediate.h | | | 13 | ++++++++----- |
diff --git a/hm.cc b/hm.cc @@ -21,17 +21,23 @@ static const GLchar * const vert_src = GLSL( in vec3 position; + in vec3 colour; + + out vec3 frag_colour; void main() { gl_Position = vec4( position, 1.0 ); + frag_colour = colour; } ); static const GLchar * frag_src = GLSL( + in vec3 frag_colour; + out vec4 screen_colour; void main() { - screen_colour = vec4( 1.0, 0.0, 0.0, 1.0 ); + screen_colour = vec4( frag_colour, 1.0 ); } ); @@ -74,6 +80,7 @@ extern "C" GAME_INIT( game_init ) { state->test_shader = compile_shader( vert_src, frag_src, "screen_colour" ); state->test_at_position = glGetAttribLocation( state->test_shader, "position" ); + state->test_at_colour = glGetAttribLocation( state->test_shader, "colour" ); // TODO: persistent memory should be an arena const size_t triangles = 65536; @@ -87,22 +94,26 @@ extern "C" GAME_INIT( game_init ) { immediate_triangle( &state->test_immediate, glm::vec3( -crosshair_length, -crosshair_thickness, 0 ), glm::vec3( -crosshair_length, crosshair_thickness, 0 ), - glm::vec3( crosshair_length, crosshair_thickness, 0 ) + glm::vec3( crosshair_length, crosshair_thickness, 0 ), + glm::vec3( 1, 0, 0 ) ); immediate_triangle( &state->test_immediate, glm::vec3( crosshair_length, crosshair_thickness, 0 ), glm::vec3( crosshair_length, -crosshair_thickness, 0 ), - glm::vec3( -crosshair_length, -crosshair_thickness, 0 ) + glm::vec3( -crosshair_length, -crosshair_thickness, 0 ), + glm::vec3( 1, 0, 0 ) ); immediate_triangle( &state->test_immediate, glm::vec3( -crosshair_thickness / aspect, crosshair_length * aspect , 0 ), glm::vec3( crosshair_thickness / aspect, crosshair_length * aspect , 0 ), - glm::vec3( crosshair_thickness / aspect, -crosshair_length * aspect , 0 ) + glm::vec3( crosshair_thickness / aspect, -crosshair_length * aspect , 0 ), + glm::vec3( 1, 0, 0 ) ); immediate_triangle( &state->test_immediate, glm::vec3( crosshair_thickness / aspect, -crosshair_length * aspect , 0 ), glm::vec3( -crosshair_thickness / aspect, -crosshair_length * aspect , 0 ), - glm::vec3( -crosshair_thickness / aspect, crosshair_length * aspect , 0 ) + glm::vec3( -crosshair_thickness / aspect, crosshair_length * aspect , 0 ), + glm::vec3( 1, 0, 0 ) ); glClearColor( 0, 0.5, 0.7, 1 ); @@ -148,6 +159,6 @@ extern "C" GAME_FRAME( game_frame ) { glDisable( GL_DEPTH_TEST ); glUseProgram( state->test_shader ); - immediate_render( &state->test_immediate, state->test_at_position ); + immediate_render( &state->test_immediate, state->test_at_position, state->test_at_colour ); glEnable( GL_DEPTH_TEST ); } diff --git a/immediate.cc b/immediate.cc @@ -10,25 +10,38 @@ void immediate_init( ImmediateContext * const ctx, ImmediateTriangle * const mem ctx->max_triangles = max_triangles; } -void immediate_triangle( ImmediateContext * const ctx, const glm::vec3 v1, const glm::vec3 v2, const glm::vec3 v3 ) { +void immediate_triangle( ImmediateContext * const ctx, + const glm::vec3 v1, const glm::vec3 v2, const glm::vec3 v3, + const glm::vec3 colour +) { assert( ctx->num_triangles < ctx->max_triangles - 1 ); - ctx->triangles[ ctx->num_triangles++ ] = { v1, v2, v3 }; + const ImmediateTriangle triangle = { { + { v1, colour }, + { v2, colour }, + { v3, colour }, + } }; + + ctx->triangles[ ctx->num_triangles++ ] = triangle; } -void immediate_render( ImmediateContext * const ctx, const GLuint at_position ) { - GLuint vbo; - glGenBuffers( 1, &vbo ); +void immediate_render( ImmediateContext * const ctx, const GLuint at_position, const GLuint at_colour ) { + GLuint vbos[ 2 ]; + glGenBuffers( 2, vbos ); - glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glBindBuffer( GL_ARRAY_BUFFER, vbos[ 0 ] ); glBufferData( GL_ARRAY_BUFFER, ctx->num_triangles * sizeof( ImmediateTriangle ), ctx->triangles, GL_STATIC_DRAW ); - glEnableVertexAttribArray( at_position ); - glVertexAttribPointer( at_position, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + glVertexAttribPointer( at_position, 3, GL_FLOAT, GL_FALSE, sizeof( ImmediateVertex ), 0 ); + + glBindBuffer( GL_ARRAY_BUFFER, vbos[ 1 ] ); + glBufferData( GL_ARRAY_BUFFER, ctx->num_triangles * sizeof( ImmediateTriangle ), ctx->triangles, GL_STATIC_DRAW ); + glEnableVertexAttribArray( at_colour ); + glVertexAttribPointer( at_colour, 3, GL_FLOAT, GL_FALSE, sizeof( ImmediateVertex ), ( GLvoid * ) sizeof( glm::vec3 ) ); glDrawArrays( GL_TRIANGLES, 0, ctx->num_triangles * 3 ); - glDeleteBuffers( 1, &vbo ); + glDeleteBuffers( 2, vbos ); } void immediate_clear( ImmediateContext * const ctx ) { diff --git a/immediate.h b/immediate.h @@ -3,10 +3,13 @@ #include <glm/glm.hpp> +struct ImmediateVertex { + glm::vec3 pos; + glm::vec3 colour; +}; + struct ImmediateTriangle { - glm::vec3 v1; - glm::vec3 v2; - glm::vec3 v3; + ImmediateVertex vertices[ 3 ]; }; struct ImmediateContext { @@ -16,8 +19,8 @@ struct ImmediateContext { }; void immediate_init( ImmediateContext * const ctx, ImmediateTriangle * const memory, const size_t max_triangles ); -void immediate_triangle( ImmediateContext * const ctx, const glm::vec3 v1, const glm::vec3 v2, const glm::vec3 v3 ); -void immediate_render( ImmediateContext * const ctx, const GLuint at_position ); +void immediate_triangle( ImmediateContext * const ctx, const glm::vec3 v1, const glm::vec3 v2, const glm::vec3 v3, const glm::vec3 colour ); +void immediate_render( ImmediateContext * const ctx, const GLuint at_position, const GLuint at_colour ); void immediate_clear( ImmediateContext * const ctx ); #endif // _IMMEDIATE_H_