14#ifndef CSCI441_TEXTURE_UTILS_HPP
15#define CSCI441_TEXTURE_UTILS_HPP
17#ifdef CSCI441_USE_GLEW
57 bool loadPPM(
const char *filename,
int &imageWidth,
int &imageHeight,
unsigned char* &imageData );
74 GLint minFilter = GL_LINEAR,
75 GLint magFilter = GL_LINEAR,
76 GLint wrapS = GL_REPEAT,
77 GLint wrapT = GL_REPEAT,
78 GLboolean flipOnY = GL_TRUE,
79 GLboolean printAllMessages = GL_TRUE,
80 GLboolean enableMipmaps = GL_TRUE,
81 GLboolean enableAniso = GL_TRUE);
100 GLint minFilter = GL_LINEAR,
101 GLint magFilter = GL_LINEAR,
102 GLint wrapS = GL_REPEAT,
103 GLint wrapT = GL_REPEAT,
104 GLboolean flipOnY = GL_TRUE,
105 GLboolean printAllMessages = GL_TRUE,
106 GLboolean enableMipmaps = GL_TRUE,
107 GLboolean enableAniso = GL_TRUE);
123 FILE *fp = fopen(filename,
"r");
125 fscanf(fp,
"P%d", &temp);
127 fprintf(stderr,
"[ERROR]: CSCI441::TextureUtils::loadPPM(): PPM file is not of correct format! (Must be P3, is P%d.)\n", temp);
133 fscanf(fp,
"%d", &imageWidth);
134 fscanf(fp,
"%d", &imageHeight);
135 fscanf(fp,
"%d", &maxValue);
138 imageData =
new unsigned char[imageWidth*imageHeight*3];
140 fprintf(stderr,
"[ERROR]: CSCI441::TextureUtils::loadPPM(): couldn't allocate image memory. Dimensions: %d x %d.\n", imageWidth, imageHeight);
146 for(
int j = 0; j < imageHeight; j++) {
147 for(
int i = 0; i < imageWidth; i++) {
149 fscanf(fp,
"%d", &r);
150 fscanf(fp,
"%d", &g);
151 fscanf(fp,
"%d", &b);
153 imageData[(j*imageWidth+i)*3+0] = r;
154 imageData[(j*imageWidth+i)*3+1] = g;
155 imageData[(j*imageWidth+i)*3+2] = b;
164inline GLuint
CSCI441::TextureUtils::loadAndRegisterTexture(
const char *filename,
const GLint minFilter,
const GLint magFilter,
const GLint wrapS,
const GLint wrapT,
const GLboolean flipOnY,
const GLboolean printAllMessages,
const GLboolean enableMipmaps,
const GLboolean enableAniso ) {
165 return loadAndRegister2DTexture( filename, minFilter, magFilter, wrapS, wrapT, flipOnY, printAllMessages, enableMipmaps, enableAniso );
168inline GLuint
CSCI441::TextureUtils::loadAndRegister2DTexture(
const char *filename,
const GLint minFilter,
const GLint magFilter,
const GLint wrapS,
const GLint wrapT,
const GLboolean flipOnY,
const GLboolean printAllMessages,
const GLboolean enableMipmaps,
const GLboolean enableAniso ) {
169 int imageWidth, imageHeight, imageChannels;
170 GLuint texHandle = 0;
171 stbi_set_flip_vertically_on_load(flipOnY);
172 unsigned char *data = stbi_load( filename, &imageWidth, &imageHeight, &imageChannels, 0);
175 if( strstr(filename,
".ppm") != NULL ) {
176 loadPPM(filename, imageWidth, imageHeight, data);
180 if(printAllMessages) printf(
"[ERROR]: CSCI441::TextureUtils::loadAndRegister2DTexture(): Could not load texture \"%s\"\n", filename );
185 glGenTextures(1, &texHandle );
186 glBindTexture( GL_TEXTURE_2D, texHandle );
187 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter );
188 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter );
189 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS );
190 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT );
191 const GLint STORAGE_TYPE = (imageChannels == 4 ? GL_RGBA : GL_RGB);
192 glTexImage2D( GL_TEXTURE_2D, 0, STORAGE_TYPE, imageWidth, imageHeight, 0, STORAGE_TYPE, GL_UNSIGNED_BYTE, data);
194 if(enableMipmaps) glGenerateMipmap(GL_TEXTURE_2D);
198 GLint major = 0, minor = 0;
199 glGetIntegerv(GL_MAJOR_VERSION, &major);
200 glGetIntegerv(GL_MINOR_VERSION, &minor);
202 if( (major > 4 || (major == 4 && minor >= 6)) || GL_EXT_texture_filter_anisotropic ) {
203 GLfloat maxAniso = 1.0f;
204 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso);
205 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAniso);
209 printf(
"[INFO]: Successfully loaded texture \"%s\" with handle %d\n", filename, texHandle );
216 int imageWidth, imageHeight, imageChannels;
217 unsigned char *data = stbi_load( FILENAME, &imageWidth, &imageHeight, &imageChannels, 0);
220 const GLint STORAGE_TYPE = (imageChannels == 4 ? GL_RGBA : GL_RGB);
221 glTexImage2D(cubeMapFace, 0, STORAGE_TYPE, imageWidth, imageHeight, 0, STORAGE_TYPE, GL_UNSIGNED_BYTE, data);
222 stbi_image_free(data);
224 fprintf( stderr,
"[ERROR]: CSCI441::TextureUtils::loadCubeMapFaceTexture(): Could not load texture map \"%s\"\n", FILENAME );
void loadCubeMapFaceTexture(GLint cubeMapFace, const char *filename)
loads a texture into memory of a cube face
Definition: TextureUtils.hpp:215
bool loadPPM(const char *filename, int &imageWidth, int &imageHeight, unsigned char *&imageData)
loads a PPM into memory
Definition: TextureUtils.hpp:122
GLuint loadAndRegister2DTexture(const char *filename, GLint minFilter=GL_LINEAR, GLint magFilter=GL_LINEAR, GLint wrapS=GL_REPEAT, GLint wrapT=GL_REPEAT, GLboolean flipOnY=GL_TRUE, GLboolean printAllMessages=GL_TRUE, GLboolean enableMipmaps=GL_TRUE, GLboolean enableAniso=GL_TRUE)
loads and registers a texture into memory returning a texture handle
Definition: TextureUtils.hpp:168
GLuint loadAndRegisterTexture(const char *filename, GLint minFilter=GL_LINEAR, GLint magFilter=GL_LINEAR, GLint wrapS=GL_REPEAT, GLint wrapT=GL_REPEAT, GLboolean flipOnY=GL_TRUE, GLboolean printAllMessages=GL_TRUE, GLboolean enableMipmaps=GL_TRUE, GLboolean enableAniso=GL_TRUE)
loads and registers a texture into memory returning a texture handle
Definition: TextureUtils.hpp:164
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17
OpenGL Texture Utility functions.