source: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp @ 162

Revision 162, 5.2 KB checked in by bittner, 19 years ago (diff)

functional raycasting version

Line 
1#include <stack>
2#include "common.h"
3#include "SceneGraph.h"
4#include "X3dExporter.h"
5#include "Mesh.h"
6#include "KdTree.h"
7
8
9X3dExporter::X3dExporter(const string filename):Exporter(filename)
10{
11  stream.open(mFilename.c_str());
12  stream<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
13  stream<<"<X3D>"<<endl;
14  stream<<"<Scene>"<<endl;
15 
16}
17
18X3dExporter::~X3dExporter()
19{
20  stream<<"</Scene>"<<endl;
21  stream<<"</X3D>"<<endl;
22  stream.close();
23}
24
25bool
26X3dExporter::ExportRays(const vector<Ray> &rays, const float length)
27{
28  vector<Ray>::const_iterator ri = rays.begin();
29  stream<<"<Shape>"<<endl;
30  stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
31
32  int index = 0;
33  for (; ri != rays.end(); ri++) {
34    stream<<index<<" "<<index+1<<" -1\n";
35    index+=2;
36  }
37 
38  stream<<"\" >"<<endl;
39 
40  stream<<"<Coordinate  point=\""<<endl;
41 
42  ri = rays.begin();
43  for (; ri != rays.end(); ri++) {
44    Vector3 a = (*ri).GetLoc();
45    Vector3 b = (*ri).GetLoc() + length*(*ri).GetDir();
46   
47    stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
48    stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
49  }
50 
51  stream<<"\" >"<<endl;
52  stream<<"</Coordinate>"<<endl;
53  stream<<"</IndexedLineSet>"<<endl;
54  stream<<"</Shape>"<<endl;
55  return true;
56}
57
58void
59X3dExporter::ExportSceneNode(SceneGraphNode *node)
60{
61  stream<<"<Group>"<<endl;
62
63  SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
64  for (; ni != node->mChildren.end(); ni++)
65    ExportSceneNode(*ni);
66 
67 
68  MeshContainer::const_iterator mi = node->mGeometry.begin();
69  for (; mi != node->mGeometry.end(); mi++) {
70    // export the transform...
71    ExportMesh((*mi)->GetMesh());
72  }
73 
74  stream<<"</Group>"<<endl;
75
76}
77
78void
79X3dExporter::ExportMesh(Mesh *mesh)
80{
81
82  stream<<"<Shape>"<<endl;
83  stream<<"<Appearance>"<<endl;
84
85  // $$ tmp -> random material
86 
87  float r, g, b;
88
89  if (mesh->mMaterial) {
90    r = mesh->mMaterial->mDiffuseColor.r;
91    g = mesh->mMaterial->mDiffuseColor.g;
92    b = mesh->mMaterial->mDiffuseColor.b;
93  } else {
94    r = RandomValue(0.5, 1.0);
95    g = RandomValue(0.5, 1.0);
96    b = RandomValue(0.5, 1.0);
97  }
98  stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
99    "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
100  stream<<"</Appearance>"<<endl;
101
102
103  if (wireframe)
104    stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
105  else
106    stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
107
108  FaceContainer::const_iterator fi = mesh->mFaces.begin();
109
110  int index = 0;
111 
112  for (; fi != mesh->mFaces.end(); fi++) {
113    Face *face = *fi;
114    VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
115    for (; vi != face->mVertexIndices.end(); vi++)
116      stream<<*vi<<" ";
117    stream<<"-1"<<endl;
118  }
119  stream<<"\" >"<<endl;
120
121  stream<<"<Coordinate  point=\""<<endl;
122 
123  VertexContainer::const_iterator vi = mesh->mVertices.begin();
124  for (; vi != mesh->mVertices.end(); vi++) {
125    stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
126    stream<<","<<endl;
127  }
128 
129  stream<<"\" >"<<endl;
130  stream<<"</Coordinate>"<<endl;
131
132  if (wireframe)
133    stream<<"</IndexedLineSet>"<<endl;
134  else
135    stream<<"</IndexedFaceSet>"<<endl;
136 
137  stream<<"</Shape>"<<endl;
138
139}
140
141bool
142X3dExporter::ExportBox(const AxisAlignedBox3 &box)
143{
144  Mesh *mesh = new Mesh;
145  // add 6 vertices of the box
146  int index = mesh->mVertices.size();
147  for (int i=0; i < 8; i++) {
148    Vector3 v;
149    box.GetVertex(i, v);
150    mesh->mVertices.push_back(v);
151  }
152 
153  mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
154  mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
155  mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
156 
157  mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
158  mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
159  mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
160 
161  ExportMesh(mesh);
162  delete mesh;
163  return true;
164}
165
166bool
167X3dExporter::ExportKdTree(const KdTree &tree)
168{
169  stack<KdNode *> tStack;
170
171  tStack.push(tree.GetRoot());
172
173  Mesh *mesh = new Mesh;
174 
175  while (!tStack.empty()) {
176    KdNode *node = tStack.top();
177    tStack.pop();
178    AxisAlignedBox3 box = tree.GetBox(node);
179    // add 6 vertices of the box
180    int index = mesh->mVertices.size();
181    for (int i=0; i < 8; i++) {
182      Vector3 v;
183      box.GetVertex(i, v);
184      mesh->mVertices.push_back(v);
185    }
186    mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
187    mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
188    mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
189
190    mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
191    mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
192    mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
193
194    if (!node->IsLeaf()) {
195      KdInterior *interior = (KdInterior *)node;
196      tStack.push(interior->mFront);
197      tStack.push(interior->mBack);
198    }
199  }
200 
201  ExportMesh(mesh);
202  delete mesh;
203  return true;
204}
205
206
207void
208X3dExporter::ExportMeshInstance(MeshInstance *mi)
209{
210  ExportMesh(mi->GetMesh());
211}
Note: See TracBrowser for help on using the repository browser.