Code cleanup: remove unused includes
[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_mesh.h"
49 #include "BKE_DerivedMesh.h"
50
51 #include "MOD_modifiertypes.h"
52 #include "MOD_util.h"
53
54 #include "MEM_guardedalloc.h"
55 #include "depsgraph_private.h"
56
57 static void initData(ModifierData *md)
58 {
59         UVProjectModifierData *umd = (UVProjectModifierData *) md;
60
61         umd->flags = 0;
62         umd->num_projectors = 1;
63         umd->aspectx = umd->aspecty = 1.0f;
64         umd->scalex = umd->scaley = 1.0f;
65 }
66
67 static void copyData(ModifierData *md, ModifierData *target)
68 {
69 #if 0
70         UVProjectModifierData *umd = (UVProjectModifierData *) md;
71         UVProjectModifierData *tumd = (UVProjectModifierData *) target;
72 #endif
73         modifier_copyData_generic(md, target);
74 }
75
76 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md))
77 {
78         CustomDataMask dataMask = 0;
79
80         /* ask for UV coordinates */
81         dataMask |= CD_MLOOPUV | CD_MTEXPOLY;
82
83         return dataMask;
84 }
85
86 static void foreachObjectLink(ModifierData *md, Object *ob,
87                               ObjectWalkFunc walk, void *userData)
88 {
89         UVProjectModifierData *umd = (UVProjectModifierData *) md;
90         int i;
91
92         for (i = 0; i < MOD_UVPROJECT_MAXPROJECTORS; ++i)
93                 walk(userData, ob, &umd->projectors[i]);
94 }
95
96 static void foreachIDLink(ModifierData *md, Object *ob,
97                           IDWalkFunc walk, void *userData)
98 {
99         UVProjectModifierData *umd = (UVProjectModifierData *) md;
100
101         walk(userData, ob, (ID **)&umd->image);
102
103         foreachObjectLink(md, ob, (ObjectWalkFunc)walk,
104                           userData);
105 }
106
107 static void updateDepgraph(ModifierData *md, DagForest *forest,
108                            struct Scene *UNUSED(scene),
109                            Object *UNUSED(ob),
110                            DagNode *obNode)
111 {
112         UVProjectModifierData *umd = (UVProjectModifierData *) md;
113         int i;
114
115         for (i = 0; i < umd->num_projectors; ++i) {
116                 if (umd->projectors[i]) {
117                         DagNode *curNode = dag_get_node(forest, umd->projectors[i]);
118
119                         dag_add_relation(forest, curNode, obNode,
120                                          DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "UV Project Modifier");
121                 }
122         }
123 }
124
125 typedef struct Projector {
126         Object *ob;             /* object this projector is derived from */
127         float projmat[4][4];    /* projection matrix */
128         float normal[3];        /* projector normal in world space */
129         void *uci;              /* optional uv-project info (panorama projection) */
130 } Projector;
131
132 static DerivedMesh *uvprojectModifier_do(UVProjectModifierData *umd,
133                                          Object *ob, DerivedMesh *dm)
134 {
135         float (*coords)[3], (*co)[3];
136         MLoopUV *mloop_uv;
137         MTexPoly *mtexpoly, *mt = NULL;
138         int i, numVerts, numPolys, numLoops;
139         Image *image = umd->image;
140         MPoly *mpoly, *mp;
141         MLoop *mloop;
142         int override_image = ((umd->flags & MOD_UVPROJECT_OVERRIDEIMAGE) != 0);
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         /* can be NULL */
228         mt = mtexpoly = CustomData_duplicate_referenced_layer_named(&dm->polyData,
229                                                                     CD_MTEXPOLY, uvname, numPolys);
230
231         numVerts = dm->getNumVerts(dm);
232
233         coords = MEM_mallocN(sizeof(*coords) * numVerts,
234                              "uvprojectModifier_do coords");
235         dm->getVertCos(dm, coords);
236
237         /* convert coords to world space */
238         for (i = 0, co = coords; i < numVerts; ++i, ++co)
239                 mul_m4_v3(ob->obmat, *co);
240         
241         /* if only one projector, project coords to UVs */
242         if (num_projectors == 1 && projectors[0].uci == NULL)
243                 for (i = 0, co = coords; i < numVerts; ++i, ++co)
244                         mul_project_m4_v3(projectors[0].projmat, *co);
245
246         mpoly = dm->getPolyArray(dm);
247         mloop = dm->getLoopArray(dm);
248
249         /* apply coords as UVs, and apply image if tfaces are new */
250         for (i = 0, mp = mpoly; i < numPolys; ++i, ++mp, ++mt) {
251                 if (override_image || !image || (mtexpoly == NULL || mt->tpage == image)) {
252                         if (num_projectors == 1) {
253                                 if (projectors[0].uci) {
254                                         unsigned int fidx = mp->totloop - 1;
255                                         do {
256                                                 unsigned int lidx = mp->loopstart + fidx;
257                                                 unsigned int vidx = mloop[lidx].v;
258                                                 BLI_uvproject_from_camera(mloop_uv[lidx].uv, coords[vidx], projectors[0].uci);
259                                         } while (fidx--);
260                                 }
261                                 else {
262                                         /* apply transformed coords as UVs */
263                                         unsigned int fidx = mp->totloop - 1;
264                                         do {
265                                                 unsigned int lidx = mp->loopstart + fidx;
266                                                 unsigned int vidx = mloop[lidx].v;
267                                                 copy_v2_v2(mloop_uv[lidx].uv, coords[vidx]);
268                                         } while (fidx--);
269                                 }
270                         }
271                         else {
272                                 /* multiple projectors, select the closest to face normal direction */
273                                 float face_no[3];
274                                 int j;
275                                 Projector *best_projector;
276                                 float best_dot;
277
278                                 /* get the untransformed face normal */
279                                 BKE_mesh_calc_poly_normal_coords(mp, mloop + mp->loopstart, (const float (*)[3])coords, face_no);
280
281                                 /* find the projector which the face points at most directly
282                                  * (projector normal with largest dot product is best)
283                                  */
284                                 best_dot = dot_v3v3(projectors[0].normal, face_no);
285                                 best_projector = &projectors[0];
286
287                                 for (j = 1; j < num_projectors; ++j) {
288                                         float tmp_dot = dot_v3v3(projectors[j].normal,
289                                                                  face_no);
290                                         if (tmp_dot > best_dot) {
291                                                 best_dot = tmp_dot;
292                                                 best_projector = &projectors[j];
293                                         }
294                                 }
295
296                                 if (best_projector->uci) {
297                                         unsigned int fidx = mp->totloop - 1;
298                                         do {
299                                                 unsigned int lidx = mp->loopstart + fidx;
300                                                 unsigned int vidx = mloop[lidx].v;
301                                                 BLI_uvproject_from_camera(mloop_uv[lidx].uv, coords[vidx], best_projector->uci);
302                                         } while (fidx--);
303                                 }
304                                 else {
305                                         unsigned int fidx = mp->totloop - 1;
306                                         do {
307                                                 unsigned int lidx = mp->loopstart + fidx;
308                                                 unsigned int vidx = mloop[lidx].v;
309                                                 mul_v2_project_m4_v3(mloop_uv[lidx].uv, best_projector->projmat, coords[vidx]);
310                                         } while (fidx--);
311                                 }
312                         }
313                 }
314
315                 if (override_image && mtexpoly) {
316                         mt->tpage = image;
317                 }
318         }
319
320         MEM_freeN(coords);
321         
322         if (free_uci) {
323                 int j;
324                 for (j = 0; j < num_projectors; ++j) {
325                         if (projectors[j].uci) {
326                                 MEM_freeN(projectors[j].uci);
327                         }
328                 }
329         }
330
331         /* Mark tessellated CD layers as dirty. */
332         dm->dirty |= DM_DIRTY_TESS_CDLAYERS;
333
334         return dm;
335 }
336
337 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
338                                   DerivedMesh *derivedData,
339                                   ModifierApplyFlag UNUSED(flag))
340 {
341         DerivedMesh *result;
342         UVProjectModifierData *umd = (UVProjectModifierData *) md;
343
344         result = uvprojectModifier_do(umd, ob, derivedData);
345
346         return result;
347 }
348
349
350 ModifierTypeInfo modifierType_UVProject = {
351         /* name */              "UVProject",
352         /* structName */        "UVProjectModifierData",
353         /* structSize */        sizeof(UVProjectModifierData),
354         /* type */              eModifierTypeType_NonGeometrical,
355         /* flags */             eModifierTypeFlag_AcceptsMesh |
356                                 eModifierTypeFlag_SupportsMapping |
357                                 eModifierTypeFlag_SupportsEditmode |
358                                 eModifierTypeFlag_EnableInEditmode,
359
360         /* copyData */          copyData,
361         /* deformVerts */       NULL,
362         /* deformMatrices */    NULL,
363         /* deformVertsEM */     NULL,
364         /* deformMatricesEM */  NULL,
365         /* applyModifier */     applyModifier,
366         /* applyModifierEM */   NULL,
367         /* initData */          initData,
368         /* requiredDataMask */  requiredDataMask,
369         /* freeData */          NULL,
370         /* isDisabled */        NULL,
371         /* updateDepgraph */    updateDepgraph,
372         /* dependsOnTime */     NULL,
373         /* dependsOnNormals */  NULL,
374         /* foreachObjectLink */ foreachObjectLink,
375         /* foreachIDLink */     foreachIDLink,
376         /* foreachTexLink */    NULL,
377 };