From 470dac0e891c1ec59dfe52e77861cf068837976d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 31 Jul 2024 20:55:37 +0200 Subject: Add mipmap option --- src/graphics/sprite.c | 8 ++++++++ src/graphics/texture.c | 22 ++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'src/graphics') diff --git a/src/graphics/sprite.c b/src/graphics/sprite.c index 95f733b..3110cf9 100644 --- a/src/graphics/sprite.c +++ b/src/graphics/sprite.c @@ -47,6 +47,14 @@ void mgl_sprite_set_height(mgl_sprite *self, float height) { self->scale.y = self->scale.x; } +mgl_vec2f mgl_sprite_get_size(const mgl_sprite *self) { + if(self->texture) { + return (mgl_vec2f){ (float)self->texture->width * self->scale.x, (float)self->texture->height * self->scale.y }; + } else { + return (mgl_vec2f){ 0.0f, 0.0f }; + } +} + /* TODO: Cache texture bind to not bind texture if its already bound and do not bind texture 0 */ void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite) { if(!sprite->texture) diff --git a/src/graphics/texture.c b/src/graphics/texture.c index e098dd2..5de3d2b 100644 --- a/src/graphics/texture.c +++ b/src/graphics/texture.c @@ -96,22 +96,30 @@ int mgl_texture_load_from_memory(mgl_texture *self, const unsigned char *data, i if(width > self->max_width || height > self->max_height) return -1; + mgl_context *context = mgl_get_context(); + self->width = width; self->height = height; self->format = mgl_image_format_to_mgl_texture_format(format); - self->pixel_coordinates = load_options ? load_options->pixel_coordinates : false; + self->pixel_coordinates = load_options && load_options->pixel_coordinates; + self->mipmap = load_options && load_options->mipmap && context->gl.glGenerateMipmap; /* TODO: Check if glGenerateMipmap is actually available */ int opengl_texture_format = mgl_texture_format_to_opengl_format(self->format); if(load_options && load_options->compressed) opengl_texture_format = mgl_texture_format_to_compressed_opengl_format(self->format); - mgl_context *context = mgl_get_context(); context->gl.glBindTexture(GL_TEXTURE_2D, self->id); context->gl.glTexImage2D(GL_TEXTURE_2D, 0, opengl_texture_format, self->width, self->height, 0, mgl_texture_format_to_source_opengl_format(self->format), GL_UNSIGNED_BYTE, data); context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + if(self->mipmap) { + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + context->gl.glGenerateMipmap(GL_TEXTURE_2D); + } else { + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + } context->gl.glBindTexture(GL_TEXTURE_2D, 0); return 0; @@ -125,6 +133,9 @@ int mgl_texture_update(mgl_texture *self, const unsigned char *data, int offset_ context->gl.glBindTexture(GL_TEXTURE_2D, self->id); const mgl_texture_format texture_format = mgl_image_format_to_mgl_texture_format(format); context->gl.glTexSubImage2D(GL_TEXTURE_2D, 0, offset_x, offset_y, width, height, mgl_texture_format_to_source_opengl_format(texture_format), GL_UNSIGNED_BYTE, data); + if(self->mipmap) { + context->gl.glGenerateMipmap(GL_TEXTURE_2D); + } context->gl.glBindTexture(GL_TEXTURE_2D, 0); return 0; } @@ -147,6 +158,9 @@ int mgl_texture_resize(mgl_texture *self, int new_width, int new_height, const m mgl_context *context = mgl_get_context(); context->gl.glBindTexture(GL_TEXTURE_2D, self->id); context->gl.glTexImage2D(GL_TEXTURE_2D, 0, opengl_texture_format, self->width, self->height, 0, mgl_texture_format_to_source_opengl_format(self->format), GL_UNSIGNED_BYTE, NULL); + if(self->mipmap) { + context->gl.glGenerateMipmap(GL_TEXTURE_2D); + } context->gl.glBindTexture(GL_TEXTURE_2D, 0); return 0; } -- cgit v1.2.3