#include using namespace std; //--------------------------------------------------------------------------- // Private: //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Search material files. //--------------------------------------------------------------------------- void GeoMaterialLoader::search(string path) { dirent **files; int num_files; num_files = filename_list(path.c_str(), &files); // For each file. for (int i = 0; i < num_files; i++) { // If is ".material" file. if (filename_match(files[i]->d_name, "*.material")) { // Add filename to list. mFiles.push_back(string(files[i]->d_name)); } // Free filename. free(files[i]); } // Free memory of filenames. free(files); } //--------------------------------------------------------------------------- // Load materials. //--------------------------------------------------------------------------- void GeoMaterialLoader::load() { ifstream *in_file; string filename; char line[256]; char delim[4] = " \n\t"; char *elem; string mat_name; // For each file in list. for (int i = 0; i < mFiles.size(); i++) { // File name. filename =mMatPath + mFiles[i]; // Open file. in_file = new ifstream(filename.c_str()); // While no EOF reached. while (in_file->good()) { // Read line. in_file->getline(line, 256); // Gets first token. elem = strtok(line, delim); if (elem != NULL) { if (strcmp(elem, "material") == 0) { // Gets material name. elem = strtok(NULL, delim); // Save material name. mat_name = string(elem); mMatMap[mat_name] = string(""); } else { if (strcmp(elem, "texture") == 0) { // Gets texture filename. elem = strtok(NULL, delim); mMatMap[mat_name] = string(elem); // Debug. cout << mat_name << ": " << string(elem) << endl; } } } } // Close file. in_file->close(); } } //--------------------------------------------------------------------------- // Public: //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Constructor. //--------------------------------------------------------------------------- GeoMaterialLoader::GeoMaterialLoader() { mMatPath = string("./media/materials/"); mTexPath = string("./media/textures/"); search(mMatPath); load(); } //--------------------------------------------------------------------------- // Destroyer. //--------------------------------------------------------------------------- GeoMaterialLoader::~GeoMaterialLoader() { } //--------------------------------------------------------------------------- // Check for a material name. //--------------------------------------------------------------------------- bool GeoMaterialLoader::existsMaterial(string matName) { // If material not found. if (mMatMap.find(matName) == mMatMap.end()) { return false; } else { return true; } } //--------------------------------------------------------------------------- // Gets texture file name. //--------------------------------------------------------------------------- string GeoMaterialLoader::getTextureFileName(string matName) { return mTexPath + mMatMap[matName]; }