12-12-2002, 01:23 AM
Im trying to bind a texture to a Quadrilateral in a class Quad
ive used a fair chunk of code from Nehe's Tutorial 7, but i cant get it to apply the texture.
The vertice drawing code works and allows textured polygons, however this code makes the quad white all over, as if i hadnt enabled GL_TEXTURE_2D.
I cant figure out why it isnt working, all the correct lines execute, ive stepped through it with a debugger, the file exists and the line ive used to call the function is
CQuad Q;
Q.ApplyTexture("crate.bmp");
then later
Q.Draw(Offset);
//Apply A Bitmap File Texture to a polygon
void CQuad::ApplyTexture(const char *cpInput)
{
//If so, calculate Texture Doodads. ( i dont understand how it works)
//Do We Have a texture?
if(cpInput) //yes we have a texture
{
//Check that it exists.
fstream TexFile(cpInput, ios::in | ios:: binary);
ASSERT(TexFile);
TexFile.close();
cpTextureFile = new char[strlen(cpInput)+1];
//copy strings (to <- from)
strcpy(cpTextureFile, cpInput);
}
else //We Dont have a texture
{
cpTextureFile = 0;
bIsTextured = false;
return;
}
if (strlen(cpInput)) //Is a non null string
{
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
//Yes We Are Texxing
bIsTextured = true;
//GL Texture Info
AUX_RGBImageRec *TextureImage;
// Load The Bitmap
TextureImage=auxDIBImageLoad(cpInput);
glGenTextures(1, &texture); // Create Texture
// Create Nearest mipmapped Texture
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage->sizeX, TextureImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
delete TextureImage->data; // Free The Texture Image Memory
delete TextureImage; // Free The Image Structure
}
}
//Draws the Quad using OpenGL
//if a texture is specified, enable texturing
void CQuad: http://www.opengl.org/discussion_boards/ubb/biggrin.gifraw(CPosition CDrawOrigin)
{
if (bIsTextured)
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
else
glDisable(GL_TEXTURE_2D); //No Texture for you.
glBegin(GL_QUADS);
//Vertex 1 of 4
if(bIsTextured)
glTexCoord2f(0.0f, 0.0f); // Bottom Left Of The Texture
CVertice[0].Draw(CDrawOrigin, bIsTextured);
//Vertex 2 of 4
if(bIsTextured)
glTexCoord2f(1.0f, 0.0f); // Bottom Right Of The Texture
CVertice[1].Draw(CDrawOrigin, bIsTextured);
//Vertex 3 of 4
if(bIsTextured)
glTexCoord2f(1.0f, 1.0f); // Top Right Of The Texture
CVertice[2].Draw(CDrawOrigin, bIsTextured);
//Vertex 4 of 4
if(bIsTextured)
glTexCoord2f(0.0f, 1.0f); // Top Left Of The Texture
CVertice[3].Draw(CDrawOrigin, bIsTextured);
glEnd();
}
ive used a fair chunk of code from Nehe's Tutorial 7, but i cant get it to apply the texture.
The vertice drawing code works and allows textured polygons, however this code makes the quad white all over, as if i hadnt enabled GL_TEXTURE_2D.
I cant figure out why it isnt working, all the correct lines execute, ive stepped through it with a debugger, the file exists and the line ive used to call the function is
CQuad Q;
Q.ApplyTexture("crate.bmp");
then later
Q.Draw(Offset);
//Apply A Bitmap File Texture to a polygon
void CQuad::ApplyTexture(const char *cpInput)
{
//If so, calculate Texture Doodads. ( i dont understand how it works)
//Do We Have a texture?
if(cpInput) //yes we have a texture
{
//Check that it exists.
fstream TexFile(cpInput, ios::in | ios:: binary);
ASSERT(TexFile);
TexFile.close();
cpTextureFile = new char[strlen(cpInput)+1];
//copy strings (to <- from)
strcpy(cpTextureFile, cpInput);
}
else //We Dont have a texture
{
cpTextureFile = 0;
bIsTextured = false;
return;
}
if (strlen(cpInput)) //Is a non null string
{
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
//Yes We Are Texxing
bIsTextured = true;
//GL Texture Info
AUX_RGBImageRec *TextureImage;
// Load The Bitmap
TextureImage=auxDIBImageLoad(cpInput);
glGenTextures(1, &texture); // Create Texture
// Create Nearest mipmapped Texture
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage->sizeX, TextureImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
delete TextureImage->data; // Free The Texture Image Memory
delete TextureImage; // Free The Image Structure
}
}
//Draws the Quad using OpenGL
//if a texture is specified, enable texturing
void CQuad: http://www.opengl.org/discussion_boards/ubb/biggrin.gifraw(CPosition CDrawOrigin)
{
if (bIsTextured)
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
else
glDisable(GL_TEXTURE_2D); //No Texture for you.
glBegin(GL_QUADS);
//Vertex 1 of 4
if(bIsTextured)
glTexCoord2f(0.0f, 0.0f); // Bottom Left Of The Texture
CVertice[0].Draw(CDrawOrigin, bIsTextured);
//Vertex 2 of 4
if(bIsTextured)
glTexCoord2f(1.0f, 0.0f); // Bottom Right Of The Texture
CVertice[1].Draw(CDrawOrigin, bIsTextured);
//Vertex 3 of 4
if(bIsTextured)
glTexCoord2f(1.0f, 1.0f); // Top Right Of The Texture
CVertice[2].Draw(CDrawOrigin, bIsTextured);
//Vertex 4 of 4
if(bIsTextured)
glTexCoord2f(0.0f, 1.0f); // Top Left Of The Texture
CVertice[3].Draw(CDrawOrigin, bIsTextured);
glEnd();
}