Fix building with latest versions of ffmpeg.
[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_mesh.h"
51 #include "BKE_DerivedMesh.h"
52
53 #include "MOD_modifiertypes.h"
54
55 #include "MEM_guardedalloc.h"
56
57 #include "depsgraph_private.h"
58 #include "DEG_depsgraph_build.h"
59
60 static void initData(ModifierData *md)
61 {
62         UVProjectModifierData *umd = (UVProjectModifierData *) md;
63
64         umd->flags = 0;
65         umd->num_projectors = 1;
66         umd->aspectx = umd->aspecty = 1.0f;
67         umd->scalex = umd->scaley = 1.0f;
68 }
69
70 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md))
71 {
72         CustomDataMask dataMask = 0;
73
74         /* ask for UV coordinates */
75         dataMask |= CD_MLOOPUV | CD_MTEXPOLY;
76
77         return dataMask;
78 }
79
80 static void foreachObjectLink(ModifierData *md, Object *ob,
81                               ObjectWalkFunc walk, void *userData)
82 {
83         UVProjectModifierData *umd = (UVProjectModifierData *) md;
84         int i;
85
86         for (i = 0; i < MOD_UVPROJECT_MAXPROJECTORS; ++i)
87                 walk(userData, ob, &umd->projectors[i], IDWALK_CB_NOP);
88 }
89
90 static void foreachIDLink(ModifierData *md, Object *ob,
91                           IDWalkFunc walk, void *userData)
92 {
93         UVProjectModifierData *umd = (UVProjectModifierData *) md;
94
95         walk(userData, ob, (ID **)&umd->image, IDWALK_CB_USER);
96
97         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
98 }
99
100 static void updateDepgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
101 {
102         UVProjectModifierData *umd = (UVProjectModifierData *) md;
103         int i;
104
105         for (i = 0; i < umd->num_projectors; ++i) {
106                 if (umd->projectors[i]) {
107                         DagNode *curNode = dag_get_node(ctx->forest, umd->projectors[i]);
108
109                         dag_add_relation(ctx->forest, curNode, ctx->obNode,
110                                          DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "UV Project Modifier");
111                 }
112         }
113 }
114
115 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
116 {
117         UVProjectModifierData *umd = (UVProjectModifierData *)md;
118         int i;
119         for (i = 0; i < umd->num_projectors; ++i) {
120                 if (umd->projectors[i] != NULL) {
121                         DEG_add_object_relation(ctx->node, umd->projectors[i], DEG_OB_COMP_TRANSFORM, "UV Project Modifier");
122                 }
123         }
124 }
125
126 typedef struct Projector {
127         Object *ob;             /* object this projector is derived from */
128         float projmat[4][4];    /* projection matrix */
129         float normal[3];        /* projector normal in world space */
130         void *uci;              /* optional uv-project info (panorama projection) */
131 } Projector;
132
133 static DerivedMesh *uvprojectModifier_do(UVProjectModifierData *umd,
134                                          Object *ob, DerivedMesh *dm)
135 {
136         float (*coords)[3], (*co)[3];
137         MLoopUV *mloop_uv;
138         MTexPoly *mtexpoly, *mt = NULL;
139         int i, numVerts, numPolys, numLoops;
140         Image *image = umd->image;
141         MPoly *mpoly, *mp;
142         MLoop *mloop;
143         const bool override_image = (umd->flags & MOD_UVPROJECT_OVERRIDEIMAGE) != 0;
144         Projector projectors[MOD_UVPROJECT_MAXPROJECTORS];
145         int num_projectors = 0;
146         char uvname[MAX_CUSTOMDATA_LAYER_NAME];
147         float aspx = umd->aspectx ? umd->aspectx : 1.0f;
148         float aspy = umd->aspecty ? umd->aspecty : 1.0f;
149         float scax = umd->scalex ? umd->scalex : 1.0f;
150         float scay = umd->scaley ? umd->scaley : 1.0f;
151         int free_uci = 0;
152
153         for (i = 0; i < umd->num_projectors; ++i)
154                 if (umd->projectors[i])
155                         projectors[num_projectors++].ob = umd->projectors[i];
156
157         if (num_projectors == 0) return dm;
158
159         /* make sure there are UV Maps available */
160
161         if (!CustomData_has_layer(&dm->loopData, CD_MLOOPUV)) return dm;
162
163         /* make sure we're using an existing layer */
164         CustomData_validate_layer_name(&dm->loopData, CD_MLOOPUV, umd->uvlayer_name, uvname);
165
166         /* calculate a projection matrix and normal for each projector */
167         for (i = 0; i < num_projectors; ++i) {
168                 float tmpmat[4][4];
169                 float offsetmat[4][4];
170                 Camera *cam = NULL;
171                 /* calculate projection matrix */
172                 invert_m4_m4(projectors[i].projmat, projectors[i].ob->obmat);
173
174                 projectors[i].uci = NULL;
175
176                 if (projectors[i].ob->type == OB_CAMERA) {
177                         
178                         cam = (Camera *)projectors[i].ob->data;
179                         if (cam->type == CAM_PANO) {
180                                 projectors[i].uci = BLI_uvproject_camera_info(projectors[i].ob, NULL, aspx, aspy);
181                                 BLI_uvproject_camera_info_scale(projectors[i].uci, scax, scay);
182                                 free_uci = 1;
183                         }
184                         else {
185                                 CameraParams params;
186
187                                 /* setup parameters */
188                                 BKE_camera_params_init(&params);
189                                 BKE_camera_params_from_object(&params, projectors[i].ob);
190
191                                 /* compute matrix, viewplane, .. */
192                                 BKE_camera_params_compute_viewplane(&params, 1, 1, aspx, aspy);
193
194                                 /* scale the view-plane */
195                                 params.viewplane.xmin *= scax;
196                                 params.viewplane.xmax *= scax;
197                                 params.viewplane.ymin *= scay;
198                                 params.viewplane.ymax *= scay;
199
200                                 BKE_camera_params_compute_matrix(&params);
201                                 mul_m4_m4m4(tmpmat, params.winmat, projectors[i].projmat);
202                         }
203                 }
204                 else {
205                         copy_m4_m4(tmpmat, projectors[i].projmat);
206                 }
207
208                 unit_m4(offsetmat);
209                 mul_mat3_m4_fl(offsetmat, 0.5);
210                 offsetmat[3][0] = offsetmat[3][1] = offsetmat[3][2] = 0.5;
211
212                 mul_m4_m4m4(projectors[i].projmat, offsetmat, tmpmat);
213
214                 /* calculate worldspace projector normal (for best projector test) */
215                 projectors[i].normal[0] = 0;
216                 projectors[i].normal[1] = 0;
217                 projectors[i].normal[2] = 1;
218                 mul_mat3_m4_v3(projectors[i].ob->obmat, projectors[i].normal);
219         }
220
221         numPolys = dm->getNumPolys(dm);
222         numLoops = dm->getNumLoops(dm);
223
224         /* make sure we are not modifying the original UV map */
225         mloop_uv = CustomData_duplicate_referenced_layer_named(&dm->loopData,
226                                                                CD_MLOOPUV, uvname, numLoops);
227
228         /* can be NULL */
229         mt = mtexpoly = CustomData_duplicate_referenced_layer_named(&dm->polyData,
230                                                                     CD_MTEXPOLY, uvname, numPolys);
231
232         numVerts = dm->getNumVerts(dm);
233
234         coords = MEM_malloc_arrayN(numVerts, sizeof(*coords),
235                              "uvprojectModifier_do coords");
236         dm->getVertCos(dm, coords);
237
238         /* convert coords to world space */
239         for (i = 0, co = coords; i < numVerts; ++i, ++co)
240                 mul_m4_v3(ob->obmat, *co);
241         
242         /* if only one projector, project coords to UVs */
243         if (num_projectors == 1 && projectors[0].uci == NULL)
244                 for (i = 0, co = coords; i < numVerts; ++i, ++co)
245                         mul_project_m4_v3(projectors[0].projmat, *co);
246
247         mpoly = dm->getPolyArray(dm);
248         mloop = dm->getLoopArray(dm);
249
250         /* apply coords as UVs, and apply image if tfaces are new */
251         for (i = 0, mp = mpoly; i < numPolys; ++i, ++mp, ++mt) {
252                 if (override_image || !image || (mtexpoly == NULL || mt->tpage == 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                 if (override_image && mtexpoly) {
317                         mt->tpage = image;
318                 }
319         }
320
321         MEM_freeN(coords);
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, Object *ob,
339                                   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 */          modifier_copyData_generic,
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         /* updateDepgraph */    updateDepgraph,
373         /* updateDepsgraph */   updateDepsgraph,
374         /* dependsOnTime */     NULL,
375         /* dependsOnNormals */  NULL,
376         /* foreachObjectLink */ foreachObjectLink,
377         /* foreachIDLink */     foreachIDLink,
378         /* foreachTexLink */    NULL,
379 };