1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup collada
19  */
20 
21 #pragma once
22 
23 #include <set>
24 #include <string>
25 #include <vector>
26 
27 #include "COLLADASWInputList.h"
28 #include "COLLADASWLibraryGeometries.h"
29 #include "COLLADASWStreamWriter.h"
30 
31 #include "DNA_key_types.h"
32 #include "DNA_mesh_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_scene_types.h"
35 
36 #include "BKE_key.h"
37 #include "BlenderContext.h"
38 #include "ExportSettings.h"
39 #include "collada_utils.h"
40 
41 class Normal {
42  public:
43   float x;
44   float y;
45   float z;
46 
47   friend bool operator<(const Normal &, const Normal &);
48 };
49 
50 bool operator<(const Normal &, const Normal &);
51 
52 /* TODO: optimize UV sets by making indexed list with duplicates removed */
53 class GeometryExporter : COLLADASW::LibraryGeometries {
54   struct Face {
55     unsigned int v1, v2, v3, v4;
56   };
57 
58  public:
59   /* TODO: optimize UV sets by making indexed list with duplicates removed */
GeometryExporter(BlenderContext & blender_context,COLLADASW::StreamWriter * sw,BCExportSettings & export_settings)60   GeometryExporter(BlenderContext &blender_context,
61                    COLLADASW::StreamWriter *sw,
62                    BCExportSettings &export_settings)
63       : COLLADASW::LibraryGeometries(sw),
64         blender_context(blender_context),
65         export_settings(export_settings)
66   {
67   }
68 
69   void exportGeom();
70 
71   void operator()(Object *ob);
72 
73   void createLooseEdgeList(Object *ob, Mesh *me, std::string &geom_id);
74 
75   /* powerful because it handles both cases when there is material and when there's not */
76   void create_mesh_primitive_list(short material_index,
77                                   bool has_uvs,
78                                   bool has_color,
79                                   Object *ob,
80                                   Mesh *me,
81                                   std::string &geom_id,
82                                   std::vector<BCPolygonNormalsIndices> &norind);
83 
84   /* creates <source> for positions */
85   void createVertsSource(std::string geom_id, Mesh *me);
86 
87   void createVertexColorSource(std::string geom_id, Mesh *me);
88 
89   std::string makeTexcoordSourceId(std::string &geom_id, int layer_index, bool is_single_layer);
90 
91   /* creates <source> for texcoords */
92   void createTexcoordsSource(std::string geom_id, Mesh *me);
93   void createTesselatedTexcoordsSource(std::string geom_id, Mesh *me);
94 
95   /* creates <source> for normals */
96   void createNormalsSource(std::string geom_id, Mesh *me, std::vector<Normal> &nor);
97 
98   void create_normals(std::vector<Normal> &nor,
99                       std::vector<BCPolygonNormalsIndices> &polygons_normals,
100                       Mesh *me);
101 
102   std::string getIdBySemantics(std::string geom_id,
103                                COLLADASW::InputSemantic::Semantics type,
104                                std::string other_suffix = "");
105   std::string makeVertexColorSourceId(std::string &geom_id, char *layer_name);
106 
107   COLLADASW::URI getUrlBySemantics(std::string geom_id,
108                                    COLLADASW::InputSemantic::Semantics type,
109                                    std::string other_suffix = "");
110 
111   COLLADASW::URI makeUrl(std::string id);
112 
113   void export_key_mesh(Object *ob, Mesh *me, KeyBlock *kb);
114 
115  private:
116   std::set<std::string> exportedGeometry;
117   BlenderContext &blender_context;
118   BCExportSettings &export_settings;
119 
120   Mesh *get_mesh(Scene *sce, Object *ob, int apply_modifiers);
121 };
122 
123 struct GeometryFunctor {
124   /* f should have
125    * void operator()(Object *ob) */
126   template<class Functor>
forEachMeshObjectInExportSetGeometryFunctor127   void forEachMeshObjectInExportSet(Scene *sce, Functor &f, LinkNode *export_set)
128   {
129     LinkNode *node;
130     for (node = export_set; node; node = node->next) {
131       Object *ob = (Object *)node->link;
132       if (ob->type == OB_MESH) {
133         f(ob);
134       }
135     }
136   }
137 };
138