commit 74af19324d87e6ab3ac371643f8246ddefe0e754 parent 69dd4523afc8d96fc241b726a22667ce83aba225 Author: Michael Savage <mikejsavage@gmail.com> Date: Sun Aug 23 12:42:47 +0200 Add texturing to the immediate renderer Diffstat:
immediate.cc | | | 38 | +++++++++++++++++++++++++++++++------- |
immediate.h | | | 11 | +++++++++-- |
diff --git a/immediate.cc b/immediate.cc @@ -10,32 +10,47 @@ void immediate_init( ImmediateContext * const ctx, ImmediateTriangle * const mem ctx->max_triangles = max_triangles; } -void immediate_triangle( ImmediateContext * const ctx, +void immediate_triangle( + ImmediateContext * const ctx, const glm::vec3 v1, const glm::vec3 v2, const glm::vec3 v3, const glm::vec4 colour ) { assert( ctx->num_triangles < ctx->max_triangles - 1 ); + const glm::vec2 uv; const ImmediateTriangle triangle = { { - { v1, colour }, - { v2, colour }, - { v3, colour }, + { v1, colour, uv }, + { v2, colour, uv }, + { v3, colour, uv }, } }; ctx->triangles[ ctx->num_triangles++ ] = triangle; } +void immediate_triangle( + ImmediateContext * const ctx, + const ImmediateVertex v1, const ImmediateVertex v2, const ImmediateVertex v3 +) { + assert( ctx->num_triangles < ctx->max_triangles - 1 ); + + const ImmediateTriangle triangle = { { v1, v2, v3 } }; + + ctx->triangles[ ctx->num_triangles++ ] = triangle; +} + void immediate_render( ImmediateContext * const ctx, const GLint at_position, const GLint at_colour, + const bool textured, const GLint at_uv, const GLint un_texture ) { GLuint vao; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao ); - GLuint vbos[ 2 ]; - glGenBuffers( 2, vbos ); + const u32 num_vbos = 3; + GLuint vbos[ num_vbos ]; + glGenBuffers( num_vbos, vbos ); glBindBuffer( GL_ARRAY_BUFFER, vbos[ 0 ] ); glBufferData( GL_ARRAY_BUFFER, ctx->num_triangles * sizeof( ImmediateTriangle ), ctx->triangles, GL_STATIC_DRAW ); @@ -47,12 +62,21 @@ void immediate_render( glEnableVertexAttribArray( at_colour ); glVertexAttribPointer( at_colour, 3, GL_FLOAT, GL_FALSE, sizeof( ImmediateVertex ), ( GLvoid * ) offsetof( ImmediateVertex, colour ) ); + if( textured ) { + glBindBuffer( GL_ARRAY_BUFFER, vbos[ 2 ] ); + glBufferData( GL_ARRAY_BUFFER, ctx->num_triangles * sizeof( ImmediateTriangle ), ctx->triangles, GL_STATIC_DRAW ); + glEnableVertexAttribArray( at_uv ); + glVertexAttribPointer( at_uv, 3, GL_FLOAT, GL_FALSE, sizeof( ImmediateVertex ), ( GLvoid * ) offsetof( ImmediateVertex, uv ) ); + + glUniform1i( un_texture, 0 ); + } + glDrawArrays( GL_TRIANGLES, 0, ctx->num_triangles * 3 ); glBindVertexArray( 0 ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); - glDeleteBuffers( 2, vbos ); + glDeleteBuffers( num_vbos, vbos ); glDeleteVertexArrays( 1, &vao ); } diff --git a/immediate.h b/immediate.h @@ -6,6 +6,7 @@ struct ImmediateVertex { glm::vec3 pos; glm::vec4 colour; + glm::vec2 uv; }; struct ImmediateTriangle { @@ -18,12 +19,18 @@ struct ImmediateContext { size_t max_triangles; }; -void immediate_init( ImmediateContext * const ctx, ImmediateTriangle * const memory, const size_t max_triangles ); +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, const glm::vec4 colour ); +void immediate_triangle( ImmediateContext * const ctx, + const ImmediateVertex v1, const ImmediateVertex v2, const ImmediateVertex v3 ); + +void immediate_render( ImmediateContext * const ctx, + const GLint at_position, const GLint at_colour, + const bool textured = false, const GLint at_uv = 0, const GLint un_texture = 0 ); -void immediate_render( ImmediateContext * const ctx, const GLint at_position, const GLint at_colour ); void immediate_clear( ImmediateContext * const ctx ); #endif // _IMMEDIATE_H_