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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527 */
/* without consent of either the College of William & Mary or */
/* Pieter Peers. */
/******************************************************************/
#include <vector>
#include <memory>
#include <string>
#include <cstdio>
#include <cassert>
#include "sceneIO_xml.h"
#include "sceneIO_core.h"
#include "sceneIO_cache.h"
#include "sceneIO_geometry.h"
#include "sceneIO_material.h"
#include "util.h"
#include "vec2d.h"
#include "vec3d.h"
#include "triangle.h"
#include "constants.h"
#include "importOBJ.h"
#include "errorMessage.h"
#include "triangleMesh.h"
#include "random_number.h"
#include "sceneGraphNode.h"
static void importTriangle(const XMLNode& node, std::vector<triangle>& triangle_list)
{
// sanity check
assert(node.name() == "triangle" || node.name() == "polygon");
// allocate temp data
unsigned int idx=0;
vec3d v[3], n[3];
vec2d t[3];
// for each child node, parse all vertices
for(XMLNode child = node.firstChild(); child.isValid(); child++)
{
std::string name = child.name();
if(name == "vertex")
{
v[idx] = getVec3d(child, "v", vec3d(+LARGE));
n[idx] = getVec3d(child, "n", vec3d(0.0f));
t[idx] = getVec2d(child, "t", vec2d(+LARGE));
// check if valid vertex
if(v[idx].x >= +LARGE || v[idx].y >= +LARGE || v[idx].z >= +LARGE)
errorMessage("Invalid vertex in %s definition.", node.name().c_str());
// create triangle
if(idx == 2)
{
// determine which attributes are defined on the triangle
bool hasNormals = (n[0].squared_length() > +EPSILON) && (n[0].squared_length() > +EPSILON) && (n[0].squared_length() > +EPSILON);
bool hasTextureCoord = (fabs(t[0].x) < +LARGE && fabs(t[0].y) < +LARGE) && (fabs(t[1].x) < +LARGE && fabs(t[1].y) < +LARGE) && (fabs(t[2].x) < +LARGE && fabs(t[2].y) < +LARGE);
if(!hasNormals && !hasTextureCoord) triangle_list.push_back(triangle(v[0], v[1], v[2]));
else if(hasNormals && !hasTextureCoord) triangle_list.push_back(triangle(v[0], v[1], v[2], n[1], n[2], n[3]));
else if(!hasNormals && hasTextureCoord) triangle_list.push_back(triangle(v[0], v[1], v[2], t[0], t[1], t[2]));
else triangle_list.push_back(triangle(v[0], v[1], v[2], n[1], n[2], n[3], t[0], t[1], t[2]));
// cycle (to support polygons)
v[1] = v[2];
n[1] = v[2];
t[1] = t[2];
}
else idx++;
}
else errorMessage("Unknown child element in %s node (%s).", node.name().c_str(), name.c_str());
}
// check if at least one full triangle was found
if(idx != 2)
errorMessage("%s requires at least 3 vertices.", node.name().c_str());
// Done.
}
/////////////////////////////////////////////////////////////////
static std::shared_ptr<boundedPrimitive> importTriangleMesh(const XMLNode& node, nodeCache<boundedPrimitive>& shape_cache, nodeCache<shader_base>& shader_cache, nodeCache<texture_base>& texture_cache, const std::string& rootDir)
{
// sanity check
assert(node.name() == "triangleMesh");
// allocate node properties
transformation3d transform;
std::shared_ptr<const shader_base> material;
std::vector<triangle> triangle_list;
// import OBJ triangles
std::string objname = getString(node, "filename");
if(objname != "")
{
std::string path = getDirectory(objname);
if(path == "") objname = rootDir + objname;
importOBJ(objname, triangle_list);
}
// check child nodes
for(XMLNode child = node.firstChild(); child.isValid(); child++)
{
std::string name = child.name();
// general child nodes
auto tempmat = importMaterial(child, shader_cache, texture_cache, rootDir);
if(tempmat) material = tempmat;
// specialized child nodes
else if(name == "triangle" || name == "polygon") importTriangle(child, triangle_list);
else errorMessage("Unknown child-node in triangleMesh (%s).", name.c_str());
}
// Done.
return std::shared_ptr<boundedPrimitive>(new triangleMesh(triangle_list, material, transform));
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
std::shared_ptr<boundedPrimitive> importGeometry(const XMLNode& node, nodeCache<boundedPrimitive>& shape_cache, nodeCache<shader_base>& shader_cache, nodeCache<texture_base>& texture_cache, const std::string& rootDir)
{
// check if geometry type
if(node.name() != "sceneGraphNode" && node.name() != "triangleMesh") return std::shared_ptr<boundedPrimitive>(nullptr);
// check if reference
std::string ref = getString(node, "ref");
std::shared_ptr<boundedPrimitive> shape = shape_cache.get(ref);
if(shape) return shape;
else if(ref != "") errorMessage("Unknown %s name (%s).", node.name().c_str(), ref.c_str());
// generate default name & get id
char defaultname[32]; sprintf(defaultname, "noname-%u", random_int());
std::string id = getString(node, "id", defaultname);
if(shape_cache.get(id))
errorMessage("%s-node id is not unique (%s).", node.name().c_str(), id.c_str());
// process primitive based on type
else if(node.name() == "triangleMesh") shape = importTriangleMesh(node, shape_cache, shader_cache, texture_cache, rootDir);
// update cache
if(id != "") shape_cache.add(id, shape);
// Done.
return shape;
}
|