Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MeshImporter.h « collada « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5ad2fb17fcefd0f0a9ebc3d463bf03d37a08b1db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup collada
 */

#ifndef __MESHIMPORTER_H__
#define __MESHIMPORTER_H__

#include <map>
#include <vector>

#include "COLLADAFWIndexList.h"
#include "COLLADAFWInstanceGeometry.h"
#include "COLLADAFWMaterialBinding.h"
#include "COLLADAFWMesh.h"
#include "COLLADAFWMeshVertexData.h"
#include "COLLADAFWNode.h"
#include "COLLADAFWPolygons.h"
#include "COLLADAFWTextureCoordinateBinding.h"
#include "COLLADAFWTypes.h"
#include "COLLADAFWUniqueId.h"

#include "ArmatureImporter.h"
#include "collada_utils.h"

extern "C" {
#include "BLI_edgehash.h"
#include "DNA_material_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
}

/* only for ArmatureImporter to "see" MeshImporter::get_object_by_geom_uid */
class MeshImporterBase {
 public:
  virtual Object *get_object_by_geom_uid(const COLLADAFW::UniqueId &geom_uid) = 0;
  virtual Mesh *get_mesh_by_geom_uid(const COLLADAFW::UniqueId &mesh_uid) = 0;
  virtual std::string *get_geometry_name(const std::string &mesh_name) = 0;
};

class UVDataWrapper {
  COLLADAFW::MeshVertexData *mVData;

 public:
  UVDataWrapper(COLLADAFW::MeshVertexData &vdata);

#ifdef COLLADA_DEBUG
  void print();
#endif

  void getUV(int uv_index, float *uv);
};

class VCOLDataWrapper {
  COLLADAFW::MeshVertexData *mVData;

 public:
  VCOLDataWrapper(COLLADAFW::MeshVertexData &vdata);
  void get_vcol(int v_index, MLoopCol *mloopcol);
};

class MeshImporter : public MeshImporterBase {
 private:
  UnitConverter *unitconverter;

  Main *m_bmain;
  Scene *scene;
  ViewLayer *view_layer;

  ArmatureImporter *armature_importer;

  std::map<std::string, std::string> mesh_geom_map;       /* needed for correct shape key naming */
  std::map<COLLADAFW::UniqueId, Mesh *> uid_mesh_map;     /* geometry unique id-to-mesh map */
  std::map<COLLADAFW::UniqueId, Object *> uid_object_map; /* geom uid-to-object */
  std::vector<Object *> imported_objects;                 /* list of imported objects */

  /* this structure is used to assign material indices to polygons
   * it holds a portion of Mesh faces and corresponds to a DAE primitive list
   * (<triangles>, <polylist>, etc.) */
  struct Primitive {
    MPoly *mpoly;
    unsigned int totpoly;
  };
  typedef std::map<COLLADAFW::MaterialId, std::vector<Primitive>> MaterialIdPrimitiveArrayMap;
  /* crazy name! */
  std::map<COLLADAFW::UniqueId, MaterialIdPrimitiveArrayMap> geom_uid_mat_mapping_map;
  /* < materials that have already been mapped to a geometry.
   * A pair/of geom uid and mat uid, one geometry can have several materials */
  std::multimap<COLLADAFW::UniqueId, COLLADAFW::UniqueId> materials_mapped_to_geom;

  bool set_poly_indices(
      MPoly *mpoly, MLoop *mloop, int loop_index, unsigned int *indices, int loop_count);

  void set_face_uv(MLoopUV *mloopuv,
                   UVDataWrapper &uvs,
                   int loop_index,
                   COLLADAFW::IndexList &index_list,
                   int count);

  void set_vcol(MLoopCol *mloopcol,
                VCOLDataWrapper &vob,
                int loop_index,
                COLLADAFW::IndexList &index_list,
                int count);

#ifdef COLLADA_DEBUG
  void print_index_list(COLLADAFW::IndexList &index_list);
#endif

  bool is_nice_mesh(COLLADAFW::Mesh *mesh);

  void read_vertices(COLLADAFW::Mesh *mesh, Mesh *me);

  bool primitive_has_useable_normals(COLLADAFW::MeshPrimitive *mp);
  bool primitive_has_faces(COLLADAFW::MeshPrimitive *mp);

  static void mesh_add_edges(Mesh *mesh, int len);

  unsigned int get_loose_edge_count(COLLADAFW::Mesh *mesh);

  CustomData create_edge_custom_data(EdgeHash *eh);

  void allocate_poly_data(COLLADAFW::Mesh *collada_mesh, Mesh *me);

  /* TODO: import uv set names */
  void read_polys(COLLADAFW::Mesh *mesh, Mesh *me);
  void read_lines(COLLADAFW::Mesh *mesh, Mesh *me);
  unsigned int get_vertex_count(COLLADAFW::Polygons *mp, int index);

  void get_vector(float v[3], COLLADAFW::MeshVertexData &arr, int i, int stride);

  bool is_flat_face(unsigned int *nind, COLLADAFW::MeshVertexData &nor, int count);

  std::vector<Object *> get_all_users_of(Mesh *reference_mesh);

 public:
  MeshImporter(UnitConverter *unitconv,
               ArmatureImporter *arm,
               Main *bmain,
               Scene *sce,
               ViewLayer *view_layer);

  virtual Object *get_object_by_geom_uid(const COLLADAFW::UniqueId &geom_uid);

  virtual Mesh *get_mesh_by_geom_uid(const COLLADAFW::UniqueId &geom_uid);

  void optimize_material_assignements();

  void assign_material_to_geom(COLLADAFW::MaterialBinding cmaterial,
                               std::map<COLLADAFW::UniqueId, Material *> &uid_material_map,
                               Object *ob,
                               const COLLADAFW::UniqueId *geom_uid,
                               short mat_index);

  Object *create_mesh_object(COLLADAFW::Node *node,
                             COLLADAFW::InstanceGeometry *geom,
                             bool isController,
                             std::map<COLLADAFW::UniqueId, Material *> &uid_material_map);

  /* create a mesh storing a pointer in a map so it can be retrieved later by geometry UID */
  bool write_geometry(const COLLADAFW::Geometry *geom);
  std::string *get_geometry_name(const std::string &mesh_name);
};

#endif