medfall

A super great game engine
Log | Files | Refs

commit fb274c453c24297df8f4ce0c292f54c33f09c6a5
parent 242e1b4e05b5d6fb56f4f5450ef910bd1647eaf6
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed, 19 Dec 2018 18:05:41 +0200

Support u16 indices and non-float3 vertex data

Diffstat:
renderer.cc | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
renderer.h | 29+++++++++++++++++++++++++++++
2 files changed, 89 insertions(+), 23 deletions(-)

diff --git a/renderer.cc b/renderer.cc @@ -284,7 +284,7 @@ static void drawcall_single( const Mesh & mesh, const RenderState & state ) { glBindVertexArray( mesh.vao ); GLenum primitive = primitivetype_to_glenum( mesh.primitive_type ); if( mesh.indices != 0 ) { - glDrawElements( primitive, mesh.num_vertices, GL_UNSIGNED_INT, 0 ); + glDrawElements( primitive, mesh.num_vertices, mesh.indices_format == INDEXFMT_U16 ? GL_UNSIGNED_SHORT: GL_UNSIGNED_INT, 0 ); } else { glDrawArrays( primitive, 0, mesh.num_vertices ); @@ -984,8 +984,53 @@ void renderer_delete_texture( Texture texture ) { glDeleteTextures( 1, &texture ); } -static GLvoid * offset_to_glvoidp( u32 offset ) { - return checked_cast< GLvoid * >( checked_cast< uptr >( offset ) ); +static void vertexfmt_to_glenum( VertexFormat format, GLenum * type, int * components ) { + switch( format ) { + case VERTEXFMT_U8x3: + *type = GL_UNSIGNED_BYTE; + *components = 3; + break; + case VERTEXFMT_U8x4: + *type = GL_UNSIGNED_BYTE; + *components = 4; + break; + + case VERTEXFMT_HALFx2: + *type = GL_HALF_FLOAT; + *components = 2; + break; + case VERTEXFMT_HALFx3: + *type = GL_HALF_FLOAT; + *components = 3; + break; + case VERTEXFMT_HALFx4: + *type = GL_HALF_FLOAT; + *components = 4; + break; + + case VERTEXFMT_FLOATx2: + *type = GL_FLOAT; + *components = 2; + break; + case VERTEXFMT_FLOATx3: + *type = GL_FLOAT; + *components = 3; + break; + case VERTEXFMT_FLOATx4: + *type = GL_FLOAT; + *components = 4; + break; + } +} + +static void setup_vertex_attrib( GLuint index, VertexFormat format, u32 stride = 0, u32 offset = 0 ) { + GLvoid * gl_offset = checked_cast< GLvoid * >( checked_cast< uptr >( offset ) ); + GLenum type; + int components; + vertexfmt_to_glenum( format, &type, &components ); + + glEnableVertexAttribArray( index ); + glVertexAttribPointer( index, components, type, GL_FALSE, stride, gl_offset ); } Mesh renderer_new_mesh( MeshConfig config ) { @@ -1013,58 +1058,49 @@ Mesh renderer_new_mesh( MeshConfig config ) { ASSERT( config.positions != 0 ); glBindBuffer( GL_ARRAY_BUFFER, config.positions ); - glEnableVertexAttribArray( ATTR_POSITION ); - glVertexAttribPointer( ATTR_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + setup_vertex_attrib( ATTR_POSITION, config.positions_format ); if( config.normals != 0 ) { glBindBuffer( GL_ARRAY_BUFFER, config.normals ); - glEnableVertexAttribArray( ATTR_NORMAL ); - glVertexAttribPointer( ATTR_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + setup_vertex_attrib( ATTR_NORMAL, config.normals_format ); } if( config.tex_coords0 != 0 ) { glBindBuffer( GL_ARRAY_BUFFER, config.tex_coords0 ); - glEnableVertexAttribArray( ATTR_TEX_COORD0 ); - glVertexAttribPointer( ATTR_TEX_COORD0, 2, GL_FLOAT, GL_FALSE, 0, 0 ); + setup_vertex_attrib( ATTR_TEX_COORD0, config.tex_coords0_format ); } if( config.tex_coords1 != 0 ) { glBindBuffer( GL_ARRAY_BUFFER, config.tex_coords1 ); - glEnableVertexAttribArray( ATTR_TEX_COORD1 ); - glVertexAttribPointer( ATTR_TEX_COORD1, 2, GL_FLOAT, GL_FALSE, 0, 0 ); + setup_vertex_attrib( ATTR_TEX_COORD1, config.tex_coords1_format ); } if( config.colours != 0 ) { glBindBuffer( GL_ARRAY_BUFFER, config.colours ); - glEnableVertexAttribArray( ATTR_COLOUR ); - glVertexAttribPointer( ATTR_COLOUR, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + setup_vertex_attrib( ATTR_COLOUR, config.colours_format ); } } else { ASSERT( config.stride != 0 ); + glBindBuffer( GL_ARRAY_BUFFER, config.unified_buffer ); - glEnableVertexAttribArray( ATTR_POSITION ); - glVertexAttribPointer( ATTR_POSITION, 3, GL_FLOAT, GL_FALSE, config.stride, offset_to_glvoidp( config.positions_offset ) ); + setup_vertex_attrib( ATTR_POSITION, config.positions_format, config.stride, 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 ) ); + setup_vertex_attrib( ATTR_NORMAL, config.normals_format, config.stride, config.normals_offset ); } 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 ) ); + setup_vertex_attrib( ATTR_TEX_COORD0, config.tex_coords0_format, config.stride, 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 ) ); + setup_vertex_attrib( ATTR_TEX_COORD1, config.tex_coords1_format, config.stride, 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 ) ); + setup_vertex_attrib( ATTR_COLOUR, config.colours_format, config.stride, config.colours_offset ); } } @@ -1089,6 +1125,7 @@ Mesh renderer_new_mesh( MeshConfig config ) { mesh.positions = config.unified_buffer; } mesh.indices = config.indices; + mesh.indices_format = config.indices_format; return mesh; } diff --git a/renderer.h b/renderer.h @@ -142,6 +142,24 @@ struct RenderState { bool wireframe = false; }; +enum VertexFormat { + VERTEXFMT_U8x3, + VERTEXFMT_U8x4, + + VERTEXFMT_HALFx2, + VERTEXFMT_HALFx3, + VERTEXFMT_HALFx4, + + VERTEXFMT_FLOATx2, + VERTEXFMT_FLOATx3, + VERTEXFMT_FLOATx4, +}; + +enum IndexFormat { + INDEXFMT_U16, + INDEXFMT_U32, +}; + struct Mesh { u32 num_vertices; PrimitiveType primitive_type; @@ -152,6 +170,7 @@ struct Mesh { VB tex_coords1; VB colours; IB indices; + IndexFormat indices_format; }; struct MeshConfig { @@ -165,6 +184,7 @@ struct MeshConfig { VB unified_buffer = 0; u32 stride = 0; + union { struct { VB positions; @@ -181,8 +201,17 @@ struct MeshConfig { u32 colours_offset; }; }; + + VertexFormat positions_format = VERTEXFMT_FLOATx3; + VertexFormat normals_format = VERTEXFMT_FLOATx3; + VertexFormat tex_coords0_format = VERTEXFMT_FLOATx2; + VertexFormat tex_coords1_format = VERTEXFMT_FLOATx2; + VertexFormat colours_format = VERTEXFMT_FLOATx3; + IndexFormat indices_format = INDEXFMT_U32; + IB indices = 0; u32 num_vertices = 0; + PrimitiveType primitive_type = PRIMITIVETYPE_TRIANGLES; };