2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory.
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file MeshImporter.h
27 #ifndef __MESHIMPORTER_H__
28 #define __MESHIMPORTER_H__
33 #include "COLLADAFWIndexList.h"
34 #include "COLLADAFWInstanceGeometry.h"
35 #include "COLLADAFWMaterialBinding.h"
36 #include "COLLADAFWMesh.h"
37 #include "COLLADAFWMeshVertexData.h"
38 #include "COLLADAFWNode.h"
39 #include "COLLADAFWTextureCoordinateBinding.h"
40 #include "COLLADAFWTypes.h"
41 #include "COLLADAFWUniqueId.h"
43 #include "ArmatureImporter.h"
44 #include "collada_utils.h"
47 #include "BLI_edgehash.h"
48 #include "DNA_material_types.h"
49 #include "DNA_mesh_types.h"
50 #include "DNA_meshdata_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_texture_types.h"
57 // only for ArmatureImporter to "see" MeshImporter::get_object_by_geom_uid
58 class MeshImporterBase
61 virtual Object *get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid) = 0;
62 virtual Mesh *get_mesh_by_geom_uid(const COLLADAFW::UniqueId& mesh_uid) = 0;
67 COLLADAFW::MeshVertexData *mVData;
69 UVDataWrapper(COLLADAFW::MeshVertexData& vdata);
75 void getUV(int uv_index, float *uv);
78 class MeshImporter : public MeshImporterBase
82 UnitConverter *unitconverter;
85 ArmatureImporter *armature_importer;
87 std::map<COLLADAFW::UniqueId, Mesh*> uid_mesh_map; // geometry unique id-to-mesh map
88 std::map<COLLADAFW::UniqueId, Object*> uid_object_map; // geom uid-to-object
89 std::vector<Object*> imported_objects; // list of imported objects
90 // this structure is used to assign material indices to faces
91 // it holds a portion of Mesh faces and corresponds to a DAE primitive list (<triangles>, <polylist>, etc.)
96 typedef std::map<COLLADAFW::MaterialId, std::vector<Primitive> > MaterialIdPrimitiveArrayMap;
97 std::map<COLLADAFW::UniqueId, MaterialIdPrimitiveArrayMap> geom_uid_mat_mapping_map; // crazy name!
98 std::multimap<COLLADAFW::UniqueId, COLLADAFW::UniqueId> materials_mapped_to_geom; //< materials that have already been mapped to a geometry. A pair of geom uid and mat uid, one geometry can have several materials
101 void set_face_indices(MFace *mface, unsigned int *indices, bool quad);
103 // not used anymore, test_index_face from blenkernel is better
105 // change face indices order so that v4 is not 0
106 void rotate_face_indices(MFace *mface);
109 void set_face_uv(MTFace *mtface, UVDataWrapper &uvs,
110 COLLADAFW::IndexList& index_list, unsigned int *tris_indices);
112 void set_face_uv(MTFace *mtface, UVDataWrapper &uvs,
113 COLLADAFW::IndexList& index_list, int index, bool quad);
116 void print_index_list(COLLADAFW::IndexList& index_list);
119 bool is_nice_mesh(COLLADAFW::Mesh *mesh);
121 void read_vertices(COLLADAFW::Mesh *mesh, Mesh *me);
123 int triangulate_poly(unsigned int *indices, int totvert, MVert *verts, std::vector<unsigned int>& tri);
125 int count_new_tris(COLLADAFW::Mesh *mesh, Mesh *me);
127 bool primitive_has_useable_normals(COLLADAFW::MeshPrimitive *mp);
128 bool primitive_has_faces(COLLADAFW::MeshPrimitive *mp);
130 static void mesh_add_edges(Mesh *mesh, int len);
132 unsigned int get_loose_edge_count(COLLADAFW::Mesh *mesh);
134 CustomData create_edge_custom_data(EdgeHash *eh);
136 void allocate_face_data(COLLADAFW::Mesh *mesh, Mesh *me, int new_tris);
138 // TODO: import uv set names
139 void read_faces(COLLADAFW::Mesh *mesh, Mesh *me, int new_tris);
140 void read_lines(COLLADAFW::Mesh *mesh, Mesh *me);
142 void get_vector(float v[3], COLLADAFW::MeshVertexData& arr, int i, int stride);
144 bool flat_face(unsigned int *nind, COLLADAFW::MeshVertexData& nor, int count);
146 std::vector<Object *> get_all_users_of(Mesh *reference_mesh);
150 MeshImporter(UnitConverter *unitconv, ArmatureImporter *arm, Scene *sce);
152 void bmeshConversion();
154 virtual Object *get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid);
156 virtual Mesh *get_mesh_by_geom_uid(const COLLADAFW::UniqueId& geom_uid);
158 MTex *assign_textures_to_uvlayer(COLLADAFW::TextureCoordinateBinding &ctexture,
159 Mesh *me, TexIndexTextureArrayMap& texindex_texarray_map,
160 MTex *color_texture);
162 void optimize_material_assignements();
164 MTFace *assign_material_to_geom(COLLADAFW::MaterialBinding cmaterial,
165 std::map<COLLADAFW::UniqueId, Material*>& uid_material_map,
166 Object *ob, const COLLADAFW::UniqueId *geom_uid,
167 MTex **color_texture, char *layername, MTFace *texture_face,
168 std::map<Material*, TexIndexTextureArrayMap>& material_texture_mapping_map, short mat_index);
171 Object *create_mesh_object(COLLADAFW::Node *node, COLLADAFW::InstanceGeometry *geom,
173 std::map<COLLADAFW::UniqueId, Material*>& uid_material_map,
174 std::map<Material*, TexIndexTextureArrayMap>& material_texture_mapping_map);
176 // create a mesh storing a pointer in a map so it can be retrieved later by geometry UID
177 bool write_geometry(const COLLADAFW::Geometry* geom);