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

subdiv_displacement_multires.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5af90160f8e2c4423b6040f7936e8fa4fed3482d (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
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
/*
 * ***** 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 by Blender Foundation.
 * All rights reserved.
 *
 * Contributor(s): Sergey Sharybin.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/blenkernel/intern/subdiv_displacement_multires.c
 *  \ingroup bke
 */

#include "BKE_subdiv.h"

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_object_types.h"

#include "BLI_utildefines.h"
#include "BLI_math_vector.h"

#include "BKE_customdata.h"

#include "MEM_guardedalloc.h"

typedef struct PolyCornerIndex {
	int poly_index;
	int corner;
} PolyCornerIndex;

typedef struct MultiresDisplacementData {
	int grid_size;
	const MPoly *mpoly;
	const MDisps *mdisps;
	/* Indexed by ptex face index, contains polygon/corner which corresponds
	 * to it.
	 *
	 * NOTE: For quad polygon this is an index of first corner only, since
	 * there we only have one ptex.
	 */
	PolyCornerIndex *ptex_poly_corner;
} MultiresDisplacementData;

/* Denotes which grid to use to average value of the displacement read from the
 * grid which corresponds to the ptex face.
 */
typedef enum eAverageWith {
	AVERAGE_WITH_NONE,
	AVERAGE_WITH_ALL,
	AVERAGE_WITH_PREV,
	AVERAGE_WITH_NEXT,
} eAverageWith;

/* 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;
}

static int displacement_get_grid_and_coord(
        SubdivDisplacement *displacement,
        const int ptex_face_index, const float u, const float v,
        const MDisps **r_displacement_grid,
        float *grid_u, float *grid_v)
{
	MultiresDisplacementData *data = displacement->user_data;
	const PolyCornerIndex *poly_corner =
	        &data->ptex_poly_corner[ptex_face_index];
	const MPoly *poly = &data->mpoly[poly_corner->poly_index];
	const int start_grid_index = poly->loopstart + poly_corner->corner;
	int corner = 0;
	if (poly->totloop == 4) {
		float corner_u, corner_v;
		corner = rotate_quad_to_corner(u, v, &corner_u, &corner_v);
		*r_displacement_grid = &data->mdisps[start_grid_index + corner];
		ptex_uv_to_grid_uv(corner_u, corner_v, grid_u, grid_v);
	}
	else {
		*r_displacement_grid = &data->mdisps[start_grid_index];
		ptex_uv_to_grid_uv(u, v, grid_u, grid_v);
	}
	return corner;
}

static const MDisps *displacement_get_next_grid(
        SubdivDisplacement *displacement,
        const int ptex_face_index, const int corner)
{
	MultiresDisplacementData *data = displacement->user_data;
	const PolyCornerIndex *poly_corner =
	        &data->ptex_poly_corner[ptex_face_index];
	const MPoly *poly = &data->mpoly[poly_corner->poly_index];
	const int effective_corner = (poly->totloop == 4) ? corner
	                                                  : poly_corner->corner;
	const int next_corner = (effective_corner + 1) % poly->totloop;
	return &data->mdisps[poly->loopstart + next_corner];
}

static const MDisps *displacement_get_prev_grid(
        SubdivDisplacement *displacement,
        const int ptex_face_index, const int corner)
{
	MultiresDisplacementData *data = displacement->user_data;
	const PolyCornerIndex *poly_corner =
	        &data->ptex_poly_corner[ptex_face_index];
	const MPoly *poly = &data->mpoly[poly_corner->poly_index];
	const int effective_corner = (poly->totloop == 4) ? corner
	                                                  : poly_corner->corner;
	const int prev_corner =
	        (effective_corner - 1 + poly->totloop) % poly->totloop;
	return &data->mdisps[poly->loopstart + prev_corner];
}

/* NOTE: Derivatives are in ptex face space. */
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]);
}

