Archived

This topic is now archived and is closed to further replies.

Vidar

Loading textures in classes

Recommended Posts

I have a problem oading textures in classes. this is my class: class Ctest { public: Ctest(); TextureImage textures[1]; }; Ctest::Ctest() { LoadTGA(&textures[0],"BM_FULL.tga"); } My drawing code looks like this: glBindTexture(GL_TEXTURE_2D,test.textures[0].texID); glBegin (GL_QUADS); glTexCoord2f(0.0f,0.0f ); glVertex2f (-0.3f, -0.3f); glTexCoord2f(0.0f,1.0f ); glVertex2f ( 0.3f, -0.3f); glTexCoord2f(1.0f,1.0f ); glVertex2f ( 0.3f, 0.3f); glTexCoord2f(1.0f,0.0f ); glVertex2f (-0.3f, 0.3f); glEnd (); The result is a white square. If I load my texture outside my class like so (see below) the texture shows up fine. void Init() { glEnable(GL_TEXTURE_2D); LoadTGA(&test.textures[0],"BM_FULL.tga"); } But I want my classes to manage their own textures. Can this be done? PS. The LoadTGA function above is NEHE''s function from lesson 24.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Texturing is enabled, and the texture uploading code is just as in Nehe''s lesson 24:

bool LoadTGA(TextureImage *texture, char *filename)
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
GLubyte TGAcompare[12];
GLubyte header[6];
GLuint bytesPerPixel;
GLuint imageSize;
GLuint temp;
GLuint type=GL_RGBA;

FILE *file= fopen(filename,"rb";
if ( file==NULL ||
fread( TGAcompare,1, sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||
memcmp(TGAheader, TGAcompare, sizeof(TGAheader))!=0 ||
fread(header,1,sizeof(header),file)!=sizeof(header))
{
if (file==NULL)
{
return false;
}
else
{
fclose(file);
return false;
}
}
texture->width = header[1]*256+header[0];
texture->height = header[3]*256+header[2];
if (texture->width<=0 ||
texture->height<=0||
(header[4]!=24 && header[4]!=32))
{
fclose(file);
return false;
}
texture->bpp = header[4];
bytesPerPixel = texture->bpp / 8;
imageSize= texture->width * texture->height * bytesPerPixel;

texture->imageData= (GLubyte *) malloc(imageSize);

if (texture->imageData== NULL |
fread(texture->imageData,1,imageSize,file)!=imageSize)
{
if(texture->imageData!=NULL)
free(texture->imageData);

fclose(file);
return false;
}

for (GLuint i=0; i< int(imageSize); i+=bytesPerPixel)
{
temp=texture->imageData;
texture->imageData[i]=texture->imageData[i+2];
texture->imageData[i+2]=temp;
}
fclose(file);

glGenTextures(1, &texture[0].texID);
glBindTexture(GL_TEXTURE_2D,texture[0].texID);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
if (texture[0].bpp==24)
{
type=GL_RGB;
}
glTexImage2D(GL_TEXTURE_2D,0,type,texture[0].width, texture[0].height,0, type, GL_UNSIGNED_BYTE, texture[0].imageData);
return true;
}

Share this post


Link to post
Share on other sites
OK, I think I must have skimmed your first post earlier...

The reason it might not be working is that your CTest object is probably being constructed before OpenGL is initialised, and hence fails to upload the texture properly. This is why it works for you if you load it outside the class in your Init() function (which is probably called after you''ve created your window etc.).
Instead of putting the loading code in the constructor, try making a method called ''LoadTexture'' or something similar and calling that to load your texture.

BTW, your texture coordinates look like they are y-axis inverted too, but that might be intentional.

-Mezz

Share this post


Link to post
Share on other sites