Pass EvaluationContext argument everywhere
[blender.git] / source / blender / modifiers / intern / MOD_uvproject.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) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_uvproject.c
32  *  \ingroup modifiers
33  */
34
35
36 /* UV Project modifier: Generates UVs projected from an object */
37
38 #include "DNA_meshdata_types.h"
39 #include "DNA_camera_types.h"
40 #include "DNA_object_types.h"
41
42 #include "BLI_math.h"
43 #include "BLI_uvproject.h"
44 #include "BLI_utildefines.h"
45
46
47 #include "BKE_camera.h"
48 #include "BKE_library.h"
49 #include "BKE_library_query.h"
50 #include "BKE_material.h"
51 #include "BKE_mesh.h"
52 #include "BKE_DerivedMesh.h"
53
54 #include "MOD_modifiertypes.h"
55
56 #include "MEM_guardedalloc.h"
57
58 #include "DEG_depsgraph_build.h"
59
60 static void initData(ModifierData *md)
61 {
62         UVProjectModifierData *umd = (UVProjectModifierData *) md;
63
64
65         umd->num_projectors = 1;
66         umd->aspectx = umd->aspecty = 1.0f;
67         umd->scalex = umd->scaley = 1.0f;
68 }
69
70 static void copyData(ModifierData *md, ModifierData *target)
71 {
72 #if 0
73         UVProjectModifierData *umd = (UVProjectModifierData *) md;
74 #endif
75         UVProjectModifierData *tumd = (UVProjectModifierData *) target;
76
77         modifier_copyData_generic(md, target);
78
79         id_us_plus((ID *)tumd->image);
80 }
81
82 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md))
83 {
84         CustomDataMask dataMask = 0;
85
86         /* ask for UV coordinates */
87         dataMask |= CD_MLOOPUV;
88
89         return dataMask;
90 }
91
92 static void foreachObjectLink(ModifierData *md, Object *ob,
93                               ObjectWalkFunc walk, void *userData)
94 {
95         UVProjectModifierData *umd = (UVProjectModifierData *) md;
96         int i;
97
98         for (i = 0; i < MOD_UVPROJECT_MAXPROJECTORS; ++i)
99                 walk(userData, ob, &umd->projectors[i], IDWALK_CB_NOP);
100 }
101
102 static void foreachIDLink(ModifierData *md, Object *ob,
103                           IDWalkFunc walk, void *userData)
104 {
105         UVProjectModifierData *umd = (UVProjectModifierData *) md;
106
107         walk(userData, ob, (ID **)&umd->image, IDWALK_CB_USER);
108
109         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
110 }
111
112 static void updateDepsgraph(ModifierData *md,
113                             struct Main *UNUSED(bmain),
114                             struct Scene *UNUSED(scene),
115                             Object *UNUSED(ob),
116                             struct DepsNodeHandle *node)
117 {
118         UVProjectModifierData *umd = (UVProjectModifierData *)md;
119         int i;
120         for (i = 0; i < umd->num_projectors; ++i) {
121                 if (umd->projectors[i] != NULL) {
122                         DEG_add_object_relation(node, umd->projectors[i], DEG_OB_COMP_TRANSFORM, "UV Project Modifier");
123                 }
124         }
125 }
126
127 typedef struct Projector {
128         Object *ob;             /* object this projector is derived from */
129         float projmat[4][4];    /* projection matrix */
130         float normal[3];        /* projector normal in world space */
131         void *uci;              /* optional uv-project info (panorama projection) */
132 } Projector;
133
134 static DerivedMesh *uvprojectModifier_do(UVProjectModifierData *umd,
135                                          Object *ob, DerivedMesh *dm)
136 {
137         float (*coords)[3], (*co)[3];
138         MLoopUV *mloop_uv;
139         int i, numVerts, numPolys, numLoops;
140         Image *image = umd->image;
141         MPoly *mpoly, *mp;
142         MLoop *mloop;
143         Projector projectors[MOD_UVPROJECT_MAXPROJECTORS];
144         int num_projectors = 0;
145         char uvname[MAX_CUSTOMDATA_LAYER_NAME];
146         float aspx = umd->aspectx ? umd->aspectx : 1.0f;
147         float aspy = umd->aspecty ? umd->aspecty : 1.0f;
148         float scax = umd->scalex ? umd->scalex : 1.0f;
149         float scay = umd->scaley ? umd->scaley : 1.0f;
150         int free_uci = 0;
151
152         for (i = 0; i < umd->num_projectors; ++i)
153                 if (umd->projectors[i])
154                         projectors[num_projectors++].ob = umd->projectors[i];
155
156         if (num_projectors == 0) return dm;
157
158         /* make sure there are UV Maps available */
159
160         if (!CustomData_has_layer(&dm->loopData, CD_MLOOPUV)) return dm;
161
162         /* make sure we're using an existing layer */
163         CustomData_validate_layer_name(&dm->loopData, CD_MLOOPUV, umd->uvlayer_name, uvname);
164
165         /* calculate a projection matrix and normal for each projector */
166         for (i = 0; i < num_projectors; ++i) {
167                 float tmpmat[4][4];
168                 float offsetmat[4][4];
169                 Camera *cam = NULL;
170                 /* calculate projection matrix */
171                 invert_m4_m4(projectors[i].projmat, projectors[i].ob->obmat);
172
173                 projectors[i].uci = NULL;
174
175                 if (projectors[i].ob->type == OB_CAMERA) {
176                         
177                         cam = (Camera *)projectors[i].ob->data;
178                         if (cam->type == CAM_PANO) {
179                                 projectors[i].uci = BLI_uvproject_camera_info(projectors[i].ob, NULL, aspx, aspy);
180                                 BLI_uvproject_camera_info_scale(projectors[i].uci, scax, scay);
181                                 free_uci = 1;
182                         }
183                         else {
184                                 CameraParams params;
185
186                                 /* setup parameters */
187                                 BKE_camera_params_init(&params);
188                                 BKE_camera_params_from_object(&params, projectors[i].ob);
189
190                                 /* compute matrix, viewplane, .. */
191                                 BKE_camera_params_compute_viewplane(&params, 1, 1, aspx, aspy);
192
193                                 /* scale the view-plane */
194                                 params.viewplane.xmin *= scax;
195                                 params.viewplane.xmax *= scax;
196                                 params.viewplane.ymin *= scay;
197                                 params.viewplane.ymax *= scay;
198
199                                 BKE_camera_params_compute_matrix(&params);
200                                 mul_m4_m4m4(tmpmat, params.winmat, projectors[i].projmat);
201                         }
202                 }
203                 else {
204                         copy_m4_m4(tmpmat, projectors[i].projmat);
205                 }
206
207                 unit_m4(offsetmat);
208                 mul_mat3_m4_fl(offsetmat, 0.5);
209                 offsetmat[3][0] = offsetmat[3][1] = offsetmat[3][2] = 0.5;
210
211                 mul_m4_m4m4(projectors[i].projmat, offsetmat, tmpmat);
212
213                 /* calculate worldspace projector normal (for best projector test) */
214                 projectors[i].normal[0] = 0;
215                 projectors[i].normal[1] = 0;
216                 projectors[i].normal[2] = 1;
217                 mul_mat3_m4_v3(projectors[i].ob->obmat, projectors[i].normal);
218         }
219
220         numPolys = dm->getNumPolys(dm);
221         numLoops = dm->getNumLoops(dm);
222
223         /* make sure we are not modifying the original UV map */
224         mloop_uv = CustomData_duplicate_referenced_layer_named(&dm->loopData,
225                                                                CD_MLOOPUV, uvname, numLoops);
226
227         numVerts = dm->getNumVerts(dm);
228
229         coords = MEM_mallocN(sizeof(*coords) * numVerts,
230                              "uvprojectModifier_do coords");
231         dm->getVertCos(dm, coords);
232
233         /* convert coords to world space */
234         for (i = 0, co = coords; i < numVerts; ++i, ++co)
235                 mul_m4_v3(ob->obmat, *co);
236         
237         /* if only one projector, project coords to UVs */
238         if (num_projectors == 1 && projectors[0].uci == NULL)
239                 for (i = 0, co = coords; i < numVerts; ++i, ++co)
240                         mul_project_m4_v3(projectors[0].projmat, *co);
241
242         mpoly = dm->getPolyArray(dm);
243         mloop = dm->getLoopArray(dm);
244
245         Image **ob_image_array = NULL;
246         if (image) {
247                 ob_image_array = BKE_object_material_edit_image_get_array(ob);
248         }
249
250         /* apply coords as UVs, and apply image if tfaces are new */
251         for (i = 0, mp = mpoly; i < numPolys; ++i, ++mp) {
252                 if (!image || (mp->mat_nr < ob->totcol ? ob_image_array[mp->mat_nr] : NULL) == image) {
253                         if (num_projectors == 1) {
254                                 if (projectors[0].uci) {
255                                         unsigned int fidx = mp->totloop - 1;
256                                         do {
257                                                 unsigned int lidx = mp->loopstart + fidx;
258                                                 unsigned int vidx = mloop[lidx].v;
259                                                 BLI_uvproject_from_camera(mloop_uv[lidx].uv, coords[vidx], projectors[0].uci);
260                                         } while (fidx--);
261                                 }
262                                 else {
263                                         /* apply transformed coords as UVs */
264                                         unsigned int fidx = mp->totloop - 1;
265                                         do {
266                                                 unsigned int lidx = mp->loopstart + fidx;
267                                                 unsigned int vidx = mloop[lidx].v;
268                                                 copy_v2_v2(mloop_uv[lidx].uv, coords[vidx]);
269                                         } while (fidx--);
270                                 }
271                         }
272                         else {
273                                 /* multiple projectors, select the closest to face normal direction */
274                                 float face_no[3];
275                                 int j;
276                                 Projector *best_projector;
277                                 float best_dot;
278
279                                 /* get the untransformed face normal */
280                                 BKE_mesh_calc_poly_normal_coords(mp, mloop + mp->loopstart, (const float (*)[3])coords, face_no);
281
282                                 /* find the projector which the face points at most directly
283                                  * (projector normal with largest dot product is best)
284                                  */
285                                 best_dot = dot_v3v3(projectors[0].normal, face_no);
286                                 best_projector = &projectors[0];
287
288                                 for (j = 1; j < num_projectors; ++j) {
289                                         float tmp_dot = dot_v3v3(projectors[j].normal,
290                                                                  face_no);
291                                         if (tmp_dot > best_dot) {
292                                                 best_dot = tmp_dot;
293                                                 best_projector = &projectors[j];
294                                         }
295                                 }
296
297                                 if (best_projector->uci) {
298                                         unsigned int fidx = mp->totloop - 1;
299                                         do {
300                                                 unsigned int lidx = mp->loopstart + fidx;
301                                                 unsigned int vidx = mloop[lidx].v;
302                                                 BLI_uvproject_from_camera(mloop_uv[lidx].uv, coords[vidx], best_projector->uci);
303                                         } while (fidx--);
304                                 }
305                                 else {
306                                         unsigned int fidx = mp->totloop - 1;
307                                         do {
308                                                 unsigned int lidx = mp->loopstart + fidx;
309                                                 unsigned int vidx = mloop[lidx].v;
310                                                 mul_v2_project_m4_v3(mloop_uv[lidx].uv, best_projector->projmat, coords[vidx]);
311                                         } while (fidx--);
312                                 }
313                         }
314                 }
315         }
316
317         MEM_freeN(coords);
318
319         if (ob_image_array) {
320                 MEM_freeN(ob_image_array);
321         }
322         
323         if (free_uci) {
324                 int j;
325                 for (j = 0; j < num_projectors; ++j) {
326                         if (projectors[j].uci) {
327                                 MEM_freeN(projectors[j].uci);
328                         }
329                 }
330         }
331
332         /* Mark tessellated CD layers as dirty. */
333         dm->dirty |= DM_DIRTY_TESS_CDLAYERS;
334
335         return dm;
336 }
337
338 static DerivedMesh *applyModifier(ModifierData *md, struct EvaluationContext *UNUSED(eval_ctx),
339                                   Object *ob, DerivedMesh *derivedData,
340                                   ModifierApplyFlag UNUSED(flag))
341 {
342         DerivedMesh *result;
343         UVProjectModifierData *umd = (UVProjectModifierData *) md;
344
345         result = uvprojectModifier_do(umd, ob, derivedData);
346
347         return result;
348 }
349
350
351 ModifierTypeInfo modifierType_UVProject = {
352         /* name */              "UVProject",
353         /* structName */        "UVProjectModifierData",
354         /* structSize */        sizeof(UVProjectModifierData),
355         /* type */              eModifierTypeType_NonGeometrical,
356         /* flags */             eModifierTypeFlag_AcceptsMesh |
357                                 eModifierTypeFlag_SupportsMapping |
358                                 eModifierTypeFlag_SupportsEditmode |
359                                 eModifierTypeFlag_EnableInEditmode,
360
361         /* copyData */          copyData,
362         /* deformVerts */       NULL,
363         /* deformMatrices */    NULL,
364         /* deformVertsEM */     NULL,
365         /* deformMatricesEM */  NULL,
366         /* applyModifier */     applyModifier,
367         /* applyModifierEM */   NULL,
368         /* initData */          initData,
369         /* requiredDataMask */  requiredDataMask,
370         /* freeData */          NULL,
371         /* isDisabled */        NULL,
372         /* updateDepsgraph */   updateDepsgraph,
373         /* dependsOnTime */     NULL,
374         /* dependsOnNormals */  NULL,
375         /* foreachObjectLink */ foreachObjectLink,
376         /* foreachIDLink */     foreachIDLink,
377         /* foreachTexLink */    NULL,
378 };