CSCI441 OpenGL Library 5.17.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#include "constants.h"
18
19#ifdef CSCI441_USE_GLEW
20 #include <GL/glew.h>
21#else
22 #include <glad/gl.h>
23#endif
24
25#include <stb_image.h>
26
27#include <cstdio>
28#include <cstring>
29#include <string>
30
31//**********************************************************************************
32
33namespace CSCI441 {
34
39 namespace TextureUtils {
40
59 bool loadPPM( const char *filename, int &imageWidth, int &imageHeight, unsigned char* &imageData );
60
75 [[maybe_unused]] GLuint loadAndRegisterTexture( const char *filename,
76 GLint minFilter = GL_LINEAR,
77 GLint magFilter = GL_LINEAR,
78 GLint wrapS = GL_REPEAT,
79 GLint wrapT = GL_REPEAT,
80 GLboolean flipOnY = GL_TRUE,
81 GLboolean printAllMessages = GL_TRUE,
82 GLboolean enableMipmaps = GL_TRUE,
83 GLboolean enableAniso = GL_TRUE);
84
101 GLuint loadAndRegister2DTexture( const char *filename,
102 GLint minFilter = GL_LINEAR,
103 GLint magFilter = GL_LINEAR,
104 GLint wrapS = GL_REPEAT,
105 GLint wrapT = GL_REPEAT,
106 GLboolean flipOnY = GL_TRUE,
107 GLboolean printAllMessages = GL_TRUE,
108 GLboolean enableMipmaps = GL_TRUE,
109 GLboolean enableAniso = GL_TRUE);
110
117 [[maybe_unused]] void loadCubeMapFaceTexture(GLint cubeMapFace, const char* filename);
118 }
119}
120
121//**********************************************************************************
122// Outward facing function implementations
123
124inline bool CSCI441::TextureUtils::loadPPM( const char *filename, int &imageWidth, int &imageHeight, unsigned char* &imageData ) {
125 FILE *fp = fopen(filename, "r");
126 int temp, maxValue;
127 fscanf(fp, "P%d", &temp);
128 if(temp != 3) {
129 fprintf(stderr, "[ERROR]: CSCI441::TextureUtils::loadPPM(): PPM file is not of correct format! (Must be P3, is P%d.)\n", temp);
130 fclose(fp);
131 return false;
132 }
133
134 //got the file header right...
135 fscanf(fp, "%d", &imageWidth);
136 fscanf(fp, "%d", &imageHeight);
137 fscanf(fp, "%d", &maxValue);
138
139 //now that we know how big it is, allocate the buffer...
140 imageData = new unsigned char[imageWidth*imageHeight*3];
141 if(!imageData) {
142 fprintf(stderr, "[ERROR]: CSCI441::TextureUtils::loadPPM(): couldn't allocate image memory. Dimensions: %d x %d.\n", imageWidth, imageHeight);
143 fclose(fp);
144 return false;
145 }
146
147 //and read the data in.
148 for(int j = 0; j < imageHeight; j++) {
149 for(int i = 0; i < imageWidth; i++) {
150 int r, g, b;
151 fscanf(fp, "%d", &r);
152 fscanf(fp, "%d", &g);
153 fscanf(fp, "%d", &b);
154
155 imageData[(j*imageWidth+i)*3+0] = r;
156 imageData[(j*imageWidth+i)*3+1] = g;
157 imageData[(j*imageWidth+i)*3+2] = b;
158 }
159 }
160
161 fclose(fp);
162 return true;
163}
164
165[[maybe_unused]]
166inline 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 ) {
167 return loadAndRegister2DTexture( filename, minFilter, magFilter, wrapS, wrapT, flipOnY, printAllMessages, enableMipmaps, enableAniso );
168}
169
170inline 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 ) {
171 int imageWidth, imageHeight, imageChannels;
172 GLuint texHandle = 0;
173 stbi_set_flip_vertically_on_load(flipOnY);
174 unsigned char *data = stbi_load( filename, &imageWidth, &imageHeight, &imageChannels, 0);
175
176 if( !data ) {
177 if( strstr(filename, ".ppm") != NULL ) {
178 loadPPM(filename, imageWidth, imageHeight, data);
179 imageChannels = 3;
180 }
181 if( !data ) {
182 if(printAllMessages) printf( "[ERROR]: CSCI441::TextureUtils::loadAndRegister2DTexture(): Could not load texture \"%s\"\n", filename );
183 return texHandle;
184 }
185 }
186
187 glGenTextures(1, &texHandle );
188 glBindTexture( GL_TEXTURE_2D, texHandle );
189 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter );
190 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter );
191 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS );
192 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT );
193 const GLint STORAGE_TYPE = (imageChannels == 4 ? GL_RGBA : GL_RGB);
194 glTexImage2D( GL_TEXTURE_2D, 0, STORAGE_TYPE, imageWidth, imageHeight, 0, STORAGE_TYPE, GL_UNSIGNED_BYTE, data);
195
196 if(enableMipmaps) glGenerateMipmap(GL_TEXTURE_2D);
197
198 if(enableAniso) {
199 // anisotropic filtering became core in OpenGL 4.6, but was widely supported via extensions prior to then
200 GLint major = 0, minor = 0;
201 glGetIntegerv(GL_MAJOR_VERSION, &major);
202 glGetIntegerv(GL_MINOR_VERSION, &minor);
203 // check if anisotropic filtering is enabled
204 if( (major > 4 || (major == 4 && minor >= 6)) || GL_EXT_texture_filter_anisotropic ) {
205 GLfloat maxAniso = 1.0f;
206 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso);
207 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAniso);
208 }
209 }
210
211 printf( "[INFO]: Successfully loaded texture \"%s\" with handle %d\n", filename, texHandle );
212
213 return texHandle;
214}
215
216[[maybe_unused]]
217inline void CSCI441::TextureUtils::loadCubeMapFaceTexture(const GLint cubeMapFace, const char* FILENAME) {
218 int imageWidth, imageHeight, imageChannels;
219 unsigned char *data = stbi_load( FILENAME, &imageWidth, &imageHeight, &imageChannels, 0);
220
221 if( data ) {
222 const GLint STORAGE_TYPE = (imageChannels == 4 ? GL_RGBA : GL_RGB);
223 glTexImage2D(cubeMapFace, 0, STORAGE_TYPE, imageWidth, imageHeight, 0, STORAGE_TYPE, GL_UNSIGNED_BYTE, data);
224 stbi_image_free(data);
225 } else {
226 fprintf( stderr, "[ERROR]: CSCI441::TextureUtils::loadCubeMapFaceTexture(): Could not load texture map \"%s\"\n", FILENAME );
227 }
228}
229
230#endif // CSCI441_TEXTURE_UTILS_HPP
void loadCubeMapFaceTexture(GLint cubeMapFace, const char *filename)
loads a texture into memory of a cube face
Definition: TextureUtils.hpp:217
bool loadPPM(const char *filename, int &imageWidth, int &imageHeight, unsigned char *&imageData)
loads a PPM into memory
Definition: TextureUtils.hpp:124
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:170
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:166
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17
OpenGL Texture Utility functions.