[1024] | 1 | #include <GeoMaterialLoader.h>
|
---|
| 2 |
|
---|
| 3 | using namespace std;
|
---|
| 4 |
|
---|
| 5 | //---------------------------------------------------------------------------
|
---|
| 6 | // Private:
|
---|
| 7 | //---------------------------------------------------------------------------
|
---|
| 8 |
|
---|
| 9 | //---------------------------------------------------------------------------
|
---|
| 10 | // Search material files.
|
---|
| 11 | //---------------------------------------------------------------------------
|
---|
| 12 | void GeoMaterialLoader::search(string path)
|
---|
| 13 | {
|
---|
| 14 | dirent **files;
|
---|
| 15 | int num_files;
|
---|
| 16 |
|
---|
| 17 | num_files = filename_list(path.c_str(), &files);
|
---|
| 18 |
|
---|
| 19 | // For each file.
|
---|
| 20 | for (int i = 0; i < num_files; i++)
|
---|
| 21 | {
|
---|
| 22 | // If is ".material" file.
|
---|
| 23 | if (filename_match(files[i]->d_name, "*.material"))
|
---|
| 24 | {
|
---|
| 25 | // Add filename to list.
|
---|
| 26 | mFiles.push_back(string(files[i]->d_name));
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | // Free filename.
|
---|
| 30 | free(files[i]);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | // Free memory of filenames.
|
---|
| 34 | free(files);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | //---------------------------------------------------------------------------
|
---|
| 38 | // Load materials.
|
---|
| 39 | //---------------------------------------------------------------------------
|
---|
| 40 | void GeoMaterialLoader::load()
|
---|
| 41 | {
|
---|
| 42 | ifstream *in_file;
|
---|
| 43 | string filename;
|
---|
| 44 | char line[256];
|
---|
| 45 | char delim[4] = " \n\t";
|
---|
| 46 | char *elem;
|
---|
| 47 | string mat_name;
|
---|
| 48 |
|
---|
| 49 | // For each file in list.
|
---|
| 50 | for (int i = 0; i < mFiles.size(); i++)
|
---|
| 51 | {
|
---|
| 52 | // File name.
|
---|
| 53 | filename =mMatPath + mFiles[i];
|
---|
| 54 |
|
---|
| 55 | // Open file.
|
---|
| 56 | in_file = new ifstream(filename.c_str());
|
---|
| 57 |
|
---|
| 58 | // While no EOF reached.
|
---|
| 59 | while (in_file->good())
|
---|
| 60 | {
|
---|
| 61 | // Read line.
|
---|
| 62 | in_file->getline(line, 256);
|
---|
| 63 |
|
---|
| 64 | // Gets first token.
|
---|
| 65 | elem = strtok(line, delim);
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | if (elem != NULL)
|
---|
| 69 | {
|
---|
| 70 | if (strcmp(elem, "material") == 0)
|
---|
| 71 | {
|
---|
| 72 | // Gets material name.
|
---|
| 73 | elem = strtok(NULL, delim);
|
---|
| 74 |
|
---|
| 75 | // Save material name.
|
---|
| 76 | mat_name = string(elem);
|
---|
| 77 |
|
---|
| 78 | mMatMap[mat_name] = string("");
|
---|
| 79 | }
|
---|
| 80 | else
|
---|
| 81 | {
|
---|
| 82 | if (strcmp(elem, "texture") == 0)
|
---|
| 83 | {
|
---|
| 84 | // Gets texture filename.
|
---|
| 85 | elem = strtok(NULL, delim);
|
---|
| 86 |
|
---|
| 87 | mMatMap[mat_name] = string(elem);
|
---|
| 88 |
|
---|
| 89 | // Debug.
|
---|
| 90 | cout << mat_name
|
---|
| 91 | << ": "
|
---|
| 92 | << string(elem)
|
---|
| 93 | << endl;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | // Close file.
|
---|
| 100 | in_file->close();
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | //---------------------------------------------------------------------------
|
---|
| 105 | // Public:
|
---|
| 106 | //---------------------------------------------------------------------------
|
---|
| 107 |
|
---|
| 108 | //---------------------------------------------------------------------------
|
---|
| 109 | // Constructor.
|
---|
| 110 | //---------------------------------------------------------------------------
|
---|
| 111 | GeoMaterialLoader::GeoMaterialLoader()
|
---|
| 112 | {
|
---|
| 113 | mMatPath = string("./media/materials/");
|
---|
| 114 | mTexPath = string("./media/textures/");
|
---|
| 115 |
|
---|
| 116 | search(mMatPath);
|
---|
| 117 |
|
---|
| 118 | load();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | //---------------------------------------------------------------------------
|
---|
| 122 | // Destroyer.
|
---|
| 123 | //---------------------------------------------------------------------------
|
---|
| 124 | GeoMaterialLoader::~GeoMaterialLoader()
|
---|
| 125 | {
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | //---------------------------------------------------------------------------
|
---|
| 129 | // Check for a material name.
|
---|
| 130 | //---------------------------------------------------------------------------
|
---|
| 131 | bool GeoMaterialLoader::existsMaterial(string matName)
|
---|
| 132 | {
|
---|
| 133 | // If material not found.
|
---|
| 134 | if (mMatMap.find(matName) == mMatMap.end())
|
---|
| 135 | {
|
---|
| 136 | return false;
|
---|
| 137 | }
|
---|
| 138 | else
|
---|
| 139 | {
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | //---------------------------------------------------------------------------
|
---|
| 145 | // Gets texture file name.
|
---|
| 146 | //---------------------------------------------------------------------------
|
---|
| 147 | string GeoMaterialLoader::getTextureFileName(string matName)
|
---|
| 148 | {
|
---|
| 149 | return mTexPath + mMatMap[matName];
|
---|
| 150 | }
|
---|