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

texture.cpp File Reference

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <GL/glu.h>
#include "config.h"
#include "texture.h"

Go to the source code of this file.

Typedefs

typedef unsigned char rgba [4]
typedef rgba teximage [256 *256]

Functions

void initTextures ()
void deleteTextures ()
GLuint texture (TEXTURE t)

Variables

const char * g_texfiles [TEX_N]
const char * g_cube_map_files [6]
GLuint g_cube_map_handle
GLuint g_handles [TEX_N]
teximage g_textures [TEX_N]
teximage g_cube_map_textures [6]


Typedef Documentation

typedef unsigned char rgba[4]
 

Definition at line 35 of file texture.cpp.

typedef rgba teximage[ 256*256 ]
 

Definition at line 36 of file texture.cpp.


Function Documentation

void deleteTextures  ) 
 

Definition at line 134 of file texture.cpp.

References g_cube_map_handle, g_handles, and TEX_N.

00135 {
00136   // Release handles
00137   glDeleteTextures( TEX_N, g_handles );
00138   
00139   glDeleteTextures( 1, &g_cube_map_handle);
00140 }

void initTextures  ) 
 

Definition at line 42 of file texture.cpp.

References g_cube_map_files, g_cube_map_handle, g_cube_map_textures, g_handles, g_strResourceDir, g_texfiles, g_textures, and TEX_N.

Referenced by initGL().

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 }

GLuint texture TEXTURE  t  ) 
 

Definition at line 144 of file texture.cpp.

References g_handles, and TEX_N.

Referenced by drawHeightField(), and drawMultipassQuad().

00145 {
00146   assert( t>=0 && t<TEX_N );
00147   return g_handles[t];
00148 }


Variable Documentation

const char* g_cube_map_files[6] [static]
 

Initial value:

 {
    "tropical_lf.raw_256x256_rgba_byte",
    "tropical_rt.raw_256x256_rgba_byte",
    "tropical_up.raw_256x256_rgba_byte",
    "tropical_dn.raw_256x256_rgba_byte",
    "tropical_ft.raw_256x256_rgba_byte",
    "tropical_rt.raw_256x256_rgba_byte" }

Definition at line 21 of file texture.cpp.

Referenced by initTextures().

GLuint g_cube_map_handle
 

Definition at line 29 of file texture.cpp.

Referenced by deleteTextures(), and initTextures().

teximage g_cube_map_textures[6] [static]
 

Definition at line 39 of file texture.cpp.

Referenced by initTextures().

GLuint g_handles[TEX_N] [static]
 

Definition at line 33 of file texture.cpp.

Referenced by deleteTextures(), initTextures(), and texture().

const char* g_texfiles[TEX_N] [static]
 

Initial value:

 {
  "rock.raw_256x256_rgba_byte",
  "snow.raw_256x256_rgba_byte",
  "grass.raw_256x256_rgba_byte",
  "sand.raw_256x256_rgba_byte",
  "grass-shtrih"
}

Definition at line 13 of file texture.cpp.

Referenced by initTextures().

teximage g_textures[TEX_N] [static]
 

Definition at line 37 of file texture.cpp.

Referenced by initTextures().


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