Main Page | Namespace List | 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   "grass-shtrih"
00019 };
00020 
00021 static const char *g_cube_map_files[6] = {
00022     "tropical_lf.raw_256x256_rgba_byte",
00023     "tropical_rt.raw_256x256_rgba_byte",
00024     "tropical_up.raw_256x256_rgba_byte",
00025     "tropical_dn.raw_256x256_rgba_byte",
00026     "tropical_ft.raw_256x256_rgba_byte",
00027     "tropical_rt.raw_256x256_rgba_byte" };
00028     
00029 GLuint g_cube_map_handle;
00030     
00031 
00032 // Texture handles
00033 static GLuint g_handles[TEX_N];
00034 // Texture data
00035 typedef unsigned char rgba[4];
00036 typedef rgba teximage[ 256*256 ];
00037 static teximage g_textures[TEX_N];
00038 
00039 static teximage g_cube_map_textures[6];
00040 
00041 // Initialize all textures
00042 void initTextures()
00043 {
00044   // Generate handles
00045   glGenTextures( TEX_N, g_handles );
00046 
00047   glGenTextures( 1, &g_cube_map_handle);
00048 
00049   // Load textures
00050   for (int i=0; i<TEX_N; i++) {
00051 
00052     // Generate filename
00053     char filename[300];
00054     strcpy( filename, g_strResourceDir );
00055     strcat( filename, g_texfiles[i] );
00056 
00057     // Open and read file (binary mode)
00058     FILE *f = fopen( filename, "rb" );
00059     if (f == NULL) {
00060       printf( "Error opening texture file %s\n.", g_texfiles[i] );
00061       exit(1);
00062     }
00063 
00064     int n = fread( g_textures[i], 4, 256*256, f );
00065     if (n != 256*256) {
00066       printf( "Error reading texture file %s: Only %i pixels read.\n",
00067               g_texfiles[i], n );
00068     }
00069     else {
00070       printf( "Successfully loaded texture %s.\n", g_texfiles[i] );
00071     }
00072 
00073     // Load texture image to OpenGL
00074     glBindTexture( GL_TEXTURE_2D, g_handles[i] );
00075     // Build all mipmap stages
00076     gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, g_textures[i] );
00077 
00078     // Set filtering modes
00079     // LINEAR_MIPMAP_LINEAR specifies the highest quality mode, sometimes called
00080     // "trilinear filtering". Anisotropic is even better, but not yet officially supported in OpenGL.
00081     //   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00082     //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00083 
00084     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
00085     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00086   }
00087 
00088 //loading cube map
00089 GLenum Texture;
00090     for (int i=0; i<6; i++)
00091     {
00092     // Generate filename
00093     char filename[200];
00094     strcpy( filename, g_strResourceDir );
00095     strcat( filename, g_cube_map_files[i] );
00096 
00097     // Open and read file (binary mode)
00098     FILE *f = fopen( filename, "rb" );
00099     if (f == NULL) {
00100       printf( "Error opening texture file %s\n.", g_cube_map_files[i] );
00101       exit(1);
00102     }
00103 
00104     int n = fread( g_cube_map_textures[i], 4, 256*256, f );
00105     if (n != 256*256) {
00106       printf( "Error reading texture file %s: Only %i pixels read.\n",
00107               g_cube_map_files[i], n );
00108     }
00109     else {
00110       printf( "Successfully loaded texture %s.\n", g_cube_map_files[i] );
00111     }
00112 
00113     // Load texture image to OpenGL
00114         switch (i)
00115         {
00116         case 0: Texture = GL_TEXTURE_CUBE_MAP_POSITIVE_X; break;
00117         case 1: Texture = GL_TEXTURE_CUBE_MAP_NEGATIVE_X; break;
00118         case 2: Texture = GL_TEXTURE_CUBE_MAP_POSITIVE_Y; break;
00119         case 3: Texture = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; break;
00120         case 4: Texture = GL_TEXTURE_CUBE_MAP_POSITIVE_Z; break;
00121         case 5: Texture = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; break;
00122         }
00123 //    glBindTexture( GL_TEXTURE_2D, g_handles[i] );
00124     // Build all mipmap stages
00125     gluBuild2DMipmaps( Texture,//GL_TEXTURE_2D, 
00126         GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, g_cube_map_textures[i] );
00127     }
00128     glBindTexture( GL_TEXTURE_CUBE_MAP, g_cube_map_handle);
00129 //    glEnable( GL_TEXTURE_CUBE_MAP);
00130 
00131 }
00132 
00133 // Delete all textures
00134 void deleteTextures()
00135 {
00136   // Release handles
00137   glDeleteTextures( TEX_N, g_handles );
00138   
00139   glDeleteTextures( 1, &g_cube_map_handle);
00140 }
00141 
00142 
00143 // Get the OpenGL texture handle of a texture
00144 GLuint texture( TEXTURE t )
00145 {
00146   assert( t>=0 && t<TEX_N );
00147   return g_handles[t];
00148 }

Generated on Thu Jan 20 02:46:58 2005 for Main_Demo by doxygen 1.3.6