diff options
author | dec05eba <dec05eba@protonmail.com> | 2025-04-14 23:13:50 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2025-04-14 23:13:50 +0200 |
commit | 0243be4eebe488b449742474d4301a39f00ac67f (patch) | |
tree | b1109991e8a3da169e562f9878769b99ffd3820f /src | |
parent | 46962ea3fc83a26fcf0851ccb748b08eecf7f8e8 (diff) |
Add scaling option for texture, allow nearest neighbour scaling
Diffstat (limited to 'src')
-rw-r--r-- | src/graphics/texture.c | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/src/graphics/texture.c b/src/graphics/texture.c index 9a13314..d33bf58 100644 --- a/src/graphics/texture.c +++ b/src/graphics/texture.c @@ -70,7 +70,7 @@ int mgl_texture_init(mgl_texture *self) { self->max_width = 0; self->max_height = 0; self->pixel_coordinates = false; - self->mipmap = false; + self->scale_type = MGL_TEXTURE_SCALE_LINEAR; self->owned = true; mgl_context *context = mgl_get_context(); @@ -96,7 +96,7 @@ int mgl_texture_init_reference_existing_gl_texture(mgl_texture *self, unsigned i self->max_width = 0; self->max_height = 0; self->pixel_coordinates = reference_options && reference_options->pixel_coordinates; - self->mipmap = false; + self->scale_type = reference_options ? reference_options->scale_type : MGL_TEXTURE_SCALE_LINEAR; self->owned = false; gl_get_texture_size(self->id, &self->width, &self->height); @@ -137,7 +137,7 @@ int mgl_texture_load_from_memory(mgl_texture *self, const unsigned char *data, i self->height = height; self->format = mgl_image_format_to_mgl_texture_format(format); 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 */ + self->scale_type = load_options ? load_options->scale_type : MGL_TEXTURE_SCALE_LINEAR; /* 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) @@ -147,13 +147,28 @@ int mgl_texture_load_from_memory(mgl_texture *self, const unsigned char *data, i 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); - 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); + switch(self->scale_type) { + default: + case MGL_TEXTURE_SCALE_LINEAR: { + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + break; + } + case MGL_TEXTURE_SCALE_NEAREST: { + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + break; + } + case MGL_TEXTURE_SCALE_LINEAR_MIPMAP: { + context->gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + if(context->gl.glGenerateMipmap) { + 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_MIN_FILTER, GL_LINEAR); + } + break; + } } context->gl.glBindTexture(GL_TEXTURE_2D, 0); @@ -168,9 +183,8 @@ 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) { + if(self->scale_type == MGL_TEXTURE_SCALE_LINEAR_MIPMAP && context->gl.glGenerateMipmap) context->gl.glGenerateMipmap(GL_TEXTURE_2D); - } context->gl.glBindTexture(GL_TEXTURE_2D, 0); return 0; } @@ -193,9 +207,8 @@ 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) { + if(self->scale_type == MGL_TEXTURE_SCALE_LINEAR_MIPMAP && context->gl.glGenerateMipmap) context->gl.glGenerateMipmap(GL_TEXTURE_2D); - } context->gl.glBindTexture(GL_TEXTURE_2D, 0); return 0; } |