diff options
Diffstat (limited to 'src/Data/Triangle.cpp')
-rw-r--r-- | src/Data/Triangle.cpp | 74 |
1 files changed, 70 insertions, 4 deletions
diff --git a/src/Data/Triangle.cpp b/src/Data/Triangle.cpp index 8966b09..d334c12 100644 --- a/src/Data/Triangle.cpp +++ b/src/Data/Triangle.cpp @@ -44,12 +44,28 @@ Vector Triangle::loadVector(xmlpp::Element *node) const { } TexCoords Triangle::loadTexCoords(xmlpp::Element *node) const { - TexCoords t; + TexCoords t(1); + xmlpp::Attribute *attr; t.setS(std::atof(node->get_attribute_value("s").c_str())); - t.setT(std::atof(node->get_attribute_value("t").c_str())); - t.setR(std::atof(node->get_attribute_value("r").c_str())); - t.setQ(std::atof(node->get_attribute_value("q").c_str())); + + attr = node->get_attribute("t"); + if(attr) { + t.setT(std::atof(attr->get_value().c_str())); + t.setCoordCount(2); + } + + attr = node->get_attribute("r"); + if(attr) { + t.setR(std::atof(attr->get_value().c_str())); + t.setCoordCount(3); + } + + attr = node->get_attribute("q"); + if(attr) { + t.setQ(std::atof(attr->get_value().c_str())); + t.setCoordCount(4); + } return t; } @@ -83,6 +99,9 @@ Triangle::Triangle(xmlpp::Element *node) : triangleNode(node) { if(e->get_name() == "vertex") { vertexNodes[++i] = e; vertices[i] = loadVertex(e); + + normalNodes[i] = NULL; + texCoordsNodes[i] = NULL; } } @@ -90,5 +109,52 @@ Triangle::Triangle(xmlpp::Element *node) : triangleNode(node) { texture = node->get_attribute_value("texture"); } +void Triangle::setVertex(unsigned int i, const Vertex &v) { + vertices[i] = v; + + vertexNodes[i]->set_attribute("x", Glib::ustring::format(v.getX())); + vertexNodes[i]->set_attribute("y", Glib::ustring::format(v.getY())); + vertexNodes[i]->set_attribute("z", Glib::ustring::format(v.getZ())); +} + +void Triangle::setNormal(unsigned int i, const Vector &n) { + normals[i] = n; + + if(!normalNodes[i]) + normalNodes[i] = triangleNode->add_child(static_cast<xmlpp::Node*>(vertexNodes[i]), "normal"); + + normalNodes[i]->set_attribute("x", Glib::ustring::format(n.getX())); + normalNodes[i]->set_attribute("y", Glib::ustring::format(n.getY())); + normalNodes[i]->set_attribute("z", Glib::ustring::format(n.getZ())); +} + +void Triangle::setTexCoords(unsigned int i, const TexCoords &t) { + texCoords[i] = t; + + if(!texCoordsNodes[i]) { + if(normalNodes[i]) + texCoordsNodes[i] = triangleNode->add_child(static_cast<xmlpp::Node*>(normalNodes[i]), "texcoords"); + else + texCoordsNodes[i] = triangleNode->add_child(static_cast<xmlpp::Node*>(vertexNodes[i]), "texcoords"); + } + + texCoordsNodes[i]->set_attribute("s", Glib::ustring::format(t.getS())); + + if(t.getCoordCount() >= 2) + texCoordsNodes[i]->set_attribute("t", Glib::ustring::format(t.getT())); + else + texCoordsNodes[i]->remove_attribute("t"); + + if(t.getCoordCount() >= 3) + texCoordsNodes[i]->set_attribute("r", Glib::ustring::format(t.getR())); + else + texCoordsNodes[i]->remove_attribute("r"); + + if(t.getCoordCount() >= 4) + texCoordsNodes[i]->set_attribute("q", Glib::ustring::format(t.getQ())); + else + texCoordsNodes[i]->remove_attribute("q"); +} + } } |