PDA

View Full Version : Binding a texture in a class



12-12-2002, 02: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, &amp;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();
}

MickeyMouse
12-12-2002, 03:15 AM
Hi!
Make sure auxDIBImageLoad(..) doesn't return 0.
One more thing is you probably wanted to have:
delete[] TextureImage->data;
in your code, rather than:
delete TextureImage->data;
..but it's not white texture related

12-12-2002, 03:21 AM
What causes auxDIBImageLoad to return 0?

MickeyMouse
12-12-2002, 03:44 AM
Usually loaders return 0 in case loading failed, auxDIBImageLoad is probably the same about returned values.

[This message has been edited by MickeyMouse (edited 12-12-2002).]

12-12-2002, 04:35 AM
I just noticed that it is not always solid white

it is solid whatever i put in the call to glColour4f(..)

12-12-2002, 04:46 AM
Figured it out, i was calling ApplyTexture before i had properly initialised the window. stupid me http://www.opengl.org/discussion_boards/ubb/smile.gif

DFrey
12-12-2002, 05:31 AM
In other words, you didn't have an active render context. Without one of those, most OpenGL functions will simply fail quietly.