1 #include "../include/animorph/RIBExporter.h"
2
3 using namespace std;
4 using namespace Animorph;
5
6
createObjectStream(ostringstream & out_stream,const FGroupData & facegroupdata,const VertexData & vertexgroupdata)7 void RIBExporter::createObjectStream (ostringstream &out_stream,
8 const FGroupData &facegroupdata,
9 const VertexData &vertexgroupdata)
10 {
11 const VertexVector &vertexvector(mesh.getVertexVectorRef ());
12 const FaceVector &facevector(mesh.getFaceVectorRef ());
13 const TextureVector &texturevector(mesh.getTextureVectorRef ());
14
15 out_stream << "Declare \"st\" \"facevarying float[2]\"" << endl;
16
17 // write header
18 out_stream << "SubdivisionMesh \"catmull-clark\" ";
19
20 // write number of vertices in a face
21 out_stream << "[";
22
23 for (unsigned int i = 0; i < facegroupdata.size (); i++)
24 {
25 const Face &face(facevector[facegroupdata[i]]);
26
27 out_stream << face.getSize() << " ";
28 }
29 out_stream << "] " << endl;
30
31 // write all vertex numbers in a face
32 out_stream << "[";
33 for (unsigned int i = 0; i < facegroupdata.size (); i++)
34 {
35 const Face &face(facevector[facegroupdata[i]]);
36
37 for (unsigned int n = face.getSize(); n > 0; n--)
38 {
39 VertexData::const_iterator vertexData_it;
40 vertexData_it = vertexgroupdata.find(face.getVertexAtIndex(n - 1));
41 if (vertexData_it != vertexgroupdata.end())
42 {
43 const int v = vertexData_it->second;
44
45 out_stream << v << " ";
46 }
47 }
48 }
49 out_stream << "]" << endl;
50 out_stream << "[\"interpolateboundary\"] [0 0] [0] [0]" << endl;
51
52 // write vertices
53 out_stream << "\"P\" [";
54 for (VertexData::const_iterator vertexgroup_it = vertexgroupdata.begin ();
55 vertexgroup_it != vertexgroupdata.end ();
56 vertexgroup_it++)
57 {
58 const Vertex &vertex(vertexvector[(*vertexgroup_it).first]);
59 Vector3f vector = vertex.co/* * tm*/;
60
61 out_stream << -vector.x <<
62 " " << vector.y <<
63 " " << vector.z << " ";
64 }
65 out_stream << "]";
66
67 // write UV coordinates
68 if (facevector.size () == texturevector.size ())
69 {
70 out_stream << "\"st\" [";
71
72 for (unsigned int i = 0; i < facegroupdata.size (); i++)
73 {
74 const TextureFace &texture_face(texturevector[facegroupdata[i]]);
75
76 for (unsigned int n = texture_face.size (); n > 0; n--)
77 //for (unsigned int n = 0; n < texture_face.size (); n++)
78 {
79
80 const Vector2f &uv_texture(texture_face[n - 1]);
81
82 // TODO: prevent useless spaces here?
83 // TODO: if UV input data changes use 1.0-y here!
84 out_stream << uv_texture.x << " "
85 << uv_texture.y << " ";
86 }
87 }
88 out_stream << "]";
89 }
90 }
91
replaceRIBTags(ifstream & in_stream,ostringstream & outStream,const list<StringPair> & replaceList)92 void RIBExporter::replaceRIBTags (ifstream &in_stream,
93 ostringstream &outStream,
94 const list <StringPair> &replaceList)
95 {
96 char buffer[MAX_LINE_BUFFER];
97 string hs_name;
98 while (in_stream.getline (buffer, MAX_LINE_BUFFER))
99 {
100 string newLine (buffer);
101
102 for (list<StringPair>::const_iterator sl_it = replaceList.begin ();
103 sl_it != replaceList.end ();
104 sl_it++)
105 {
106 const StringPair &strP_ref = *sl_it;
107 const string &s1 = strP_ref.first;
108 const string &s2 = strP_ref.second;
109
110 replaceString (s1, s2, newLine);
111 }
112
113
114 outStream << newLine << endl;
115 }
116 }
117
exportFile(const string & outFile)118 bool RIBExporter::exportFile (const string &outFile)
119 {
120 FaceGroup &facegroup(mesh.getFaceGroupRef());
121 facegroup.calcVertexes(mesh.getFaceVectorRef ());
122
123 FaceGroup &clothesgroup (mesh.getClothesGroupRef ());
124 clothesgroup.calcVertexes (mesh.getFaceVectorRef ());
125
126 FileWriter file_writer;
127 for (FaceGroup::iterator facegroup_it = facegroup.begin ();
128 facegroup_it != facegroup.end ();
129 facegroup_it++)
130 {
131 const string& partname((*facegroup_it).first);
132 FGroupData &groupdata = (*facegroup_it).second.facesIndexes;
133
134 file_writer.open (outFile + partname + ".rib");
135
136 if (file_writer)
137 {
138 std::ostringstream out_stream;
139
140 createObjectStream (out_stream,
141 groupdata,
142 facegroup.getPartVertexesRef(partname));
143
144 file_writer << out_stream.str ();
145 file_writer.close ();
146 }
147
148 if (!file_writer)
149 return false;
150 }
151
152 for (FaceGroup::const_iterator clothesgroup_it = clothesgroup.begin ();
153 clothesgroup_it != clothesgroup.end ();
154 clothesgroup_it++)
155 {
156 FGroup clothes = (*clothesgroup_it).second;
157 if(!clothes.visible) continue;
158
159 string cl_name("_clothes_" + (*clothesgroup_it).first);
160 FGroupData &groupdata = clothes.facesIndexes;
161 file_writer.open (outFile + cl_name + ".rib");
162
163 if (file_writer)
164 {
165 std::ostringstream out_stream;
166
167 createObjectStream (out_stream,
168 groupdata,
169 facegroup.getPartVertexesRef(cl_name));
170
171 file_writer << out_stream.str ();
172 file_writer.close ();
173 }
174
175 if (!file_writer)
176 return false;
177 }
178
179 return true;
180 }
181
exportFile(const string & templateDirectory,const string & templateFile,const string & outFile,const list<StringPair> & replaceList)182 bool RIBExporter::exportFile (const string &templateDirectory,
183 const string &templateFile,
184 const string &outFile,
185 const list <StringPair> &replaceList)
186 {
187 string file_ending_cut = cutFileEnding (outFile, ".rib");
188
189 string bname = file_ending_cut.substr (outFile.find_last_of (PATH_SEPARATOR)+1, outFile.size ());
190
191 FileReader file_reader;
192
193 file_reader.open (templateDirectory + PATH_SEPARATOR + templateFile);
194
195 if (!file_reader)
196 return false;
197
198 FileWriter file_writer;
199 file_writer.open (file_ending_cut + ".rib");
200
201 if (file_writer)
202 {
203 std::ostringstream out_stream;
204 //createObjectStream (out_stream, outFile);
205 replaceRIBTags (file_reader, out_stream, replaceList);
206
207 file_writer << out_stream.str ();
208 file_writer.close ();
209 }
210
211 if (!file_writer)
212 return false;
213
214 return true;
215 }
216