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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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) 2018 Blender Foundation.
* All rights reserved.
*
* Contributor(s): Sergey Sharybin.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/blenkernel/intern/multires_reshape.c
* \ingroup bke
*/
#include "MEM_guardedalloc.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_scene_types.h"
#include "BLI_utildefines.h"
#include "BLI_math_vector.h"
#include "BKE_library.h"
#include "BKE_mesh.h"
#include "BKE_mesh_runtime.h"
#include "BKE_modifier.h"
#include "BKE_multires.h"
#include "BKE_subdiv.h"
#include "DEG_depsgraph_query.h"
/* TODO(sergey): De-duplicate with subdiv_displacement_multires.c. */
/* Coordinates within grid has different convention from PTex coordinates.
* This function converts the latter ones to former.
*/
BLI_INLINE void ptex_uv_to_grid_uv(const float ptex_u, const float ptex_v,
float *r_grid_u, float *r_grid_v)
{
*r_grid_u = 1.0f - ptex_v;
*r_grid_v = 1.0f - ptex_u;
}
/* Simplified version of mdisp_rot_face_to_crn, only handles quad and
* works in normalized coordinates.
*
* NOTE: Output coordinates are in ptex coordinates.
*/
BLI_INLINE int rotate_quad_to_corner(const float u, const float v,
float *r_u, float *r_v)
{
int corner;
if (u <= 0.5f && v <= 0.5f) {
corner = 0;
*r_u = 2.0f * u;
*r_v = 2.0f * v;
}
else if (u > 0.5f && v <= 0.5f) {
corner = 1;
*r_u = 2.0f * v;
*r_v = 2.0f * (1.0f - u);
}
else if (u > 0.5f && v > 0.5f) {
corner = 2;
*r_u = 2.0f * (1.0f - u);
*r_v = 2.0f * (1.0f - v);
}
else if (u <= 0.5f && v >= 0.5f) {
corner = 3;
*r_u = 2.0f * (1.0f - v);
*r_v = 2.0f * u;
}
else {
BLI_assert(!"Unexpected corner configuration");
}
return corner;
}
BLI_INLINE void construct_tangent_matrix(float tangent_matrix[3][3],
const float dPdu[3],
const float dPdv[3],
const int corner)
{
if (corner == 0) {
copy_v3_v3(tangent_matrix[0], dPdv);
copy_v3_v3(tangent_matrix[1], dPdu);
mul_v3_fl(tangent_matrix[0], -1.0f);
mul_v3_fl(tangent_matrix[1], -1.0f);
}
else if (corner == 1) {
copy_v3_v3(tangent_matrix[0], dPdu);
copy_v3_v3(tangent_matrix[1], dPdv);
mul_v3_fl(tangent_matrix[1], -1.0f);
}
else if (corner == 2) {
copy_v3_v3(tangent_matrix[0], dPdv);
copy_v3_v3(tangent_matrix[1], dPdu);
}
else if (corner == 3) {
copy_v3_v3(tangent_matrix[0], dPdu);
copy_v3_v3(tangent_matrix[1], dPdv);
mul_v3_fl(tangent_matrix[0], -1.0f);
}
cross_v3_v3v3(tangent_matrix[2], dPdu, dPdv);
normalize_v3(tangent_matrix[0]);
normalize_v3(tangent_matrix[1]);
normalize_v3(tangent_matrix[2]);
}
/* =============================================================================
* Reshape internal functionality..
*/
typedef struct MultiresReshapeContext {
Subdiv *subdiv;
Object *object;
const Mesh *coarse_mesh;
MDisps *mdisps;
const float (*deformed_verts)[3];
int num_deformed_verts;
int grid_size;
} MultiresReshapeContext;
static bool multires_reshape_topology_info(
const SubdivForeachContext *foreach_context,
const int num_vertices,
const int UNUSED(num_edges),
const int UNUSED(num_loops),
const int UNUSED(num_polygons))
{
MultiresReshapeContext *ctx = foreach_context->user_data;
if (num_vertices != ctx->num_deformed_verts) {
return false;
}
return true;
}
static void multires_reshape_vertex_copy_to_next(
MultiresReshapeContext *ctx,
const MPoly *coarse_poly,
const int current_corner,
const MDisps *current_displacement_grid,
const int current_grid_x, const int current_grid_y)
{
const int grid_size = ctx->grid_size;
const int next_current_corner = (current_corner + 1) % coarse_poly->totloop;
MDisps *next_displacement_grid = &ctx->mdisps[
coarse_poly->loopstart + next_current_corner];
const int next_grid_x = 0;
const int next_grid_y = current_grid_x;
const int current_index = current_grid_y * grid_size + current_grid_x;
const int next_index = next_grid_y * grid_size + next_grid_x;
float *next_displacement = next_displacement_grid->disps[next_index];
copy_v3_v3(next_displacement,
current_displacement_grid->disps[current_index]);
SWAP(float, next_displacement[0], next_displacement[1]);
next_displacement[0] = -next_displacement[0];
}
static void multires_reshape_vertex_copy_to_prev(
MultiresReshapeContext *ctx,
const MPoly *coarse_poly,
const int current_corner,
const MDisps *current_displacement_grid,
const int current_grid_x, const int current_grid_y)
{
const int grid_size = ctx->grid_size;
const int prev_current_corner =
(current_corner - 1 + coarse_poly->totloop) % coarse_poly->totloop;
MDisps *prev_displacement_grid = &ctx->mdisps[
coarse_poly->loopstart + prev_current_corner];
const int prev_grid_x = current_grid_y;
const int prev_grid_y = 0;
const int current_index = current_grid_y * grid_size + current_grid_x;
const int prev_index = prev_grid_y * grid_size + prev_grid_x;
float *prev_displacement = prev_displacement_grid->disps[prev_index];
copy_v3_v3(prev_displacement,
current_displacement_grid->disps[current_index]);
SWAP(float, prev_displacement[0], prev_displacement[1]);
prev_displacement[1] = -prev_displacement[1];
}
static void copy_boundary_displacement(
MultiresReshapeContext *ctx,
const MPoly *coarse_poly,
const int corner,
const int grid_x, const int grid_y,
const MDisps *displacement_grid)
{
if (grid_x == 0 && grid_y == 0) {
for (int i = 0; i < coarse_poly->totloop; i++) {
const int current_face_corner =
(corner + i) % coarse_poly->totloop;
MDisps *current_displacement_grid = &ctx->mdisps[
coarse_poly->loopstart + current_face_corner];
multires_reshape_vertex_copy_to_next(
ctx,
coarse_poly,
current_face_corner,
current_displacement_grid,
0, 0);
}
}
else if (grid_x == 0) {
multires_reshape_vertex_copy_to_prev(
ctx,
coarse_poly,
corner,
displacement_grid,
grid_x, grid_y);
}
else if (grid_y == 0) {
multires_reshape_vertex_copy_to_next(
ctx,
coarse_poly,
corner,
displacement_grid,
grid_x, grid_y);
}
}
static void multires_reshape_vertex(
MultiresReshapeContext *ctx,
const int ptex_face_index,
const float u, const float v,
const int coarse_poly_index,
const int coarse_corner,
const int subdiv_vertex_index)
{
Subdiv *subdiv = ctx->subdiv;
const int grid_size = ctx->grid_size;
const Mesh *coarse_mesh = ctx->coarse_mesh;
const MPoly *coarse_mpoly = coarse_mesh->mpoly;
const MPoly *coarse_poly = &coarse_mpoly[coarse_poly_index];
const int loop_index = coarse_poly->loopstart + coarse_corner;
/* Evaluate limit surface. */
float P[3], dPdu[3], dPdv[3];
BKE_subdiv_eval_limit_point_and_derivatives(
subdiv, ptex_face_index, u, v, P, dPdu, dPdv);
/* Get coordinate and corner configuration.. */
float grid_u, grid_v;
MDisps *displacement_grid;
int face_corner = coarse_corner;
int grid_corner = 0;
if (coarse_poly->totloop == 4) {
float corner_u, corner_v;
face_corner = rotate_quad_to_corner(u, v, &corner_u, &corner_v);
grid_corner = face_corner;
displacement_grid = &ctx->mdisps[loop_index + face_corner];
ptex_uv_to_grid_uv(corner_u, corner_v, &grid_u, &grid_v);
}
else {
displacement_grid = &ctx->mdisps[loop_index];
ptex_uv_to_grid_uv(u, v, &grid_u, &grid_v);
}
/* Convert object coordinate to a tangent space of displacement grid. */
const float *final_P = ctx->deformed_verts[subdiv_vertex_index];
float D[3];
sub_v3_v3v3(D, final_P, P);
float tangent_matrix[3][3];
construct_tangent_matrix(tangent_matrix, dPdu, dPdv, grid_corner);
float inv_tangent_matrix[3][3];
invert_m3_m3(inv_tangent_matrix, tangent_matrix);
float tangent_D[3];
mul_v3_m3v3(tangent_D, inv_tangent_matrix, D);
/* Write tangent displacement. */
const int grid_x = (grid_u * (grid_size - 1) + 0.5f);
const int grid_y = (grid_v * (grid_size - 1) + 0.5f);
const int index = grid_y * grid_size + grid_x;
copy_v3_v3(displacement_grid->disps[index], tangent_D);
/* Copy boundary to the next/previous grids */
copy_boundary_displacement(
ctx, coarse_poly, face_corner, grid_x, grid_y, displacement_grid);
}
static void multires_reshape_vertex_inner(
const SubdivForeachContext *foreach_context,
void *UNUSED(tls_v),
const int ptex_face_index,
const float u, const float v,
const int coarse_poly_index,
const int coarse_corner,
const int subdiv_vertex_index)
{
MultiresReshapeContext *ctx = foreach_context->user_data;
multires_reshape_vertex(
ctx,
ptex_face_index, u, v,
coarse_poly_index,
coarse_corner,
subdiv_vertex_index);
}
static void multires_reshape_vertex_every_corner(
const struct SubdivForeachContext *foreach_context,
void *UNUSED(tls_v),
const int ptex_face_index,
const float u, const float v,
const int UNUSED(coarse_vertex_index),
const int coarse_poly_index,
const int coarse_corner,
const int subdiv_vertex_index)
{
MultiresReshapeContext *ctx = foreach_context->user_data;
multires_reshape_vertex(
ctx,
ptex_face_index, u, v,
coarse_poly_index,
coarse_corner,
subdiv_vertex_index);
}
static void multires_reshape_vertex_every_edge(
const struct SubdivForeachContext *foreach_context,
void *UNUSED(tls_v),
const int ptex_face_index,
const float u, const float v,
const int UNUSED(coarse_edge_index),
const int coarse_poly_index,
const int coarse_corner,
const int subdiv_vertex_index)
{
MultiresReshapeContext *ctx = foreach_context->user_data;
multires_reshape_vertex(
ctx,
ptex_face_index, u, v,
coarse_poly_index,
coarse_corner,
subdiv_vertex_index);
}
static Subdiv *multires_subdiv_for_reshape(struct Depsgraph *depsgraph,
/*const*/ Object *object,
const MultiresModifierData *mmd)
{
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
Object *object_eval = DEG_get_evaluated_object(depsgraph, object);
Mesh *deformed_mesh = mesh_get_eval_deform(
depsgraph, scene_eval, object_eval, CD_MASK_BAREMESH);
SubdivSettings subdiv_settings;
BKE_multires_subdiv_settings_init(&subdiv_settings, mmd);
Subdiv *subdiv = BKE_subdiv_new_from_mesh(&subdiv_settings, deformed_mesh);
if (!BKE_subdiv_eval_update_from_mesh(subdiv, deformed_mesh)) {
BKE_subdiv_free(subdiv);
return NULL;
}
return subdiv;
}
static bool multires_reshape_from_vertcos(
struct Depsgraph *depsgraph,
Object *object,
const MultiresModifierData *mmd,
const float (*deformed_verts)[3],
const int num_deformed_verts,
const bool use_render_params)
{
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
Mesh *coarse_mesh = object->data;
MultiresReshapeContext ctx = {
.object = object,
.coarse_mesh = coarse_mesh,
.deformed_verts = deformed_verts,
.mdisps = CustomData_get_layer(&coarse_mesh->ldata, CD_MDISPS),
.num_deformed_verts = num_deformed_verts,
.grid_size = (1 << (mmd->totlvl - 1)) + 1,
};
SubdivForeachContext foreach_context = {
.topology_info = multires_reshape_topology_info,
.vertex_inner = multires_reshape_vertex_inner,
.vertex_every_edge = multires_reshape_vertex_every_edge,
.vertex_every_corner = multires_reshape_vertex_every_corner,
.user_data = &ctx,
};
/* Initialize subdivision surface. */
ctx.subdiv = multires_subdiv_for_reshape(depsgraph, object, mmd);
if (ctx.subdiv == NULL) {
return false;
}
/* Initialize mesh rasterization settings. */
SubdivToMeshSettings mesh_settings;
BKE_multires_subdiv_mesh_settings_init(
&mesh_settings, scene_eval, object, mmd, use_render_params, true);
/* Run all the callbacks. */
BKE_subdiv_foreach_subdiv_geometry(
ctx.subdiv,
&foreach_context,
&mesh_settings,
coarse_mesh);
BKE_subdiv_free(ctx.subdiv);
return true;
}
static void multires_reshape_init_mmd(MultiresModifierData *reshape_mmd,
const MultiresModifierData *mmd)
{
/* It is possible that the current subdivision level of multires is lower
* that it's maximum possible one (i.e., viewport is set to a lower level
* for the performance purposes). But even then, we want all the multires
* levels to be reshaped. Most accurate way to do so is to ignore all
* simplifications and calculate deformation modifier for the highest
* possible multires level.
* Alternative would be propagate displacement from current level to a
* higher ones, but that is likely to cause artifacts.
*/
*reshape_mmd = *mmd;
reshape_mmd->lvl = reshape_mmd->totlvl;
}
/* =============================================================================
* Public entry points..
*/
/* Returns truth on success, false otherwise.
*
* This function might fail in cases like source and destination not having
* matched amount of vertices.
*/
bool multiresModifier_reshape(
struct Depsgraph *depsgraph,
MultiresModifierData *mmd,
Object *dst,
Object *src)
{
/* Would be cool to support this eventually, but it is very tricky to match
* vertices order even for meshes, when mixing meshes and other objects it's
* even more tricky.
*/
if (src->type != OB_MESH) {
return false;
}
MultiresModifierData highest_mmd;
multires_reshape_init_mmd(&highest_mmd, mmd);
/* Get evaluated vertices locations to reshape to. */
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
Object *src_eval = DEG_get_evaluated_object(depsgraph, src);
Mesh *src_mesh_eval = mesh_get_eval_final(
depsgraph, scene_eval, src_eval, CD_MASK_BAREMESH);
int num_deformed_verts;
float (*deformed_verts)[3] = BKE_mesh_vertexCos_get(
src_mesh_eval, &num_deformed_verts);
bool result = multires_reshape_from_vertcos(
depsgraph,
dst,
&highest_mmd,
deformed_verts,
num_deformed_verts,
false);
MEM_freeN(deformed_verts);
return result;
}
bool multiresModifier_reshapeFromDeformModifier(
struct Depsgraph *depsgraph,
MultiresModifierData *mmd,
Object *object,
ModifierData *md)
{
MultiresModifierData highest_mmd;
multires_reshape_init_mmd(&highest_mmd, mmd);
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
/* Perform sanity checks and early output. */
if (multires_get_level(
scene_eval, object, &highest_mmd, false, true) == 0)
{
return false;
}
/* Create mesh for the multires, ignoring any further modifiers (leading
* deformation modifiers will be applied though).
*/
Mesh *multires_mesh = get_multires_mesh(
depsgraph, scene_eval, &highest_mmd, object);
int num_deformed_verts;
float (*deformed_verts)[3] = BKE_mesh_vertexCos_get(
multires_mesh, &num_deformed_verts);
/* Apply deformation modifier on the multires, */
const ModifierEvalContext modifier_ctx = {
.depsgraph = depsgraph,
.object = object,
.flag = MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY};
modifier_deformVerts_ensure_normals(
md, &modifier_ctx, multires_mesh, deformed_verts,
multires_mesh->totvert);
BKE_id_free(NULL, multires_mesh);
/* Reshaping */
bool result = multires_reshape_from_vertcos(
depsgraph,
object,
&highest_mmd,
deformed_verts,
num_deformed_verts,
false);
/* Cleanup */
MEM_freeN(deformed_verts);
return result;
}
|