00001
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
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
00033 static GLuint g_handles[TEX_N];
00034
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
00042 void initTextures()
00043 {
00044
00045 glGenTextures( TEX_N, g_handles );
00046
00047 glGenTextures( 1, &g_cube_map_handle);
00048
00049
00050 for (int i=0; i<TEX_N; i++) {
00051
00052
00053 char filename[300];
00054 strcpy( filename, g_strResourceDir );
00055 strcat( filename, g_texfiles[i] );
00056
00057
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
00074 glBindTexture( GL_TEXTURE_2D, g_handles[i] );
00075
00076 gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, g_textures[i] );
00077
00078
00079
00080
00081
00082
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
00089 GLenum Texture;
00090 for (int i=0; i<6; i++)
00091 {
00092
00093 char filename[200];
00094 strcpy( filename, g_strResourceDir );
00095 strcat( filename, g_cube_map_files[i] );
00096
00097
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
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
00124
00125 gluBuild2DMipmaps( Texture,
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
00130
00131 }
00132
00133
00134 void deleteTextures()
00135 {
00136
00137 glDeleteTextures( TEX_N, g_handles );
00138
00139 glDeleteTextures( 1, &g_cube_map_handle);
00140 }
00141
00142
00143
00144 GLuint texture( TEXTURE t )
00145 {
00146 assert( t>=0 && t<TEX_N );
00147 return g_handles[t];
00148 }