BLI_INLINE eAverageWith read_displacement_grid(
        const MDisps *displacement_grid,
        const int grid_size,
        const float grid_u, const float grid_v,
        float r_tangent_D[3])
{
	if (displacement_grid->disps == NULL) {
		zero_v3(r_tangent_D);
		return AVERAGE_WITH_NONE;
	}
	const int x = (grid_u * (grid_size - 1) + 0.5f);
	const int y = (grid_v * (grid_size - 1) + 0.5f);
	copy_v3_v3(r_tangent_D, displacement_grid->disps[y * grid_size + x]);
	if (x == 0 && y == 0) {
		return AVERAGE_WITH_ALL;
	}
	else if (x == 0) {
		return AVERAGE_WITH_PREV;
	}
	else if (y == 0) {
		return AVERAGE_WITH_NEXT;
	}
	return AVERAGE_WITH_NONE;
}

static void average_with_all(
        SubdivDisplacement *displacement,
        const int ptex_face_index, const int corner,
        const float UNUSED(grid_u), const float UNUSED(grid_v),
        float r_tangent_D[3])
{
	MultiresDisplacementData *data = displacement->user_data;
	const PolyCornerIndex *poly_corner =
	        &data->ptex_poly_corner[ptex_face_index];
	const MPoly *poly = &data->mpoly[poly_corner->poly_index];
	for (int current_corner = 0;
	     current_corner < poly->totloop;
	     current_corner++)
	{
		if (current_corner == corner) {
			continue;
		}
		const MDisps *displacement_grid =
		        &data->mdisps[poly->loopstart + current_corner];
		const float *current_tangent_D = displacement_grid->disps[0];
		r_tangent_D[2] += current_tangent_D[2];
	}
	r_tangent_D[2] /= (float)poly->totloop;
}

static void average_with_next(SubdivDisplacement *displacement,
                              const int ptex_face_index, const int corner,
                              const float grid_u, const float UNUSED(grid_v),
                              float r_tangent_D[3])
{
	MultiresDisplacementData *data = displacement->user_data;
	const int grid_size = data->grid_size;
	const MDisps *next_displacement_grid = displacement_get_next_grid(
	         displacement, ptex_face_index, corner);
	float next_tangent_D[3];
	read_displacement_grid(next_displacement_grid, grid_size,
	                       0.0f, grid_u,
	                       next_tangent_D);
	r_tangent_D[2] += next_tangent_D[2];
	r_tangent_D[2] *= 0.5f;
}

static void average_with_prev(SubdivDisplacement *displacement,
                              const int ptex_face_index, const int corner,
                              const float UNUSED(grid_u), const float grid_v,
                              float r_tangent_D[3])
{
	MultiresDisplacementData *data = displacement->user_data;
	const int grid_size = data->grid_size;
	const MDisps *prev_displacement_grid = displacement_get_prev_grid(
	         displacement, ptex_face_index, corner);
	float prev_tangent_D[3];
	read_displacement_grid(prev_displacement_grid, grid_size,
	                       grid_v, 0.0f,
	                       prev_tangent_D);
	r_tangent_D[2] += prev_tangent_D[2];
	r_tangent_D[2] *= 0.5f;
}

static void average_displacement(SubdivDisplacement *displacement,
                                 const int ptex_face_index, const int corner,
                                 eAverageWith average_with,
                                 const float grid_u, const float grid_v,
                                 float r_tangent_D[3])
{
	switch (average_with) {
		case AVERAGE_WITH_ALL:
			average_with_all(displacement,
			                 ptex_face_index, corner,
			                 grid_u, grid_v,
			                 r_tangent_D);
			break;
		case AVERAGE_WITH_PREV:
			average_with_prev(displacement,
			                  ptex_face_index, corner,
			                  grid_u, grid_v,
			                  r_tangent_D);
			break;
		case AVERAGE_WITH_NEXT:
			average_with_next(displacement,
			                  ptex_face_index, corner,
			                  grid_u, grid_v,
			                  r_tangent_D);
			break;
		case AVERAGE_WITH_NONE:
			break;
	}
}

