I have an application that I want to map various pgm or ppm files over an object. I have a couple different drawing methods such as Wireframed, Shaded, Texture Mapped with the image that creates the object, and Texture Mapped with another image. I can successfully texture map with the ppm files, but I am having problems texture mapping with the pgm files. I read in a pgm file and store its intensities at each (x,y) in an array. Since it is a pgm file, I know texture mapping will just show a grayscale image. Here is how I set up my texture mapping for when I use a pgm file to map.
// Set filtering parameter.
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, 0, ncTex, nrTex, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, image );
// Provide texture mapping method.
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
Then in my display function, I call these calls based on an if-else statement:
glEnable( GL_TEXTURE_2D );
glDisable( GL_LIGHTING );
glCallList( texture );
Here is the function I use to create my object when I COMPILE my display list.
void drawTexture( )
{
float v[3][3];
float normal[3];
float r[3], s[3];
int x;
int y;
glBindTexture( GL_TEXTURE_2D, texName[0] );
for( y = 0 ; y < ( nr - 1 ) ; y++ )
{
for( x = 0 ; x < ( nc - 1 ) ; x++ )
{
v[0][0] = x - nc / 2;
v[0][1] = y - nr / 2;
v[0][2] = image[y * nc + x];
r[0] = ( (float) x ) / ( (float) ( ncTex - 1 ) );
s[0] = ( (float) y ) / ( (float) ( nrTex - 1 ) );
v[1][0] = x - nc / 2;
v[1][1] = ( y + 1 ) - nr / 2;
v[1][2] = image[( y + 1 ) * nc + x];
r[1] = ( (float) x ) / ( (float) ( ncTex - 1 ) );
s[1] = ( (float) ( y + 1 ) ) / ( (float) ( nrTex - 1 ) );
v[2][0] = ( x + 1 ) - nc / 2;
v[2][1] = y - nr / 2;
v[2][2] = image[y * nc + ( x + 1 )];
r[2] = ( (float) ( x + 1 ) ) / ( (float) ( ncTex - 1 ) );
s[2] = ( (float) y ) / ( (float) ( nrTex - 1 ) );
calcNormal( v, normal );
glBegin( GL_POLYGON );
glNormal3fv( normal );
glTexCoord2f( r[0], s[0] );
glVertex3fv( v[0] );
glTexCoord2f( r[1], s[1] );
glVertex3fv( v[1] );
glTexCoord2f( r[2], s[2] );
glVertex3fv( v[2] );
glEnd( );
v[0][0] = x - nc / 2;
v[0][1] = ( y + 1 ) - nr / 2;
v[0][2] = image[( y + 1 ) * nc + x];
r[0] = ( (float) x ) / ( (float) ( ncTex - 1 ) );
s[0] = ( (float) ( y + 1 ) ) / ( (float) ( nrTex - 1 ) );
v[1][0] = ( x + 1 ) - nc / 2;
v[1][1] = y - nr / 2;
v[1][2] = image[y * nc + ( x + 1 )];
r[1] = ( (float) ( x + 1 ) ) / ( (float) ( ncTex - 1 ) );
s[1] = ( (float) y ) / ( (float) ( nrTex - 1 ) );
v[2][0] = ( x + 1 ) - nc / 2;
v[2][1] = ( y + 1 ) - nr / 2;
v[2][2] = image[( y + 1 ) * nc + ( x + 1 )];
r[2] = ( (float) ( x + 1 ) ) / ( (float) ( ncTex - 1 ) );
s[2] = ( (float) ( y + 1 ) ) / ( (float) ( nrTex - 1 ) );
calcNormal( v, normal );
glBegin( GL_POLYGON );
glNormal3fv( normal );
glTexCoord2f( r[0], s[0] );
glVertex3fv( v[0] );
glTexCoord2f( r[1], s[1] );
glVertex3fv( v[1] );
glTexCoord2f( r[2], s[2] );
glVertex3fv( v[2] );
glEnd( );
} // end for( x = 0 ; x < ( nc - 1 ) ; x++ )
} // end for( y = 0 ; y < ( nr - 1 ) ; y++ )
}
I am drawing this image, triangle-by-triangle and I draw a whole square each iteration through the for-loop. ncTex and nrTex are the width and height of the object in a value of a power of 2, like 512x1024. I think I am mapping the texture correctly, especially since the ppm files work. The only thing I can think of is because the pgm files are grayscale. I have tried making them into RGBA with making R=G=B and A=255, but this seemed to make no difference. Also, when I try to map a pgm file after mapping the ppm file, the ppm file remains. It is as if no change is occurring. Thanks for your help!