aboutsummaryrefslogtreecommitdiffstats
path: root/tex.c
diff options
context:
space:
mode:
Diffstat (limited to 'tex.c')
-rw-r--r--tex.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/tex.c b/tex.c
new file mode 100644
index 0000000..6f27b8a
--- /dev/null
+++ b/tex.c
@@ -0,0 +1,36 @@
+#include <assert.h>
+#include <png.h>
+#include <stdlib.h>
+
+#include "eprintf.h"
+#include "tex.h"
+
+GLuint png2tex(FILE *f)
+{
+ png_image pi = { .version = PNG_IMAGE_VERSION };
+ unsigned char *data;
+ size_t size;
+ GLuint id;
+
+ assert(f != NULL);
+
+ png_image_begin_read_from_stdio(&pi, f);
+ pi.format = PNG_FORMAT_RGB;
+ size = sizeof *data * 3 * pi.width * pi.height;
+ data = emalloc(size);
+ png_image_finish_read(&pi, NULL, data, size / pi.height, NULL);
+
+ gl_tex_gen(1, &id);
+ gl_tex_bind(GL_TEXTURE_2D, id);
+ gl_tex_parami(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ gl_tex_parami(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ gl_tex_parami(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
+ gl_tex_parami(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+ gl_tex_img2d(GL_TEXTURE_2D, 0, GL_RGB, pi.width, pi.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
+ gl_tex_genmip(GL_TEXTURE_2D);
+
+ free(data);
+ png_image_free(&pi);
+
+ return id;
+}