Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

texture.cpp

Go to the documentation of this file.
00001 // Texture handling
00002 
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <assert.h>
00006 #include <string.h>
00007 #include <GL/glu.h>
00008 
00009 #include "config.h"
00010 #include "texture.h"
00011 
00012 // Textures file names
00013 static const char *g_texfiles[TEX_N] = {
00014   "rock.raw_256x256_rgba_byte",
00015   "snow.raw_256x256_rgba_byte",
00016   "grass.raw_256x256_rgba_byte",
00017   "sand.raw_256x256_rgba_byte"
00018 };
00019 
00020 // Texture handles
00021 static GLuint g_handles[TEX_N];
00022 // Texture data
00023 typedef unsigned char rgba[4];
00024 typedef rgba teximage[ 256*256 ];
00025 static teximage g_textures[TEX_N];
00026 
00027 
00028 // Initialize all textures
00029 void initTextures()
00030 {
00031   // Generate handles
00032   glGenTextures( TEX_N, g_handles );
00033 
00034   // Load textures
00035   for (int i=0; i<TEX_N; i++) {
00036 
00037     // Generate filename
00038     char filename[200];
00039     strcpy( filename, g_strResourceDir );
00040     strcat( filename, g_texfiles[i] );
00041 
00042     // Open and read file (binary mode)
00043     FILE *f = fopen( filename, "rb" );
00044     if (f == NULL) {
00045       printf( "Error opening texture file %s\n.", g_texfiles[i] );
00046       exit(1);
00047     }
00048 
00049     int n = fread( g_textures[i], 4, 256*256, f );
00050     if (n != 256*256) {
00051       printf( "Error reading texture file %s: Only %i pixels read.\n",
00052               g_texfiles[i], n );
00053     }
00054     else {
00055       printf( "Successfully loaded texture %s.\n", g_texfiles[i] );
00056     }
00057 
00058     // Load texture image to OpenGL
00059     glBindTexture( GL_TEXTURE_2D, g_handles[i] );
00060     // Build all mipmap stages
00061     gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, g_textures[i] );
00062 
00063     // Set filtering modes
00064     // LINEAR_MIPMAP_LINEAR specifies the highest quality mode, sometimes called
00065     // "trilinear filtering". Anisotropic is even better, but not yet officially supported in OpenGL.
00066     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
00067     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00068   }
00069 }
00070 
00071 // Delete all textures
00072 void deleteTextures()
00073 {
00074   // Release handles
00075   glDeleteTextures( TEX_N, g_handles );
00076 }
00077 
00078 
00079 // Get the OpenGL texture handle of a texture
00080 GLuint texture( TEXTURE t )
00081 {
00082   assert( t>=0 && t<TEX_N );
00083   return g_handles[t];
00084 }

Generated on Thu Jan 20 02:47:13 2005 for Projective_Texture_Effect_Demo by doxygen 1.3.6