aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mgl/gl_macro.h1
-rw-r--r--include/mgl/graphics/texture.h11
-rw-r--r--src/graphics/texture.c41
-rw-r--r--tests/main.c4
4 files changed, 39 insertions, 18 deletions
diff --git a/include/mgl/gl_macro.h b/include/mgl/gl_macro.h
index 07ce1c4..574a3d8 100644
--- a/include/mgl/gl_macro.h
+++ b/include/mgl/gl_macro.h
@@ -48,6 +48,7 @@
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#define GL_CLAMP_TO_EDGE 0x812F
+#define GL_NEAREST 0x2600
#define GL_LINEAR 0x2601
#define GL_LINEAR_MIPMAP_LINEAR 0x2703
diff --git a/include/mgl/graphics/texture.h b/include/mgl/graphics/texture.h
index d65adee..b658daf 100644
--- a/include/mgl/graphics/texture.h
+++ b/include/mgl/graphics/texture.h
@@ -14,6 +14,12 @@ typedef enum {
MGL_TEXTURE_FORMAT_RGBA
} mgl_texture_format;
+typedef enum {
+ MGL_TEXTURE_SCALE_NEAREST,
+ MGL_TEXTURE_SCALE_LINEAR,
+ MGL_TEXTURE_SCALE_LINEAR_MIPMAP /* generate mipmaps (available since opengl 3.0) */
+} mgl_texture_scale_type;
+
struct mgl_texture {
unsigned int id;
int width;
@@ -22,18 +28,19 @@ struct mgl_texture {
int max_width;
int max_height;
bool pixel_coordinates;
- bool mipmap;
+ mgl_texture_scale_type scale_type;
bool owned;
};
typedef struct {
bool compressed; /* false by default */
bool pixel_coordinates; /* false by default, texture coordinates are normalized */
- bool mipmap; /* false by default, generate mipmaps (available since opengl 3.0) */
+ mgl_texture_scale_type scale_type; /* MGL_TEXTURE_SCALE_LINEAR by default */
} mgl_texture_load_options;
typedef struct {
bool pixel_coordinates; /* false by default, texture coordinates are normalized */
+ mgl_texture_scale_type scale_type; /* MGL_TEXTURE_SCALE_LINEAR by default */
} mgl_texture_reference_options;
int mgl_texture_init(mgl_texture *self);
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;
}
diff --git a/tests/main.c b/tests/main.c
index 09e59ed..48fb2d8 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -40,7 +40,7 @@ static void draw(mgl_window *window, void *userdata) {
mgl_sprite sprite;
mgl_sprite_init(&sprite, u->texture);
- mgl_sprite_set_position(&sprite, (mgl_vec2f){ 100.0f - 10.0f + u->texture->width * 0.5f * 2.0f, u->texture->height * 0.5f * 2.0f });
+ mgl_sprite_set_position(&sprite, (mgl_vec2f){ 500.0f - 10.0f + u->texture->width * 0.5f * 2.0f, 500.0f + u->texture->height * 0.5f * 2.0f });
mgl_sprite_set_color(&sprite, (mgl_color){255, 255, 255, 128});
mgl_sprite_set_rotation(&sprite, rot);
mgl_sprite_set_origin(&sprite, (mgl_vec2f){ u->texture->width * 0.5f, u->texture->height * 0.5f });
@@ -262,7 +262,7 @@ int main(void) {
if(mgl_texture_init(&texture) != 0)
return 1;
- if(mgl_texture_load_from_file(&texture, "tests/X11.jpg", &(mgl_texture_load_options){ .compressed = false, .pixel_coordinates = false, .mipmap = false }) != 0)
+ if(mgl_texture_load_from_file(&texture, "tests/X11.jpg", &(mgl_texture_load_options){ .compressed = false, .pixel_coordinates = false, .scale_type = MGL_TEXTURE_SCALE_LINEAR }) != 0)
return 1;
if(mgl_mapped_file_load("/usr/share/fonts/TTF/Hack-Regular.ttf", &font_file, &(mgl_memory_mapped_file_load_options){ .readable = true, .writable = false }) != 0)