static void eval_displacement(SubdivDisplacement *displacement,
                              const int ptex_face_index,
                              const float u, const float v,
                              const float dPdu[3], const float dPdv[3],
                              float r_D[3])
{
	MultiresDisplacementData *data = displacement->user_data;
	const int grid_size = data->grid_size;
	/* Get displacement in tangent space. */
	const MDisps *displacement_grid;
	float grid_u, grid_v;
	int corner = displacement_get_grid_and_coord(displacement,
	                                             ptex_face_index, u, v,
	                                             &displacement_grid,
	                                             &grid_u, &grid_v);
	/* Read displacement from the current displacement grid and see if any
	 * averaging is needed.
	 */
	float tangent_D[3];
	eAverageWith average_with =
	        read_displacement_grid(displacement_grid, grid_size,
	                               grid_u, grid_v,
	                               tangent_D);
	average_displacement(displacement,
	                     ptex_face_index, corner,
	                     average_with, grid_u, grid_v,
	                     tangent_D);
	/* Convert it to the object space. */
	float tangent_matrix[3][3];
	construct_tangent_matrix(tangent_matrix, dPdu, dPdv, corner);
	mul_v3_m3v3(r_D, tangent_matrix, tangent_D);
}

static void free_displacement(SubdivDisplacement *displacement)
{
	MultiresDisplacementData *data = displacement->user_data;
	MEM_freeN(data->ptex_poly_corner);
	MEM_freeN(data);
}

/* TODO(sergey): This seems to be generally used information, which almost
 * worth adding to a subdiv itself, with possible cache of the value.
 */
static int count_num_ptex_faces(const Mesh *mesh)
{
	int num_ptex_faces = 0;
	const MPoly *mpoly = mesh->mpoly;
	for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
		const MPoly *poly = &mpoly[poly_index];
		num_ptex_faces += (poly->totloop == 4) ? 1 : poly->totloop;
	}
	return num_ptex_faces;
}

static void displacement_data_init_mapping(SubdivDisplacement *displacement,
                                           const Mesh *mesh)
{
	MultiresDisplacementData *data = displacement->user_data;
	const MPoly *mpoly = mesh->mpoly;
	const int num_ptex_faces = count_num_ptex_faces(mesh);
	/* Allocate memory. */
	data->ptex_poly_corner = MEM_malloc_arrayN(num_ptex_faces,
	                                           sizeof(*data->ptex_poly_corner),
	                                           "ptex poly corner");
	/* Fill in offsets. */
	int ptex_face_index = 0;
	PolyCornerIndex *ptex_poly_corner = data->ptex_poly_corner;
	for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
		const MPoly *poly = &mpoly[poly_index];
		if (poly->totloop == 4) {
			ptex_poly_corner[ptex_face_index].poly_index = poly_index;
			ptex_poly_corner[ptex_face_index].corner = 0;
			ptex_face_index++;
		}
		else {
			for (int corner = 0; corner < poly->totloop; corner++) {
				ptex_poly_corner[ptex_face_index].poly_index = poly_index;
				ptex_poly_corner[ptex_face_index].corner = corner;
				ptex_face_index++;
			}
		}
	}
}

static void displacement_init_data(SubdivDisplacement *displacement,
                                   const Mesh *mesh,
                                   const MultiresModifierData *mmd)
{
	MultiresDisplacementData *data = displacement->user_data;
	data->grid_size = (1 << (mmd->totlvl - 1)) + 1;
	data->mpoly = mesh->mpoly;
	data->mdisps = CustomData_get_layer(&mesh->ldata, CD_MDISPS);
	displacement_data_init_mapping(displacement, mesh);
}

static void displacement_init_functions(SubdivDisplacement *displacement)
{
	displacement->eval_displacement = eval_displacement;
	displacement->free = free_displacement;
}

void BKE_subdiv_displacement_attach_from_multires(
        Subdiv *subdiv,
        const Mesh *mesh,
        const MultiresModifierData *mmd)
{
	/* Make sure we don't have previously assigned displacement. */
	BKE_subdiv_displacement_detach(subdiv);
	/* Allocate all required memory. */
	SubdivDisplacement *displacement = MEM_callocN(sizeof(SubdivDisplacement),
	                                               "multires displacement");
	displacement->user_data = MEM_callocN(sizeof(MultiresDisplacementData),
	                                      "multires displacement data");
	displacement_init_data(displacement, mesh, mmd);
	displacement_init_functions(displacement);
	/* Finish. */
	subdiv->displacement_evaluator = displacement;
}