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