aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/texture.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics/texture.c')
-rw-r--r--src/graphics/texture.c22
1 files changed, 18 insertions, 4 deletions
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;
}