//------------------------------------------------------------------;
//
//------------------------------------------------------------------;

c_img::c_img ( void ) {

   return;
}

//------------------------------------------------------------------;
// TODO : dealloc for members globally - ?
//------------------------------------------------------------------;

c_img::~c_img ( void ) {

   delete [] e_img;

   return;
}

//------------------------------------------------------------------;
// looks for the materials that have a bump map. converts the bump map
// into normal map and upload the normal map to gl, while updating the material
// reference
//------------------------------------------------------------------;

void c_img::m_load_bump_tex ( ase::model * model ) {

   ase::mat_it it = model->mat.begin();

   while(it != model->mat.end()) {

      ase::matobj * matobj = (*it);
      ase::map_it map_it   = matobj->map_bump.begin();

      while (map_it != matobj->map_bump.end()) {

         ase::tex_it tex_it = model->tex.find((*map_it)->bitmap);

         if (tex_it != model->tex.end()) {

            ase::texdata * txdata = (*tex_it).second;
            glBindTexture   (GL_TEXTURE_2D, txdata->textid);
            e_nm->m_make_normal_map (txdata, 10.0);
            glTexImage2D    (GL_TEXTURE_2D, 0, txdata->pixelfmt, txdata->width, txdata->height, 0, txdata->pixelfmt, GL_UNSIGNED_BYTE, txdata->pixels);
            glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
         }
         ++map_it;
      }
      ++it;
   }
}

//------------------------------------------------------------------;
//
//------------------------------------------------------------------;

void c_img::m_load_tex( const ase::model * model ) {

   ase::tex_cit it = model->tex.begin();

   while (it != model->tex.end()) {

      ase::texdata * txdata = (*it).second;
      glGenTextures (1,&txdata->textid);
      glBindTexture (GL_TEXTURE_2D, txdata->textid);

      switch (txdata->components) {

         case 1 :
            txdata->pixelfmt = GL_INTENSITY;
            break;
         case 3 :
            txdata->pixelfmt = GL_RGB;
            break;
         case 4 :
            txdata->pixelfmt = GL_RGBA;
            break;
         default :
            assert(false);
            break;
      }
      gluBuild2DMipmaps ( GL_TEXTURE_2D, 
                          txdata->pixelfmt, 
                          txdata->width, txdata->height,
                          txdata->pixelfmt, 
                          GL_UNSIGNED_BYTE, txdata->pixels );

      glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      ++it;
   }
}
