void Mesh::render(const MaterialMap &mats) const { // index aktualni skupiny size_t index = 0; // pomocny seznam materialu pro predavani vertexgrupe pri renderu MaterialMap localMats; // iterace nad skupinami a jejich vykresleni for (VGList::const_iterator pos = vg.begin(); pos != vg.end(); pos++, index++) { // vyhodnoceni materialu // pokud index v danem seznamu existuje, zapise se MaterialMap::const_iterator matPos = mats.find(index); if (matPos != mats.end()) { localMats[0] = matPos->second; } else { localMats[0] = 0x0; } pos->render(localMats); } }
bool load_scene( Scene* scene, const char* filename ) { TiXmlDocument doc( filename ); const TiXmlElement* root = 0; const TiXmlElement* elem = 0; MaterialMap materials; MeshMap meshes; TriVertMap triverts; assert( scene ); // load the document if ( !doc.LoadFile() ) { std::cout << "ERROR, " << doc.ErrorRow() << ":" << doc.ErrorCol() << "; " << "parse error: " << doc.ErrorDesc() << "\n"; return false; } // check for root element root = doc.RootElement(); if ( !root ) { std::cout << "No root element.\n"; return false; } // reset the scene scene->reset(); try { // parse the camera elem = get_unique_child( root, true, STR_CAMERA ); parse_camera( elem, &scene->camera ); // parse background color parse_elem( root, true, STR_BACKGROUND, &scene->background_color ); // parse refractive index parse_elem( root, true, STR_REFRACT, &scene->refractive_index ); // parse ambient light parse_elem( root, false, STR_AMLIGHT, &scene->ambient_light ); // parse the lights elem = root->FirstChildElement( STR_PLIGHT ); while ( elem ) { PointLight pl; parse_point_light( elem, &pl ); scene->add_light( pl ); elem = elem->NextSiblingElement( STR_PLIGHT ); } // parse the materials elem = root->FirstChildElement( STR_MATERIAL ); while ( elem ) { Material* mat = new Material(); check_mem( mat ); scene->add_material( mat ); const char* name = parse_material( elem, mat ); assert( name ); // place each material in map by it's name, so we can associate geometries // with them when loading geometries // check for repeat name if ( !materials.insert( std::make_pair( name, mat ) ).second ) { print_error_header( elem ); std::cout << "Material '" << name << "' multiply defined.\n"; throw std::exception(); } elem = elem->NextSiblingElement( STR_MATERIAL ); } // parse the meshes elem = root->FirstChildElement( STR_MESH ); while ( elem ) { Mesh* mesh = new Mesh(); check_mem( mesh ); scene->add_mesh( mesh ); const char* name = parse_mesh( elem, mesh ); assert( name ); // place each mesh in map by it's name, so we can associate geometries // with them when loading geometries if ( !meshes.insert( std::make_pair( name, mesh ) ).second ) { print_error_header( elem ); std::cout << "Mesh '" << name << "' multiply defined.\n"; throw std::exception(); } elem = elem->NextSiblingElement( STR_MESH ); } // parse vertices (used by triangles) elem = root->FirstChildElement( STR_VERTEX ); while ( elem ) { Triangle::Vertex v; const char* name = parse_triangle_vertex( materials, elem, &v ); assert( name ); // place each vertex in map by it's name, so we can associate triangles // with them when loading geometries if ( !triverts.insert( std::make_pair( name, v ) ).second ) { print_error_header( elem ); std::cout << "Triangle vertex '" << name << "' multiply defined.\n"; throw std::exception(); } elem = elem->NextSiblingElement( STR_VERTEX ); } // parse the geometries // spheres elem = root->FirstChildElement( STR_SPHERE ); while ( elem ) { Sphere* geom = new Sphere(); check_mem( geom ); scene->add_geometry( geom ); parse_geom_sphere( materials, elem, geom ); elem = elem->NextSiblingElement( STR_SPHERE ); } // triangles elem = root->FirstChildElement( STR_TRIANGLE ); while ( elem ) { Triangle* geom = new Triangle(); check_mem( geom ); scene->add_geometry( geom ); parse_geom_triangle( materials, triverts, elem, geom ); elem = elem->NextSiblingElement( STR_TRIANGLE ); } // models elem = root->FirstChildElement( STR_MODEL ); while ( elem ) { Model* geom = new Model(); check_mem( geom ); scene->add_geometry( geom ); parse_geom_model( materials, meshes, elem, geom ); elem = elem->NextSiblingElement( STR_MODEL ); } // TODO add you own geometries here } catch ( std::bad_alloc const& ) { std::cout << "Out of memory error while loading scene\n."; scene->reset(); return false; } catch ( ... ) { scene->reset(); return false; } return true; }
//------------------------------------------------------------------------ Mtl::MaterialIterator::MaterialIterator( const MaterialMap& _materialMap ) : IteratorWrapper<MaterialMap, MaterialMap::const_iterator, MaterialPtr> ( _materialMap.begin(), _materialMap.end() ) { }