• Advertisement
Sign in to follow this  

Problem adapting lession 17

This topic is 4636 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 have a problem with the texture coords coming out messed up when i try to make a texture mapped font class. NeHe Load function
GLuint C_GLTextureMappedFont::LoadGLTexture( std::string filename )
{
   GLuint texture;
   AUX_RGBImageRec *TextureImage = NULL;               // Create Storage Space For The Textures

   if( ( TextureImage = auxDIBImageLoad( filename.c_str() ) ) )
   {
      ::glGenTextures( 1, &texture ); // Create Two Texture
      ::glBindTexture( GL_TEXTURE_2D, texture );
      ::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
      ::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
      ::glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data );
      if( TextureImage->data ) // If Texture Image Exists
      {
         free( TextureImage->data ); // Free The Texture Image Memory
      }
      free( TextureImage ); // Free The Image Structure
   }
  return texture;
}


My load function
GLuint C_GLTextureMappedFont::LoadGLTextureBitmapFile( char path[], COLORREF transparent_color )
{
   HBITMAP hBMP;
   BITMAP  BMP;

   GLuint Texture = 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, 3 ); // 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


      ::glTexImage2D( GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, (unsigned char*)BMP.bmBits );

      ::DeleteObject( hBMP );  // Delete The Bitmap Object
      delete[] data;
      
      return Texture;
   }
   return ERROR_CNLT;
}


The font build function.
bool C_GLTextureMappedFont::BuildFont( std::string filename, COLORREF transparent_color, float scale_x, float scale_y )
{
   Destroy();
   m_texture_id = LoadGLTexture( filename );
//   m_texture_id = LoadGLTextureBitmapFile( filename, transparent_color );
   float cx;                     // Holds Our X Character Coord
   float cy;                     // Holds Our Y Character Coord
   m_base = ::glGenLists( 256 ); // Creating 256 Display Lists
   float char_width  = 1.0f/16.0f*scale_x;
   float char_height = 1.0f/16.0f*scale_y;
   m_char_width  = char_width;
   m_char_height = char_height;
   ::glLoadIdentity();
   ::glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
   ::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
   ::glShadeModel( GL_SMOOTH );                    // Enables Smooth Color Shading
   ::glBindTexture( GL_TEXTURE_2D, m_texture_id ); // Select Our Font Texture
   for( int loop=0; loop<256 ; loop++ ) // Loop Through All 256 Lists
   {
      cx = float(loop%16)/16.0f; // X Position Of Current Character
      cy = float(loop/16)/16.0f; // Y Position Of Current Character

      ::glNewList( m_base+loop, GL_COMPILE );          // Start Building A List
      ::glBegin( GL_QUADS );                           // Use A Quad For Each Character
         ::glTexCoord2f( cx, 1-cy-0.0625f );           // Texture Coord (Bottom Left)
         ::glVertex3f( 0, 0, 0 ) ;                     // Vertex Coord (Bottom Left)
         ::glTexCoord2f( cx+0.0625f, 1-cy-0.0625f );   // Texture Coord (Bottom Right)
         ::glVertex3f( char_width, 0, 0 );             // Vertex Coord (Bottom Right)
         ::glTexCoord2f( cx+0.0625f, 1-cy );           // Texture Coord (Top Right)
         ::glVertex3f( char_width, char_height, 0 );   // Vertex Coord (Top Right)
         ::glTexCoord2f( cx, 1-cy );                   // Texture Coord (Top Left)
         ::glVertex3f( 0, char_height, 0 );            // Vertex Coord (Top Left)
      ::glEnd();                                       // Done Building Our Quad (Character)
      ::glTranslatef( char_width, 0, 0 );          // Move To The Right Of The Character
      ::glEndList();                                   // Done Building The Display List
   } // Loop Until All 256 Are Built
   ::glLoadIdentity();
   m_bFontBuilt = true;
   return true;
}


The difference is I use GL_BGR and nehe uses GL_RGB. I have no Idea why mine is messed up and nehe's isnt. Any Idea why this happens? [Edited by - try_catch_this on June 9, 2005 11:03:40 PM]

Share this post


Link to post
Share on other sites
Advertisement
This is what happens when I use my texture load function
Image Hosted by ImageShack.us

This is what happens when I use nehe texture load function
Image Hosted by ImageShack.us

As you can see after loading the texture with NeHe's glaux load function everything displays correctly.

// My load
::glPixelStorei( GL_UNPACK_ALIGNMENT, 3 );
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // Linear Filtering
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // Linear Filtering
::glTexImage2D( GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, (unsigned char*)BMP.bmBits );

// NeHe Load
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
::glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data );

[Edited by - try_catch_this on June 9, 2005 11:48:05 PM]

Share this post


Link to post
Share on other sites
Problem solved.

It seems they are slight differences between the bitmap format that nehe shipped and the one that ms paint does.

I made a fresh 256*256 image in paint and it worked. 0_o

Share this post


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

  • Advertisement