medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 3fd090d0fabccae35ba2bb2a354cb8aee9caa9ff
parent 25708262df01768c75c99a1afa6e19ca8676de9f
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Nov 27 13:55:23 +0200

Add unified vertex buffer support to the renderer

Diffstat:
renderer.cc | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
renderer.h | 23++++++++++++++++++-----
2 files changed, 73 insertions(+), 27 deletions(-)
diff --git a/renderer.cc b/renderer.cc @@ -335,39 +335,72 @@ void renderer_delete_texture( Texture texture ) { glDeleteTextures( 1, &gl_texture ); } +static GLvoid * offset_to_glvoidp( u32 offset ) { + return checked_cast< GLvoid * >( checked_cast< uptr >( offset ) ); +} + Mesh renderer_new_mesh( MeshConfig config ) { // TODO: assert num_vertices is sane for this primitive type GLuint vao; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao ); - ASSERT( config.positions != 0 ); - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.positions ) ); - glEnableVertexAttribArray( ATTR_POSITION ); - glVertexAttribPointer( ATTR_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + if( config.unified_buffer == 0 ) { + ASSERT( config.positions != 0 ); - if( config.normals != 0 ) { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.normals ) ); - glEnableVertexAttribArray( ATTR_NORMAL ); - glVertexAttribPointer( ATTR_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0 ); - } + glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.positions ) ); + glEnableVertexAttribArray( ATTR_POSITION ); + glVertexAttribPointer( ATTR_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0 ); - if( config.tex_coords0 != 0 ) { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.tex_coords0 ) ); - glEnableVertexAttribArray( ATTR_TEX_COORD0 ); - glVertexAttribPointer( ATTR_TEX_COORD0, 2, GL_FLOAT, GL_FALSE, 0, 0 ); - } + if( config.normals != 0 ) { + glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.normals ) ); + glEnableVertexAttribArray( ATTR_NORMAL ); + glVertexAttribPointer( ATTR_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + } + + if( config.tex_coords0 != 0 ) { + glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.tex_coords0 ) ); + glEnableVertexAttribArray( ATTR_TEX_COORD0 ); + glVertexAttribPointer( ATTR_TEX_COORD0, 2, GL_FLOAT, GL_FALSE, 0, 0 ); + } + + if( config.tex_coords1 != 0 ) { + glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.tex_coords1 ) ); + glEnableVertexAttribArray( ATTR_TEX_COORD1 ); + glVertexAttribPointer( ATTR_TEX_COORD1, 2, GL_FLOAT, GL_FALSE, 0, 0 ); + } - if( config.tex_coords1 != 0 ) { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.tex_coords1 ) ); - glEnableVertexAttribArray( ATTR_TEX_COORD1 ); - glVertexAttribPointer( ATTR_TEX_COORD1, 2, GL_FLOAT, GL_FALSE, 0, 0 ); + if( config.colours != 0 ) { + glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.colours ) ); + glEnableVertexAttribArray( ATTR_COLOUR ); + glVertexAttribPointer( ATTR_COLOUR, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + } } + else { + glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.unified_buffer ) ); + + glEnableVertexAttribArray( ATTR_POSITION ); + glVertexAttribPointer( ATTR_POSITION, 3, GL_FLOAT, GL_FALSE, config.stride, offset_to_glvoidp( config.positions_offset ) ); + + if( config.normals_offset != 0 ) { + glEnableVertexAttribArray( ATTR_NORMAL ); + glVertexAttribPointer( ATTR_NORMAL, 3, GL_FLOAT, GL_FALSE, config.stride, offset_to_glvoidp( config.normals_offset ) ); + } - if( config.colours != 0 ) { - glBindBuffer( GL_ARRAY_BUFFER, checked_cast< GLuint >( config.colours ) ); - glEnableVertexAttribArray( ATTR_COLOUR ); - glVertexAttribPointer( ATTR_COLOUR, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + if( config.tex_coords0_offset != 0 ) { + glEnableVertexAttribArray( ATTR_TEX_COORD0 ); + glVertexAttribPointer( ATTR_TEX_COORD0, 2, GL_FLOAT, GL_FALSE, config.stride, offset_to_glvoidp( config.tex_coords0_offset ) ); + } + + if( config.tex_coords1_offset != 0 ) { + glEnableVertexAttribArray( ATTR_TEX_COORD1 ); + glVertexAttribPointer( ATTR_TEX_COORD1, 2, GL_FLOAT, GL_FALSE, config.stride, offset_to_glvoidp( config.tex_coords1_offset ) ); + } + + if( config.colours_offset != 0 ) { + glEnableVertexAttribArray( ATTR_COLOUR ); + glVertexAttribPointer( ATTR_COLOUR, 3, GL_FLOAT, GL_FALSE, config.stride, offset_to_glvoidp( config.colours_offset ) ); + } } if( config.indices != 0 ) { diff --git a/renderer.h b/renderer.h @@ -85,11 +85,24 @@ struct Mesh { }; struct MeshConfig { - VB positions; - VB normals; - VB tex_coords0; - VB tex_coords1; - VB colours; + VB unified_buffer; + u32 stride; + union { + struct { + VB positions; + VB normals; + VB tex_coords0; + VB tex_coords1; + VB colours; + }; + struct { + u32 positions_offset; + u32 normals_offset; + u32 tex_coords0_offset; + u32 tex_coords1_offset; + u32 colours_offset; + }; + }; IB indices; u32 num_vertices; PrimitiveType primitive_type;