medfall

A super great game engine
Log | Files | Refs

commit 7508d242ccf8168171ff7a86e613a7939781d652
parent f1dfaca068a2640c86150dd3bc83491143341505
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon May 15 03:24:25 +0300

Add texture wrap mode to the renderer

Diffstat:
renderer.cc | 84++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
renderer.h | 21++++++++++++++++-----
2 files changed, 65 insertions(+), 40 deletions(-)
diff --git a/renderer.cc b/renderer.cc @@ -76,6 +76,22 @@ static GLenum textureformat_to_glenum( TextureFormat format, bool srgb ) { return GL_INVALID_ENUM; } +static GLenum texturewrap_to_glenum( TextureWrapMode mode ) { + switch( mode ) { + case TEXWRAP_REPEAT: + return GL_REPEAT; + case TEXWRAP_CLAMP: + return GL_CLAMP_TO_EDGE; + case TEXWRAP_MIRROR: + return GL_MIRRORED_REPEAT; + case TEXWRAP_BORDER: + return GL_CLAMP_TO_BORDER; + } + + FATAL( "unsupported TextureWrapMode: {}", mode ); + return GL_INVALID_ENUM; +} + static bool is_compressed( TextureFormat format ) { return format == TEXFMT_BC1 || format == TEXFMT_BC3 || format == TEXFMT_BC4 || format == TEXFMT_BC5; @@ -332,50 +348,48 @@ 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 = textureformat_to_glenum( config.format, config.srgb ); - GLenum channels = GL_INVALID_ENUM; - GLenum type = GL_INVALID_ENUM; - - switch( config.format ) { - case TEXFMT_RGBA_FLOAT: - channels = GL_RGBA; - type = GL_FLOAT; - break; - - case TEXFMT_RGB_FLOAT: - channels = GL_RGB; - type = GL_FLOAT; - break; - - case TEXFMT_R_FLOAT: - channels = GL_RED; - type = GL_FLOAT; - break; - - case TEXFMT_R_U8: - channels = GL_RED; - type = GL_UNSIGNED_BYTE; - break; - - case TEXFMT_BC4: - break; + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0 ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0 ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texturewrap_to_glenum( config.wrap ) ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texturewrap_to_glenum( config.wrap ) ); - default: - FATAL( "unsupported TextureFormat: {}", config.format ); - break; + if( config.wrap == TEXWRAP_BORDER ) { + glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, ( GLfloat * ) &config.border_colour ); } + GLenum internal_format = textureformat_to_glenum( config.format, config.srgb ); + if( is_compressed( config.format ) ) { u32 dxt_size = texture_byte_size( config.width, config.height, TEXFMT_BC4, false ); glCompressedTexImage2D( GL_TEXTURE_2D, 0, internal_format, config.width, config.height, 0, dxt_size, config.data ); } else { + GLenum channels = GL_INVALID_ENUM; + GLenum type = GL_INVALID_ENUM; + + switch( config.format ) { + case TEXFMT_RGBA_FLOAT: + channels = GL_RGBA; + type = GL_FLOAT; + break; + case TEXFMT_RGB_FLOAT: + channels = GL_RGB; + type = GL_FLOAT; + break; + case TEXFMT_R_FLOAT: + channels = GL_RED; + type = GL_FLOAT; + break; + case TEXFMT_R_U8: + channels = GL_RED; + type = GL_UNSIGNED_BYTE; + break; + default: + FATAL( "implement channel/type selection for {}", config.format ); + break; + } + glTexImage2D( GL_TEXTURE_2D, 0, internal_format, config.width, config.height, 0, channels, type, config.data ); } diff --git a/renderer.h b/renderer.h @@ -1,7 +1,7 @@ #pragma once #include "intrinsics.h" - +#include "linear_algebra.h" #define GLSL( shader ) "#version 330\n" #shader /* @@ -132,15 +132,26 @@ enum TextureFormat { TEXFMT_BC5, }; +enum TextureWrapMode { + TEXWRAP_REPEAT, + TEXWRAP_CLAMP, + TEXWRAP_MIRROR, + TEXWRAP_BORDER, +}; + struct TextureConfig { u32 width = 0; u32 height = 0; - TextureFormat format = TEXFMT_INVALID; - bool srgb = false; + const void * data = NULL; u32 data_size; - // TODO: internal format? - // TODO: min/mag filters, wrap modes, border + + TextureFormat format = TEXFMT_INVALID; + TextureWrapMode wrap = TEXWRAP_REPEAT; + // TODO: min/mag filters, mipmaps + + v4 border_colour; + bool srgb = false; }; enum ClearColourBool { CLEARCOLOUR_DONT, CLEARCOLOUR_DO };