For some reason the texture is not being applied to the polygon. it draws the polygon with the last color set. what is wrong with the code? as it is obivous from the looking at the code, it is using MFC/C++. no glut* solutions please. and ignore potential memory leaks, etc. i take care of it in a different routine.
void COpenGL3View::LoadTexture(
const char * path)
{
struct bmp_header
{
char sig[2];
unsigned int fileSize, res1, dataOffset;
unsigned int bmpHeaderSize, width, height;
unsigned short planes, bpp;
unsigned int compression, dataSize;
unsigned int hres, vres;
unsigned int numColors, impColors;
} header;
ifstream infile (path);
infile.read(&header.sig[0]
, 2);
infile.read((char*)&header
.fileSize,
4*6);
infile.read((char*)&header
.planes, 2*2);
infile.read((char*)&header
.compressi
on, 4*5);
char str[512] = "";
sprintf(str, "filesize = %d\ndata = %d\nheadersize = %d\nwidth = %d, height = %d\n"
"planes = %d, bpp = %d\ncompression = %d\ndatasize = %d\n"
"hres = %d, vres = %d\nnumcolors = %d, impcolors = %x",
header.fileSize, header.dataOffset, header.bmpHeaderSize, header.width, header.height,
header.planes, header.bpp, header.compression, header.dataSize,
header.hres, header.vres, header.numColors, header.impColors);
//MessageBox(str);
texture = malloc(header.dataSize);
if ( texture != NULL )
{
infile.seekg(header.dataOf
fset);
infile.read((char*)texture
, header.dataSize);
sprintf(str, "file read success\nfirst pixel is (%02x,%02x,%02x)",
((unsigned char*)texture)[0],
((unsigned char*)texture)[1],
((unsigned char*)texture)[2]);
//MessageBox(str);
}
::glBindTexture(GL_TEXTURE
_2D, 1);
::glPixelStorei(GL_UNPACK_
ALIGNMENT,
1);
::glTexParameteri(GL_TEXTU
RE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // not really relevant
::glTexParameteri(GL_TEXTU
RE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // -"-
::glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE, GL_MODULATE); // -"-
::glTexImage2D(GL_TEXTURE_
2D, 0, GL_RGB, header.width, header.height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture);
::glEnable(GL_TEXTURE_2D);
}
BOOL COpenGL3View::RenderScene(
)
{
::glBindTexture(GL_TEXTURE
_2D, 1);
::glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
{
::glTexCoord2i( 0, 0); ::glVertex3f(0, 0, 0);
::glTexCoord2i(171, 0); ::glVertex3f(0, 0,100);
::glTexCoord2i(171,171); ::glVertex3f(0,100,100);
::glTexCoord2i( 0,171); ::glVertex3f(0,100, 0);
}
glEnd();
::glDisable(GL_TEXTURE_2D)
;
/* the code below is fine */
/*
glEnable(GL_LIGHTING);
auxSolidSphere( 0.7*50 );
glDisable(GL_LIGHTING);
*/
glBegin(GL_LINES); // x y z axes
{
::glColor3f(1,0,0); ::glVertex3f( 0, 0, 0); ::glVertex3f( 200, 0, 0);
::glColor3f(0,1,0); ::glVertex3f( 0, 0, 0); ::glVertex3f( 0, 200, 0);
::glColor3f(0,0,1); ::glVertex3f( 0, 0, 0); ::glVertex3f( 0, 0, 200);
}
glEnd();
return true;
}