diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2018-05-02 21:22:03 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2018-05-02 21:22:03 +0100 |
commit | b659c412263f6dd7df962f7d42f4f51b7c3d5609 (patch) | |
tree | e2495e0b1446527b909a9c9fa2776797c7c28221 /tex.c | |
parent | ec93a8eddf70adef2b1439a511d990021ad25d99 (diff) | |
download | faqe-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.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -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; +} |