aboutsummaryrefslogtreecommitdiffstats
path: root/tex.c
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2018-05-02 21:22:03 +0100
committerTomasz Kramkowski <tk@the-tk.com>2018-05-02 21:22:03 +0100
commitb659c412263f6dd7df962f7d42f4f51b7c3d5609 (patch)
treee2495e0b1446527b909a9c9fa2776797c7c28221 /tex.c
parentec93a8eddf70adef2b1439a511d990021ad25d99 (diff)
downloadfaqe-b659c412263f6dd7df962f7d42f4f51b7c3d5609.tar.gz
faqe-b659c412263f6dd7df962f7d42f4f51b7c3d5609.tar.xz
faqe-b659c412263f6dd7df962f7d42f4f51b7c3d5609.zip
Implement diffuse texture loading and basic material support
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;
+}