00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _COLLADA_FORMAT_H
00024 #define _COLLADA_FORMAT_H
00025
00026 #include <wrap/dae/xmldocumentmanaging.h>
00027 #include <vcg/space/point4.h>
00028 #include <QtGui/QImage>
00029 #include <QtCore/QVector>
00030
00031 #include <QDateTime>
00032
00033
00034
00035 template<typename POINTTYPE>
00036 struct CoordNumber{public: static unsigned int coord() { return 0; }};
00037 template<> struct CoordNumber<vcg::Point2f> { public: static unsigned int coord() { return 2; } };
00038 template<> struct CoordNumber<vcg::Point3f> { public: static unsigned int coord() { return 3; } };
00039 template<> struct CoordNumber<vcg::Point4f> { public: static unsigned int coord() { return 4; } };
00040
00041
00042 namespace Collada
00043 {
00044 namespace Tags
00045 {
00046 static const QString testSharp(const QString& str)
00047 {
00048 QString sharp = "";
00049 if (str.at(0) != '#')
00050 sharp = '#';
00051 return (sharp + str);
00052 }
00053
00054 class ColladaTag : public XMLTag
00055 {
00056 public:
00057 ColladaTag()
00058 :XMLTag("COLLADA")
00059 {
00060 _attributes.push_back(TagAttribute("xmlns","http://www.collada.org/2005/11/COLLADASchema"));
00061 _attributes.push_back(TagAttribute("version","1.4.1"));
00062 }
00063 };
00064
00065 class AssetTag : public XMLTag
00066 {
00067 public:
00068 AssetTag()
00069 :XMLTag("asset")
00070 {
00071 }
00072 };
00073
00074 class ContributorTag : public XMLTag
00075 {
00076 public:
00077 ContributorTag()
00078 :XMLTag("contributor")
00079 {
00080 }
00081 };
00082
00083 class AuthorTag : public XMLLeafTag
00084 {
00085 public:
00086 AuthorTag()
00087 :XMLLeafTag("author")
00088 {
00089 _text.push_back("VCGLab");
00090 }
00091 };
00092
00093 class AuthoringToolTag : public XMLLeafTag
00094 {
00095 public:
00096 AuthoringToolTag()
00097 :XMLLeafTag("authoring_tool")
00098 {
00099 _text.push_back("VCGLib | MeshLab");
00100 }
00101 };
00102
00103 class UpAxisTag : public XMLLeafTag
00104 {
00105 public:
00106 UpAxisTag(const QString& up = "Y_UP")
00107 :XMLLeafTag("up_axis")
00108 {
00109 _text.push_back(up);
00110 }
00111 };
00112
00113 class LibraryImagesTag : public XMLTag
00114 {
00115 public:
00116 LibraryImagesTag()
00117 :XMLTag("library_images")
00118 {
00119 }
00120 };
00121
00122 class ImageTag : public XMLTag
00123 {
00124 public:
00125 ImageTag(const QString& id,const QString& name)
00126 :XMLTag("image")
00127 {
00128 _attributes.push_back(TagAttribute("id",id));
00129 _attributes.push_back(TagAttribute("name",name));
00130 }
00131 };
00132
00133 class InitFromTag : public XMLLeafTag
00134 {
00135 public:
00136 InitFromTag(const QString& txtpathname)
00137 :XMLLeafTag("init_from")
00138 {
00139 _text.push_back(txtpathname);
00140 }
00141 };
00142
00143 class LibraryMaterialsTag : public XMLTag
00144 {
00145 public:
00146 LibraryMaterialsTag()
00147 :XMLTag("library_materials")
00148 {
00149 }
00150 };
00151
00152 class MaterialTag : public XMLTag
00153 {
00154 public:
00155 MaterialTag(const QString& id,const QString& name)
00156 :XMLTag("material")
00157 {
00158 _attributes.push_back(TagAttribute("id",id));
00159 _attributes.push_back(TagAttribute("name",name));
00160 }
00161 };
00162
00163 class InstanceEffectTag : public XMLLeafTag
00164 {
00165 public:
00166 InstanceEffectTag(const QString& url)
00167 :XMLLeafTag("instance_effect")
00168 {
00169 _attributes.push_back(TagAttribute("url",testSharp(url)));
00170 }
00171 };
00172
00173 class LibraryEffectsTag : public XMLTag
00174 {
00175 public:
00176 LibraryEffectsTag()
00177 :XMLTag("library_effects")
00178 {
00179 }
00180 };
00181
00182 class EffectTag : public XMLTag
00183 {
00184 public:
00185 EffectTag(const QString& id)
00186 :XMLTag("effect")
00187 {
00188 _attributes.push_back(TagAttribute("id",id));
00189 }
00190 };
00191
00192 class ProfileCommonTag : public XMLTag
00193 {
00194 public:
00195 ProfileCommonTag()
00196 :XMLTag("profile_COMMON")
00197 {
00198 }
00199 };
00200
00201 class NewParamTag : public XMLTag
00202 {
00203 public:
00204 NewParamTag(const QString& sid)
00205 :XMLTag("newparam")
00206 {
00207 _attributes.push_back(TagAttribute("sid",sid));
00208 }
00209 };
00210
00211 class SurfaceTag : public XMLTag
00212 {
00213 public:
00214 SurfaceTag(const QString& type = QString("2D"))
00215 :XMLTag("surface")
00216 {
00217 _attributes.push_back(TagAttribute("type",type));
00218 }
00219 };
00220
00221 class FormatTag : public XMLLeafTag
00222 {
00223 public:
00224 FormatTag(const QString& format)
00225 :XMLLeafTag("format")
00226 {
00227 _text.push_back(format);
00228 }
00229 };
00230
00231 class Sampler2DTag : public XMLTag
00232 {
00233 public:
00234 Sampler2DTag()
00235 :XMLTag("sampler2D")
00236 {
00237 }
00238 };
00239
00240 class SourceTag : public XMLLeafTag
00241 {
00242 public:
00243 SourceTag(const QString& id,const QString& name)
00244 :XMLLeafTag("source")
00245 {
00246 _attributes.push_back(TagAttribute("id",id));
00247 _attributes.push_back(TagAttribute("name",name));
00248 }
00249
00250 SourceTag(const QString& source)
00251 :XMLLeafTag("source")
00252 {
00253 _text.push_back(source);
00254 }
00255 };
00256
00257 class MinFilterTag : public XMLLeafTag
00258 {
00259 public:
00260 MinFilterTag(const QString& filter)
00261 :XMLLeafTag("minfilter")
00262 {
00263 _text.push_back(filter);
00264 }
00265 };
00266
00267 class MagFilterTag : public XMLLeafTag
00268 {
00269 public:
00270 MagFilterTag(const QString& filter)
00271 :XMLLeafTag("magfilter")
00272 {
00273 _text.push_back(filter);
00274 }
00275 };
00276
00277 class TechniqueTag : public XMLTag
00278 {
00279 public:
00280 TechniqueTag(const QString& sid)
00281 :XMLTag("technique")
00282 {
00283 _attributes.push_back(TagAttribute("sid",sid));
00284 }
00285 };
00286
00287 class TechniqueCommonTag : public XMLTag
00288 {
00289 public:
00290 TechniqueCommonTag()
00291 :XMLTag("technique_common")
00292 {
00293 }
00294 };
00295
00296 class BlinnTag : public XMLTag
00297 {
00298 public:
00299 BlinnTag()
00300 :XMLTag("blinn")
00301 {
00302 }
00303 };
00304
00305 class EmissionTag : public XMLTag
00306 {
00307 public:
00308 EmissionTag()
00309 :XMLTag("emission")
00310 {
00311 }
00312 };
00313
00314 class ColorTag : public XMLLeafTag
00315 {
00316 public:
00317 ColorTag(const float r,const float g,const float b,const float a)
00318 :XMLLeafTag("color")
00319 {
00320
00321 _text.push_back(QString::number(r));
00322 _text.push_back(QString::number(g));
00323 _text.push_back(QString::number(b));
00324 _text.push_back(QString::number(a));
00325 }
00326 };
00327
00328 class AmbientTag : public XMLTag
00329 {
00330 public:
00331 AmbientTag()
00332 :XMLTag("ambient")
00333 {
00334 }
00335 };
00336
00337 class DiffuseTag : public XMLTag
00338 {
00339 public:
00340 DiffuseTag()
00341 :XMLTag("diffuse")
00342 {
00343 }
00344 };
00345
00346 class TextureTag : public XMLLeafTag
00347 {
00348 public:
00349 TextureTag(const QString& texture,const QString& texcoord)
00350 :XMLLeafTag("texture")
00351 {
00352 _attributes.push_back(TagAttribute("texture",texture));
00353 _attributes.push_back(TagAttribute("texcoord",texcoord));
00354 }
00355 };
00356
00357 class SpecularTag : public XMLTag
00358 {
00359 public:
00360 SpecularTag()
00361 :XMLTag("specular")
00362 {
00363 }
00364 };
00365
00366 class ShininessTag : public XMLTag
00367 {
00368 public:
00369 ShininessTag()
00370 :XMLTag("shininess")
00371 {
00372 }
00373 };
00374
00375 class FloatTag : public XMLLeafTag
00376 {
00377 public:
00378 FloatTag(const float floatnum)
00379 :XMLLeafTag("float")
00380 {
00381 _text.push_back(QString::number(floatnum));
00382 }
00383 };
00384
00385 class ReflectiveTag : public XMLTag
00386 {
00387 public:
00388 ReflectiveTag()
00389 :XMLTag("reflective")
00390 {
00391 }
00392 };
00393
00394 class ReflectivityTag : public XMLTag
00395 {
00396 public:
00397 ReflectivityTag()
00398 :XMLTag("reflectivity")
00399 {
00400 }
00401 };
00402
00403 class TransparentTag : public XMLTag
00404 {
00405 public:
00406 TransparentTag()
00407 :XMLTag("transparent")
00408 {
00409 }
00410 };
00411
00412 class TransparencyTag : public XMLTag
00413 {
00414 public:
00415 TransparencyTag()
00416 :XMLTag("transparency")
00417 {
00418 }
00419 };
00420
00421 class IndexOfRefractionTag : public XMLTag
00422 {
00423 public:
00424 IndexOfRefractionTag()
00425 :XMLTag("index_of_refraction")
00426 {
00427 }
00428 };
00429
00430 class LibraryGeometriesTag : public XMLTag
00431 {
00432 public:
00433 LibraryGeometriesTag()
00434 :XMLTag("library_geometries")
00435 {
00436 }
00437 };
00438
00439 class GeometryTag : public XMLTag
00440 {
00441 public:
00442 GeometryTag(const QString& id,const QString& name)
00443 :XMLTag("geometry")
00444 {
00445 _attributes.push_back(TagAttribute("id",id));
00446 _attributes.push_back(TagAttribute("name",name));
00447 }
00448 };
00449
00450 class MeshTag : public XMLTag
00451 {
00452 public:
00453 MeshTag()
00454 :XMLTag("mesh")
00455 {
00456 }
00457 };
00458
00459 class ArraySourceTag : public XMLTag
00460 {
00461 public:
00462 ArraySourceTag(const QString& id,const QString& name)
00463 :XMLTag("source")
00464 {
00465 _attributes.push_back(TagAttribute("id",id));
00466 _attributes.push_back(TagAttribute("name",name));
00467 }
00468 };
00469
00470 class FloatArrayTag : public XMLLeafTag
00471 {
00472 public:
00473 enum ARRAYSEMANTIC {VERTPOSITION,VERTNORMAL,FACENORMAL,WEDGETEXCOORD};
00474
00475 template<typename MESHTYPE>
00476 FloatArrayTag(const QString& id,const int count,const MESHTYPE& m,ARRAYSEMANTIC sem,const unsigned int componenttype)
00477 :XMLLeafTag("float_array")
00478 {
00479 _attributes.push_back(TagAttribute("id",id));
00480 _attributes.push_back(TagAttribute("count",QString::number(count)));
00481
00482 if ((sem == VERTPOSITION) || (sem == VERTNORMAL))
00483 {
00484 for(typename MESHTYPE::ConstVertexIterator vit = m.vert.begin();vit != m.vert.end();++vit)
00485 {
00486 for(unsigned int ii = 0; ii < componenttype;++ii)
00487 {
00488 if (sem == VERTPOSITION)
00489 _text.push_back(QString::number(vit->P()[ii]));
00490 else
00491 {
00492 typename MESHTYPE::VertexType::NormalType r = vit->cN();
00493 r.Normalize();
00494 _text.push_back(QString::number(r[ii]));
00495
00496 }
00497 }
00498 }
00499 }
00500 else
00501 {
00502 for(typename MESHTYPE::ConstFaceIterator fit = m.face.begin();fit != m.face.end();++fit)
00503 {
00504 if (sem == FACENORMAL)
00505 {
00506 for(unsigned int ii = 0; ii < componenttype;++ii)
00507 {
00508 typename MESHTYPE::FaceType::NormalType r = fit->cN();
00509 r.Normalize();
00510 _text.push_back(QString::number(r[ii]));
00511 }
00512 }
00513 else
00514 {
00515 for(unsigned int ii = 0; ii < 3;++ii)
00516 {
00517 _text.push_back(QString::number(fit->cWT(ii).U()));
00518 _text.push_back(QString::number(fit->cWT(ii).V()));
00519 }
00520 }
00521 }
00522 }
00523 }
00524 };
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 class AccessorTag : public XMLTag
00547 {
00548 public:
00549 AccessorTag(const int count,const QString& source,const int stride)
00550 :XMLTag("accessor")
00551 {
00552 _attributes.push_back(TagAttribute("count",QString::number(count)));
00553 _attributes.push_back(TagAttribute("source",testSharp(source)));
00554 _attributes.push_back(TagAttribute("stride",QString::number(stride)));
00555 }
00556 };
00557
00558 class ParamTag : public XMLTag
00559 {
00560 public:
00561 ParamTag(const QString& name,const QString& type)
00562 :XMLTag("param")
00563 {
00564 _attributes.push_back(TagAttribute("name",name));
00565 _attributes.push_back(TagAttribute("type",type));
00566 }
00567 };
00568
00569 class VerticesTag : public XMLTag
00570 {
00571 public:
00572 VerticesTag(const QString& id)
00573 :XMLTag("vertices")
00574 {
00575 _attributes.push_back(TagAttribute("id",id));
00576 }
00577 };
00578
00579 class InputTag : public XMLTag
00580 {
00581 public:
00582
00583 InputTag(const QString& semantic,const QString& source)
00584 :XMLTag("input")
00585 {
00586 _attributes.push_back(TagAttribute("semantic",semantic));
00587 _attributes.push_back(TagAttribute("source",testSharp(source)));
00588 }
00589
00590 InputTag(const int offset,const QString& semantic,const QString& source)
00591 :XMLTag("input")
00592 {
00593 _attributes.push_back(TagAttribute("offset",QString::number(offset)));
00594 _attributes.push_back(TagAttribute("semantic",semantic));
00595 _attributes.push_back(TagAttribute("source",testSharp(source)));
00596 }
00597 };
00598
00599 class TrianglesTag : public XMLTag
00600 {
00601 public:
00602 TrianglesTag(const int count)
00603 :XMLTag("triangles")
00604 {
00605 _attributes.push_back(TagAttribute("count",QString::number(count)));
00606 }
00607
00608 TrianglesTag(const int count,const QString& material)
00609 :XMLTag("triangles")
00610 {
00611 _attributes.push_back(TagAttribute("count",QString::number(count)));
00612 _attributes.push_back(TagAttribute("material",material));
00613 }
00614 };
00615
00616 class PTag : public XMLLeafTag
00617 {
00618 public:
00619 template<typename MESHTYPE>
00620 PTag(const MESHTYPE& m,const unsigned int nedge,bool norm = false,bool texcoord = false)
00621 :XMLLeafTag("p")
00622 {
00623 int cont = 0;
00624 for(typename MESHTYPE::ConstFaceIterator it= m.face.begin();it != m.face.end(); ++it)
00625 {
00626 for(unsigned int ii = 0; ii < nedge; ++ii)
00627 {
00628 int dist = it->V(ii) - &(*m.vert.begin());
00629 _text.push_back(QString::number(dist));
00630 if (norm)
00631 _text.push_back(QString::number(cont));
00632 if (texcoord)
00633 _text.push_back(QString::number(cont * nedge + ii));
00634 }
00635 ++cont;
00636 }
00637 }
00638
00639 template<typename MESHTYPE>
00640 PTag(const MESHTYPE& m,const unsigned int nedge,QVector<int>& patchfaces,bool norm = false,bool texcoord = false)
00641 :XMLLeafTag("p")
00642 {
00643 int cont = 0;
00644 for(QVector<int>::iterator it = patchfaces.begin();it != patchfaces .end(); ++it)
00645 {
00646 for(unsigned int ii = 0; ii < nedge; ++ii)
00647 {
00648 const typename MESHTYPE::FaceType& f = m.face[*it];
00649 int dist = f.V(ii) - &(*m.vert.begin());
00650 _text.push_back(QString::number(dist));
00651 if (norm)
00652 _text.push_back(QString::number(*it));
00653 if (texcoord)
00654 _text.push_back(QString::number(*it * nedge + ii));
00655 }
00656 ++cont;
00657 }
00658 }
00659 };
00660
00661 class LibraryVisualScenesTag : public XMLTag
00662 {
00663 public:
00664 LibraryVisualScenesTag()
00665 :XMLTag("library_visual_scenes")
00666 {
00667 }
00668 };
00669
00670 class VisualSceneTag : public XMLTag
00671 {
00672 public:
00673 VisualSceneTag(const QString& id,const QString& name)
00674 :XMLTag("visual_scene")
00675 {
00676 _attributes.push_back(TagAttribute("id",id));
00677 _attributes.push_back(TagAttribute("name",name));
00678 }
00679 };
00680
00681 class NodeTag : public XMLTag
00682 {
00683 public:
00684 NodeTag(const QString& id,const QString& name)
00685 :XMLTag("node")
00686 {
00687 _attributes.push_back(TagAttribute("id",id));
00688 _attributes.push_back(TagAttribute("name",name));
00689 }
00690 };
00691
00692 class RotateTag : public XMLLeafTag
00693 {
00694 public:
00695 RotateTag(const QString& sid,const vcg::Point4f& p)
00696 :XMLLeafTag("rotate")
00697 {
00698 _attributes.push_back(TagAttribute("sid",sid));
00699
00700 for(unsigned int ii =0;ii < 4; ++ii)
00701 _text.push_back(QString::number(p[ii]));
00702 }
00703 };
00704
00705 class TranslateTag : public XMLLeafTag
00706 {
00707 public:
00708 TranslateTag(const QString& sid,const vcg::Point4f& p)
00709 :XMLLeafTag("translate")
00710 {
00711 _attributes.push_back(TagAttribute("sid",sid));
00712
00713 for(unsigned int ii =0;ii < 4; ++ii)
00714 _text.push_back(QString::number(p[ii]));
00715 }
00716 };
00717
00718 class InstanceGeometryTag : public XMLTag
00719 {
00720 public:
00721 InstanceGeometryTag(const QString& url)
00722 :XMLTag("instance_geometry")
00723 {
00724 _attributes.push_back(TagAttribute("url",testSharp(url)));
00725 }
00726 };
00727
00728 class BindMaterialTag : public XMLTag
00729 {
00730 public:
00731 BindMaterialTag()
00732 :XMLTag("bind_material")
00733 {
00734 }
00735 };
00736
00737 class InstanceMaterialTag : public XMLTag
00738 {
00739 public:
00740 InstanceMaterialTag(const QString& symbol,const QString& target)
00741 :XMLTag("instance_material")
00742 {
00743 _attributes.push_back(TagAttribute("symbol",symbol));
00744 _attributes.push_back(TagAttribute("target",testSharp(target)));
00745 }
00746 };
00747
00748 class BindVertexInputTag : public XMLTag
00749 {
00750 public:
00751 BindVertexInputTag(const QString& semantic,const QString& input_semantic,const QString& input_set)
00752 :XMLTag("bind_vertex_input")
00753 {
00754 _attributes.push_back(TagAttribute("semantic",semantic));
00755 _attributes.push_back(TagAttribute("input_semantic",input_semantic));
00756 }
00757 };
00758
00759 class SceneTag : public XMLTag
00760 {
00761 public:
00762 SceneTag()
00763 :XMLTag("scene")
00764 {
00765 }
00766 };
00767
00768 class CreatedTag : public XMLLeafTag
00769 {
00770 public:
00771 CreatedTag()
00772 :XMLLeafTag("created")
00773 {
00774 QDateTime dateCreated = QDateTime::currentDateTime().toUTC();
00775 QString dateCreatedStr = dateCreated.toString();
00776 _text.push_back(dateCreatedStr);
00777 }
00778 };
00779
00780 class ModifiedTag : public XMLLeafTag
00781 {
00782 public:
00783 ModifiedTag()
00784 :XMLLeafTag("modified")
00785 {
00786 QDateTime dateModified = QDateTime::currentDateTime().toUTC();
00787 QString dateModifiedStr = dateModified.toString();
00788 _text.push_back(dateModifiedStr);
00789 }
00790 };
00791
00792 class InstanceVisualSceneTag : public XMLTag
00793 {
00794 public:
00795 InstanceVisualSceneTag(const QString& url)
00796 :XMLTag("instance_visual_scene")
00797 {
00798 _attributes.push_back(TagAttribute("url",testSharp(url)));
00799 }
00800 };
00801 }
00802
00803 class DocumentManager
00804 {
00805 private:
00806 static void connectHierarchyNode(XMLInteriorNode* node0,XMLInteriorNode* node1,XMLLeafNode* leaf)
00807 {
00808 node1->_sons.push_back(leaf);
00809 node0->_sons.push_back(node1);
00810 }
00811
00812 static void connectHierarchyNode(XMLInteriorNode* node0,XMLInteriorNode* node1,XMLInteriorNode* node2,XMLInteriorNode* node3,XMLNode* node4)
00813 {
00814 node3->_sons.push_back(node4);
00815 node2->_sons.push_back(node3);
00816 node1->_sons.push_back(node2);
00817 node0->_sons.push_back(node1);
00818 }
00819
00820 static void connectHierarchyNode(XMLInteriorNode* node0,XMLInteriorNode* node1,XMLInteriorNode* node2,XMLInteriorNode* node3)
00821 {
00822 node2->_sons.push_back(node3);
00823 node1->_sons.push_back(node2);
00824 node0->_sons.push_back(node1);
00825 }
00826
00827 static void connectHierarchyNode(XMLInteriorNode* node0,XMLInteriorNode* node1,XMLInteriorNode* node2)
00828 {
00829 node1->_sons.push_back(node2);
00830 node0->_sons.push_back(node1);
00831 }
00832
00833 template<typename MESHMODELTYPE>
00834 static void splitMeshInTexturedPatches(const MESHMODELTYPE& m,QVector<QVector<int> >& patches)
00835 {
00836 patches.resize(m.textures.size());
00837 int cc = 0;
00838 for(typename MESHMODELTYPE::ConstFaceIterator itf = m.face.begin();itf != m.face.end();++itf)
00839 {
00840 int tmp = itf->cWT(0).N();
00841 assert(tmp>=0 && tmp<patches.size());
00842 patches[tmp].push_back(cc);
00843 ++cc;
00844 }
00845 }
00846
00847 public:
00848 template<typename MESHMODELTYPE>
00849 static XMLDocument* createColladaDocument(const MESHMODELTYPE& m,const int mask)
00850 {
00851
00852 const unsigned int edgefacenum = 3;
00853 typedef XMLInteriorNode XNode;
00854 typedef XMLLeafNode XLeaf;
00855
00856 XNode* root = new XNode(new Tags::ColladaTag());
00857 XNode* assetnode = new XNode(new Tags::AssetTag());
00858 XNode* contributornode = new XNode(new Tags::ContributorTag());
00859 contributornode->_sons.push_back(new XLeaf(new Tags::AuthorTag()));
00860 contributornode->_sons.push_back(new XLeaf(new Tags::AuthoringToolTag()));
00861
00862 assetnode->_sons.push_back(contributornode);
00863 assetnode->_sons.push_back(new XLeaf(new Tags::UpAxisTag()));
00864 assetnode->_sons.push_back(new XLeaf(new Tags::CreatedTag()));
00865 assetnode->_sons.push_back(new XLeaf(new Tags::ModifiedTag()));
00866 root->_sons.push_back(assetnode);
00867
00868 XNode* libimages = new XNode(new Tags::LibraryImagesTag());
00869 for(unsigned int ii = 0;ii < m.textures.size();++ii)
00870 {
00871 QString subfix = QString::number(ii);
00872 XNode* imagenode = new XNode(new Tags::ImageTag(QString("texture") + subfix,QString("texture") + subfix));
00873 XLeaf* initfromnode = new XLeaf(new Tags::InitFromTag(QString::fromStdString(m.textures[ii])));
00874 imagenode->_sons.push_back(initfromnode);
00875 libimages->_sons.push_back(imagenode);
00876 }
00877 root->_sons.push_back(libimages);
00878
00879 XNode* libmaterials = new XNode(new Tags::LibraryMaterialsTag());
00880 for(unsigned int ii = 0;ii < m.textures.size();++ii)
00881 {
00882 QString subfix = QString::number(ii);
00883 QString mat = "material" + subfix;
00884 XNode* materialnode = new XNode(new Tags::MaterialTag(mat,mat));
00885 XLeaf* instanceeff = new XLeaf(new Tags::InstanceEffectTag(mat+"-fx"));
00886 materialnode->_sons.push_back(instanceeff);
00887 libmaterials->_sons.push_back(materialnode);
00888 }
00889 root->_sons.push_back(libmaterials);
00890
00891 XNode* libeffects = new XNode(new Tags::LibraryEffectsTag());
00892 for(unsigned int ii = 0;ii < m.textures.size();++ii)
00893 {
00894 QString subfix = QString::number(ii);
00895 QString mat = "material" + subfix + "-fx";
00896 XNode* effectnode = new XNode(new Tags::EffectTag(mat));
00897 XNode* procommnode = new XNode(new Tags::ProfileCommonTag());
00898 QString tex = QString("texture")+subfix;
00899 XNode* newparamnode = new XNode(new Tags::NewParamTag(tex+"-surface"));
00900 XNode* surfacenode = new XNode(new Tags::SurfaceTag());
00901 XLeaf* initfromnode = new XLeaf(new Tags::InitFromTag(tex));
00902 QImage img(QString::fromStdString(m.textures[ii]));
00903 QImage::Format f = img.format();
00904 QString form = "R8G8B8";
00905 if (f==QImage::Format_ARGB32)
00906 form = "A8R8G8B8";
00907 XLeaf* formatnode = new XLeaf(new Tags::FormatTag(form));
00908 surfacenode->_sons.push_back(initfromnode);
00909 surfacenode->_sons.push_back(formatnode);
00910 newparamnode->_sons.push_back(surfacenode);
00911 procommnode->_sons.push_back(newparamnode);
00912
00913 XNode* newparamnode2 = new XNode(new Tags::NewParamTag(tex+"-sampler"));
00914 XNode* samplernode = new XNode(new Tags::Sampler2DTag());
00915 XLeaf* sourcenode = new XLeaf(new Tags::SourceTag(tex+"-surface"));
00916 XLeaf* minfilt = new XLeaf(new Tags::MinFilterTag("LINEAR"));
00917 XLeaf* magfilt = new XLeaf(new Tags::MagFilterTag("LINEAR"));
00918 samplernode->_sons.push_back(sourcenode);
00919 samplernode->_sons.push_back(minfilt);
00920 samplernode->_sons.push_back(magfilt);
00921 newparamnode2->_sons.push_back(samplernode);
00922 procommnode->_sons.push_back(newparamnode2);
00923
00924 XNode* technode = new XNode(new Tags::TechniqueTag("common"));
00925 XNode* blinnnode = new XNode(new Tags::BlinnTag());
00926
00927 XNode* emissionnode = new XNode(new Tags::EmissionTag());
00928 XLeaf* colorem = new XLeaf(new Tags::ColorTag(0,0,0,1));
00929 XNode* ambientnode = new XNode(new Tags::AmbientTag());
00930 XLeaf* coloramb = new XLeaf(new Tags::ColorTag(0,0,0,1));
00931 XNode* diffusenode = new XNode(new Tags::DiffuseTag());
00932 XLeaf* texturenode = new XLeaf(new Tags::TextureTag(tex,"UVSET0"));
00933 XNode* specularnode = new XNode(new Tags::SpecularTag());
00934 XLeaf* colorspec = new XLeaf(new Tags::ColorTag(0,0,0,1));
00935 XNode* shinenode = new XNode(new Tags::ShininessTag());
00936 XLeaf* floatshine = new XLeaf(new Tags::FloatTag(0.3));
00937 XNode* reflectivenode = new XNode(new Tags::ReflectiveTag());
00938 XLeaf* colorref = new XLeaf(new Tags::ColorTag(0,0,0,1));
00939 XNode* reflectivitynode = new XNode(new Tags::ReflectivityTag());
00940 XLeaf* floatrefl = new XLeaf(new Tags::FloatTag(0.5));
00941 XNode* transparentnode = new XNode(new Tags::TransparentTag());
00942 XLeaf* colortra = new XLeaf(new Tags::ColorTag(0,0,0,1));
00943 XNode* transparencynode = new XNode(new Tags::TransparencyTag());
00944 XLeaf* floattra = new XLeaf(new Tags::FloatTag(0.0));
00945 XNode* refranode = new XNode(new Tags::IndexOfRefractionTag());
00946 XLeaf* floatrefrac = new XLeaf(new Tags::FloatTag(0.0));
00947
00948 connectHierarchyNode(blinnnode,emissionnode,colorem);
00949 connectHierarchyNode(blinnnode,ambientnode,coloramb);
00950 connectHierarchyNode(blinnnode,diffusenode,texturenode);
00951 connectHierarchyNode(blinnnode,specularnode,colorspec);
00952 connectHierarchyNode(blinnnode,shinenode,floatshine);
00953 connectHierarchyNode(blinnnode,reflectivenode,colorref);
00954 connectHierarchyNode(blinnnode,reflectivitynode,floatrefl);
00955 connectHierarchyNode(blinnnode,transparentnode,colortra);
00956 connectHierarchyNode(blinnnode,transparencynode,floattra);
00957 connectHierarchyNode(blinnnode,refranode,floatrefrac);
00958
00959 connectHierarchyNode(procommnode,technode,blinnnode);
00960
00961 effectnode->_sons.push_back(procommnode);
00962 libeffects->_sons.push_back(effectnode);
00963 }
00964 root->_sons.push_back(libeffects);
00965
00966 XNode* libgeo = new XNode(new Tags::LibraryGeometriesTag());
00967 QString subfix = "0";
00968 QString shape = "shape" + subfix;
00969 XNode* geometrynode = new XNode(new Tags::GeometryTag(shape+"-lib",shape));
00970 XNode* meshnode = new XNode(new Tags::MeshTag());
00971 XNode* sourcepos = new XNode(new Tags::SourceTag(shape+"-lib-positions","position"));
00972
00973 unsigned int return_component_number = CoordNumber<typename MESHMODELTYPE::CoordType>::coord();
00974 XLeaf* floatarr = new XLeaf(new Tags::FloatArrayTag(shape+"-lib-positions-array",m.vert.size() * return_component_number,m,Tags::FloatArrayTag::VERTPOSITION,return_component_number));
00975 XNode* techcommnode = new XNode(new Tags::TechniqueCommonTag());
00976 XNode* accessornode = new XNode(new Tags::AccessorTag(m.vert.size(),shape+"-lib-positions-array",return_component_number));
00977 XNode* paramx = new XNode(new Tags::ParamTag("X","float"));
00978 XNode* paramy = new XNode(new Tags::ParamTag("Y","float"));
00979 XNode* paramz = new XNode(new Tags::ParamTag("Z","float"));
00980
00981 sourcepos->_sons.push_back(floatarr);
00982 accessornode->_sons.push_back(paramx);
00983 accessornode->_sons.push_back(paramy);
00984 accessornode->_sons.push_back(paramz);
00985
00986 techcommnode->_sons.push_back(accessornode);
00987 sourcepos->_sons.push_back(techcommnode);
00988
00989 meshnode->_sons.push_back(sourcepos);
00990
00991
00992
00993 bool normalmask = bool((mask & vcg::tri::io::Mask::IOM_FACENORMAL) || (mask & vcg::tri::io::Mask::IOM_WEDGNORMAL) || (mask & vcg::tri::io::Mask::IOM_VERTNORMAL));
00994 if (normalmask)
00995 {
00996 XNode* sourcenormal = new XNode(new Tags::SourceTag(shape+"-lib-normals","normal"));
00997
00998
00999 XLeaf* floatnormarr = new XLeaf(new Tags::FloatArrayTag(shape+"-lib-normals-array",m.face.size() * return_component_number,m,Tags::FloatArrayTag::FACENORMAL,return_component_number));
01000 XNode* techcommnormnode = new XNode(new Tags::TechniqueCommonTag());
01001 XNode* accessornormnode = new XNode(new Tags::AccessorTag(m.face.size(),shape+"-lib-normals-array",return_component_number));
01002
01003
01004 XNode* paramnormx = new XNode(new Tags::ParamTag("X","float"));
01005 XNode* paramnormy = new XNode(new Tags::ParamTag("Y","float"));
01006 XNode* paramnormz = new XNode(new Tags::ParamTag("Z","float"));
01007
01008 sourcenormal->_sons.push_back(floatnormarr);
01009
01010 accessornormnode->_sons.push_back(paramnormx);
01011 accessornormnode->_sons.push_back(paramnormy);
01012 accessornormnode->_sons.push_back(paramnormz);
01013
01014 techcommnormnode->_sons.push_back(accessornormnode);
01015 sourcenormal->_sons.push_back(techcommnormnode);
01016
01017 meshnode->_sons.push_back(sourcenormal);
01018 }
01019
01020 bool texmask = bool(mask & vcg::tri::io::Mask::IOM_WEDGTEXCOORD);
01021 if (texmask)
01022 {
01023 XNode* sourcewedge = new XNode(new Tags::SourceTag(shape+"-lib-map","map"));
01024 return_component_number = CoordNumber<typename MESHMODELTYPE::FaceType::TexCoordType::PointType>::coord();
01025
01026 XLeaf* floatwedgearr = new XLeaf(new Tags::FloatArrayTag(shape+"-lib-map-array",m.face.size() * return_component_number * edgefacenum,m,Tags::FloatArrayTag::WEDGETEXCOORD,return_component_number));
01027 XNode* techcommwedgenode = new XNode(new Tags::TechniqueCommonTag());
01028 XNode* accessorwedgenode = new XNode(new Tags::AccessorTag(m.face.size() * edgefacenum,shape+"-lib-map-array",return_component_number));
01029
01030
01031 XNode* paramwedgeu = new XNode(new Tags::ParamTag("U","float"));
01032 XNode* paramwedgev = new XNode(new Tags::ParamTag("V","float"));
01033
01034 sourcewedge->_sons.push_back(floatwedgearr);
01035
01036 accessorwedgenode->_sons.push_back(paramwedgeu);
01037 accessorwedgenode->_sons.push_back(paramwedgev);
01038
01039 techcommwedgenode->_sons.push_back(accessorwedgenode);
01040 sourcewedge->_sons.push_back(techcommwedgenode);
01041
01042 meshnode->_sons.push_back(sourcewedge);
01043 }
01044
01045 XNode* vertnode = new XNode(new Tags::VerticesTag(shape+"-lib-vertices"));
01046 XNode* inputposnode = new XNode(new Tags::InputTag("POSITION",shape+"-lib-positions"));
01047 vertnode->_sons.push_back(inputposnode);
01048 meshnode->_sons.push_back(vertnode);
01049 XNode* trianglesnode = NULL;
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068 QVector<QVector<int> > mytripatches;
01069 if ((texmask) && (m.textures.size() != 0))
01070 splitMeshInTexturedPatches(m,mytripatches);
01071
01072 QVector<QVector<int> >::iterator itp = mytripatches.begin();
01073 int indmat = 0;
01074 do
01075 {
01076 if ((m.textures.size() == 0) || (!texmask))
01077 {
01078
01079 trianglesnode = new XNode(new Tags::TrianglesTag(m.face.size()));
01080 }
01081 else
01082 trianglesnode = new XNode(new Tags::TrianglesTag(mytripatches[indmat].size(),"material" + QString::number(indmat)));
01083
01084 XNode* inputtrinode = new XNode(new Tags::InputTag(0,"VERTEX",shape+"-lib-vertices"));
01085 trianglesnode->_sons.push_back(inputtrinode);
01086 if (normalmask)
01087 {
01088 XNode* inputnormnode = new XNode(new Tags::InputTag(1,"NORMAL",shape+"-lib-normals"));
01089 trianglesnode->_sons.push_back(inputnormnode);
01090 }
01091
01092 if (texmask)
01093 {
01094 XNode* inputwedgenode = new XNode(new Tags::InputTag(2,"TEXCOORD",shape+"-lib-map"));
01095 trianglesnode->_sons.push_back(inputwedgenode);
01096 }
01097
01098 XLeaf* polyleaf = NULL;
01099 if (itp == mytripatches.end())
01100 polyleaf = new XLeaf(new Tags::PTag(m,edgefacenum,normalmask,texmask));
01101 else
01102 polyleaf = new XLeaf(new Tags::PTag(m,edgefacenum,(*itp),normalmask,texmask));
01103
01104 trianglesnode->_sons.push_back(polyleaf);
01105 meshnode->_sons.push_back(trianglesnode);
01106
01107 ++indmat;
01108 if (itp != mytripatches.end())
01109 ++itp;
01110 }while(itp != mytripatches.end());
01111
01112 connectHierarchyNode(libgeo,geometrynode,meshnode);
01113 root->_sons.push_back(libgeo);
01114
01115 XNode* libvisualnode = new XNode(new Tags::LibraryVisualScenesTag());
01116 XNode* visualscenenode = new XNode(new Tags::VisualSceneTag("VisualSceneNode","VisualScene"));
01117 XNode* nodenode = new XNode(new Tags::NodeTag("node","node"));
01118 XNode* instgeonode = new XNode(new Tags::InstanceGeometryTag(shape+"-lib"));
01119 XNode* bindnode = new XNode(new Tags::BindMaterialTag());
01120 XNode* techcommmatnode = new XNode(new Tags::TechniqueCommonTag());
01121 for(unsigned int ii = 0; ii < m.textures.size(); ++ii)
01122 {
01123 XNode* instmatnode = new XNode(new Tags::InstanceMaterialTag("material" + QString::number(ii),"material" + QString::number(ii)));
01124 XNode* bindvertnode = new XNode(new Tags::BindVertexInputTag("UVSET0","TEXCOORD","0"));
01125 connectHierarchyNode(techcommmatnode,instmatnode,bindvertnode);
01126 }
01127
01128 connectHierarchyNode(visualscenenode,nodenode,instgeonode,bindnode,techcommmatnode);
01129 libvisualnode->_sons.push_back(visualscenenode);
01130 root->_sons.push_back(libvisualnode);
01131
01132 XNode* scenenode = new XNode(new Tags::SceneTag());
01133 XNode* instvisualscenenode = new XNode(new Tags::InstanceVisualSceneTag("VisualSceneNode"));
01134
01135 scenenode->_sons.push_back(instvisualscenenode);
01136 root->_sons.push_back(scenenode);
01137
01138 return new XMLDocument(root);
01139 }
01140
01141 static void destroyColladaDocument(XMLDocument* doc)
01142 {
01143 delete doc;
01144 }
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157 };
01158 }
01159 #endif