Blender  V2.93
multires_reshape_subdivide.c
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) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "DNA_mesh_types.h"
27 #include "DNA_meshdata_types.h"
28 #include "DNA_modifier_types.h"
29 #include "DNA_scene_types.h"
30 
31 #include "BKE_customdata.h"
32 #include "BKE_lib_id.h"
33 #include "BKE_mesh.h"
34 #include "BKE_mesh_runtime.h"
35 #include "BKE_modifier.h"
36 #include "BKE_multires.h"
37 #include "BKE_subdiv.h"
38 #include "BKE_subsurf.h"
39 #include "BLI_math_vector.h"
40 
41 #include "DEG_depsgraph_query.h"
42 
43 #include "multires_reshape.h"
44 
46 {
48  const int totpoly = mesh->totpoly;
49  for (int p = 0; p < totpoly; p++) {
50  MPoly *poly = &mesh->mpoly[p];
51  float poly_center[3];
52  BKE_mesh_calc_poly_center(poly, &mesh->mloop[poly->loopstart], mesh->mvert, poly_center);
53  for (int l = 0; l < poly->totloop; l++) {
54  const int loop_index = poly->loopstart + l;
55 
56  float(*disps)[3] = mdisps[loop_index].disps;
57  mdisps[loop_index].totdisp = 4;
58  mdisps[loop_index].level = 1;
59 
60  int prev_loop_index = l - 1 >= 0 ? loop_index - 1 : loop_index + poly->totloop - 1;
61  int next_loop_index = l + 1 < poly->totloop ? loop_index + 1 : poly->loopstart;
62 
63  MLoop *loop = &mesh->mloop[loop_index];
64  MLoop *loop_next = &mesh->mloop[next_loop_index];
65  MLoop *loop_prev = &mesh->mloop[prev_loop_index];
66 
67  copy_v3_v3(disps[0], poly_center);
68  mid_v3_v3v3(disps[1], mesh->mvert[loop->v].co, mesh->mvert[loop_next->v].co);
69  mid_v3_v3v3(disps[2], mesh->mvert[loop->v].co, mesh->mvert[loop_prev->v].co);
70  copy_v3_v3(disps[3], mesh->mvert[loop->v].co);
71  }
72  }
73 }
74 
77 {
78  Mesh *coarse_mesh = object->data;
80 
81  MultiresReshapeContext reshape_context;
82 
83  const int new_top_level = mmd->totlvl + 1;
84 
85  const bool has_mdisps = CustomData_has_layer(&coarse_mesh->ldata, CD_MDISPS);
86  if (!has_mdisps) {
87  CustomData_add_layer(&coarse_mesh->ldata, CD_MDISPS, CD_CALLOC, NULL, coarse_mesh->totloop);
88  }
89 
90  if (new_top_level == 1) {
91  /* No MDISPS. Create new grids for level 1 using the edges mid point and poly centers. */
92  multires_reshape_ensure_grids(coarse_mesh, 1);
94  }
95 
96  /* Convert the new grids to tangent displacement. */
97  multires_set_tot_level(object, mmd, new_top_level);
98 
100  &reshape_context, object, mmd, new_top_level)) {
101  return;
102  }
103 
105  multires_reshape_context_free(&reshape_context);
106 }
typedef float(TangentPoint)[2]
CustomData interface, see also DNA_customdata_types.h.
@ CD_CALLOC
bool CustomData_has_layer(const struct CustomData *data, int type)
void * CustomData_get_layer(const struct CustomData *data, int type)
void * CustomData_add_layer(struct CustomData *data, int type, eCDAllocType alloctype, void *layer, int totelem)
Definition: customdata.c:2620
void BKE_mesh_calc_poly_center(const struct MPoly *mpoly, const struct MLoop *loopstart, const struct MVert *mvarray, float r_cent[3])
void multires_set_tot_level(struct Object *ob, struct MultiresModifierData *mmd, int lvl)
Definition: multires.c:384
void multires_force_sculpt_rebuild(struct Object *object)
Definition: multires.c:456
MINLINE void copy_v3_v3(float r[3], const float a[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
Definition: math_vector.c:270
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMLoop * l
void multires_reshape_ensure_grids(struct Mesh *mesh, const int level)
void multires_reshape_context_free(MultiresReshapeContext *reshape_context)
bool multires_reshape_context_create_from_modifier(MultiresReshapeContext *reshape_context, struct Object *object, struct MultiresModifierData *mmd, int top_level)
void multires_reshape_object_grids_to_tangent_displacement(const MultiresReshapeContext *reshape_context)
static void multires_subdivide_create_object_space_linear_grids(Mesh *mesh)
void multires_subdivide_create_tangent_displacement_linear_grids(Object *object, MultiresModifierData *mmd)
float(* disps)[3]
unsigned int v
float co[3]
struct CustomData pdata ldata
struct MVert * mvert
struct MLoop * mloop
int totpoly
int totloop
struct MPoly * mpoly