/* * Copyright © 2009 Eric Anholt * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. * * Authors: * Eric Anholt * */ #include #include #include #include #include #include #include #include #include #include #include #include "glass.h" /* libpng sucks and I hope I never have to read this file again. */ GLuint load_texture_rgb(const char *filename) { GLuint tex; FILE *f; png_uint_32 w, h; int depth; unsigned char *data; png_structp png; png_infop png_info; int png_type; int stride; png_bytep *row_pointers; int i; GLenum format; glGenTextures(1, &tex); f = fopen(filename, "rb"); png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); assert(png); png_info = png_create_info_struct(png); assert(png_info); if (setjmp(png->jmpbuf)) { png_destroy_read_struct(&png, &png_info, NULL); abort(); } png_init_io(png, f); png_read_info(png, png_info); png_get_IHDR(png, png_info, &w, &h, &depth, &png_type, NULL, NULL, NULL); /* Give us sensible data. This API is terrible */ png_set_expand(png); /* No, really. Sensible data. */ if (depth == 16) png_set_strip_16(png); /* Not even kidding. */ png_set_gray_to_rgb(png); png_read_update_info(png, png_info); stride = png_get_rowbytes(png, png_info); data = malloc(h * stride); assert(data); /* So, you've told us what the stride needs to be, but you make * us give you pointers to the data? Brilliant. */ row_pointers = malloc(sizeof(png_bytep) * h); assert(row_pointers); for (i = 0; i < h; i++) row_pointers[i] = data + i * stride; png_read_image(png, row_pointers); glBindTexture(GL_TEXTURE_2D, tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); switch (png_info->color_type) { case PNG_COLOR_TYPE_RGB: format = GL_RGB; break; case PNG_COLOR_TYPE_RGBA: format = GL_RGBA; break; default: abort(); } glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, format, GL_UNSIGNED_BYTE, data); free(data); free(row_pointers); png_destroy_read_struct(&png, &png_info, NULL); return tex; }