4c2ed07cdfab257fdbb53e0dd98e273979203afd
[blender.git] / source / blender / blenkernel / intern / subdiv_eval.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2018 by Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Sergey Sharybin.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/subdiv_eval.c
27  *  \ingroup bke
28  */
29
30 #include "BKE_subdiv.h"
31
32 #include "DNA_mesh_types.h"
33 #include "DNA_meshdata_types.h"
34
35 #include "BLI_utildefines.h"
36 #include "BLI_math_vector.h"
37
38 #include "BKE_customdata.h"
39
40 #ifdef WITH_OPENSUBDIV
41 #  include "opensubdiv_evaluator_capi.h"
42 #  include "opensubdiv_topology_refiner_capi.h"
43 #endif
44
45 void BKE_subdiv_eval_begin(Subdiv *subdiv)
46 {
47 #ifdef WITH_OPENSUBDIV
48         if (subdiv->evaluator == NULL) {
49                 BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
50                 subdiv->evaluator = openSubdiv_createEvaluatorFromTopologyRefiner(
51                         subdiv->topology_refiner);
52                 BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_CREATE);
53         }
54         else {
55                 /* TODO(sergey): Check for topology change. */
56         }
57 #else
58         UNUSED_VARS(subdiv);
59 #endif
60 }
61
62 #ifdef WITH_OPENSUBDIV
63 static void set_face_varying_data_from_uv(Subdiv *subdiv,
64                                           const MLoopUV *mloopuv,
65                                           const int layer_index)
66 {
67         OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
68         OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
69         const int num_faces = topology_refiner->getNumFaces(topology_refiner);
70         const MLoopUV *mluv = mloopuv;
71         /* TODO(sergey): OpenSubdiv's C-API converter can change winding of
72          * loops of a face, need to watch for that, to prevent wrong UVs assigned.
73          */
74         for (int face_index = 0; face_index < num_faces; ++face_index) {
75                 const int num_face_vertices = topology_refiner->getNumFaceVertices(
76                         topology_refiner, face_index);
77                 const int *uv_indicies = topology_refiner->getFaceFVarValueIndices(
78                         topology_refiner, face_index, layer_index);
79                 for (int vertex_index = 0;
80                      vertex_index < num_face_vertices;
81                      vertex_index++, mluv++)
82                 {
83                         evaluator->setFaceVaryingData(evaluator,
84                                                       mluv->uv,
85                                                       uv_indicies[vertex_index],
86                                                       1);
87                 }
88         }
89 }
90 #endif
91
92 void BKE_subdiv_eval_update_from_mesh(Subdiv *subdiv, const Mesh *mesh)
93 {
94 #ifdef WITH_OPENSUBDIV
95         BKE_subdiv_eval_begin(subdiv);
96         /* Set coordinates of base mesh vertices. */
97         subdiv->evaluator->setCoarsePositionsFromBuffer(
98                 subdiv->evaluator,
99                 mesh->mvert,
100                 offsetof(MVert, co),
101                 sizeof(MVert),
102                 0, mesh->totvert);
103         /* Set face-varyign data to UV maps. */
104         const int num_uv_layers =
105                 CustomData_number_of_layers(&mesh->ldata, CD_MLOOPUV);
106         for (int layer_index = 0; layer_index < num_uv_layers; layer_index++) {
107                 const MLoopUV *mloopuv = CustomData_get_layer_n(
108                         &mesh->ldata, CD_MLOOPUV, layer_index);
109                 set_face_varying_data_from_uv(subdiv, mloopuv, layer_index);
110                 /* NOTE: Currently evaluator can only handle single face varying layer.
111                  * This is a limitation of C-API and some underlying helper classes from
112                  * our side which will get fixed.
113                  */
114                 break;
115         }
116         /* Update evaluator to the new coarse geometry. */
117         BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
118         subdiv->evaluator->refine(subdiv->evaluator);
119         BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
120 #else
121         UNUSED_VARS(subdiv, mesh);
122 #endif
123 }
124
125 /* ========================== Single point queries ========================== */
126
127 void BKE_subdiv_eval_limit_point(
128         Subdiv *subdiv,
129         const int ptex_face_index,
130         const float u, const float v,
131         float P[3])
132 {
133         BKE_subdiv_eval_limit_point_and_derivatives(subdiv,
134                                                     ptex_face_index,
135                                                     u, v,
136                                                     P, NULL, NULL);
137 }
138
139 void BKE_subdiv_eval_limit_point_and_derivatives(
140         Subdiv *subdiv,
141         const int ptex_face_index,
142         const float u, const float v,
143         float P[3], float dPdu[3], float dPdv[3])
144 {
145 #ifdef WITH_OPENSUBDIV
146         subdiv->evaluator->evaluateLimit(subdiv->evaluator,
147                                          ptex_face_index,
148                                          u, v,
149                                          P, dPdu, dPdv);
150 #else
151         UNUSED_VARS(subdiv, ptex_face_index, u, v, P, dPdu, dPdv);
152 #endif
153 }
154
155 void BKE_subdiv_eval_limit_point_and_normal(
156         Subdiv *subdiv,
157         const int ptex_face_index,
158         const float u, const float v,
159         float P[3], float N[3])
160 {
161         float dPdu[3], dPdv[3];
162         BKE_subdiv_eval_limit_point_and_derivatives(subdiv,
163                                                     ptex_face_index,
164                                                     u, v,
165                                                     P, dPdu, dPdv);
166         cross_v3_v3v3(N, dPdu, dPdv);
167         normalize_v3(N);
168 }
169
170 void BKE_subdiv_eval_limit_point_and_short_normal(
171         Subdiv *subdiv,
172         const int ptex_face_index,
173         const float u, const float v,
174         float P[3], short N[3])
175 {
176         float N_float[3];
177         BKE_subdiv_eval_limit_point_and_normal(subdiv,
178                                                ptex_face_index,
179                                                u, v,
180                                                P, N_float);
181         normal_float_to_short_v3(N, N_float);
182 }
183
184 void BKE_subdiv_eval_face_varying(
185         Subdiv *subdiv,
186         const int ptex_face_index,
187         const float u, const float v,
188         float face_varying[2])
189 {
190 #ifdef WITH_OPENSUBDIV
191         subdiv->evaluator->evaluateFaceVarying(subdiv->evaluator,
192                                                ptex_face_index,
193                                                u, v,
194                                                face_varying);
195 #else
196         UNUSED_VARS(subdiv, ptex_face_index, u, v, face_varying);
197 #endif
198 }
199
200 /* ===================  Patch queries at given resolution =================== */
201
202 /* Move buffer forward by a given number of bytes. */
203 static void buffer_apply_offset(void **buffer, const int offset)
204 {
205   *buffer = ((unsigned char *)*buffer) + offset;
206 }
207
208 /* Write given number of floats to the beginning of given buffer.  */
209 static void buffer_write_float_value(void **buffer,
210                                      const float *values_buffer, int num_values)
211 {
212         memcpy(*buffer, values_buffer, sizeof(float) * num_values);
213 }
214
215 /* Similar to above, just operates with short values. */
216 static void buffer_write_short_value(void **buffer,
217                                      const short *values_buffer, int num_values)
218 {
219         memcpy(*buffer, values_buffer, sizeof(short) * num_values);
220 }
221
222 void BKE_subdiv_eval_limit_patch_resolution_point(
223         Subdiv *subdiv,
224         const int ptex_face_index,
225         const int resolution,
226         void *buffer, const int offset, const int stride)
227 {
228         buffer_apply_offset(&buffer, offset);
229         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
230         for (int y = 0; y < resolution; y++) {
231                 const float v = y * inv_resolution_1;
232                 for (int x = 0; x < resolution; x++) {
233                         const float u = x * inv_resolution_1;
234                         BKE_subdiv_eval_limit_point(subdiv,
235                                                     ptex_face_index,
236                                                     u, v,
237                                                     buffer);
238                         buffer_apply_offset(&buffer, stride);
239                 }
240         }
241 }
242
243 void BKE_subdiv_eval_limit_patch_resolution_point_and_derivatives(
244         Subdiv *subdiv,
245         const int ptex_face_index,
246         const int resolution,
247         void *point_buffer, const int point_offset, const int point_stride,
248         void *du_buffer, const int du_offset, const int du_stride,
249         void *dv_buffer, const int dv_offset, const int dv_stride)
250 {
251         buffer_apply_offset(&point_buffer, point_offset);
252         buffer_apply_offset(&du_buffer, du_offset);
253         buffer_apply_offset(&dv_buffer, dv_offset);
254         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
255         for (int y = 0; y < resolution; y++) {
256                 const float v = y * inv_resolution_1;
257                 for (int x = 0; x < resolution; x++) {
258                         const float u = x * inv_resolution_1;
259                         BKE_subdiv_eval_limit_point_and_derivatives(
260                                 subdiv,
261                                 ptex_face_index,
262                                 u, v,
263                                 point_buffer, du_buffer, dv_buffer);
264                         buffer_apply_offset(&point_buffer, point_stride);
265                         buffer_apply_offset(&du_buffer, du_stride);
266                         buffer_apply_offset(&dv_buffer, dv_stride);
267                 }
268         }
269 }
270
271 void BKE_subdiv_eval_limit_patch_resolution_point_and_normal(
272         Subdiv *subdiv,
273         const int ptex_face_index,
274         const int resolution,
275         void *point_buffer, const int point_offset, const int point_stride,
276         void *normal_buffer, const int normal_offset, const int normal_stride)
277 {
278         buffer_apply_offset(&point_buffer, point_offset);
279         buffer_apply_offset(&normal_buffer, normal_offset);
280         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
281         for (int y = 0; y < resolution; y++) {
282                 const float v = y * inv_resolution_1;
283                 for (int x = 0; x < resolution; x++) {
284                         const float u = x * inv_resolution_1;
285                         float normal[3];
286                         BKE_subdiv_eval_limit_point_and_normal(
287                                 subdiv,
288                                 ptex_face_index,
289                                 u, v,
290                                 point_buffer, normal);
291                         buffer_write_float_value(&normal_buffer, normal, 3);
292                         buffer_apply_offset(&point_buffer, point_stride);
293                         buffer_apply_offset(&normal_buffer, normal_stride);
294                 }
295         }
296 }
297
298 void BKE_subdiv_eval_limit_patch_resolution_point_and_short_normal(
299         Subdiv *subdiv,
300         const int ptex_face_index,
301         const int resolution,
302         void *point_buffer, const int point_offset, const int point_stride,
303         void *normal_buffer, const int normal_offset, const int normal_stride)
304 {
305         buffer_apply_offset(&point_buffer, point_offset);
306         buffer_apply_offset(&normal_buffer, normal_offset);
307         const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
308         for (int y = 0; y < resolution; y++) {
309                 const float v = y * inv_resolution_1;
310                 for (int x = 0; x < resolution; x++) {
311                         const float u = x * inv_resolution_1;
312                         short normal[3];
313                         BKE_subdiv_eval_limit_point_and_short_normal(
314                                 subdiv,
315                                 ptex_face_index,
316                                 u, v,
317                                 point_buffer, normal);
318                         buffer_write_short_value(&normal_buffer, normal, 3);
319                         buffer_apply_offset(&point_buffer, point_stride);
320                         buffer_apply_offset(&normal_buffer, normal_stride);
321                 }
322         }
323 }