- fg_gzifstream in( mpath );
- if ( ! in.is_open() ) {
- FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << mpath );
- exit(-1);
- }
-
-#ifndef __MWERKS__
- while ( ! in.eof() ) {
-#else
- char c = '\0';
- while ( in.get(c) && c != '\0' ) {
- in.putback(c);
-#endif
- // printf("%s", line);
-
- // strip leading white space and comments
- in >> skipcomment;
-
- // set to zero to prevent its value accidently being '{'
- // after a failed >> operation.
- char token = 0;
-
- in >> material_name >> token;
-
- if ( token == '{' ) {
- FGNewMat m;
- in >> m;
-
- // build the ssgSimpleState
- FGPath tex_path( current_options.get_fg_root() );
- tex_path.append( "Textures" );
-
- FG_LOG( FG_TERRAIN, FG_INFO, " Loading material "
- << material_name << " (" << tex_path.c_str() << ")");
-
- GLenum shade_model = GL_SMOOTH;
- if ( current_options.get_shading() == 1 ) {
- shade_model = GL_SMOOTH;
- } else {
- shade_model = GL_FLAT;
- }
-
- m.build_ssg_state( tex_path.str(), shade_model,
- current_options.get_textures() );
-
-#if EXTRA_DEBUG
- m.dump_info();
-#endif
-
- matlib[material_name] = m;
- }
+ SGPropertyNode materials;
+
+ cout << "Reading materials from " << mpath << endl;
+ try {
+ readProperties(mpath, &materials);
+ } catch (const sg_exception &ex) {
+ SG_LOG(SG_INPUT, SG_ALERT, "Error reading materials: " << ex.getMessage());
+ throw ex;
+ }
+
+ int nMaterials = materials.nChildren();
+ for (int i = 0; i < nMaterials; i++) {
+ const SGPropertyNode * node = materials.getChild(i);
+ if (node->getName() == "material") {
+ FGNewMat * m = new FGNewMat(node);
+
+ vector<const SGPropertyNode *>names = node->getChildren("name");
+ for (unsigned int j = 0; j < names.size(); j++) {
+ m->ref();
+ matlib[names[j]->getStringValue()] = m;
+ SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
+ << names[j]->getStringValue());
+ }
+ } else {
+ SG_LOG(SG_INPUT, SG_ALERT,
+ "Skipping bad material entry " << node->getName());