Hiya
Im writing a program in C using openGL (GLUT) and im having a little trouble with the function to process textures (RAW files). It compiles ok but when i bind the texture in my display function and apply it to a quad it does not display it at runtime (just has a simple RGB default colour i had assigned with glColor3f). It has to be because its not allocating the memory correctly with the malloc i think? but i cant think of a way to just get it working, can anyone please maybe help me.. Pretty please? Im getting really frustrated lol.
Below is the texture processing function:
Code:
// load a 256x256 RGB .RAW file as a texture
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 = (BYTE*)malloc( width * height * 3 ); // <---- is this right?
// 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 MIP map
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first MIP map
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 MIP maps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );
// free buffer
free( data );
return texture;
}
Im using it like this (just showing the parts that matter):
Code:
GLuint texture;
texture = LoadTextureRAW("bush.raw", TRUE);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glColor3f(0.0, 0.6, 0.0); //Just the default colour
glTexCoord2f(0.0,0.0);
glVertex3f(-1000.0, 0.0, 1000.0);
glTexCoord2f(0.0,1.0);
glVertex3f(1000.0, 0.0, 1000.0);
glTexCoord2f(1.0,1.0);
glVertex3f(1000.0, 0.0, -1000.0);
glTexCoord2f(1.0,0.0);
glVertex3f(-1000.0, 0.0, -1000.0);
glEnd();
I really need to get this working
Thanks.