summaryrefslogtreecommitdiffstats
path: root/src/Data/Triangle.cpp
blob: 8966b0959f97a5060de704330e1b6182aaf097b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Triangle.cpp
 * 
 * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
 * 
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Triangle.h"
#include <cstdlib>

namespace ZoomEdit {
namespace Data {

Vertex Triangle::loadVertex(xmlpp::Element *node) const {
  Vertex v;
  
  v.setX(std::atof(node->get_attribute_value("x").c_str()));
  v.setY(std::atof(node->get_attribute_value("y").c_str()));
  v.setZ(std::atof(node->get_attribute_value("z").c_str()));
  
  return v;
}

Vector Triangle::loadVector(xmlpp::Element *node) const {
  Vector v;
  
  v.setX(std::atof(node->get_attribute_value("x").c_str()));
  v.setY(std::atof(node->get_attribute_value("y").c_str()));
  v.setZ(std::atof(node->get_attribute_value("z").c_str()));
  
  return v;
}

TexCoords Triangle::loadTexCoords(xmlpp::Element *node) const {
  TexCoords t;
  
  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()));
  
  return t;
}

Triangle::Triangle(xmlpp::Element *node) : triangleNode(node) {
  xmlpp::Node::NodeList nodeList = node->get_children();
  
  int i = -1;
  
  std::memset(vertexNodes, 0, sizeof(vertexNodes));
  
  for(xmlpp::Node::NodeList::iterator n = nodeList.begin(); n != nodeList.end(); ++n) {
    xmlpp::Element *e = dynamic_cast<xmlpp::Element*>(*n);
    if(!e)
      continue;
    
    if(i >= 0) {
      if(e->get_name() == "normal") {
        normalNodes[i] = e;
        normals[i] = loadVector(e);
        continue;
      }
      else if(e->get_name() == "texcoords") {
        texCoordsNodes[i] = e;
        texCoords[i] = loadTexCoords(e);
        
        continue;
      }
    }
    
    if(e->get_name() == "vertex") {
      vertexNodes[++i] = e;
      vertices[i] = loadVertex(e);
    }
  }
  
  visible = (node->get_attribute_value("visible").lowercase() != "false");
  texture = node->get_attribute_value("texture");
}

}
}