bool ObjModel::loadMTLFile(const char* fileName) { cout << "Loading material file: " << fileName << endl; FILE* fp; string* modelPath = getCvarAddress_S("r_modelPath"); string canonicalPath = *modelPath + "obj/" + fileName; cout << "From file path: " << canonicalPath << endl; if( !(fp=fopen(canonicalPath.c_str(), "r")) ) { error = "OBJ Materials file not found"; Con_print("Obj: File not found - %s", canonicalPath.c_str()); return false; } MaterialManager* tm = getMaterialManager(); material_t* mat; while( !feof(fp) ) { char in[MAX_OBJ_LINE_LEN]; fgets(in, MAX_OBJ_LINE_LEN, fp); char incpy[MAX_OBJ_LINE_LEN]; #ifdef OBJMATDEBUG cout << "MAT Line: " << in << endl; #endif strcpy(incpy, in); // if its a comment or whitespace skip it if( in[0] == '#' || in[0] == ' ' || in[0] == '\n' || in[0] == '\r' || in[0] == '\t') continue; else { // otherwise we need to process some data char* token = strtok(in, WHITESPACE); if( token == NULL ) break; if( !strcmp(token, "newmtl") ) { material_t* newmat = new material_t; initializeMaterial(newmat); token = strtok(NULL, WHITESPACE); if( !tm->hasMaterial(token) ) { tm->addMaterial(token, newmat); mat = newmat; #ifdef OBJMATDEBUG cout << "New material created: " << token << endl; #endif } else { #ifdef OBJMATDEBUG cout << "MTL Error: Material redefinition: " << token << endl; #endif } } else if( !strcmp(token, "Ns") ) { sscanf(incpy, "Ns %f", &mat->Ns); } else if( !strcmp(token, "Ka") ) { sscanf(incpy, "Ka %f %f %f", &mat->Ka[0], &mat->Ka[1], &mat->Ka[2]); } else if( !strcmp(token, "Kd") ) { sscanf(incpy, "Kd %f %f %f", &mat->Kd[0], &mat->Kd[1], &mat->Kd[2]); } else if( !strcmp(token, "Ks") ) { sscanf(incpy, "Ks %f %f %f", &mat->Ks[0], &mat->Ks[1],& mat->Ks[2]); } else if( !strcmp(token, "Ni") ) { sscanf(incpy, "Ni %f", &mat->Ni); } else if( !strcmp(token, "d") ) { sscanf(incpy, "d %f", &mat->d); } else if( !strcmp(token, "illum") ) { sscanf(incpy, "illum %d", &mat->illum); } else if( !strcmp(token, "map_Kd") ) { token = strtok(NULL, WHITESPACE); strcpy(mat->map_Kd, token); #ifdef OBJMATDEBUG cout << "Loading texture: " << mat->map_Kd << endl; #endif getMaterialManager()->loadBitmap(mat->map_Kd); } } } fclose(fp); return true; }
void WFLoader::parseMaterialFile( const char *line ) { /* Variables */ char tempStr[20] = "\0"; MaterialManager *matMngrPtr = NULL; MaterialData matData; float r, g, b; /* Code */ matMngrPtr = MaterialManager::getInstance(); if( strlen( line ) > 0 ) { if( line[ 0 ] == 'n' ) { sscanf( line, "%s", tempStr ); /* Material name */ if( strcmp( tempStr, "newmtl" ) == 0 ) { sscanf( line, "%*s %s", m_CurrentMatName ); matMngrPtr->addMaterial( m_CurrentMatName, matData ); } } else if( line[ 0 ] == 'N' ) { switch( line[ 1 ] ) { case 's': /* Specular exponent ( shininess ) */ sscanf( line, "%*s %f", &r ); /* Even though Color4f is used, only the value of r gets saved into the Material values */ matMngrPtr->setValue( m_CurrentMatName, MaterialManager::MAT_SHININESS, Color4f( r, 0.0, 0.0, 1.0 ) ); break; } } /* Reflectivity information */ else if( line[ 0 ] == 'K' ) { switch( line[ 1 ] ) //if( line[ 1 ] == 'a' ) { case 'a': /* Ambient */ sscanf( line, "%*s %f %f %f", &r, &g, &b ); matMngrPtr->setValue( m_CurrentMatName, MaterialManager::MAT_AMBIENT, Color4f( r, g, b, 1.0 ) ); break; case 'd': /* Diffuse */ sscanf( line, "%*s %f %f %f", &r, &g, &b ); matMngrPtr->setValue( m_CurrentMatName, MaterialManager::MAT_DIFFUSE, Color4f( r, g, b, 1.0 ) ); break; case 's': /* Specular */ sscanf( line, "%*s %f %f %f", &r, &g, &b ); matMngrPtr->setValue( m_CurrentMatName, MaterialManager::MAT_SPECULAR, Color4f( r, g, b, 1.0 ) ); break; } } } }