Debian Codegraph @

vcg/wrap/io_trimesh/export_vrml.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25 $Log: export_vrml.h,v $
26 Revision 1.9 2008/02/27 23:35:56 cignoni
27 removed useless include
28
29 Revision 1.8 2008/02/21 17:47:29 cignoni
30 corrected texture saving. Still broken the multitexturing
31
32 Revision 1.7 2008/02/21 17:23:57 cignoni
33 Corrected various bug, involving spurious commas, and pervertex color saved as per wedge color.
34
35 Revision 1.6 2007/06/12 10:15:35 cignoni
36 Very important change. No more scaling and translation in the saved file!
37
38 Revision 1.5 2007/03/20 16:47:49 cignoni
39 Update to the new texture syntax
40
41 Revision 1.4 2006/11/21 19:22:53 e_cerisoli
42 Added Comments for documentation
43
44 ****************************************************************************/
45
46 #ifndef __VCGLIB_EXPORT_WRL
47 #define __VCGLIB_EXPORT_WRL
48
49 #include <stdio.h>
50 #include <wrap/io_trimesh/io_mask.h>
51
52 namespace vcg {
53 namespace tri {
54 namespace io {
55
56 template <class SaveMeshType>
57 /**
58 This class encapsulate a filter for save vrml meshes.
59 */
60 class ExporterWRL
61 {
62 public:
63 typedef typename SaveMeshType::VertexPointer VertexPointer;
64 typedef typename SaveMeshType::ScalarType ScalarType;
65 typedef typename SaveMeshType::VertexType VertexType;
66 typedef typename SaveMeshType::FaceType FaceType;
67 typedef typename SaveMeshType::VertexIterator VertexIterator;
68 typedef typename SaveMeshType::FaceIterator FaceIterator;
69
70 ///Standard call for saving a mesh
71 static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *cb=0)
72 {
73 FILE *fp;
74 fp = fopen(filename,"wb");
75 if(fp==NULL)
76 return 1;
77
78 // Header
79 fprintf(fp,
80 "#VRML V2.0 utf8\n"
81 "\n"
82 "# Generated by VCGLIB, (C)Copyright 1999-2001 VCG, IEI-CNR\n"
83 "\n"
84 "NavigationInfo {\n"
85 " type [ \"EXAMINE\", \"ANY\" ]\n"
86 "}\n"
87 );
88
89 // Tranche principale
90 double ss = 8.0/m.bbox.Diag();
91
92 fprintf(fp,
93 "Transform {\n"
94 " scale %g %g %g\n"
95 " translation %g %g %g\n"
96 " children\n"
97 " [\n"
98 ,1.0,1.0,1.0,
99 0.0,0.0,0.0);
100 // ,ss,ss,ss
101 // , -m.bbox.Center()[0]*ss, -m.bbox.Center()[1]*ss,-3-m.bbox.Center()[2]*ss );
102
103 // Start Shape
104 fprintf(fp,
105 " Shape\n"
106 " {\n"
107 " geometry IndexedFaceSet\n"
108 " {\n"
109 " creaseAngle .5\n"
110 " solid FALSE\n"
111 " coord Coordinate\n"
112 " {\n"
113 " point\n"
114 " ["
115 );
116 FaceIterator fi;
117 VertexIterator vi;
118 std::map<VertexPointer,int> index;
119 int ind;
120
121 // Vertici
122 for(ind=0,vi=m.vert.begin(); vi!=m.vert.end(); ++vi, ++ind)
123 if(!(*vi).IsD())
124 {
125 if(vi!=m.vert.begin()) fprintf(fp,", ");
126 if(ind%4==0) fprintf(fp, "\n " );
127 fprintf(fp, "%g %g %g" ,(*vi).P()[0] ,(*vi).P()[1] ,(*vi).P()[2] );
128 index[&*vi] = ind;
129 }
130 fprintf(fp,"\n"
131 " ]\n"
132 " }\n"
133 );
134
135
136 if( HasPerVertexColor(m) && (mask & vcg::tri::io::Mask::IOM_VERTCOLOR))
137 {
138 fprintf(fp,
139 " color Color\n"
140 " {\n"
141 " color\n"
142 " ["
143 );
144 for(ind=0,vi=m.vert.begin();vi!=m.vert.end();++vi,++ind)
145 if(!(*vi).IsD())
146 {
147 if(vi!=m.vert.begin()) fprintf(fp,", ");
148 float r = float(vi->C()[0])/255;
149 float g = float(vi->C()[1])/255;
150 float b = float(vi->C()[2])/255;
151
152 if(ind%4==0)
153 fprintf(fp,"\n ");
154 fprintf(fp,"%g %g %g",r,g,b);
155 }
156 fprintf(fp,
157 "\n"
158 " ]\n"
159 " }\n"
160 );
161 }
162 else if( HasPerWedgeColor(m) && (mask & vcg::tri::io::Mask::IOM_WEDGCOLOR ))
163 {
164 fprintf(fp,
165 " color Color\n"
166 " {\n"
167 " color\n"
168 " [" );
169 for(ind=0,fi=m.face.begin();fi!=m.face.end();++fi,++ind)
170 if(!(*fi).IsD())
171 {
172 if(fi!=m.face.begin()) fprintf(fp,", ");
173 if(ind%4==0) fprintf(fp,"\n ");
174 for(int z=0;z<3;++z)
175 {
176 if(z!=0) fprintf(fp,", ");
177 float r = float(fi->WC(z)[0])/255;
178 float g = float(fi->WC(z)[1])/255;
179 float b = float(fi->WC(z)[2])/255;
180 fprintf(fp,"%g %g %g",r,g,b);
181 }
182 }
183 fprintf(fp,
184 "\n"
185 " ]\n"
186 " }\n"
187 " colorIndex\n"
188 " [" );
189 int nn = 0;
190 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
191 if(!(*fi).IsD())
192 {
193 //if(fi!=m.face.begin()) fprintf(fp,", ");
194 if(ind%4==0) fprintf(fp,"\n ");
195 fprintf(fp,"%i",nn++);
196 fprintf(fp,"%i",nn++);
197 fprintf(fp,"%i",nn++);
198 fprintf(fp,"-1");
199 }
200 fprintf(fp,
201 "\n"
202 " ]\n"
203 );
204 }
205 // NOTE MULTITEXTURE WRL SAVING DO NOT WORK!
206 else if (HasPerWedgeTexCoord(m) &&(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD))
207 {
208 fprintf(fp,
209 "\n"
210 " texCoord TextureCoordinate\n"
211 " {\n"
212 " point\n"
213 " [\n"
214 );
215 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
216 if(!(*fi).IsD())
217 {
218 //if(fi!=m.face.begin()) fprintf(fp,", ");
219 if(ind%4==0) fprintf(fp,"\n ");
220 for (int j = 0; j < 3; j++)
221 fprintf(fp,"%g %g ",fi->WT(j).u(),fi->WT(j).v());
222 }
223 fprintf(fp,
224 "\n"
225 " ]\n"
226 " }\n"
227 " texCoordIndex\n"
228 " [\n"
229 );
230 int nn = 0;
231 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
232 if(!(*fi).IsD())
233 {
234 //if(fi!=m.face.begin()) fprintf(fp,", ");
235 if(ind%4==0) fprintf(fp,"\n ");
236 for (int j = 0; j < 3; j++)
237 fprintf(fp,"%d ",nn++);
238 fprintf(fp,"-1 ");
239 }
240 fprintf(fp,
241 "\n"
242 " ]\n"
243 );
244 }
245 fprintf(fp,
246 " coordIndex\n"
247 " ["
248 );
249 // Facce
250 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
251 if(!(*fi).IsD())
252 {
253 if(fi!=m.face.begin()) fprintf(fp,", ");
254 if(ind%6==0) fprintf(fp,"\n ");
255 for (int j = 0; j < 3; j++)
256 fprintf(fp,"%i,",index[(*fi).V(j)]);
257
258 fprintf(fp,"-1");
259 }
260 fprintf(fp,
261 "\n"
262 " ]\n"
263 " }\n"
264 " appearance Appearance\n"
265 " {\n"
266 " material Material\n"
267 " {\n"
268 " ambientIntensity 0.2\n"
269 " diffuseColor 0.9 0.9 0.9\n"
270 " specularColor .1 .1 .1\n"
271 " shininess .5\n"
272 " }\n"
273 );
274 if(m.textures.size())
275 {
276 fprintf(fp,
277 " texture ImageTexture { url \" %s \" }\n"
278 ,m.textures[0].c_str()
279 );
280 }
281 fprintf(fp,
282 " }\n"
283 " }\n"
284 " ]\n"
285 "}\n"
286 );
287 fclose(fp);
288 return 0;
289 }
290 ///Returns mask of capability one define with what are the saveable information of the format.
291 static int GetExportMaskCapability()
292 {
293 int capability = 0;
294
295 //vert
296 capability |= MeshModel::IOM_VERTCOLOR;
297
298 //wedg
299 capability |= MeshModel::IOM_WEDGTEXCOORD;
300 capability |= MeshModel::IOM_WEDGCOLOR;
301
302 return capability;
303 }
304
305 /// Standard call for knowing the meaning of an error code
306 static const char *ErrorMsg(int error)
307 {
308 static std::vector<std::string> wrl_error_msg;
309 if(wrl_error_msg.empty())
310 {
311 wrl_error_msg.resize(2 );
312 wrl_error_msg[0]="No errors";
313 wrl_error_msg[1]="Can't open file";
314 }
315 if(error>1 || error<0) return "Unknown error";
316 else return wrl_error_msg[error].c_str();
317 }
318
319 }; // end class
320 } // end Namespace io
321 } // end Namespace tri
322 } // end Namespace vcg
323
324 #endif