GLuint texture

Started by
1 comment, last by icecubeflower 15 years, 9 months ago
Hey I used to use NeHe's TGA loading function:

struct TextureImage
{
   GLubyte *imageData;
   GLuint bpp;
   GLuint width;
   GLuint height;
   GLuint texID;
};

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)
         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);  //Ah, malloc, C's answer to "new".  So when is this memory freed?
                                                       //you have to free it yourself NeHe has a Deinitialize function

   if(texture->imageData==NULL||fread(texture->imageData, 1, imageSize, file)!=imageSize)
   {
      if(texture->imageData!=NULL)
      {
         free(texture->imageData);  //well, I guess this is how you free it
         texture->imageData = NULL;
      }
      fclose(file);
      return false;
   }

   //for a fun time comment this out
   for(GLuint i=0; i<(unsigned int)imageSize; i+=bytesPerPixel)
   {
      temp=texture->imageData;
      texture->imageData = texture->imageData;
      texture-&gt;imageData = temp;
   }

   fclose (file);

   <span class="cpp-comment">// Build A Texture From The Data</span>
   glGenTextures(<span class="cpp-number">1</span>, &amp;texture[<span class="cpp-number">0</span>].texID);  <span class="cpp-comment">//texture is a pointer to a TextureImage</span>
                                         <span class="cpp-comment">//why not just texture.texID?  why &amp;texture[0]??  wtf</span>
                                         <span class="cpp-comment">//the the address of a pointer?  the first element of it?  it has no elements, wtf?</span>
                                         <span class="cpp-comment">//wtf could [0] possibly mean?</span>
   <span class="cpp-comment">//oh I remember, you use -&gt; for pointers instead of . like above</span>
   <span class="cpp-comment">//so here he wants to use . so maybe ampersand on a pointer "converts" it back to a regular data type?</span>
   <span class="cpp-comment">//I have no idea what's going on</span>

   glBindTexture(GL_TEXTURE_2D, texture[<span class="cpp-number">0</span>].texID);  <span class="cpp-comment">//And here it needs no ampersand.  Why?  I don't know.</span>
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

   <span class="cpp-keyword">if</span>(texture[<span class="cpp-number">0</span>].bpp==<span class="cpp-number">24</span>)
   {
      type=GL_RGB;
   }

   <span class="cpp-comment">//Here using . instead of -&gt; so maybe [0] takes care of it?  Then why the ampersand above?</span>
   glTexImage2D(GL_TEXTURE_2D, <span class="cpp-number">0</span>, type, texture[<span class="cpp-number">0</span>].width, texture[<span class="cpp-number">0</span>].height,<span class="cpp-number">0</span>, type, GL_UNSIGNED_BYTE, texture[<span class="cpp-number">0</span>].imageData);

   <span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;
}

</pre></div><!–ENDSCRIPT–>

Recently I switched over to the SDL_image library because it kicks more ass.  However I used to do a trick back in my TGA days where I would go like this:
<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-keyword">if</span>(textures[<span class="cpp-number">2</span>].imageData!=NULL)
   then blah blah blah

</pre></div><!–ENDSCRIPT–>
So I would execute a bunch of stuff, like display a 2nd layer, but &#111;nly if an image was loaded up.  These days all I have around is an array of GLuints that correspond to the png files that OpenGL loaded into memory.  Is there any way to see if there's a loaded image that corresponds to your GLuint?  I don't want to have to make a bunch of booleans to store along with my GLuints just to record if they're being used or not.  If I set a GLuint to NULL that just makes it zero, doesn't it?  For all I know that might actually correspond with an image loaded in memory so that wouldn't work.  Does OpenGL have a specific range that it sets the GLuints to?  Is there some magic number I can set GLuints to so that I know they aren't being used?

I'm looking at glAreTexturesResident and I'm thinking I can send a GLuint to that and it will return false if that GLuint does not correspond to a loaded image.  But what if I'm misunderstanding it?  What if OpenGL runs out of video memory so it unloads an image that isn't being used much into regular memory?  Then would glAreTexturesResident return false for that GLuint even though it's technically loaded, just not in video memory?

Advertisement
Quote:Original post by icecubeflower
So I would execute a bunch of stuff, like display a 2nd layer, but only if an image was loaded up. These days all I have around is an array of GLuints that correspond to the png files that OpenGL loaded into memory. Is there any way to see if there's a loaded image that corresponds to your GLuint? I don't want to have to make a bunch of booleans to store along with my GLuints just to record if they're being used or not. If I set a GLuint to NULL that just makes it zero, doesn't it? For all I know that might actually correspond with an image loaded in memory so that wouldn't work. Does OpenGL have a specific range that it sets the GLuints to? Is there some magic number I can set GLuints to so that I know they aren't being used?
OpenGL use 0 (zero) to signify no texture. glGenTextures() will always return a non-zero id (unless an error occurs, which is very unlikely).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Sweet! That was the best possible answer I could have hoped for.

This topic is closed to new replies.

Advertisement