Texture Loader for NeHe’s tutorials
I haven’t been writing posts for a while (I’ve been trying to learn 3D game programming), but I thought I’d write something today. I’ve been reading NeHe’s OpenGL tutorials and he uses GLaux, an extremely outdated library (his tutorials are now almost 13 years old!). Here’s what he said about GLaux: “
When the first tutorials were written, GLAUX was the way to go. Over time GLAUX lost support. Many of the tutorials on this site still use the old GLAUX code. If your compiler does not support GLAUX or you would rather not use it, download the GLAUX REPLACEMENT CODE from the main page (left menu).
” – NeHe’s 1st tutorial at http://nehe.gamedev.net/tutorial/creating_an_opengl_window_%28win32%29/13001/.
Although his tutorials are outdated and occasionally frustrating, they’re usually worth a look. Find them at nehe.gamedev.net and then go to the right column and look for Legacy Tutorials 01-05.
So, this code comes from the Swiftless tutorial series at http://www.swiftless.com/opengltuts.html.
Swiftless also has tutorials on PhysX and other programming related things.
Back to the code:
GLuint LoadTexture(const char * filename, int width, int height) {
GLuint texture;
unsigned char * data;
FILE * file;
file = fopen(filename, “rb”);
if (file == NULL) return 0;
data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
free(data);
return texture;
}
void FreeTexture(GLuint texture) {
glDeleteTextures(1, &texture);
}
It only works with RAW formatted textures, I’ll work on converting his code to the BMP format.
NOTE: I haven’t been all the way through NeHe’s tutorials, so I can’t test it. However, I have tested it with another tutorial series and it worked fine. Thanks for reading, I should post something on 3D games soon.
Wow!
Ken - 2013/01/25 at 9:34 pm |