From 6ad248c86e45933f9c61a7ce9f0546429d41c97e Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 12 Jul 2011 22:45:49 -0500 Subject: add loading texture functions and tweak sdl init --- sdl/gltools.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'sdl/gltools.c') diff --git a/sdl/gltools.c b/sdl/gltools.c index afb0777..2449cfc 100644 --- a/sdl/gltools.c +++ b/sdl/gltools.c @@ -11,6 +11,42 @@ #include #include "gltools.h" +/* mipmapping enabled by default */ +GLuint gltLoadTGATexture(const char *fname) +{ + GLuint handle; + + /* variables used for texture loading */ + GLbyte *pBytes; + GLint iWidth, iHeight, iComponents; + GLenum eFormat; + + glGenTextures(1, &handle); + glBindTexture(GL_TEXTURE_2D, handle); + + /* load texture */ + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + pBytes = gltLoadTGA(fname, &iWidth, &iHeight, &iComponents, &eFormat); + if (!pBytes) + fprintf(stderr, "gltLoadTGA: failed to load texture!\n"); + + glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, + GL_UNSIGNED_BYTE, pBytes); + free(pBytes); + + /* seems like mipmapping only applies to MIN_FILTER since mipmapping divides + * current texture into smaller and smaller pieces + */ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); /* GL_LINEAR to disable mipmapping */ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + return handle; +} + void gltListExtensions(void) { const GLubyte *ext_str; @@ -145,7 +181,7 @@ GLint gltWriteTGA(const char *szFileName) return 1; } -GLbyte *gltLoadTGA(const char *szFileName, GLint *iWidth, GLint *iHeight, +GLbyte *gltLoadTGA(const char *szFileName, GLint *iWidth, GLint *iHeight, GLint *iComponents, GLenum *eFormat) { FILE *pFile; /* file pointer */ -- cgit v1.2.3