PDA

View Full Version : Textures



chris_golby
02-16-2010, 08:17 AM
Hi, I'm pretty new to OpenGL and I'm trying to add some textures to a room I've created. I've managed to get the textures to display but with some pretty adverse side effects.

The floor in the room is displaying an extra line, as is the back wall. The side walls and floor also have areas where the texture is distorting.

Here is my function for loading a texture:







GLuint LoadTexture( const char * filename, int wrap )
{

GLuint texture;
int width, height;
unsigned char * data;
FILE * file;

file = fopen( filename, "rb" );
if ( file == NULL ) return 0;

width = 300;
height = 300;
data = (unsigned char*)malloc( width * height * 3);

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

glGenTextures( 1, &texture );

glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,
height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data );

free( data );

return texture;

}





And here is my code that utilises this function:




//Floor:
texture = LoadTexture( "woodenFloor.bmp", TRUE );
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture );
glBegin(GL_POLYGON);
glTexCoord2f (0.0,0.0); glVertex3f(-20.0,-20.0,1);
glTexCoord2f (1.0,0.0); glVertex3f(20.0,-20.0,1);
glTexCoord2f (1.0,1.0); glVertex3f(110.0,10.0,-300);
glTexCoord2f (0.0,1.0); glVertex3f(-110.0,10.0,-300);
glEnd();
glDisable(GL_TEXTURE_2D);


//Wallpaper Texture:
texture = LoadTexture( "leaves.bmp", TRUE );

//Back Wall:
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture );
glBegin(GL_POLYGON);
glTexCoord2f (0.0,0.0); glVertex3f(110.0,10.0,-300);
glTexCoord2f (1.0,0.0); glVertex3f(-110.0,10.0,-300);
glTexCoord2f (1.0,1.0); glVertex3f(-110.0,200.0,-300);
glTexCoord2f (0.0,1.0); glVertex3f(110.0,200.0,-300);
glEnd();
glDisable(GL_TEXTURE_2D);


//Left Wall:
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture );
glColor3f(0.6,0.6,0.6);
glBegin(GL_POLYGON);
glTexCoord2f (0.0,0.0); glVertex3f(-110.0,10.0,-300);
glTexCoord2f (1.0,0.0); glVertex3f(-110.0,200.0,-300);
glTexCoord2f (1.0,1.0); glVertex3f(-200.0,10.0,1);
glTexCoord2f (0.0,1.0); glVertex3f(-20.0,-20.0,1);
glEnd();
glDisable(GL_TEXTURE_2D);

//Right Wall:
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture );
glColor3f(0.6,0.6,0.6);
glBegin(GL_POLYGON);
glTexCoord2f (0.0,0.0); glVertex3f(110.0,10.0,-300);
glTexCoord2f (1.0,0.0); glVertex3f(110.0,200.0,-300);
glTexCoord2f (1.0,1.0); glVertex3f(200.0,10.0,1);
glTexCoord2f (0.0,1.0); glVertex3f(20.0,-20.0,1);
glEnd();
glDisable(GL_TEXTURE_2D);





Any help would be much appreciated!

Chris

strattonbrazil
02-16-2010, 09:34 AM
The floor in the room is displaying an extra line, as is the back wall. The side walls and floor also have areas where the texture is distorting.

What is an extra line? Do you have a texture of lines or something? How is it distorted? You get the correct geometry, but the texture is too big? Too small? It repeats 10 times when it should only fit once on the quad? It's correct for three corners, but the fourth is wrong?