2011-04-14 09:11:17 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
2016-12-04 18:36:53 +00:00
|
|
|
//
|
2011-04-14 09:11:17 +00:00
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
2011-05-27 18:53:16 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2011-06-04 19:27:04 +00:00
|
|
|
#if defined (SG_MAC)
|
2016-12-04 18:36:53 +00:00
|
|
|
#include <OpenGL/gl.h>
|
2011-06-04 19:27:04 +00:00
|
|
|
#include <GLUT/glut.h>
|
2016-12-04 18:36:53 +00:00
|
|
|
#elif defined (_GLES2)
|
|
|
|
#include <GLES2/gl2.h>
|
2011-06-04 19:27:04 +00:00
|
|
|
#else
|
2016-12-04 18:36:53 +00:00
|
|
|
#include <GL/glew.h> // Must be included before <GL/gl.h>
|
|
|
|
#include <GL/gl.h>
|
2011-06-04 19:27:04 +00:00
|
|
|
#include <GL/glut.h>
|
|
|
|
#endif
|
2016-12-04 18:36:53 +00:00
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
#include <png.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <iostream>
|
2016-12-04 18:36:53 +00:00
|
|
|
|
|
|
|
#include "FGPNGTextureLoader.hxx"
|
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
using namespace std;
|
2016-12-04 18:36:53 +00:00
|
|
|
|
|
|
|
GLuint
|
|
|
|
FGPNGTextureLoader::loadTexture (const string &filename) {
|
2011-04-14 09:11:17 +00:00
|
|
|
//header for testing if it is a png
|
|
|
|
png_byte header[8];
|
2016-12-04 18:36:53 +00:00
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
//open file as binary
|
2016-12-04 18:36:53 +00:00
|
|
|
FILE * const fp (fopen (filename.c_str (), "rb"));
|
2011-04-14 09:11:17 +00:00
|
|
|
if (!fp) {
|
|
|
|
return NOTEXTURE;
|
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
//read the header
|
2016-12-04 18:36:53 +00:00
|
|
|
fread (header, 1, 8, fp);
|
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
//test if png
|
2016-12-04 18:36:53 +00:00
|
|
|
const int is_png (!png_sig_cmp (header, 0, 8));
|
2011-04-14 09:11:17 +00:00
|
|
|
if (!is_png) {
|
2016-12-04 18:36:53 +00:00
|
|
|
fclose (fp);
|
2011-04-14 09:11:17 +00:00
|
|
|
return NOTEXTURE;
|
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
//create png struct
|
2016-12-04 18:36:53 +00:00
|
|
|
png_structp png_ptr (png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL));
|
2011-04-14 09:11:17 +00:00
|
|
|
if (!png_ptr) {
|
2016-12-04 18:36:53 +00:00
|
|
|
fclose (fp);
|
|
|
|
return NOTEXTURE;
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
//create png info struct
|
2016-12-04 18:36:53 +00:00
|
|
|
png_infop info_ptr (png_create_info_struct (png_ptr));
|
2011-04-14 09:11:17 +00:00
|
|
|
if (!info_ptr) {
|
2016-12-04 18:36:53 +00:00
|
|
|
png_destroy_read_struct (&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
|
|
|
|
fclose (fp);
|
|
|
|
return NOTEXTURE;
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//create png info struct
|
2016-12-04 18:36:53 +00:00
|
|
|
png_infop end_info (png_create_info_struct (png_ptr));
|
2011-04-14 09:11:17 +00:00
|
|
|
if (!end_info) {
|
2016-12-04 18:36:53 +00:00
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
|
|
|
|
fclose (fp);
|
|
|
|
return NOTEXTURE;
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//png error stuff, not sure libpng man suggests this.
|
2016-12-04 18:36:53 +00:00
|
|
|
if (setjmp (png_jmpbuf (png_ptr))) {
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
|
|
|
fclose (fp);
|
|
|
|
return NOTEXTURE;
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
// init png reading
|
|
|
|
png_init_io (png_ptr, fp);
|
|
|
|
|
|
|
|
// let libpng know you already read the first 8 bytes
|
|
|
|
png_set_sig_bytes (png_ptr, 8);
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
// read all the info up to the image data
|
2016-12-04 18:36:53 +00:00
|
|
|
png_read_info (png_ptr, info_ptr);
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
// variables to pass to get info
|
2011-04-14 09:11:17 +00:00
|
|
|
int bit_depth, color_type;
|
|
|
|
png_uint_32 twidth, theight;
|
|
|
|
|
|
|
|
// get info about png
|
2016-12-04 18:36:53 +00:00
|
|
|
png_get_IHDR (png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, NULL, NULL, NULL);
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
// Update the png info struct.
|
2016-12-04 18:36:53 +00:00
|
|
|
png_read_update_info (png_ptr, info_ptr);
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
// Row size in bytes.
|
2016-12-04 18:36:53 +00:00
|
|
|
const int rowbytes (png_get_rowbytes (png_ptr, info_ptr));
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
// Allocate the image_data as a big block, to be given to opengl
|
2016-12-04 18:36:53 +00:00
|
|
|
png_byte *image_data (new png_byte[rowbytes * theight]);
|
2011-04-14 09:11:17 +00:00
|
|
|
if (!image_data) {
|
|
|
|
//clean up memory and close stuff
|
2016-12-04 18:36:53 +00:00
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
|
|
|
fclose (fp);
|
2011-04-14 09:11:17 +00:00
|
|
|
return NOTEXTURE;
|
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
// row_pointers is for pointing to image_data for reading the png with libpng
|
|
|
|
png_bytep *row_pointers (new png_bytep[theight]);
|
2011-04-14 09:11:17 +00:00
|
|
|
if (!row_pointers) {
|
|
|
|
//clean up memory and close stuff
|
2016-12-04 18:36:53 +00:00
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
|
|
|
delete []image_data;
|
|
|
|
fclose (fp);
|
2011-04-14 09:11:17 +00:00
|
|
|
return NOTEXTURE;
|
|
|
|
}
|
|
|
|
// set the individual row_pointers to point at the correct offsets of image_data
|
2016-12-04 18:36:53 +00:00
|
|
|
for (png_uint_32 i = 0; i < theight; ++i) {
|
2011-04-14 09:11:17 +00:00
|
|
|
row_pointers[theight - 1 - i] = image_data + i * rowbytes;
|
2016-12-04 18:36:53 +00:00
|
|
|
}
|
2011-04-14 09:11:17 +00:00
|
|
|
//read the png into image_data through row_pointers
|
2016-12-04 18:36:53 +00:00
|
|
|
png_read_image (png_ptr, row_pointers);
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
//Now generate the OpenGL texture object
|
|
|
|
GLuint texture;
|
2016-12-04 18:36:53 +00:00
|
|
|
glGenTextures (1, &texture);
|
|
|
|
glBindTexture (GL_TEXTURE_2D, texture);
|
|
|
|
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) image_data);
|
|
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
//clean up memory and close stuff
|
2016-12-04 18:36:53 +00:00
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
|
|
|
delete []image_data;
|
|
|
|
delete []row_pointers;
|
|
|
|
fclose (fp);
|
2011-04-14 09:11:17 +00:00
|
|
|
return texture;
|
|
|
|
}
|