Ejemplo n.º 1
0
int DeRotatorGraphics::load_texture(const char* fname, GLuint& tex_id)
{
  using namespace logging::trivial;
  src::severity_logger< severity_level > lg;
  
 // initialize the texture map
  glGenTextures(1, &tex_id);
  glBindTexture(GL_TEXTURE_2D, tex_id);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  Fl_Shared_Image *image;  
  // check that the fname exists
  using namespace boost::filesystem;
  path p(fname);
  if(exists(p)){
    image = Fl_Shared_Image::get(fname);
  }
  else {
    // fname does not exist in the current directory.
    // Check whether this file exists in the Resources directory of
    // a Mac bundle
    
    #ifdef __APPLE__
      unsigned short ufname[64];
      for(int i=0; i<strlen(fname); i++){
	ufname[i] = fname[i];
      }
      CFStringRef s = CFStringCreateWithCharacters(NULL, ufname, strlen(fname));
      CFURLRef appUrlRef;
      appUrlRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), s, NULL, NULL);

      CFStringRef filePathRef = CFURLCopyPath(appUrlRef);
      const char* filePath = CFStringGetCStringPtr(filePathRef, kCFStringEncodingUTF8);

      path p1(filePath);
      if(exists(p1)){
	image = Fl_Shared_Image::get(filePath);
	CFRelease(filePathRef);
	CFRelease(appUrlRef);
      }
      else {
	LOG_ERROR << "DeRotator::load_texture(): " << filePath
		  << " does not exist\n";
	CFRelease(filePathRef);
	CFRelease(appUrlRef);	
	return -1;	
      }
      
    #endif
  }

  if(image == NULL){
    LOG_ERROR << "DeRotator::load_texture(): " << fname
	      << " has an unknown format\n";
    return -1;
  }

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image->w(), image->h(), 0,
	       GL_RGBA, GL_UNSIGNED_BYTE, image->data()[0]);
  image->release();

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glEnable(GL_TEXTURE_2D);

  return 0;
}