medfall

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

commit b9d71e59dab494189c2f2802378ba4c3ab9216c7
parent af09f255b9c8286c010cbe362c11cea1b6c88a78
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon Oct 31 23:15:41 +0200

Add textures to the renderer, small reorg

Diffstat:
renderer.cc | 83++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
renderer.h | 34+++++++++++++++-------------------
2 files changed, 97 insertions(+), 20 deletions(-)
diff --git a/renderer.cc b/renderer.cc @@ -18,6 +18,29 @@ static const u32 ATTR_TEX_COORD0 = 2; static const u32 ATTR_TEX_COORD1 = 3; static const u32 ATTR_COLOUR = 4; +struct OpenGLState { + RenderState render_state; + // VB bound_vbo; + // IB bound_ebo; + UB bound_ub; + Texture bound_texture; +}; + +struct DrawCall { + DrawCallType type; + union { + struct { + void * data; + size_t len; + }; + // or Array< u8 >( data, len ); + + MeshConfig mesh_config; + ShaderConfig shader_config; + TextureConfig texture_config; + RenderState render_state; + }; +}; void renderer_begin_frame( ClearColourBool clear_colour, ClearDepthBool clear_depth ) { if( clear_colour == CLEARCOLOUR_DO || clear_depth == CLEARDEPTH_DO ) { @@ -179,6 +202,59 @@ Shader renderer_new_shader( const char * vertex_src, const char * fragment_src ) return renderer_new_shader( config ); } +Texture renderer_new_texture( TextureConfig config ) { + GLuint texture; + glGenTextures( 1, &texture ); + glBindTexture( GL_TEXTURE_2D, texture ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + GLenum internal_format; + GLenum channels, type; + + switch( config.format ) { + case TEXFMT_RGBA_FLOAT: + internal_format = GL_RGBA32F; + channels = GL_RGBA; + type = GL_FLOAT; + break; + + case TEXFMT_RGB_FLOAT: + internal_format = GL_RGB32F; + channels = GL_RGB; + type = GL_FLOAT; + break; + + case TEXFMT_R_FLOAT: + internal_format = GL_R32F; + channels = GL_RED; + type = GL_FLOAT; + break; + + case TEXFMT_R_U8: + internal_format = GL_R8; + channels = GL_RED; + type = GL_UNSIGNED_BYTE; + break; + + default: + FATAL( "unsupported TextureFormat: %d", config.format ); + break; + } + + glTexImage2D( GL_TEXTURE_2D, 0, internal_format, + config.width, config.height, 0, channels, type, config.data ); + + return static_cast< Texture >( texture ); +} + +void renderer_delete_texture( Texture texture ) { + GLuint gl_texture = checked_cast< GLuint >( texture ); + glDeleteTextures( 1, &gl_texture ); +} + Mesh renderer_new_mesh( MeshConfig config ) { // TODO: assert num_vertices is sane for this primitive type GLuint vao; @@ -253,7 +329,12 @@ void renderer_draw_mesh( const Mesh & mesh, RenderState state ) { // GLuint ubo_index = glGetUniformBlockIndex( shaderA.Program, "uniforms" ); GLuint ubo_index = 0; glBindBufferBase( GL_UNIFORM_BUFFER, ubo_index, state.ubs[ 0 ] ); - // TODO: set depth func, bind textures, etc + // TODO: set depth func, etc + + for( int i = 0; i < ARRAY_COUNT( state.textures ); i++ ) { + glActiveTexture( GL_TEXTURE0 + i ); + glBindTexture( GL_TEXTURE_2D, state.textures[ i ] ); + } GLenum primitive = primitivetype_to_glenum( mesh.primitive_type ); if( mesh.indexed ) { diff --git a/renderer.h b/renderer.h @@ -82,27 +82,20 @@ struct ShaderConfig { const char * geometry_src; }; -struct DrawCall { - DrawCallType type; - union { - struct { - void * data; - size_t len; - }; - // or Array< u8 >( data, len ); - - MeshConfig mesh_config; - ShaderConfig shader_config; - RenderState render_state; - }; +enum TextureFormat { + TEXFMT_RGBA_FLOAT, + TEXFMT_RGB_FLOAT, + TEXFMT_R_FLOAT, + TEXFMT_R_U8, }; -struct OpenGLState { - RenderState render_state; - // VB bound_vbo; - // IB bound_ebo; - UB bound_ub; - Texture bound_texture; +struct TextureConfig { + u32 width, height; + TextureFormat format; + // TODO: internal format? + const void * data; + size_t data_size; + // TODO: min/mag filters, wrap modes, border }; enum ClearColourBool { CLEARCOLOUR_DONT, CLEARCOLOUR_DO }; @@ -123,6 +116,9 @@ void renderer_ub_data( UB ub, const void * data, size_t len ); Shader renderer_new_shader( ShaderConfig config ); Shader renderer_new_shader( const char * vertex_src, const char * fragment_src ); +Texture renderer_new_texture( TextureConfig config ); +void renderer_delete_texture( Texture texture ); + Mesh renderer_new_mesh( MeshConfig config ); void renderer_draw_mesh( const Mesh & mesh, RenderState state ); void renderer_delete_mesh( const Mesh & mesh );