Blender  V2.93
BKE_subdiv_ccg.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2018 by Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #pragma once
25 
26 #include "BKE_DerivedMesh.h"
27 #include "BKE_customdata.h"
28 #include "BLI_bitmap.h"
29 #include "BLI_sys_types.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 struct CCGElem;
36 struct CCGFace;
37 struct CCGKey;
38 struct DMFlagMat;
39 struct Mesh;
40 struct Subdiv;
41 
42 /* --------------------------------------------------------------------
43  * Masks.
44  */
45 
46 /* Functor which evaluates mask value at a given (u, v) of given ptex face. */
47 typedef struct SubdivCCGMaskEvaluator {
48  float (*eval_mask)(struct SubdivCCGMaskEvaluator *mask_evaluator,
49  const int ptex_face_index,
50  const float u,
51  const float v);
52 
53  /* Free the data, not the evaluator itself. */
54  void (*free)(struct SubdivCCGMaskEvaluator *mask_evaluator);
55 
56  void *user_data;
58 
59 /* Return true if mesh has mask and evaluator can be used. */
61  const struct Mesh *mesh);
62 
63 /* --------------------------------------------------------------------
64  * Materials.
65  */
66 
67 /* Functor which evaluates material and flags of a given coarse face. */
70  struct SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator,
71  const int coarse_face_index);
72 
73  /* Free the data, not the evaluator itself. */
74  void (*free)(struct SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator);
75 
76  void *user_data;
78 
80  SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator, const struct Mesh *mesh);
81 
82 /* --------------------------------------------------------------------
83  * SubdivCCG.
84  */
85 
86 typedef struct SubdivToCCGSettings {
87  /* Resolution at which regular ptex (created for quad polygon) are being
88  * evaluated. This defines how many vertices final mesh will have: every
89  * regular ptex has resolution^2 vertices. Special (irregular, or ptex
90  * created for a corner of non-quad polygon) will have resolution of
91  * `resolution - 1`. */
93  /* Denotes which extra layers to be added to CCG elements. */
95  bool need_mask;
97 
98 typedef struct SubdivCCGCoord {
99  /* Index of the grid within SubdivCCG::grids array. */
101 
102  /* Coordinate within the grid. */
103  short x, y;
105 
106 /* This is actually a coarse face, which consists of multiple CCG grids. */
107 typedef struct SubdivCCGFace {
108  /* Total number of grids in this face.
109  *
110  * This 1:1 corresponds to a number of corners (or loops) from a coarse
111  * face. */
113  /* Index of first grid from this face in SubdivCCG->grids array. */
116 
117 /* Definition of an edge which is adjacent to at least one of the faces. */
118 typedef struct SubdivCCGAdjacentEdge {
120  /* Indexed by adjacent face index, then by point index on the edge.
121  * points to a coordinate into the grids. */
124 
125 /* Definition of a vertex which is adjacent to at least one of the faces. */
126 typedef struct SubdivCCGAdjacentVertex {
128  /* Indexed by adjacent face index, points to a coordinate in the grids. */
131 
132 /* Representation of subdivision surface which uses CCG grids. */
133 typedef struct SubdivCCG {
134  /* This is a subdivision surface this CCG was created for.
135  *
136  * TODO(sergey): Make sure the whole descriptor is valid, including all the
137  * displacement attached to the surface. */
138  struct Subdiv *subdiv;
139  /* A level at which geometry was subdivided. This is what defines grid
140  * resolution. It is NOT the topology refinement level. */
141  int level;
142  /* Resolution of grid. All grids have matching resolution, and resolution
143  * is same as ptex created for non-quad polygons. */
145  /* Size of a single element of a grid (including coordinate and all the other layers).
146  * Measured in bytes. */
148  /* Grids represent limit surface, with displacement applied. Grids are
149  * corresponding to face-corners of coarse mesh, each grid has
150  * grid_size^2 elements.
151  */
152  /* Indexed by a grid index, points to a grid data which is stored in
153  * grids_storage. */
154  struct CCGElem **grids;
155  /* Flat array of all grids' data. */
156  unsigned char *grids_storage;
158  /* Loose edges, each array element contains grid_size elements
159  * corresponding to vertices created by subdividing coarse edges. */
160  struct CCGElem **edges;
162  /* Loose vertices. Every element corresponds to a loose vertex from a coarse
163  * mesh, every coarse loose vertex corresponds to a single subdivided
164  * element. */
165  struct CCGElem *vertices;
167  /* Denotes which layers present in the elements.
168  *
169  * Grids always has coordinates, followed by extra layers which are set to
170  * truth here.
171  */
173  bool has_mask;
174  /* Offsets of corresponding data layers in the elements. */
177 
178  /* Faces from which grids are emitted. */
181  /* Indexed by grid index, points to corresponding face from `faces`. */
183 
184  /* Edges which are adjacent to faces.
185  * Used for faster grid stitching, in the cost of extra memory.
186  */
189 
190  /* Vertices which are adjacent to faces
191  * Used for faster grid stitching, in the cost of extra memory.
192  */
195 
198 
199  /* TODO(sergey): Consider adding some accessors to a "decoded" geometry,
200  * to make integration with draw manager and such easy.
201  */
202 
203  /* TODO(sergey): Consider adding CD layers here, so we can draw final mesh
204  * from grids, and have UVs and such work.
205  */
206 
207  /* Integration with sculpting. */
208  /* TODO(sergey): Is this really best way to go? Kind of annoying to have
209  * such use-related flags in a more or less generic structure. */
210  struct {
211  /* Corresponds to MULTIRES_COORDS_MODIFIED. */
212  bool coords;
213  /* Corresponds to MULTIRES_HIDDEN_MODIFIED. */
214  bool hidden;
215  } dirty;
216 
217  /* Cached values, are not supposed to be accessed directly. */
218  struct {
219  /* Indexed by face, indicates index of the first grid which corresponds to the face. */
223 
224 /* Create CCG representation of subdivision surface.
225  *
226  * NOTE: CCG stores dense vertices in a grid-like storage. There is no edges or
227  * polygons information's for the high-poly surface.
228  *
229  * NOTE: Subdiv is expected to be refined and ready for evaluation.
230  * NOTE: CCG becomes an owner of subdiv.
231  *
232  * TODO(sergey): Allow some user-counter or more explicit control over who owns
233  * the Subdiv. The goal should be to allow viewport GL Mesh and CCG to share
234  * same Subsurf without conflicts. */
235 struct SubdivCCG *BKE_subdiv_to_ccg(struct Subdiv *subdiv,
236  const SubdivToCCGSettings *settings,
237  SubdivCCGMaskEvaluator *mask_evaluator,
238  SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator);
239 
240 /* Destroy CCG representation of subdivision surface. */
241 void BKE_subdiv_ccg_destroy(SubdivCCG *subdiv_ccg);
242 
243 /* Helper function, creates Mesh structure which is properly setup to use
244  * grids.
245  */
246 struct Mesh *BKE_subdiv_to_ccg_mesh(struct Subdiv *subdiv,
247  const SubdivToCCGSettings *settings,
248  const struct Mesh *coarse_mesh);
249 
250 /* Create a key for accessing grid elements at a given level. */
251 void BKE_subdiv_ccg_key(struct CCGKey *key, const SubdivCCG *subdiv_ccg, int level);
252 void BKE_subdiv_ccg_key_top_level(struct CCGKey *key, const SubdivCCG *subdiv_ccg);
253 
254 /* Recalculate all normals based on grid element coordinates. */
255 void BKE_subdiv_ccg_recalc_normals(SubdivCCG *subdiv_ccg);
256 
257 /* Update normals of affected faces. */
259  struct CCGFace **effected_faces,
260  int num_effected_faces);
261 
262 /* Average grid coordinates and normals along the grid boundatries. */
263 void BKE_subdiv_ccg_average_grids(SubdivCCG *subdiv_ccg);
264 
265 /* Similar to above, but only updates given faces. */
267  struct CCGFace **effected_faces,
268  int num_effected_faces);
269 
270 /* Get geometry counters at the current subdivision level. */
271 void BKE_subdiv_ccg_topology_counters(const SubdivCCG *subdiv_ccg,
272  int *r_num_vertices,
273  int *r_num_edges,
274  int *r_num_faces,
275  int *r_num_loops);
276 
277 typedef struct SubdivCCGNeighbors {
279  int size;
281 
284 
285 void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord *coord);
286 bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord);
287 
288 /* CCG element neighbors.
289  *
290  * Neighbors are considered:
291  *
292  * - For an inner elements of a grid other elements which are sharing same row or column (4
293  * neighbor elements in total).
294  *
295  * - For the corner element a single neighboring element on every adjacent edge, single from
296  * every grid.
297  *
298  * - For the boundary element two neighbor elements on the boundary (from same grid) and one
299  * element inside of every neighboring grid. */
300 
301 /* Get actual neighbors of the given coordinate.
302  *
303  * SubdivCCGNeighbors.neighbors must be freed if it is not equal to
304  * SubdivCCGNeighbors.fixed_neighbors.
305  *
306  * If include_duplicates is true, vertices in other grids that match
307  * the current vertex are added at the end of the coords array. */
308 void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG *subdiv_ccg,
309  const SubdivCCGCoord *coord,
310  const bool include_duplicates,
311  SubdivCCGNeighbors *r_neighbors);
312 
313 int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG *subdiv_ccg, const int grid_index);
314 void BKE_subdiv_ccg_eval_limit_point(const SubdivCCG *subdiv_ccg,
315  const SubdivCCGCoord *coord,
316  float r_point[3]);
317 
323 
324 /* Returns if a grid coordinates is adjacent to a coarse mesh edge, vertex or nothing. If it is
325  * adjacent to an edge, r_v1 and r_v2 will be set to the two vertices of that edge. If it is
326  * adjacent to a vertex, r_v1 and r_v2 will be the index of that vertex. */
328  const SubdivCCGCoord *coord,
329  const MLoop *mloop,
330  const MPoly *mpoly,
331  int *r_v1,
332  int *r_v2);
333 
334 /* Get array which is indexed by face index and contains index of a first grid of the face.
335  *
336  * The "ensure" version allocates the mapping if it's not known yet and stores it in the subdiv_ccg
337  * descriptor. This function is NOT safe for threading.
338  *
339  * The "get" version simply returns cached array. */
341 const int *BKE_subdiv_ccg_start_face_grid_index_get(const SubdivCCG *subdiv_ccg);
342 
343 void BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG *subdiv_ccg, int grid_index);
344 
345 #ifdef __cplusplus
346 }
347 #endif
typedef float(TangentPoint)[2]
struct DMFlagMat DMFlagMat
struct CCGElem CCGElem
Definition: BKE_ccg.h:46
CustomData interface, see also DNA_customdata_types.h.
void BKE_subdiv_ccg_recalc_normals(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:813
struct SubdivCCGMaskEvaluator SubdivCCGMaskEvaluator
SubdivCCGAdjacencyType BKE_subdiv_ccg_coarse_mesh_adjacency_info_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const MLoop *mloop, const MPoly *mpoly, int *r_v1, int *r_v2)
Definition: subdiv_ccg.c:1893
bool BKE_subdiv_ccg_mask_init_from_paint(SubdivCCGMaskEvaluator *mask_evaluator, const struct Mesh *mesh)
const int * BKE_subdiv_ccg_start_face_grid_index_get(const SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:1865
struct SubdivCCGCoord SubdivCCGCoord
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1803
void BKE_subdiv_ccg_eval_limit_point(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, float r_point[3])
Definition: subdiv_ccg.c:1970
void BKE_subdiv_ccg_average_grids(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:1181
SubdivCCGAdjacencyType
@ SUBDIV_CCG_ADJACENT_EDGE
@ SUBDIV_CCG_ADJACENT_VERTEX
@ SUBDIV_CCG_ADJACENT_NONE
void BKE_subdiv_ccg_destroy(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:624
void BKE_subdiv_ccg_key(struct CCGKey *key, const SubdivCCG *subdiv_ccg, int level)
Definition: subdiv_ccg.c:662
const int * BKE_subdiv_ccg_start_face_grid_index_ensure(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:1839
void BKE_subdiv_ccg_update_normals(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:879
void BKE_subdiv_ccg_key_top_level(struct CCGKey *key, const SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:677
struct SubdivCCGFace SubdivCCGFace
struct SubdivCCGAdjacentEdge SubdivCCGAdjacentEdge
struct SubdivToCCGSettings SubdivToCCGSettings
void BKE_subdiv_ccg_average_stitch_faces(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:1221
struct SubdivCCG SubdivCCG
void BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG *subdiv_ccg, int grid_index)
Definition: subdiv_ccg.c:1927
void BKE_subdiv_ccg_topology_counters(const SubdivCCG *subdiv_ccg, int *r_num_vertices, int *r_num_edges, int *r_num_faces, int *r_num_loops)
Definition: subdiv_ccg.c:1244
int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG *subdiv_ccg, const int grid_index)
Definition: subdiv_ccg.c:1832
void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1266
void BKE_subdiv_ccg_material_flags_init_from_mesh(SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator, const struct Mesh *mesh)
struct SubdivCCG * BKE_subdiv_to_ccg(struct Subdiv *subdiv, const SubdivToCCGSettings *settings, SubdivCCGMaskEvaluator *mask_evaluator, SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator)
Definition: subdiv_ccg.c:571
struct SubdivCCGNeighbors SubdivCCGNeighbors
struct SubdivCCGMaterialFlagsEvaluator SubdivCCGMaterialFlagsEvaluator
bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1271
struct Mesh * BKE_subdiv_to_ccg_mesh(struct Subdiv *subdiv, const SubdivToCCGSettings *settings, const struct Mesh *coarse_mesh)
struct SubdivCCGAdjacentVertex SubdivCCGAdjacentVertex
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:32
ATTR_WARN_UNUSED_RESULT const BMVert * v
Definition: BKE_ccg.h:48
struct Key * key
struct SubdivCCGCoord ** boundary_coords
struct SubdivCCGCoord * corner_coords
void(* free)(struct SubdivCCGMaskEvaluator *mask_evaluator)
float(* eval_mask)(struct SubdivCCGMaskEvaluator *mask_evaluator, const int ptex_face_index, const float u, const float v)
DMFlagMat(* eval_material_flags)(struct SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator, const int coarse_face_index)
void(* free)(struct SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator)
SubdivCCGCoord * coords
SubdivCCGCoord coords_fixed[256]
struct CCGElem ** grids
struct Subdiv * subdiv
unsigned char * grids_storage
struct SubdivCCG::@69 cache_
SubdivCCGAdjacentVertex * adjacent_vertices
SubdivCCGFace * faces
int grid_element_size
int * start_face_grid_index
int num_adjacent_vertices
SubdivCCGFace ** grid_faces
BLI_bitmap ** grid_hidden
SubdivCCGAdjacentEdge * adjacent_edges
struct DMFlagMat * grid_flag_mats
struct CCGElem * vertices
int num_adjacent_edges
struct SubdivCCG::@68 dirty
struct CCGElem ** edges