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

subdiv_eval.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e23be84ee265eb04234d06081ca765b88e6eca64 (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
/*
 * ***** 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_eval.c
 *  \ingroup bke
 */

#include "BKE_subdiv.h"

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"

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

#include "BKE_customdata.h"

#include "MEM_guardedalloc.h"

#ifdef WITH_OPENSUBDIV
#  include "opensubdiv_evaluator_capi.h"
#  include "opensubdiv_topology_refiner_capi.h"
#endif

void BKE_subdiv_eval_begin(Subdiv *subdiv)
{
#ifdef WITH_OPENSUBDIV
	if (subdiv->topology_refiner == NULL) {
		/* Happens on input mesh with just loose geometry. */
	}
	else if (subdiv->evaluator == NULL) {
		BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
		subdiv->evaluator = openSubdiv_createEvaluatorFromTopologyRefiner(
		        subdiv->topology_refiner);
		BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
	}
	else {
		/* TODO(sergey): Check for topology change. */
	}
#else
	UNUSED_VARS(subdiv);
#endif
}

#ifdef WITH_OPENSUBDIV
static void set_coarse_positions(Subdiv *subdiv, const Mesh *mesh)
{
	const MVert *mvert = mesh->mvert;
	const MLoop *mloop = mesh->mloop;
	const MPoly *mpoly = mesh->mpoly;
	/* Mark vertices which needs new coordinates. */
	/* TODO(sergey): This is annoying to calculate this on every update,
	 * maybe it's better to cache this mapping. Or make it possible to have
	 * OpenSubdiv's vertices match mesh ones?
	 */
	BLI_bitmap *vertex_used_map =
	        BLI_BITMAP_NEW(mesh->totvert, "vert used map");
	for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
		const MPoly *poly = &mpoly[poly_index];
		for (int corner = 0; corner < poly->totloop; corner++) {
			const MLoop *loop = &mloop[poly->loopstart + corner];
			BLI_BITMAP_ENABLE(vertex_used_map, loop->v);
		}
	}
	for (int vertex_index = 0, manifold_veretx_index = 0;
	     vertex_index < mesh->totvert;
	     vertex_index++)
	{
		if (!BLI_BITMAP_TEST_BOOL(vertex_used_map, vertex_index)) {
			continue;
		}
		const MVert *vertex = &mvert[vertex_index];
		subdiv->evaluator->setCoarsePositions(
		        subdiv->evaluator,
		        vertex->co,
		        manifold_veretx_index, 1);
		manifold_veretx_index++;
	}
	MEM_freeN(vertex_used_map);
}

static void set_face_varying_data_from_uv(Subdiv *subdiv,
                                          const MLoopUV *mloopuv,
                                          const int layer_index)
{
	OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
	OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
	const int num_faces = topology_refiner->getNumFaces(topology_refiner);
	const MLoopUV *mluv = mloopuv;
	/* TODO(sergey): OpenSubdiv's C-API converter can change winding of
	 * loops of a face, need to watch for that, to prevent wrong UVs assigned.
	 */
	for (int face_index = 0; face_index < num_faces; ++face_index) {
		const int num_face_vertices = topology_refiner->getNumFaceVertices(
		        topology_refiner, face_index);
		const int *uv_indicies = topology_refiner->getFaceFVarValueIndices(
		        topology_refiner, face_index, layer_index);
		for (int vertex_index = 0;
		     vertex_index < num_face_vertices;
		     vertex_index++, mluv++)
		{
			evaluator->setFaceVaryingData(evaluator,
			                              layer_index,
			                              mluv->uv,
			                              uv_indicies[vertex_index],
			                              1);
		}
	}
}
#endif

void BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const Mesh *mesh)
{
#ifdef WITH_OPENSUBDIV
	BKE_subdiv_eval_begin(subdiv);
	if (subdiv->evaluator == NULL) {
		return;
	}
	/* Set coordinates of base mesh vertices. */
	set_coarse_positions(subdiv, mesh);
	/* Set face-varyign data to UV maps. */
	const int num_uv_layers =
	        CustomData_number_of_layers(&mesh->ldata, CD_MLOOPUV);
	for (int layer_index = 0; layer_index < num_uv_layers; layer_index++) {
		const MLoopUV *mloopuv = CustomData_get_layer_n(
		        &mesh->ldata, CD_MLOOPUV, layer_index);
		set_face_varying_data_from_uv(subdiv, mloopuv, layer_index);
	}
	/* Update evaluator to the new coarse geometry. */
	BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
	subdiv->evaluator->refine(subdiv->evaluator);
	BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
#else
	UNUSED_VARS(subdiv, mesh);
#endif
}

/* ========================== Single point queries ========================== */

void BKE_subdiv_eval_limit_point(
        Subdiv *subdiv,
        const int ptex_face_index,
        const float u, const float v,
        float P[3])
{
	BKE_subdiv_eval_limit_point_and_derivatives(subdiv,
	                                            ptex_face_index,
	                                            u, v,
	                                            P, NULL, NULL);
}

void BKE_subdiv_eval_limit_point_and_derivatives(
        Subdiv *subdiv,
        const int ptex_face_index,
        const float u, const float v,
        float P[3], float dPdu[3], float dPdv[3])
{
#ifdef WITH_OPENSUBDIV
	subdiv->evaluator->evaluateLimit(subdiv->evaluator,
	                                 ptex_face_index,
	                                 u, v,
	                                 P, dPdu, dPdv);
#else
	UNUSED_VARS(subdiv, ptex_face_index, u, v, P, dPdu, dPdv);
#endif
}

