Jump to content
  • Advertisement
Sign in to follow this  
try_catch_this

OpenGL texture compression

This topic is 4876 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to find out if the way I am trying to compress a texture in opengl is correct.
GLuint C_GLTextureMappedFont::LoadGLTextureBitmapFile( char path[], COLORREF transparent_color )
{
   HBITMAP hBMP;
   BITMAP  BMP;

   GLuint Texture = ERROR_CNLT;
   if( !IsExtensionSupported( "GL_EXT_bgra" ) )
   {
      return ERROR_CNLT;
   }

   ::glGenTextures( 1, &Texture ); // Generate Textures
   
   hBMP = (HBITMAP)::LoadImage( NULL, path, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );
   if( hBMP )
   {
      ::GetObject( hBMP, sizeof( BMP ), &BMP );
      ::glBindTexture( GL_TEXTURE_2D, Texture ); // Bind Our Texture
      ::glPixelStorei( GL_UNPACK_ALIGNMENT, 4 ); // Pixel Storage Mode ( Word Alignment / 4 Bytes )

      ::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // Linear Filtering
      ::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // Linear Filtering

      unsigned char *data    = new unsigned char[ BMP.bmWidth * BMP.bmHeight * 4 ];
      if( data == NULL)
         return ERROR_CNLT;
      unsigned int  pixels   = BMP.bmWidth * BMP.bmHeight;
      unsigned char color[3] = {0,0,0};
      unsigned char *bits    = (unsigned char*)BMP.bmBits;

      for( int i=0; i<pixels ; ++i )
      {
         color[0] = *(bits+(i*3));
         color[1] = *(bits+(i*3)+1);
         color[2] = *(bits+(i*3)+2);
         if( color[0] == GetBValue(transparent_color) &&
             color[1] == GetGValue(transparent_color) &&
             color[2] == GetRValue(transparent_color)
           )
         {
            data[ i*4    ] = color[0];
            data[ i*4 +1 ] = color[1];
            data[ i*4 +2 ] = color[2];
            data[ i*4 +3 ] = 0;
         }
         else
         {
            data[ i*4    ] = color[0];
            data[ i*4 +1 ] = color[1];
            data[ i*4 +2 ] = color[2];
            data[ i*4 +3 ] = 255;
         }
      }
      
      if( IsExtensionSupported( "GL_EXT_texture_compression_s3tc" ) &&
          IsExtensionSupported( "GL_ARB_texture_compression" )
        )
      {
         ::gluBuild2DMipmaps( GL_TEXTURE_2D, GL_COMPRESSED_RGBA_ARB, BMP.bmWidth, BMP.bmHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data );
         //::gluBuild2DMipmaps( GL_TEXTURE_2D, 4, BMP.bmWidth, BMP.bmHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data );         
      }
      else
      {
         // Generate Mipmapped Texture (4 Bytes, Width, Height And Data From The BMP)
         ::gluBuild2DMipmaps( GL_TEXTURE_2D, 4, BMP.bmWidth, BMP.bmHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data );
      }

      ::DeleteObject( hBMP );  // Delete The Bitmap Object
      delete[] data;

      return Texture;
   }
   Alert( std::string("Cannot load font map: ") + path + " " + C_Errors::LastSystemError() );
   return ERROR_CNLT;
}



All I do is check to see if the texture compression extension (GL_EXT_texture_compression_s3tc) is supported, then I call:
::gluBuild2DMipmaps( GL_TEXTURE_2D, GL_COMPRESSED_RGBA_ARB, BMP.bmWidth, 
                     BMP.bmHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data );
Is this all I have to do?

Share this post


Link to post
Share on other sites
Advertisement
You don't need to check if GL_EXT_texture_compression_s3tc is supported (only GL_ARB_texture_compression), since it'll revert back to the uncompressed format if it can't be compressed. Beyond that, everything else looks fine.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!