Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_SKINNING_IO_SKINNING_MATERIAL_XML_READ_H
00002 #define OPENTISSUE_KINEMATICS_SKINNING_IO_SKINNING_MATERIAL_XML_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <TinyXML/tinyxml.h>
00013
00014 #include <cassert>
00015 #include <string>
00016 #include <sstream>
00017 #include <iostream>
00018
00019 namespace OpenTissue
00020 {
00021 namespace skinning
00022 {
00023
00024 template<typename material_type>
00025 bool material_xml_read(std::string const & filename,material_type & mat)
00026 {
00027 #ifdef TIXML_USE_STL
00028 TiXmlDocument xml_document(filename);
00029 #else
00030 TiXmlDocument xml_document(filename.c_str());
00031 #endif
00032 if(!xml_document.LoadFile())
00033 {
00034 std::cerr << "file: " << filename << " not found" << std::endl;
00035 return false;
00036 }
00037 TiXmlHandle document_handle( &xml_document );
00038
00039 TiXmlElement * xml_material = document_handle.FirstChild( "material" ).Element();
00040 if(xml_material)
00041 {
00042 if(xml_material->Attribute("ambient"))
00043 {
00044 float r,g,b,a;
00045 std::istringstream str_stream(xml_material->Attribute("ambient"));
00046 str_stream >> r; if(r>1.0) r /= 255.0;
00047 str_stream >> g; if(g>1.0) g /= 255.0;
00048 str_stream >> b; if(b>1.0) b /= 255.0;
00049 str_stream >> a; if(a>1.0) a /= 255.0;
00050 mat.ambient(r,g,b,a);
00051 }
00052 if(xml_material->Attribute("diffuse"))
00053 {
00054 float r,g,b,a;
00055 std::istringstream str_stream(xml_material->Attribute("diffuse"));
00056 str_stream >> r; if(r>1.0) r /= 255.0;
00057 str_stream >> g; if(g>1.0) g /= 255.0;
00058 str_stream >> b; if(b>1.0) b /= 255.0;
00059 str_stream >> a; if(a>1.0) a /= 255.0;
00060 mat.diffuse(r,g,b,a);
00061 }
00062 if(xml_material->Attribute("specular"))
00063 {
00064 float r,g,b,a;
00065 std::istringstream str_stream(xml_material->Attribute("specular"));
00066 str_stream >> r; if(r>1.0) r /= 255.0;
00067 str_stream >> g; if(g>1.0) g /= 255.0;
00068 str_stream >> b; if(b>1.0) b /= 255.0;
00069 str_stream >> a; if(a>1.0) a /= 255.0;
00070 mat.specular(r,g,b,a);
00071 }
00072 if(xml_material->Attribute("shininess"))
00073 {
00074 float s;
00075 std::istringstream str_stream(xml_material->Attribute("shininess"));
00076 str_stream >> s;
00077 mat.shininess(s);
00078 }
00079 }
00080 xml_document.Clear();
00081 return true;
00082 };
00083
00084 }
00085 }
00086
00087
00088 #endif