00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef __VCGLIB_EXPORT_WRL
00044 #define __VCGLIB_EXPORT_WRL
00045
00046 #include <stdio.h>
00047 #include <wrap/io_trimesh/io_mask.h>
00048
00049 namespace vcg {
00050 namespace tri {
00051 namespace io {
00052
00053 template <class SaveMeshType>
00057 class ExporterWRL
00058 {
00059 public:
00060 typedef typename SaveMeshType::VertexPointer VertexPointer;
00061 typedef typename SaveMeshType::ScalarType ScalarType;
00062 typedef typename SaveMeshType::VertexType VertexType;
00063 typedef typename SaveMeshType::FaceType FaceType;
00064 typedef typename SaveMeshType::VertexIterator VertexIterator;
00065 typedef typename SaveMeshType::FaceIterator FaceIterator;
00066
00068 static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *)
00069 {
00070 FILE *fp;
00071 fp = fopen(filename,"wb");
00072 if(fp==NULL)
00073 return 1;
00074
00075
00076 fprintf(fp,
00077 "#VRML V2.0 utf8\n"
00078 "\n"
00079 "# Generated by VCGLIB, (C)Copyright 1999-2001 VCG, IEI-CNR\n"
00080 "\n"
00081 "NavigationInfo {\n"
00082 " type [ \"EXAMINE\", \"ANY\" ]\n"
00083 "}\n"
00084 );
00085
00086
00087
00088
00089 fprintf(fp,
00090 "Transform {\n"
00091 " scale %g %g %g\n"
00092 " translation %g %g %g\n"
00093 " children\n"
00094 " [\n"
00095 ,1.0,1.0,1.0,
00096 0.0,0.0,0.0);
00097
00098
00099
00100
00101 fprintf(fp,
00102 " Shape\n"
00103 " {\n"
00104 " geometry IndexedFaceSet\n"
00105 " {\n"
00106 " creaseAngle .5\n"
00107 " solid FALSE\n"
00108 " coord Coordinate\n"
00109 " {\n"
00110 " point\n"
00111 " ["
00112 );
00113 FaceIterator fi;
00114 VertexIterator vi;
00115 std::map<VertexPointer,int> index;
00116 int ind;
00117
00118
00119 for(ind=0,vi=m.vert.begin(); vi!=m.vert.end(); ++vi, ++ind)
00120 if(!(*vi).IsD())
00121 {
00122 if(vi!=m.vert.begin()) fprintf(fp,", ");
00123 if(ind%4==0) fprintf(fp, "\n " );
00124 fprintf(fp, "%g %g %g" ,(*vi).P()[0] ,(*vi).P()[1] ,(*vi).P()[2] );
00125 index[&*vi] = ind;
00126 }
00127 fprintf(fp,"\n"
00128 " ]\n"
00129 " }\n"
00130 );
00131
00132
00133 if( HasPerVertexColor(m) && (mask & vcg::tri::io::Mask::IOM_VERTCOLOR))
00134 {
00135 fprintf(fp,
00136 " color Color\n"
00137 " {\n"
00138 " color\n"
00139 " ["
00140 );
00141 for(ind=0,vi=m.vert.begin();vi!=m.vert.end();++vi,++ind)
00142 if(!(*vi).IsD())
00143 {
00144 if(vi!=m.vert.begin()) fprintf(fp,", ");
00145 float r = float(vi->C()[0])/255;
00146 float g = float(vi->C()[1])/255;
00147 float b = float(vi->C()[2])/255;
00148
00149 if(ind%4==0)
00150 fprintf(fp,"\n ");
00151 fprintf(fp,"%g %g %g",r,g,b);
00152 }
00153 fprintf(fp,
00154 "\n"
00155 " ]\n"
00156 " }\n"
00157 );
00158 }
00159 else if( HasPerWedgeColor(m) && (mask & vcg::tri::io::Mask::IOM_WEDGCOLOR ))
00160 {
00161 fprintf(fp,
00162 " color Color\n"
00163 " {\n"
00164 " color\n"
00165 " [" );
00166 for(ind=0,fi=m.face.begin();fi!=m.face.end();++fi,++ind)
00167 if(!(*fi).IsD())
00168 {
00169 if(fi!=m.face.begin()) fprintf(fp,", ");
00170 if(ind%4==0) fprintf(fp,"\n ");
00171 for(int z=0;z<3;++z)
00172 {
00173 if(z!=0) fprintf(fp,", ");
00174 float r = float(fi->WC(z)[0])/255;
00175 float g = float(fi->WC(z)[1])/255;
00176 float b = float(fi->WC(z)[2])/255;
00177 fprintf(fp,"%g %g %g",r,g,b);
00178 }
00179 }
00180 fprintf(fp,
00181 "\n"
00182 " ]\n"
00183 " }\n"
00184 " colorIndex\n"
00185 " [" );
00186 int nn = 0;
00187 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
00188 if(!(*fi).IsD())
00189 {
00190
00191 if(ind%4==0) fprintf(fp,"\n ");
00192 fprintf(fp,"%i",nn++);
00193 fprintf(fp,"%i",nn++);
00194 fprintf(fp,"%i",nn++);
00195 fprintf(fp,"-1");
00196 }
00197 fprintf(fp,
00198 "\n"
00199 " ]\n"
00200 );
00201 }
00202
00203 else if (HasPerWedgeTexCoord(m) &&(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD))
00204 {
00205 fprintf(fp,
00206 "\n"
00207 " texCoord TextureCoordinate\n"
00208 " {\n"
00209 " point\n"
00210 " [\n"
00211 );
00212 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
00213 if(!(*fi).IsD())
00214 {
00215
00216 if(ind%4==0) fprintf(fp,"\n ");
00217 for (int j = 0; j < 3; j++)
00218 fprintf(fp,"%g %g ",fi->WT(j).u(),fi->WT(j).v());
00219 }
00220 fprintf(fp,
00221 "\n"
00222 " ]\n"
00223 " }\n"
00224 " texCoordIndex\n"
00225 " [\n"
00226 );
00227 int nn = 0;
00228 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
00229 if(!(*fi).IsD())
00230 {
00231
00232 if(ind%4==0) fprintf(fp,"\n ");
00233 for (int j = 0; j < 3; j++)
00234 fprintf(fp,"%d ",nn++);
00235 fprintf(fp,"-1 ");
00236 }
00237 fprintf(fp,
00238 "\n"
00239 " ]\n"
00240 );
00241 }
00242 fprintf(fp,
00243 " coordIndex\n"
00244 " ["
00245 );
00246
00247 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
00248 if(!(*fi).IsD())
00249 {
00250 if(fi!=m.face.begin()) fprintf(fp,", ");
00251 if(ind%6==0) fprintf(fp,"\n ");
00252 for (int j = 0; j < 3; j++)
00253 fprintf(fp,"%i,",index[(*fi).V(j)]);
00254
00255 fprintf(fp,"-1");
00256 }
00257 fprintf(fp,
00258 "\n"
00259 " ]\n"
00260 " }\n"
00261 " appearance Appearance\n"
00262 " {\n"
00263 " material Material\n"
00264 " {\n"
00265 " ambientIntensity 0.2\n"
00266 " diffuseColor 0.9 0.9 0.9\n"
00267 " specularColor .1 .1 .1\n"
00268 " shininess .5\n"
00269 " }\n"
00270 );
00271 if(m.textures.size())
00272 {
00273 fprintf(fp,
00274 " texture ImageTexture { url \"%s\" }\n"
00275 ,m.textures[0].c_str()
00276 );
00277 }
00278 fprintf(fp,
00279 " }\n"
00280 " }\n"
00281 " ]\n"
00282 "}\n"
00283 );
00284 fclose(fp);
00285 return 0;
00286 }
00288 static int GetExportMaskCapability()
00289 {
00290 int capability = 0;
00291
00292
00293 capability |= Mask::IOM_VERTCOLOR;
00294
00295
00296 capability |= Mask::IOM_WEDGTEXCOORD;
00297 capability |= Mask::IOM_WEDGCOLOR;
00298
00299 return capability;
00300 }
00301
00303 static const char *ErrorMsg(int error)
00304 {
00305 static std::vector<std::string> wrl_error_msg;
00306 if(wrl_error_msg.empty())
00307 {
00308 wrl_error_msg.resize(2 );
00309 wrl_error_msg[0]="No errors";
00310 wrl_error_msg[1]="Can't open file";
00311 }
00312 if(error>1 || error<0) return "Unknown error";
00313 else return wrl_error_msg[error].c_str();
00314 }
00315
00316 };
00317 }
00318 }
00319 }
00320
00321 #endif