CSCI441 OpenGL Library 5.9.0
CS@Mines CSCI441 Computer Graphics Course Library
Loading...
Searching...
No Matches
TextureUtils.hpp
Go to the documentation of this file.
1
14#ifndef CSCI441_TEXTURE_UTILS_HPP
15#define CSCI441_TEXTURE_UTILS_HPP
16
17#ifdef CSCI441_USE_GLEW
18 #include <GL/glew.h>
19#else
20 #include <glad/gl.h>
21#endif
22
23#include <stb_image.h>
24
25#include <cstdio>
26#include <cstring>
27#include <string>
28
29//**********************************************************************************
30
31namespace CSCI441 {
32
37 namespace TextureUtils {
38
57 bool loadPPM( const char *filename, int &imageWidth, int &imageHeight, unsigned char* &imageData );
58
73 [[maybe_unused]] GLuint loadAndRegisterTexture( const char *filename,
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);
82
99 GLuint loadAndRegister2DTexture( const char *filename,
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);
108
115 [[maybe_unused]] void loadCubeMapFaceTexture(GLint cubeMapFace, const char* filename);
116 }
117}
118
119//**********************************************************************************
120// Outward facing function implementations
121
122inline bool CSCI441::TextureUtils::loadPPM( const char *filename, int &imageWidth, int &imageHeight, unsigned char* &imageData ) {
123 FILE *fp = fopen(filename, "r");
124 int temp, maxValue;
125 fscanf(fp, "P%d", &temp);
126 if(temp != 3) {
127 fprintf(stderr, "[ERROR]: CSCI441::TextureUtils::loadPPM(): PPM file is not of correct format! (Must be P3, is P%d.)\n", temp);
128 fclose(fp);
129 return false;
130 }
131
132 //got the file header right...
133 fscanf(fp, "%d", &imageWidth);
134 fscanf(fp, "%d", &imageHeight);
135 fscanf(fp, "%d", &maxValue);
136
137 //now that we know how big it is, allocate the buffer...
138 imageData = new unsigned char[imageWidth*imageHeight*3];
139 if(!imageData) {
140 fprintf(stderr, "[ERROR]: CSCI441::TextureUtils::loadPPM(): couldn't allocate image memory. Dimensions: %d x %d.\n", imageWidth, imageHeight);
141 fclose(fp);
142 return false;
143 }
144
145 //and read the data in.
146 for(int j = 0; j < imageHeight; j++) {
147 for(int i = 0; i < imageWidth; i++) {
148 int r, g, b;
149 fscanf(fp, "%d", &r);
150 fscanf(fp, "%d", &g);
151 fscanf(fp, "%d", &b);
152
153 imageData[(j*imageWidth+i)*3+0] = r;
154 imageData[(j*imageWidth+i)*3+1] = g;
155 imageData[(j*imageWidth+i)*3+2] = b;
156 }
157 }
158
159 fclose(fp);
160 return true;
161}
162
163[[maybe_unused]]
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 );
166}
167
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);
173
174 if( !data ) {
175 if( strstr(filename, ".ppm") != NULL ) {
176 loadPPM(filename, imageWidth, imageHeight, data);
177 imageChannels = 3;
178 }
179 if( !data ) {
180 if(printAllMessages) printf( "[ERROR]: CSCI441::TextureUtils::loadAndRegister2DTexture(): Could not load texture \"%s\"\n", filename );
181 return texHandle;
182 }
183 }
184
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);
193
194 if(enableMipmaps) glGenerateMipmap(GL_TEXTURE_2D);
195
196 if(enableAniso) {
197 // anisotropic filtering became core in OpenGL 4.6, but was widely supported via extensions prior to then
198 GLint major = 0, minor = 0;
199 glGetIntegerv(GL_MAJOR_VERSION, &major);
200 glGetIntegerv(GL_MINOR_VERSION, &minor);
201 // check if anisotropic filtering is enabled
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);
206 }
207 }
208
209 printf( "[INFO]: Successfully loaded texture \"%s\" with handle %d\n", filename, texHandle );
210
211 return texHandle;
212}
213
214[[maybe_unused]]
215inline void CSCI441::TextureUtils::loadCubeMapFaceTexture(const GLint cubeMapFace, const char* FILENAME) {
216 int imageWidth, imageHeight, imageChannels;
217 unsigned char *data = stbi_load( FILENAME, &imageWidth, &imageHeight, &imageChannels, 0);
218
219 if( data ) {
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);
223 } else {
224 fprintf( stderr, "[ERROR]: CSCI441::TextureUtils::loadCubeMapFaceTexture(): Could not load texture map \"%s\"\n", FILENAME );
225 }
226}
227
228#endif // CSCI441_TEXTURE_UTILS_HPP
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.