Problems with textures

Hello everyone !

I’m writing a MMORPG game, incluyding my own engine, subsystems, everything, just using raw OpenGL. For a like a month, when I made some simply texture manager, there are some problems with texture managment. Heres my texture manager:




#ifndef TEXTURE_MANAGER_H
#define TEXTURE_MANAGER_H

class TexManager{ // some manager class

public:
  GLuint text; // handle to texture
  string hash; // filename od file
  int count; // how many times that texture was used ( amount of models etc )

  TexManager & operator=( const TexManager & p ){ 
  text = p.text;
  hash = p.hash;
  count = p.count;
  return * this;
  }

  TexManager() : text( 0 ), hash( "" ), count( 1 )
  { }

  TexManager( GLuint t , const string & p ) : text( t ), hash( p ), count( 1 )
  { }

};

pojemniczek< TexManager > BazaTekstur; // some vector of textures

//----------------------------------------------------------------------------//

GLuint PobierzTesture( string nazwa , bool mipmapping = true , bool flip = false ){ // loading a new texture
if( nazwa.empty() ) return 0; // if theres no filename, no texture

char buf[ 1024 ];
nazwa = canonicalize_filename( buf , (const char*)nazwa.c_str() , 1024 ); // make canonicalize filename

string hash = upnij( nazwa ); // make proper hash

for( int i = 0; i < BazaTekstur.ilosc; i++) // through alltextures in data base
 if( BazaTekstur[i].hash == hash ){ // we are looking for the same texture
  BazaTekstur[i].count++; // another one obiect is using these one
  if( BazaTekstur[i].text ){ // if theres texture linked with that filename
   LOG( "Pobranie istniej&#261;cej tekstury: " << nazwa << " ID=" << BazaTekstur[i].text << " [" << BazaTekstur.ilosc << "]" ); // some log, NVM its polish ;)
   return BazaTekstur[i].text; // and we are giving back proper texture handle
   }
  return 0;  // 0 if not
  }

Bitmapa * text = load_texture( nazwa.c_str() , mipmapping , flip ); // now we are loading some texture ( tga, bmp, jpg, gif, png etc etc ...

if( !text ){ // if something went wrong -> file errors
 LOG( "Niepowiodlo si&#281; &#322;adowanie tekstury " << nazwa ); // some log
 return 0; // sorry, no texture :(
 }
 
bool add = true; // some teporary variable

TexManager p( text->text , hash ); // we are creating a texture obiect | text->text is a handle to texture GLuint
delete text; // deleting bitmap obiect ( only informations about texture, it not deleting texture in memory )

for( int i = 0; i < BazaTekstur.ilosc; i++) // again throught all textures
 if( !BazaTekstur[i].text ){ // if theres free slot
  BazaTekstur[i] = p; // we putting here our texture
  add = false; // we dont need to extend database
  break; // ok
  }

if( add ) // if we need another one slot in memory
 BazaTekstur.push( p ); // putting on the stack
 
glBindTexture( GL_TEXTURE_2D , p.text ); // setting up default settings

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glBindTexture( GL_TEXTURE_2D , 0 );

LOG( "Dodanie nowej tekstury do bazy: " << nazwa << " ID=" << p.text << " [" << BazaTekstur.ilosc << "]" ); // some log with name of texture and size of database, thats NVM

return p.text; // we are giving back handle to texture ( GLuint )
}

//----------------------------------------------------------------------------//

void UsunTexture( GLuint c ){ // removing texture from the database
if( !c ) return; // if theres no texture ... :(
LOG( "Usuwanie tekstury z bazy ID=" << c ); // some log

for( int i = 0; i < BazaTekstur.ilosc; i++) // through all database
 if( BazaTekstur[i].text == c ){ // we are looking for proper texture
  BazaTekstur[i].count--; // declarae 'used_count'
  if( BazaTekstur[i].count == 0 ){ // if it was last obiect that use texture...
   glDeleteTextures( 1 , &BazaTekstur[i].text ); // we are deleting texture from the memory
   BazaTekstur[i].text = 0; // erase handle
   BazaTekstur[i].hash.erase();  // erase filename
   LOG( "Usuni&#281;to pomy&#347;lnie [" << BazaTekstur.ilosc << "]" ); // loggy
   }
  break; // all rigth...
  }
  
}

//----------------------------------------------------------------------------//


#endif

And what happens:

  • If I’m loading a map several times ( run the game -> loading a logon screen with some obiects -> login onto the system -> remowing logon, loading some surrounding map -> logout -> remowe maps, load logon ). So after that, I’m loosing textures, as in a screenshot:
  1. Normal functionary:
  2. And after that:
    here you can see some strange ‘splash
    stains’ on the tree.

And also: obiect are compiled onto glList, so maybe something goes wrong with glBindTexture here. But logs seems to looks normal :stuck_out_tongue:

I have absouletly no idea, thats going wrong. If you have any sugestions, please write here.