void BKE_subdiv_eval_limit_point_and_normal(
        Subdiv *subdiv,
        const int ptex_face_index,
        const float u, const float v,
        float P[3], float N[3])
{
	float dPdu[3], dPdv[3];
	BKE_subdiv_eval_limit_point_and_derivatives(subdiv,
	                                            ptex_face_index,
	                                            u, v,
	                                            P, dPdu, dPdv);
	cross_v3_v3v3(N, dPdu, dPdv);
	normalize_v3(N);
}

void BKE_subdiv_eval_limit_point_and_short_normal(
        Subdiv *subdiv,
        const int ptex_face_index,
        const float u, const float v,
        float P[3], short N[3])
{
	float N_float[3];
	BKE_subdiv_eval_limit_point_and_normal(subdiv,
	                                       ptex_face_index,
	                                       u, v,
	                                       P, N_float);
	normal_float_to_short_v3(N, N_float);
}

void BKE_subdiv_eval_face_varying(
        Subdiv *subdiv,
        const int face_varying_channel,
        const int ptex_face_index,
        const float u, const float v,
        float face_varying[2])
{
#ifdef WITH_OPENSUBDIV
	subdiv->evaluator->evaluateFaceVarying(subdiv->evaluator,
	                                       face_varying_channel,
	                                       ptex_face_index,
	                                       u, v,
	                                       face_varying);
#else
	UNUSED_VARS(subdiv, face_varying_channel, ptex_face_index, u, v, face_varying);
#endif
}

/* ===================  Patch queries at given resolution =================== */

/* Move buffer forward by a given number of bytes. */
static void buffer_apply_offset(void **buffer, const int offset)
{
  *buffer = ((unsigned char *)*buffer) + offset;
}

/* Write given number of floats to the beginning of given buffer.  */
static void buffer_write_float_value(void **buffer,
                                     const float *values_buffer, int num_values)
{
	memcpy(*buffer, values_buffer, sizeof(float) * num_values);
}

/* Similar to above, just operates with short values. */
static void buffer_write_short_value(void **buffer,
                                     const short *values_buffer, int num_values)
{
	memcpy(*buffer, values_buffer, sizeof(short) * num_values);
}

void BKE_subdiv_eval_limit_patch_resolution_point(
        Subdiv *subdiv,
        const int ptex_face_index,
        const int resolution,
        void *buffer, const int offset, const int stride)
{
	buffer_apply_offset(&buffer, offset);
	const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
	for (int y = 0; y < resolution; y++) {
		const float v = y * inv_resolution_1;
		for (int x = 0; x < resolution; x++) {
			const float u = x * inv_resolution_1;
			BKE_subdiv_eval_limit_point(subdiv,
			                            ptex_face_index,
			                            u, v,
			                            buffer);
			buffer_apply_offset(&buffer, stride);
		}
	}
}

void BKE_subdiv_eval_limit_patch_resolution_point_and_derivatives(
        Subdiv *subdiv,
        const int ptex_face_index,
        const int resolution,
        void *point_buffer, const int point_offset, const int point_stride,
        void *du_buffer, const int du_offset, const int du_stride,
        void *dv_buffer, const int dv_offset, const int dv_stride)
{
	buffer_apply_offset(&point_buffer, point_offset);
	buffer_apply_offset(&du_buffer, du_offset);
	buffer_apply_offset(&dv_buffer, dv_offset);
	const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
	for (int y = 0; y < resolution; y++) {
		const float v = y * inv_resolution_1;
		for (int x = 0; x < resolution; x++) {
			const float u = x * inv_resolution_1;
			BKE_subdiv_eval_limit_point_and_derivatives(
			        subdiv,
			        ptex_face_index,
			        u, v,
			        point_buffer, du_buffer, dv_buffer);
			buffer_apply_offset(&point_buffer, point_stride);
			buffer_apply_offset(&du_buffer, du_stride);
			buffer_apply_offset(&dv_buffer, dv_stride);
		}
	}
}

void BKE_subdiv_eval_limit_patch_resolution_point_and_normal(
        Subdiv *subdiv,
        const int ptex_face_index,
        const int resolution,
        void *point_buffer, const int point_offset, const int point_stride,
        void *normal_buffer, const int normal_offset, const int normal_stride)
{
	buffer_apply_offset(&point_buffer, point_offset);
	buffer_apply_offset(&normal_buffer, normal_offset);
	const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
	for (int y = 0; y < resolution; y++) {
		const float v = y * inv_resolution_1;
		for (int x = 0; x < resolution; x++) {
			const float u = x * inv_resolution_1;
			float normal[3];
			BKE_subdiv_eval_limit_point_and_normal(
			        subdiv,
			        ptex_face_index,
			        u, v,
			        point_buffer, normal);
			buffer_write_float_value(&normal_buffer, normal, 3);
			buffer_apply_offset(&point_buffer, point_stride);
			buffer_apply_offset(&normal_buffer, normal_stride);
		}
	}
}

void BKE_subdiv_eval_limit_patch_resolution_point_and_short_normal(
        Subdiv *subdiv,
        const int ptex_face_index,
        const int resolution,
        void *point_buffer, const int point_offset, const int point_stride,
        void *normal_buffer, const int normal_offset, const int normal_stride)
{
	buffer_apply_offset(&point_buffer, point_offset);
	buffer_apply_offset(&normal_buffer, normal_offset);
	const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
	for (int y = 0; y < resolution; y++) {
		const float v = y * inv_resolution_1;
		for (int x = 0; x < resolution; x++) {
			const float u = x * inv_resolution_1;
			short normal[3];
			BKE_subdiv_eval_limit_point_and_short_normal(
			        subdiv,
			        ptex_face_index,
			        u, v,
			        point_buffer, normal);
			buffer_write_short_value(&normal_buffer, normal, 3);
			buffer_apply_offset(&point_buffer, point_stride);
			buffer_apply_offset(&normal_buffer, normal_stride);
		}
	}
}