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

obj_export_mesh.hh « exporter « wavefront_obj « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e6cf6383a9d403ef78a442550fc072d8f5bed99 (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
/*
 * 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.
 *
 * The Original Code is Copyright (C) 2020 Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup obj
 */

#pragma once

#include <optional>

#include "BLI_float3.hh"
#include "BLI_utility_mixins.hh"
#include "BLI_vector.hh"

#include "bmesh.h"
#include "bmesh_tools.h"

#include "DNA_material_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"

#include "IO_wavefront_obj.h"

namespace blender::io::obj {
/* Denote absence for usually non-negative numbers. */
const int NOT_FOUND = -1;
/* Any negative number other than `NOT_FOUND` to initialise usually non-negative numbers. */
const int NEGATIVE_INIT = -10;

/**
 * #std::unique_ptr deleter for BMesh.
 */
struct CustomBMeshDeleter {
  void operator()(BMesh *bmesh)
  {
    if (bmesh) {
      BM_mesh_free(bmesh);
    }
  }
};

using unique_bmesh_ptr = std::unique_ptr<BMesh, CustomBMeshDeleter>;

class OBJMesh : NonCopyable {
 private:
  Object *export_object_eval_;
  Mesh *export_mesh_eval_;
  /**
   * For curves which are converted to mesh, and triangulated meshes, a new mesh is allocated.
   */
  bool mesh_eval_needs_free_ = false;
  /**
   * Final transform of an object obtained from export settings (up_axis, forward_axis) and the
   * object's world transform matrix.
   */
  float world_and_axes_transform_[4][4];

  /**
   * Total UV vertices in a mesh's texture map.
   */
  int tot_uv_vertices_ = 0;
  /**
   * Per-polygon-per-vertex UV vertex indices.
   */
  Vector<Vector<int>> uv_indices_;
  /**
   * Total smooth groups in an object.
   */
  int tot_smooth_groups_ = NEGATIVE_INIT;
  /**
   * Polygon aligned array of their smooth groups.
   */
  int *poly_smooth_groups_ = nullptr;

 public:
  OBJMesh(Depsgraph *depsgraph, const OBJExportParams &export_params, Object *mesh_object);
  ~OBJMesh();

  int tot_vertices() const;
  int tot_polygons() const;
  int tot_uv_vertices() const;
  int tot_edges() const;

  int16_t tot_materials() const;
  const Material *get_object_material(const int16_t mat_nr) const;
  int16_t ith_poly_matnr(const int poly_index) const;

  void ensure_mesh_normals() const;
  void ensure_mesh_edges() const;

  void calc_smooth_groups(const bool use_bitflags);
  int ith_smooth_group(const int poly_index) const;
  bool is_ith_poly_smooth(const int poly_index) const;

  const char *get_object_name() const;
  const char *get_object_mesh_name() const;
  const char *get_object_material_name(const int16_t mat_nr) const;

  float3 calc_vertex_coords(const int vert_index, const float scaling_factor) const;
  Vector<int> calc_poly_vertex_indices(const int poly_index) const;
  void store_uv_coords_and_indices(Vector<std::array<float, 2>> &r_uv_coords);
  Span<int> calc_poly_uv_indices(const int poly_index) const;
  float3 calc_poly_normal(const int poly_index) const;
  std::pair<int, Vector<int>> calc_poly_normal_indices(const int poly_index,
                                                       const int object_tot_prev_normals) const;
  void calc_loop_normals(const int poly_index, Vector<float3> &r_loop_normals) const;
  int16_t get_poly_deform_group_index(const int poly_index) const;
  const char *get_poly_deform_group_name(const int16_t def_group_index) const;

  std::optional<std::array<int, 2>> calc_loose_edge_vert_indices(const int edge_index) const;

 private:
  void free_mesh_if_needed();
  std::pair<Mesh *, bool> triangulate_mesh_eval();
  void set_world_axes_transform(const eTransformAxisForward forward, const eTransformAxisUp up);
};
}  // namespace blender::io::obj