Blender  V2.59
MeshImporter.h
Go to the documentation of this file.
00001 /*
00002  * $Id: MeshImporter.h 38169 2011-07-06 21:37:31Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00029 #ifndef __BC__MESHIMPORTER_H__
00030 #define __BC__MESHIMPORTER_H__
00031 
00032 #include <map>
00033 #include <vector>
00034 
00035 #include "COLLADAFWIndexList.h"
00036 #include "COLLADAFWInstanceGeometry.h"
00037 #include "COLLADAFWMaterialBinding.h"
00038 #include "COLLADAFWMesh.h"
00039 #include "COLLADAFWMeshVertexData.h"
00040 #include "COLLADAFWNode.h"
00041 #include "COLLADAFWTextureCoordinateBinding.h"
00042 #include "COLLADAFWTypes.h"
00043 #include "COLLADAFWUniqueId.h"
00044 
00045 #include "DNA_material_types.h"
00046 #include "DNA_mesh_types.h"
00047 #include "DNA_meshdata_types.h"
00048 #include "DNA_object_types.h"
00049 #include "DNA_scene_types.h"
00050 #include "DNA_texture_types.h"
00051 
00052 #include "ArmatureImporter.h"
00053 #include "collada_utils.h"
00054 
00055 // only for ArmatureImporter to "see" MeshImporter::get_object_by_geom_uid
00056 class MeshImporterBase
00057 {
00058 public:
00059         virtual Object *get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid) = 0;
00060 };
00061 
00062 class UVDataWrapper
00063 {
00064         COLLADAFW::MeshVertexData *mVData;
00065 public:
00066         UVDataWrapper(COLLADAFW::MeshVertexData& vdata);
00067 
00068 #ifdef COLLADA_DEBUG
00069         void print();
00070 #endif
00071 
00072         void getUV(int uv_index, float *uv);
00073 };
00074 
00075 class MeshImporter : public MeshImporterBase
00076 {
00077 private:
00078 
00079         UnitConverter *unitconverter;
00080 
00081         Scene *scene;
00082         ArmatureImporter *armature_importer;
00083 
00084         std::map<COLLADAFW::UniqueId, Mesh*> uid_mesh_map; // geometry unique id-to-mesh map
00085         std::map<COLLADAFW::UniqueId, Object*> uid_object_map; // geom uid-to-object
00086         // this structure is used to assign material indices to faces
00087         // it holds a portion of Mesh faces and corresponds to a DAE primitive list (<triangles>, <polylist>, etc.)
00088         struct Primitive {
00089                 MFace *mface;
00090                 unsigned int totface;
00091         };
00092         typedef std::map<COLLADAFW::MaterialId, std::vector<Primitive> > MaterialIdPrimitiveArrayMap;
00093         std::map<COLLADAFW::UniqueId, MaterialIdPrimitiveArrayMap> geom_uid_mat_mapping_map; // crazy name!
00094         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
00095         
00096 
00097         void set_face_indices(MFace *mface, unsigned int *indices, bool quad);
00098 
00099         // not used anymore, test_index_face from blenkernel is better
00100 #if 0
00101         // change face indices order so that v4 is not 0
00102         void rotate_face_indices(MFace *mface);
00103 #endif
00104         
00105         void set_face_uv(MTFace *mtface, UVDataWrapper &uvs,
00106                                          COLLADAFW::IndexList& index_list, unsigned int *tris_indices);
00107 
00108         void set_face_uv(MTFace *mtface, UVDataWrapper &uvs,
00109                                         COLLADAFW::IndexList& index_list, int index, bool quad);
00110 
00111 #ifdef COLLADA_DEBUG
00112         void print_index_list(COLLADAFW::IndexList& index_list);
00113 #endif
00114 
00115         bool is_nice_mesh(COLLADAFW::Mesh *mesh);
00116 
00117         void read_vertices(COLLADAFW::Mesh *mesh, Mesh *me);
00118         
00119         int triangulate_poly(unsigned int *indices, int totvert, MVert *verts, std::vector<unsigned int>& tri);
00120         
00121         int count_new_tris(COLLADAFW::Mesh *mesh, Mesh *me);
00122         
00123         // TODO: import uv set names
00124         void read_faces(COLLADAFW::Mesh *mesh, Mesh *me, int new_tris);
00125 
00126         void get_vector(float v[3], COLLADAFW::MeshVertexData& arr, int i, int stride);
00127 
00128         bool flat_face(unsigned int *nind, COLLADAFW::MeshVertexData& nor, int count);
00129         
00130 public:
00131 
00132         MeshImporter(UnitConverter *unitconv, ArmatureImporter *arm, Scene *sce);
00133 
00134         virtual Object *get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid);
00135         
00136         MTex *assign_textures_to_uvlayer(COLLADAFW::TextureCoordinateBinding &ctexture,
00137                                                                          Mesh *me, TexIndexTextureArrayMap& texindex_texarray_map,
00138                                                                          MTex *color_texture);
00139         
00140         MTFace *assign_material_to_geom(COLLADAFW::MaterialBinding cmaterial,
00141                                                                         std::map<COLLADAFW::UniqueId, Material*>& uid_material_map,
00142                                                                         Object *ob, const COLLADAFW::UniqueId *geom_uid, 
00143                                                                         MTex **color_texture, char *layername, MTFace *texture_face,
00144                                                                         std::map<Material*, TexIndexTextureArrayMap>& material_texture_mapping_map, int mat_index);
00145         
00146         
00147         Object *create_mesh_object(COLLADAFW::Node *node, COLLADAFW::InstanceGeometry *geom,
00148                                                            bool isController,
00149                                                            std::map<COLLADAFW::UniqueId, Material*>& uid_material_map,
00150                                                            std::map<Material*, TexIndexTextureArrayMap>& material_texture_mapping_map);
00151 
00152         // create a mesh storing a pointer in a map so it can be retrieved later by geometry UID
00153         bool write_geometry(const COLLADAFW::Geometry* geom);
00154 
00155 };
00156 
00157 #endif