Example #1
0
void WadImageCache::initialize_cache()
{
	FileSpecifier info;
	info.SetToImageCacheDir();
	info.AddPart("Cache.ini");
	if (!info.Exists())
		return;
	
	InfoTree pt;
	try {
		pt = InfoTree::load_ini(info);
	} catch (InfoTree::ini_error e) {
		logError("Could not read image cache from %s (%s)", info.GetPath(), e.what());
	}
	
	for (InfoTree::iterator it = pt.begin(); it != pt.end(); ++it)
	{
		std::string name = it->first;
		InfoTree ptc = it->second;
		
		WadImageDescriptor desc;
		
		std::string path;
		ptc.read("path", path);
		desc.file = FileSpecifier(path);
		
		ptc.read("checksum", desc.checksum);
		ptc.read("index", desc.index);
		ptc.read("tag", desc.tag);
		
		int width = 0;
		ptc.read("width", width);
		int height = 0;
		ptc.read("height", height);
		size_t filesize = 0;
		ptc.read("filesize", filesize);
		
		cache_key_t key = cache_key_t(desc, width, height);
		cache_value_t val = cache_value_t(name, filesize);
		m_used.push_front(cache_pair_t(key, val));
		m_cacheinfo[key] = m_used.begin();
		m_cachesize += filesize;
	}
}
Example #2
0
bool PluginLoader::ParseDirectory(FileSpecifier& dir)
{
  std::vector<dir_entry> de;
  if (!dir.ReadDirectory(de)) {
    return false;
  }

  for (std::vector<dir_entry>::const_iterator it = de.begin(); it != de.end();
       ++it) {
    FileSpecifier file = dir + it->name;
    if (it->name == "Plugin.xml") {
      ParsePlugin(file);
    }
    else if (it->is_directory && it->name[0] != '.') {
      ParseDirectory(file);
    }
#ifdef HAVE_ZZIP
    else if (algo::ends_with(it->name,
                             ".zip") || algo::ends_with(it->name, ".ZIP")) {
      // search it for a Plugin.xml file
      ZZIP_DIR* zzipdir = zzip_dir_open(file.GetPath(), 0);
      if (zzipdir) {
        ZZIP_DIRENT dirent;
        while (zzip_dir_read(zzipdir, &dirent))
        {
          if (algo::ends_with(dirent.d_name, "Plugin.xml")) {
            std::string archive = file.GetPath();
            FileSpecifier file_name =
              FileSpecifier(archive.substr(0,
                                           archive.find_last_of('.'))) +
              dirent.d_name;
            ParsePlugin(file_name);
          }
        }
        zzip_dir_close(zzipdir);
      }
    }
#endif
  }

  return true;
}
Example #3
0
// DJB OpenGL
char* parseFile(FileSpecifier& fileSpec) {
  if (fileSpec == FileSpecifier() || !fileSpec.Exists()) {
    return NULL;
  }

  OpenedFile file;
  if (!fileSpec.Open(file)) {
    fprintf(stderr, "%s not found\n", fileSpec.GetPath());
    return NULL;
  }

  int32 length;
  file.GetLength(length);

  char* str = new char[length + 1];
  file.Read(length, str);
  str[length] = 0;

  return str;
}
Example #4
0
void OGL_ModelData::Load()
{
  // Already loaded?
  if (ModelPresent()) {
    return;
  }

  // Load the model
  Model.Clear();

  if (ModelFile == FileSpecifier()) {
    return;
  }
  if (!ModelFile.Exists()) {
    return;
  }

  bool Success = false;

  char *Type = &ModelType[0];
  if (StringsEqual(Type,"wave",4)) {
    // Alias|Wavefront
    Success = LoadModel_Wavefront(ModelFile, Model);
  }
  else if (StringsEqual(Type,"3ds",3)) {
    // 3D Studio Max
    Success = LoadModel_Studio(ModelFile, Model);
  }
  else if (StringsEqual(Type,"dim3",4)) {
    // Brian Barnes's "Dim3" model format (first pass: model geometry)
    Success = LoadModel_Dim3(ModelFile, Model, LoadModelDim3_First);

    // Second and third passes: frames and sequences
    try
    {
      if (ModelFile1 == FileSpecifier()) {
        throw 0;
      }
      if (!ModelFile1.Exists()) {
        throw 0;
      }
      if (!LoadModel_Dim3(ModelFile1, Model, LoadModelDim3_Rest)) {
        throw 0;
      }
    }
    catch(...)
    {}
    //
    try
    {
      if (ModelFile2 == FileSpecifier()) {
        throw 0;
      }
      if (!ModelFile2.Exists()) {
        throw 0;
      }
      if (!LoadModel_Dim3(ModelFile2, Model, LoadModelDim3_Rest)) {
        throw 0;
      }
    }
    catch(...)
    {}
  }
#if HAVE_QUESA
  else if (StringsEqual(Type,
                        "qd3d") ||
           StringsEqual(Type,"3dmf") || StringsEqual(Type,"quesa")) {
    // QuickDraw 3D / Quesa
    Success = LoadModel_QD3D(ModelFile, Model);
  }
#endif

  if (!Success) {
    Model.Clear();
    return;
  }

  // Calculate transformation matrix
  GLfloat Angle, Cosine, Sine;
  GLfloat RotMatrix[3][3], NewRotMatrix[3][3], IndivRotMatrix[3][3];
  MatIdentity(RotMatrix);

  MatIdentity(IndivRotMatrix);
  Angle = (float)Degree2Radian*XRot;
  Cosine = (float)cos(Angle);
  Sine = (float)sin(Angle);
  IndivRotMatrix[1][1] = Cosine;
  IndivRotMatrix[1][2] = -Sine;
  IndivRotMatrix[2][1] = Sine;
  IndivRotMatrix[2][2] = Cosine;
  MatMult(IndivRotMatrix,RotMatrix,NewRotMatrix);
  MatCopy(NewRotMatrix,RotMatrix);

  MatIdentity(IndivRotMatrix);
  Angle = (float)Degree2Radian*YRot;
  Cosine = (float)cos(Angle);
  Sine = (float)sin(Angle);
  IndivRotMatrix[2][2] = Cosine;
  IndivRotMatrix[2][0] = -Sine;
  IndivRotMatrix[0][2] = Sine;
  IndivRotMatrix[0][0] = Cosine;
  MatMult(IndivRotMatrix,RotMatrix,NewRotMatrix);
  MatCopy(NewRotMatrix,RotMatrix);

  MatIdentity(IndivRotMatrix);
  Angle = (float)Degree2Radian*ZRot;
  Cosine = (float)cos(Angle);
  Sine = (float)sin(Angle);
  IndivRotMatrix[0][0] = Cosine;
  IndivRotMatrix[0][1] = -Sine;
  IndivRotMatrix[1][0] = Sine;
  IndivRotMatrix[1][1] = Cosine;
  MatMult(IndivRotMatrix,RotMatrix,NewRotMatrix);
  MatCopy(NewRotMatrix,RotMatrix);

  MatScalMult(NewRotMatrix,Scale);                              // For the position vertices
  if (Scale < 0) {
    MatScalMult(RotMatrix,-1);                          // For the normals
  }
  // Is model animated or static?
  // Test by trying to find neutral positions (useful for working with the normals later on)
  if (Model.FindPositions_Neutral(false)) {
    // Copy over the vector and normal transformation matrices:
    for (int k=0; k<3; k++)
      for (int l=0; l<3; l++)
      {
        Model.TransformPos.M[k][l] = NewRotMatrix[k][l];
        Model.TransformNorm.M[k][l] = RotMatrix[k][l];
      }

    Model.TransformPos.M[0][3] = XShift;
    Model.TransformPos.M[1][3] = YShift;
    Model.TransformPos.M[2][3] = ZShift;

    // Find the transformed bounding box:
    bool RestOfCorners = false;
    GLfloat NewBoundingBox[2][3];
    // The indices i1, i2, and i3 are for selecting which of the box's two principal corners
    // to get coordinates from
    for (int i1=0; i1<2; i1++)
    {
      GLfloat X = Model.BoundingBox[i1][0];
      for (int i2=0; i2<2; i2++)
      {
        GLfloat Y = Model.BoundingBox[i2][0];
        for (int i3=0; i3<2; i3++)
        {
          GLfloat Z = Model.BoundingBox[i3][0];

          GLfloat Corner[3];
          for (int ic=0; ic<3; ic++)
          {
            GLfloat *Row = Model.TransformPos.M[ic];
            Corner[ic] = Row[0]*X + Row[1]*Y + Row[2]*Z + Row[3];
          }

          if (RestOfCorners) {
            // Find minimum and maximum for each coordinate
            for (int ic=0; ic<3; ic++)
            {
              NewBoundingBox[0][ic] = min(NewBoundingBox[0][ic],Corner[ic]);
              NewBoundingBox[1][ic] = max(NewBoundingBox[1][ic],Corner[ic]);
            }
          }
          else
          {
            // Simply copy it in:
            for (int ic=0; ic<3; ic++)
              NewBoundingBox[0][ic] = NewBoundingBox[1][ic] = Corner[ic];
            RestOfCorners = true;
          }
        }
      }
    }

    for (int ic=0; ic<2; ic++)
      objlist_copy(Model.BoundingBox[ic],NewBoundingBox[ic],3);
  }
  else
  {
    // Static model
    size_t NumVerts = Model.Positions.size()/3;

    for (size_t k=0; k<NumVerts; k++)
    {
      GLfloat *Pos = Model.PosBase() + 3*k;
      GLfloat NewPos[3];
      MatVecMult(NewRotMatrix,Pos,NewPos);                      // Has the scaling
      Pos[0] = NewPos[0] + XShift;
      Pos[1] = NewPos[1] + YShift;
      Pos[2] = NewPos[2] + ZShift;
    }

    size_t NumNorms = Model.Normals.size()/3;
    for (size_t k=0; k<NumNorms; k++)
    {
      GLfloat *Norms = Model.NormBase() + 3*k;
      GLfloat NewNorms[3];
      MatVecMult(RotMatrix,Norms,NewNorms);                     // Not scaled
      objlist_copy(Norms,NewNorms,3);
    }

    // So as to be consistent with the new points
    Model.FindBoundingBox();
  }

  Model.AdjustNormals(NormalType,NormalSplit);
  Model.CalculateTangents();

  // Don't forget the skins
  OGL_SkinManager::Load();
}
Example #5
0
void OGL_TextureOptionsBase::Load()
{
  FileSpecifier File;
  
  GLint maxTextureSize = 0;
  if (GetMaxSize()) {
    maxTextureSize = MIN(maxTextureSize, GetMaxSize());
  }

  int flags = ImageLoader_ResizeToPowersOfTwo;

  if (Type >= 0 && Type < OGL_NUMBER_OF_TEXTURE_TYPES &&
      Get_OGL_ConfigureData().TxtrConfigList[Type].FarFilter >
      1 /* GL_LINEAR */) {
    flags |= ImageLoader_LoadMipMaps;
  }

  if (hasS3TC) {
    flags |= ImageLoader_CanUseDXTC;
  }

  if (Get_OGL_ConfigureData().GeForceFix) {
    flags |= ImageLoader_LoadDXTC1AsDXTC3;
  }

  // Load the normal image with alpha channel

  // Check to see if loading needs to be done;
  // it does not need to be if an image is present.
  if (NormalImg.IsPresent()) {
    return;
  }

  NormalImg.Clear();

  // Load the normal image if it has a filename specified for it
  if (NormalColors != FileSpecifier() && NormalColors.Exists()) {
    if (!NormalImg.LoadFromFile(NormalColors,ImageLoader_Colors, flags |
                                (NormalIsPremultiplied ?
                                 ImageLoader_ImageIsAlreadyPremultiplied : 0),
                                actual_width, actual_height, maxTextureSize)) {
      // A texture must have a normal colored part
      return;
    }
  }
  else
  {
    return;
  }

  // load a heightmap
  if(OffsetMap != FileSpecifier() && OffsetMap.Exists()) {
    if(!OffsetImg.LoadFromFile(OffsetMap, ImageLoader_Colors, flags |
                               (NormalIsPremultiplied ?
                                ImageLoader_ImageIsAlreadyPremultiplied : 0),
                               actual_width, actual_height, maxTextureSize)) {
      return;
    }
  }

  // Load the normal mask if it has a filename specified for it
  if (NormalMask != FileSpecifier() && NormalMask.Exists()) {
    NormalImg.LoadFromFile(NormalMask,ImageLoader_Opacity, flags, actual_width,
                           actual_height,
                           maxTextureSize);
  }

  if (maxTextureSize) {
    while (NormalImg.GetWidth() > maxTextureSize || NormalImg.GetHeight() >
           maxTextureSize)
    {
      if (!NormalImg.Minify()) {
        break;
      }
    }

    if(OffsetImg.IsPresent()) {
      while (OffsetImg.GetWidth() > maxTextureSize || OffsetImg.GetHeight() >
             maxTextureSize) {
        if(!OffsetImg.Minify()) {
          break;
        }
      }
    }
  }

  // Load the glow image with alpha channel
  if (!GlowImg.IsPresent()) {
    GlowImg.Clear();

    // Load the glow image if it has a filename specified for it
    if (GlowColors != FileSpecifier() && GlowColors.Exists()) {
      if (GlowImg.LoadFromFile(GlowColors,ImageLoader_Colors, flags |
                               (GlowIsPremultiplied ?
                                ImageLoader_ImageIsAlreadyPremultiplied : 0),
                               actual_width, actual_height, maxTextureSize)) {
        // Load the glow mask if it has a
        // filename specified for it; only
        // loaded if an image has been loaded
        // for it
        if (GlowMask != FileSpecifier() && GlowMask.Exists()) {
          GlowImg.LoadFromFile(GlowMask,ImageLoader_Opacity, flags,
                               actual_width, actual_height,
                               maxTextureSize);
        }
      }
    }
  }

  if (GlowImg.IsPresent() && maxTextureSize) {
    while (GlowImg.GetWidth() > maxTextureSize || GlowImg.GetHeight() >
           maxTextureSize)
    {
      if (!GlowImg.Minify()) {
        break;
      }
    }
  }

  // The rest of the code is made simpler by these constraints:
  // that the glow texture only be present if the normal texture is also present,
  // and that the normal and glow textures have the same dimensions
  if (NormalImg.IsPresent()) {
    int W0 = NormalImg.GetWidth();
    int W1 = GlowImg.GetWidth();
    int H0 = NormalImg.GetHeight();
    int H1 = GlowImg.GetHeight();
    if ((W1 != W0) || (H1 != H0)) {
      GlowImg.Clear();
    }
  }
  else
  {
    GlowImg.Clear();
  }
}