Blender  V3.3
BKE_subdiv_ccg.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. All rights reserved. */
3 
8 #pragma once
9 
10 #include "BKE_DerivedMesh.h"
11 #include "BLI_bitmap.h"
12 #include "BLI_sys_types.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 struct CCGElem;
19 struct CCGFace;
20 struct CCGKey;
21 struct DMFlagMat;
22 struct Mesh;
23 struct MPoly;
24 struct MLoop;
25 struct Subdiv;
26 
27 /* --------------------------------------------------------------------
28  * Masks.
29  */
30 
31 /* Functor which evaluates mask value at a given (u, v) of given ptex face. */
32 typedef struct SubdivCCGMaskEvaluator {
33  float (*eval_mask)(struct SubdivCCGMaskEvaluator *mask_evaluator,
34  int ptex_face_index,
35  float u,
36  float v);
37 
38  /* Free the data, not the evaluator itself. */
39  void (*free)(struct SubdivCCGMaskEvaluator *mask_evaluator);
40 
41  void *user_data;
43 
44 /* Return true if mesh has mask and evaluator can be used. */
46  const struct Mesh *mesh);
47 
48 /* --------------------------------------------------------------------
49  * Materials.
50  */
51 
52 /* Functor which evaluates material and flags of a given coarse face. */
55  struct SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator, int coarse_face_index);
56 
57  /* Free the data, not the evaluator itself. */
58  void (*free)(struct SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator);
59 
60  void *user_data;
62 
64  SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator, const struct Mesh *mesh);
65 
66 /* --------------------------------------------------------------------
67  * SubdivCCG.
68  */
69 
70 typedef struct SubdivToCCGSettings {
71  /* Resolution at which regular ptex (created for quad polygon) are being
72  * evaluated. This defines how many vertices final mesh will have: every
73  * regular ptex has resolution^2 vertices. Special (irregular, or ptex
74  * created for a corner of non-quad polygon) will have resolution of
75  * `resolution - 1`. */
77  /* Denotes which extra layers to be added to CCG elements. */
79  bool need_mask;
81 
82 typedef struct SubdivCCGCoord {
83  /* Index of the grid within SubdivCCG::grids array. */
85 
86  /* Coordinate within the grid. */
87  short x, y;
89 
90 /* This is actually a coarse face, which consists of multiple CCG grids. */
91 typedef struct SubdivCCGFace {
92  /* Total number of grids in this face.
93  *
94  * This 1:1 corresponds to a number of corners (or loops) from a coarse
95  * face. */
96  int num_grids;
97  /* Index of first grid from this face in SubdivCCG->grids array. */
100 
101 /* Definition of an edge which is adjacent to at least one of the faces. */
102 typedef struct SubdivCCGAdjacentEdge {
104  /* Indexed by adjacent face index, then by point index on the edge.
105  * points to a coordinate into the grids. */
108 
109 /* Definition of a vertex which is adjacent to at least one of the faces. */
110 typedef struct SubdivCCGAdjacentVertex {
112  /* Indexed by adjacent face index, points to a coordinate in the grids. */
115 
116 /* Representation of subdivision surface which uses CCG grids. */
117 typedef struct SubdivCCG {
118  /* This is a subdivision surface this CCG was created for.
119  *
120  * TODO(sergey): Make sure the whole descriptor is valid, including all the
121  * displacement attached to the surface. */
122  struct Subdiv *subdiv;
123  /* A level at which geometry was subdivided. This is what defines grid
124  * resolution. It is NOT the topology refinement level. */
125  int level;
126  /* Resolution of grid. All grids have matching resolution, and resolution
127  * is same as ptex created for non-quad polygons. */
129  /* Size of a single element of a grid (including coordinate and all the other layers).
130  * Measured in bytes. */
132  /* Grids represent limit surface, with displacement applied. Grids are
133  * corresponding to face-corners of coarse mesh, each grid has
134  * grid_size^2 elements.
135  */
136  /* Indexed by a grid index, points to a grid data which is stored in
137  * grids_storage. */
138  struct CCGElem **grids;
139  /* Flat array of all grids' data. */
140  unsigned char *grids_storage;
142  /* Loose edges, each array element contains grid_size elements
143  * corresponding to vertices created by subdividing coarse edges. */
144  struct CCGElem **edges;
146  /* Loose vertices. Every element corresponds to a loose vertex from a coarse
147  * mesh, every coarse loose vertex corresponds to a single subdivided
148  * element. */
149  struct CCGElem *vertices;
151  /* Denotes which layers present in the elements.
152  *
153  * Grids always has coordinates, followed by extra layers which are set to
154  * truth here.
155  */
157  bool has_mask;
158  /* Offsets of corresponding data layers in the elements. */
161 
162  /* Faces from which grids are emitted. */
165  /* Indexed by grid index, points to corresponding face from `faces`. */
167 
168  /* Edges which are adjacent to faces.
169  * Used for faster grid stitching, in the cost of extra memory.
170  */
173 
174  /* Vertices which are adjacent to faces
175  * Used for faster grid stitching, in the cost of extra memory.
176  */
179 
182 
183  /* TODO(sergey): Consider adding some accessors to a "decoded" geometry,
184  * to make integration with draw manager and such easy.
185  */
186 
187  /* TODO(sergey): Consider adding CD layers here, so we can draw final mesh
188  * from grids, and have UVs and such work.
189  */
190 
191  /* Integration with sculpting. */
192  /* TODO(sergey): Is this really best way to go? Kind of annoying to have
193  * such use-related flags in a more or less generic structure. */
194  struct {
195  /* Corresponds to MULTIRES_COORDS_MODIFIED. */
196  bool coords;
197  /* Corresponds to MULTIRES_HIDDEN_MODIFIED. */
198  bool hidden;
199  } dirty;
200 
201  /* Cached values, are not supposed to be accessed directly. */
202  struct {
203  /* Indexed by face, indicates index of the first grid which corresponds to the face. */
207 
208 /* Create CCG representation of subdivision surface.
209  *
210  * NOTE: CCG stores dense vertices in a grid-like storage. There is no edges or
211  * polygons information's for the high-poly surface.
212  *
213  * NOTE: Subdiv is expected to be refined and ready for evaluation.
214  * NOTE: CCG becomes an owner of subdiv.
215  *
216  * TODO(sergey): Allow some user-counter or more explicit control over who owns
217  * the Subdiv. The goal should be to allow viewport GL Mesh and CCG to share
218  * same Subsurf without conflicts. */
219 struct SubdivCCG *BKE_subdiv_to_ccg(struct Subdiv *subdiv,
220  const SubdivToCCGSettings *settings,
221  SubdivCCGMaskEvaluator *mask_evaluator,
222  SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator);
223 
224 /* Destroy CCG representation of subdivision surface. */
225 void BKE_subdiv_ccg_destroy(SubdivCCG *subdiv_ccg);
226 
227 /* Helper function, creates Mesh structure which is properly setup to use
228  * grids.
229  */
230 struct Mesh *BKE_subdiv_to_ccg_mesh(struct Subdiv *subdiv,
231  const SubdivToCCGSettings *settings,
232  const struct Mesh *coarse_mesh);
233 
234 /* Create a key for accessing grid elements at a given level. */
235 void BKE_subdiv_ccg_key(struct CCGKey *key, const SubdivCCG *subdiv_ccg, int level);
236 void BKE_subdiv_ccg_key_top_level(struct CCGKey *key, const SubdivCCG *subdiv_ccg);
237 
238 /* Recalculate all normals based on grid element coordinates. */
239 void BKE_subdiv_ccg_recalc_normals(SubdivCCG *subdiv_ccg);
240 
241 /* Update normals of affected faces. */
243  struct CCGFace **effected_faces,
244  int num_effected_faces);
245 
246 /* Average grid coordinates and normals along the grid boundatries. */
247 void BKE_subdiv_ccg_average_grids(SubdivCCG *subdiv_ccg);
248 
249 /* Similar to above, but only updates given faces. */
251  struct CCGFace **effected_faces,
252  int num_effected_faces);
253 
254 /* Get geometry counters at the current subdivision level. */
255 void BKE_subdiv_ccg_topology_counters(const SubdivCCG *subdiv_ccg,
256  int *r_num_vertices,
257  int *r_num_edges,
258  int *r_num_faces,
259  int *r_num_loops);
260 
261 typedef struct SubdivCCGNeighbors {
263  int size;
265 
268 
269 void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord *coord);
270 bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord);
271 
272 /* CCG element neighbors.
273  *
274  * Neighbors are considered:
275  *
276  * - For an inner elements of a grid other elements which are sharing same row or column (4
277  * neighbor elements in total).
278  *
279  * - For the corner element a single neighboring element on every adjacent edge, single from
280  * every grid.
281  *
282  * - For the boundary element two neighbor elements on the boundary (from same grid) and one
283  * element inside of every neighboring grid. */
284 
285 /* Get actual neighbors of the given coordinate.
286  *
287  * SubdivCCGNeighbors.neighbors must be freed if it is not equal to
288  * SubdivCCGNeighbors.fixed_neighbors.
289  *
290  * If include_duplicates is true, vertices in other grids that match
291  * the current vertex are added at the end of the coords array. */
292 void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG *subdiv_ccg,
293  const SubdivCCGCoord *coord,
294  bool include_duplicates,
295  SubdivCCGNeighbors *r_neighbors);
296 
297 int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG *subdiv_ccg, int grid_index);
298 void BKE_subdiv_ccg_eval_limit_point(const SubdivCCG *subdiv_ccg,
299  const SubdivCCGCoord *coord,
300  float r_point[3]);
301 
307 
308 /* Returns if a grid coordinates is adjacent to a coarse mesh edge, vertex or nothing. If it is
309  * adjacent to an edge, r_v1 and r_v2 will be set to the two vertices of that edge. If it is
310  * adjacent to a vertex, r_v1 and r_v2 will be the index of that vertex. */
312  const SubdivCCGCoord *coord,
313  const struct MLoop *mloop,
314  const struct MPoly *mpoly,
315  int *r_v1,
316  int *r_v2);
317 
318 /* Get array which is indexed by face index and contains index of a first grid of the face.
319  *
320  * The "ensure" version allocates the mapping if it's not known yet and stores it in the subdiv_ccg
321  * descriptor. This function is NOT safe for threading.
322  *
323  * The "get" version simply returns cached array. */
325 const int *BKE_subdiv_ccg_start_face_grid_index_get(const SubdivCCG *subdiv_ccg);
326 
327 void BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG *subdiv_ccg, int grid_index);
328 
329 #ifdef __cplusplus
330 }
331 #endif
typedef float(TangentPoint)[2]
struct DMFlagMat DMFlagMat
struct CCGElem CCGElem
Definition: BKE_ccg.h:30
void BKE_subdiv_ccg_recalc_normals(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:804
struct SubdivCCGMaskEvaluator SubdivCCGMaskEvaluator
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:1973
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, bool include_duplicates, SubdivCCGNeighbors *r_neighbors)
Definition: subdiv_ccg.c:1911
struct SubdivCCGCoord SubdivCCGCoord
void BKE_subdiv_ccg_eval_limit_point(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, float r_point[3])
Definition: subdiv_ccg.c:2078
void BKE_subdiv_ccg_average_grids(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:1197
SubdivCCGAdjacencyType BKE_subdiv_ccg_coarse_mesh_adjacency_info_get(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord, const struct MLoop *mloop, const struct MPoly *mpoly, int *r_v1, int *r_v2)
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:615
void BKE_subdiv_ccg_key(struct CCGKey *key, const SubdivCCG *subdiv_ccg, int level)
Definition: subdiv_ccg.c:653
const int * BKE_subdiv_ccg_start_face_grid_index_ensure(SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:1947
void BKE_subdiv_ccg_update_normals(SubdivCCG *subdiv_ccg, struct CCGFace **effected_faces, int num_effected_faces)
Definition: subdiv_ccg.c:870
void BKE_subdiv_ccg_key_top_level(struct CCGKey *key, const SubdivCCG *subdiv_ccg)
Definition: subdiv_ccg.c:668
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:1329
struct SubdivCCG SubdivCCG
void BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG *subdiv_ccg, int grid_index)
Definition: subdiv_ccg.c:2035
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:1352
void BKE_subdiv_ccg_print_coord(const char *message, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1374
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:561
struct SubdivCCGNeighbors SubdivCCGNeighbors
struct SubdivCCGMaterialFlagsEvaluator SubdivCCGMaterialFlagsEvaluator
bool BKE_subdiv_ccg_check_coord_valid(const SubdivCCG *subdiv_ccg, const SubdivCCGCoord *coord)
Definition: subdiv_ccg.c:1379
struct Mesh * BKE_subdiv_to_ccg_mesh(struct Subdiv *subdiv, const SubdivToCCGSettings *settings, const struct Mesh *coarse_mesh)
int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG *subdiv_ccg, int grid_index)
Definition: subdiv_ccg.c:1940
struct SubdivCCGAdjacentVertex SubdivCCGAdjacentVertex
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
ATTR_WARN_UNUSED_RESULT const BMVert * v
SyclQueue void void size_t num_bytes void
Definition: BKE_ccg.h:32
struct Key * key
struct SubdivCCGCoord ** boundary_coords
struct SubdivCCGCoord * corner_coords
float(* eval_mask)(struct SubdivCCGMaskEvaluator *mask_evaluator, int ptex_face_index, float u, float v)
void(* free)(struct SubdivCCGMaskEvaluator *mask_evaluator)
DMFlagMat(* eval_material_flags)(struct SubdivCCGMaterialFlagsEvaluator *material_flags_evaluator, 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
SubdivCCGAdjacentVertex * adjacent_vertices
struct SubdivCCG::@69 dirty
struct SubdivCCG::@70 cache_
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 CCGElem ** edges