Blender V2.61 - r43446

MeshImporter.h

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