Advertisement

Loading textures in classes

Started by July 07, 2003 05:23 AM
3 comments, last by Vidar 21 years, 7 months ago
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.
Are you sure you''ve enabled texturing??
Advertisement
It might be helpful to show your texture uploading code as well.

-Mezz
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=texture-&gt;imageData[i+2];<br> texture-&gt;imageData[i+2]=temp;<br> }<br> fclose(file);<br> <br> glGenTextures(1, &amp;texture[0].texID);<br> glBindTexture(GL_TEXTURE_2D,texture[0].texID);<br> glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);<br> glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);<br> if (texture[0].bpp==24)<br> {<br> type=GL_RGB;<br> } <br> glTexImage2D(GL_TEXTURE_2D,0,type,texture[0].width, texture[0].height,0, type, GL_UNSIGNED_BYTE, texture[0].imageData);<br> return true;<br>}<br> </i>
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

This topic is closed to new replies.

Advertisement