00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "celestia.h"
00011 #include <celutil/debug.h>
00012 #include <iostream>
00013 #include <fstream>
00014 #include "multitexture.h"
00015 #include "texmanager.h"
00016
00017 using namespace std;
00018
00019
00020 static TextureManager* textureManager = NULL;
00021
00022 static const char *directories[]=
00023 {
00024 "/lores/",
00025 "/medres/",
00026 "/hires/"
00027 };
00028
00029
00030 TextureManager* GetTextureManager()
00031 {
00032 if (textureManager == NULL)
00033 textureManager = new TextureManager("textures");
00034 return textureManager;
00035 }
00036
00037
00038 static string resolveWildcard(const string& filename)
00039 {
00040 string base(filename, 0, filename.length() - 1);
00041
00042 string pngfile = base + "png";
00043 {
00044 ifstream in(pngfile.c_str());
00045 if (in.good())
00046 return pngfile;
00047 }
00048 string jpgfile = base + "jpg";
00049 {
00050 ifstream in(jpgfile.c_str());
00051 if (in.good())
00052 return jpgfile;
00053 }
00054 string ddsfile = base + "dds";
00055 {
00056 ifstream in(ddsfile.c_str());
00057 if (in.good())
00058 return ddsfile;
00059 }
00060 string ctxfile = base + "ctx";
00061 {
00062 ifstream in(ctxfile.c_str());
00063 if (in.good())
00064 return ctxfile;
00065 }
00066
00067 return "";
00068 }
00069
00070
00071 string TextureInfo::resolve(const string& baseDir)
00072 {
00073 bool wildcard = false;
00074 if (!source.empty() && source.at(source.length() - 1) == '*')
00075 wildcard = true;
00076
00077 if (!path.empty())
00078 {
00079 string filename = path + "/textures" + directories[resolution] + source;
00080
00081 if (wildcard)
00082 {
00083 filename = resolveWildcard(filename);
00084 if (!filename.empty())
00085 return filename;
00086 }
00087 else
00088 {
00089 ifstream in(filename.c_str());
00090 if (in.good())
00091 return filename;
00092 }
00093 }
00094
00095 string filename = baseDir + directories[resolution] + source;
00096 if (wildcard)
00097 {
00098 string matched = resolveWildcard(filename);
00099 if (matched.empty())
00100 return filename;
00101 else
00102 return matched;
00103 }
00104 else
00105 {
00106 return filename;
00107 }
00108 }
00109
00110
00111 Texture* TextureInfo::load(const string& name)
00112 {
00113 Texture::AddressMode addressMode = Texture::EdgeClamp;
00114 Texture::MipMapMode mipMode = Texture::DefaultMipMaps;
00115
00116 if (flags & WrapTexture)
00117 addressMode = Texture::Wrap;
00118 else if (flags & BorderClamp)
00119 addressMode = Texture::BorderClamp;
00120
00121 if (flags & NoMipMaps)
00122 mipMode = Texture::NoMipMaps;
00123 else if (flags & AutoMipMaps)
00124 mipMode = Texture::AutoMipMaps;
00125
00126 if (bumpHeight == 0.0f)
00127 {
00128 DPRINTF(0, "Loading texture: %s\n", name.c_str());
00129
00130
00131 return LoadTextureFromFile(name, addressMode, mipMode);
00132 }
00133 else
00134 {
00135 DPRINTF(0, "Loading bump map: %s\n", name.c_str());
00136
00137
00138 return LoadHeightMapFromFile(name, bumpHeight, addressMode);
00139 }
00140
00141 return NULL;
00142 }
00143