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 };
00019
00020
00021 static GLuint g_handles[TEX_N];
00022
00023 typedef unsigned char rgba[4];
00024 typedef rgba teximage[ 256*256 ];
00025 static teximage g_textures[TEX_N];
00026
00027
00028
00029 void initTextures()
00030 {
00031
00032 glGenTextures( TEX_N, g_handles );
00033
00034
00035 for (int i=0; i<TEX_N; i++) {
00036
00037
00038 char filename[200];
00039 strcpy( filename, g_strResourceDir );
00040 strcat( filename, g_texfiles[i] );
00041
00042
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
00059 glBindTexture( GL_TEXTURE_2D, g_handles[i] );
00060
00061 gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, g_textures[i] );
00062
00063
00064
00065
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
00072 void deleteTextures()
00073 {
00074
00075 glDeleteTextures( TEX_N, g_handles );
00076 }
00077
00078
00079
00080 GLuint texture( TEXTURE t )
00081 {
00082 assert( t>=0 && t<TEX_N );
00083 return g_handles[t];
00084 }