PDA

View Full Version : texture mapping to object



truetaurus
10-24-2009, 08:50 AM
Im trying texture mapping but it doesnt seem to work, the object stays gray, see code below:



GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;

// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;

// allocate buffer
width = 256;
height = 256;
data = malloc( width * height * 3 );

// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );

// allocate a texture name
glGenTextures( 1, &texture );

// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );

// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
wrap ? GL_REPEAT : GL_CLAMP );

// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );

// free buffer
free( data );

return texture;
}

....

GLuint texture;
glGenTextures( 1, &texture ); // allocate a texture name
texture = LoadTextureRAW("sign.gif", 1);
glEnable( GL_TEXTURE_3D );
glBindTexture( GL_TEXTURE_3D, texture );
glBegin(GL_QUADS); // Start Drawing A Quad
glTexCoord3f(-1.0,1.0, 0.0); glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left Of The Quad
glTexCoord3f(1.0,1.0, 0.0); glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right Of The Quad
glTexCoord3f(1.0,-1.0, 0.0); glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right Of The Quad
glTexCoord3f(-1.0,-1.0, 0.0); glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left Of The Quad
glEnd();



the file is 256x256 and is a gif.

todayman
10-24-2009, 12:38 PM
In the second part, I think you should use GL_TEXTURE_2D, not 3D. See if that helps.