Fix build error on Windows 32 bit.
[blender-staging.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 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 | CD_MTEXPOLY;
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 updateDepgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
113 {
114         UVProjectModifierData *umd = (UVProjectModifierData *) md;
115         int i;
116
117         for (i = 0; i < umd->num_projectors; ++i) {
118                 if (umd->projectors[i]) {
119                         DagNode *curNode = dag_get_node(ctx->forest, umd->projectors[i]);
120
121                         dag_add_relation(ctx->forest, curNode, ctx->obNode,
122                                          DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "UV Project Modifier");
123                 }
124         }
125 }
126
127 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
128 {
129         UVProjectModifierData *umd = (UVProjectModifierData *)md;
130         int i;
131         for (i = 0; i < umd->num_projectors; ++i) {
132                 if (umd->projectors[i] != NULL) {
133                         DEG_add_object_relation(ctx->node, umd->projectors[i], DEG_OB_COMP_TRANSFORM, "UV Project Modifier");
134                 }
135         }
136 }
137
138 typedef struct Projector {
139         Object *ob;             /* object this projector is derived from */
140         float projmat[4][4];    /* projection matrix */
141         float normal[3];        /* projector normal in world space */
142         void *uci;              /* optional uv-project info (panorama projection) */
143 } Projector;
144
145 static DerivedMesh *uvprojectModifier_do(UVProjectModifierData *umd,
146                                          Object *ob, DerivedMesh *dm)
147 {
148         float (*coords)[3], (*co)[3];
149         MLoopUV *mloop_uv;
150         MTexPoly *mtexpoly, *mt = NULL;
151         int i, numVerts, numPolys, numLoops;
152         Image *image = umd->image;
153         MPoly *mpoly, *mp;
154         MLoop *mloop;
155         const bool override_image = (umd->flags & MOD_UVPROJECT_OVERRIDEIMAGE) != 0;
156         Projector projectors[MOD_UVPROJECT_MAXPROJECTORS];
157         int num_projectors = 0;
158         char uvname[MAX_CUSTOMDATA_LAYER_NAME];
159         float aspx = umd->aspectx ? umd->aspectx : 1.0f;
160         float aspy = umd->aspecty ? umd->aspecty : 1.0f;
161         float scax = umd->scalex ? umd->scalex : 1.0f;
162         float scay = umd->scaley ? umd->scaley : 1.0f;
163         int free_uci = 0;
164
165         for (i = 0; i < umd->num_projectors; ++i)
166                 if (umd->projectors[i])
167                         projectors[num_projectors++].ob = umd->projectors[i];
168
169         if (num_projectors == 0) return dm;
170
171         /* make sure there are UV Maps available */
172
173         if (!CustomData_has_layer(&dm->loopData, CD_MLOOPUV)) return dm;
174
175         /* make sure we're using an existing layer */
176         CustomData_validate_layer_name(&dm->loopData, CD_MLOOPUV, umd->uvlayer_name, uvname);
177
178         /* calculate a projection matrix and normal for each projector */
179         for (i = 0; i < num_projectors; ++i) {
180                 float tmpmat[4][4];
181                 float offsetmat[4][4];
182                 Camera *cam = NULL;
183                 /* calculate projection matrix */
184                 invert_m4_m4(projectors[i].projmat, projectors[i].ob->obmat);
185
186                 projectors[i].uci = NULL;
187
188                 if (projectors[i].ob->type == OB_CAMERA) {
189                         
190                         cam = (Camera *)projectors[i].ob->data;
191                         if (cam->type == CAM_PANO) {
192                                 projectors[i].uci = BLI_uvproject_camera_info(projectors[i].ob, NULL, aspx, aspy);
193                                 BLI_uvproject_camera_info_scale(projectors[i].uci, scax, scay);
194                                 free_uci = 1;
195                         }
196                         else {
197                                 CameraParams params;
198
199                                 /* setup parameters */
200                                 BKE_camera_params_init(&params);
201                                 BKE_camera_params_from_object(&params, projectors[i].ob);
202
203                                 /* compute matrix, viewplane, .. */
204                                 BKE_camera_params_compute_viewplane(&params, 1, 1, aspx, aspy);
205
206                                 /* scale the view-plane */
207                                 params.viewplane.xmin *= scax;
208                                 params.viewplane.xmax *= scax;
209                                 params.viewplane.ymin *= scay;
210                                 params.viewplane.ymax *= scay;
211
212                                 BKE_camera_params_compute_matrix(&params);
213                                 mul_m4_m4m4(tmpmat, params.winmat, projectors[i].projmat);
214                         }
215                 }
216                 else {
217                         copy_m4_m4(tmpmat, projectors[i].projmat);
218                 }
219
220                 unit_m4(offsetmat);
221                 mul_mat3_m4_fl(offsetmat, 0.5);
222                 offsetmat[3][0] = offsetmat[3][1] = offsetmat[3][2] = 0.5;
223
224                 mul_m4_m4m4(projectors[i].projmat, offsetmat, tmpmat);
225
226                 /* calculate worldspace projector normal (for best projector test) */
227                 projectors[i].normal[0] = 0;
228                 projectors[i].normal[1] = 0;
229                 projectors[i].normal[2] = 1;
230                 mul_mat3_m4_v3(projectors[i].ob->obmat, projectors[i].normal);
231         }
232
233         numPolys = dm->getNumPolys(dm);
234         numLoops = dm->getNumLoops(dm);
235
236         /* make sure we are not modifying the original UV map */
237         mloop_uv = CustomData_duplicate_referenced_layer_named(&dm->loopData,
238                                                                CD_MLOOPUV, uvname, numLoops);
239
240         /* can be NULL */
241         mt = mtexpoly = CustomData_duplicate_referenced_layer_named(&dm->polyData,
242                                                                     CD_MTEXPOLY, uvname, numPolys);
243
244         numVerts = dm->getNumVerts(dm);
245
246         coords = MEM_malloc_arrayN(numVerts, sizeof(*coords),
247                              "uvprojectModifier_do coords");
248         dm->getVertCos(dm, coords);
249
250         /* convert coords to world space */
251         for (i = 0, co = coords; i < numVerts; ++i, ++co)
252                 mul_m4_v3(ob->obmat, *co);
253         
254         /* if only one projector, project coords to UVs */
255         if (num_projectors == 1 && projectors[0].uci == NULL)
256                 for (i = 0, co = coords; i < numVerts; ++i, ++co)
257                         mul_project_m4_v3(projectors[0].projmat, *co);
258
259         mpoly = dm->getPolyArray(dm);
260         mloop = dm->getLoopArray(dm);
261
262         /* apply coords as UVs, and apply image if tfaces are new */
263         for (i = 0, mp = mpoly; i < numPolys; ++i, ++mp, ++mt) {
264                 if (override_image || !image || (mtexpoly == NULL || mt->tpage == image)) {
265                         if (num_projectors == 1) {
266                                 if (projectors[0].uci) {
267                                         unsigned int fidx = mp->totloop - 1;
268                                         do {
269                                                 unsigned int lidx = mp->loopstart + fidx;
270                                                 unsigned int vidx = mloop[lidx].v;
271                                                 BLI_uvproject_from_camera(mloop_uv[lidx].uv, coords[vidx], projectors[0].uci);
272                                         } while (fidx--);
273                                 }
274                                 else {
275                                         /* apply transformed coords as UVs */
276                                         unsigned int fidx = mp->totloop - 1;
277                                         do {
278                                                 unsigned int lidx = mp->loopstart + fidx;
279                                                 unsigned int vidx = mloop[lidx].v;
280                                                 copy_v2_v2(mloop_uv[lidx].uv, coords[vidx]);
281                                         } while (fidx--);
282                                 }
283                         }
284                         else {
285                                 /* multiple projectors, select the closest to face normal direction */
286                                 float face_no[3];
287                                 int j;
288                                 Projector *best_projector;
289                                 float best_dot;
290
291                                 /* get the untransformed face normal */
292                                 BKE_mesh_calc_poly_normal_coords(mp, mloop + mp->loopstart, (const float (*)[3])coords, face_no);
293
294                                 /* find the projector which the face points at most directly
295                                  * (projector normal with largest dot product is best)
296                                  */
297                                 best_dot = dot_v3v3(projectors[0].normal, face_no);
298                                 best_projector = &projectors[0];
299
300                                 for (j = 1; j < num_projectors; ++j) {
301                                         float tmp_dot = dot_v3v3(projectors[j].normal,
302                                                                  face_no);
303                                         if (tmp_dot > best_dot) {
304                                                 best_dot = tmp_dot;
305                                                 best_projector = &projectors[j];
306                                         }
307                                 }
308
309                                 if (best_projector->uci) {
310                                         unsigned int fidx = mp->totloop - 1;
311                                         do {
312                                                 unsigned int lidx = mp->loopstart + fidx;
313                                                 unsigned int vidx = mloop[lidx].v;
314                                                 BLI_uvproject_from_camera(mloop_uv[lidx].uv, coords[vidx], best_projector->uci);
315                                         } while (fidx--);
316                                 }
317                                 else {
318                                         unsigned int fidx = mp->totloop - 1;
319                                         do {
320                                                 unsigned int lidx = mp->loopstart + fidx;
321                                                 unsigned int vidx = mloop[lidx].v;
322                                                 mul_v2_project_m4_v3(mloop_uv[lidx].uv, best_projector->projmat, coords[vidx]);
323                                         } while (fidx--);
324                                 }
325                         }
326                 }
327
328                 if (override_image && mtexpoly) {
329                         mt->tpage = image;
330                 }
331         }
332
333         MEM_freeN(coords);
334         
335         if (free_uci) {
336                 int j;
337                 for (j = 0; j < num_projectors; ++j) {
338                         if (projectors[j].uci) {
339                                 MEM_freeN(projectors[j].uci);
340                         }
341                 }
342         }
343
344         /* Mark tessellated CD layers as dirty. */
345         dm->dirty |= DM_DIRTY_TESS_CDLAYERS;
346
347         return dm;
348 }
349
350 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
351                                   DerivedMesh *derivedData,
352                                   ModifierApplyFlag UNUSED(flag))
353 {
354         DerivedMesh *result;
355         UVProjectModifierData *umd = (UVProjectModifierData *) md;
356
357         result = uvprojectModifier_do(umd, ob, derivedData);
358
359         return result;
360 }
361
362
363 ModifierTypeInfo modifierType_UVProject = {
364         /* name */              "UVProject",
365         /* structName */        "UVProjectModifierData",
366         /* structSize */        sizeof(UVProjectModifierData),
367         /* type */              eModifierTypeType_NonGeometrical,
368         /* flags */             eModifierTypeFlag_AcceptsMesh |
369                                 eModifierTypeFlag_SupportsMapping |
370                                 eModifierTypeFlag_SupportsEditmode |
371                                 eModifierTypeFlag_EnableInEditmode,
372
373         /* copyData */          copyData,
374         /* deformVerts */       NULL,
375         /* deformMatrices */    NULL,
376         /* deformVertsEM */     NULL,
377         /* deformMatricesEM */  NULL,
378         /* applyModifier */     applyModifier,
379         /* applyModifierEM */   NULL,
380         /* initData */          initData,
381         /* requiredDataMask */  requiredDataMask,
382         /* freeData */          NULL,
383         /* isDisabled */        NULL,
384         /* updateDepgraph */    updateDepgraph,
385         /* updateDepsgraph */   updateDepsgraph,
386         /* dependsOnTime */     NULL,
387         /* dependsOnNormals */  NULL,
388         /* foreachObjectLink */ foreachObjectLink,
389         /* foreachIDLink */     foreachIDLink,
390         /* foreachTexLink */    NULL,
391 };