From b659c412263f6dd7df962f7d42f4f51b7c3d5609 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Wed, 2 May 2018 21:22:03 +0100 Subject: Implement diffuse texture loading and basic material support --- tex.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tex.c (limited to 'tex.c') 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 +#include +#include + +#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; +} -- cgit v1.2.3-54-g00ecf