Hello, I have just copied the following code to draw a rectangle, them pasted a raw file for texture. Texture is loaded first. Then the rectangle is drawn inside the graphic loop.With raw file i can view the image properly. Now I want to paste some bmp/jpg file, but I can’t see the texture. For my coding, I need to upload JPG/bmp file. Could anyone say how the following code be changed to make it work with JPG/bmp file. I also want to know how the width/height affects the texture. Sorry for my poor knowledge. If any one clarifies, it will be highly appreciated.
/********************************************************/
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 );
// 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;
}
/*****************************************************************************************/
void drawRectangle( ){
// setup texture mapping
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );
glPushMatrix();
glBegin( GL_QUADS );
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glNormal3f(0.0, 0.0, 1.0);glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
glPopMatrix();
glDisable( GL_TEXTURE_2D );
}