changes for materials to treat them as shorts not int/chars (since they are stored...
[blender.git] / source / blender / collada / GeometryExporter.h
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
21  *                 Nathan Letwory
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file GeometryExporter.h
27  *  \ingroup collada
28  */
29
30 #ifndef __GEOMETRYEXPORTER_H__
31 #define __GEOMETRYEXPORTER_H__
32
33 #include <string>
34 #include <vector>
35 #include <set>
36
37 #include "COLLADASWStreamWriter.h"
38 #include "COLLADASWLibraryGeometries.h"
39 #include "COLLADASWInputList.h"
40
41 #include "DNA_mesh_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44
45 #include "ExportSettings.h"
46
47 // TODO: optimize UV sets by making indexed list with duplicates removed
48 class GeometryExporter : COLLADASW::LibraryGeometries
49 {
50         struct Face
51         {
52                 unsigned int v1, v2, v3, v4;
53         };
54
55         struct Normal
56         {
57                 float x, y, z;
58         };
59
60         Scene *mScene;
61
62 public:
63         GeometryExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings);
64
65         void exportGeom(Scene *sce);
66
67         void operator()(Object *ob);
68
69         // powerful because it handles both cases when there is material and when there's not
70         void createPolylist(short material_index,
71                                                 bool has_uvs,
72                                                 bool has_color,
73                                                 Object *ob,
74                                                 std::string& geom_id,
75                                                 std::vector<Face>& norind);
76         
77         // creates <source> for positions
78         void createVertsSource(std::string geom_id, Mesh *me);
79
80         void createVertexColorSource(std::string geom_id, Mesh *me);
81
82         std::string makeTexcoordSourceId(std::string& geom_id, int layer_index);
83
84         //creates <source> for texcoords
85         void createTexcoordsSource(std::string geom_id, Mesh *me);
86
87         //creates <source> for normals
88         void createNormalsSource(std::string geom_id, Mesh *me, std::vector<Normal>& nor);
89
90         void create_normals(std::vector<Normal> &nor, std::vector<Face> &ind, Mesh *me);
91         
92         std::string getIdBySemantics(std::string geom_id, COLLADASW::InputSemantic::Semantics type, std::string other_suffix = "");
93         
94         COLLADASW::URI getUrlBySemantics(std::string geom_id, COLLADASW::InputSemantic::Semantics type, std::string other_suffix = "");
95
96         COLLADASW::URI makeUrl(std::string id);
97         
98         /* int getTriCount(MFace *faces, int totface);*/
99 private:
100         std::set<std::string> exportedGeometry;
101         
102         const ExportSettings *export_settings;
103 };
104
105 struct GeometryFunctor {
106         // f should have
107         // void operator()(Object* ob)
108         template<class Functor>
109         void forEachMeshObjectInScene(Scene *sce, Functor &f, bool export_selected)
110         {
111                 
112                 Base *base= (Base*) sce->base.first;
113                 while(base) {
114                         Object *ob = base->object;
115                         
116                         if (ob->type == OB_MESH && ob->data
117                                 && !(export_selected && !(ob->flag && SELECT))
118                                 && ((sce->lay & ob->lay)!=0)) {
119                                 f(ob);
120                         }
121                         base= base->next;
122                         
123                 }
124         }
125 };
126
127 #endif