use gcc attrubutes to warn on unused return values and arguments which shouldnt be...
[blender.git] / source / blender / modifiers / intern / MOD_multires.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_multires.c
32  *  \ingroup modifiers
33  */
34
35
36 #include <stddef.h>
37
38 #include "DNA_mesh_types.h"
39 #include "DNA_object_types.h"
40
41 #include "BLI_utildefines.h"
42
43 #include "BKE_cdderivedmesh.h"
44 #include "BKE_mesh.h"
45 #include "BKE_multires.h"
46 #include "BKE_modifier.h"
47 #include "BKE_paint.h"
48 #include "BKE_subsurf.h"
49
50 #include "MOD_util.h"
51
52 static void initData(ModifierData *md)
53 {
54         MultiresModifierData *mmd = (MultiresModifierData *)md;
55
56         mmd->lvl = 0;
57         mmd->sculptlvl = 0;
58         mmd->renderlvl = 0;
59         mmd->totlvl = 0;
60 }
61
62 static void copyData(ModifierData *md, ModifierData *target)
63 {
64         MultiresModifierData *mmd = (MultiresModifierData *) md;
65         MultiresModifierData *tmmd = (MultiresModifierData *) target;
66
67         tmmd->lvl = mmd->lvl;
68         tmmd->sculptlvl = mmd->sculptlvl;
69         tmmd->renderlvl = mmd->renderlvl;
70         tmmd->totlvl = mmd->totlvl;
71         tmmd->simple = mmd->simple;
72         tmmd->flags = mmd->flags;
73 }
74
75 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *dm,
76                                   ModifierApplyFlag flag)
77 {
78         MultiresModifierData *mmd = (MultiresModifierData *)md;
79         DerivedMesh *result;
80         Mesh *me = (Mesh *)ob->data;
81         const int useRenderParams = flag & MOD_APPLY_RENDER;
82         MultiresFlags flags = 0;
83
84         if (mmd->totlvl) {
85                 if (!CustomData_get_layer(&me->ldata, CD_MDISPS)) {
86                         /* multires always needs a displacement layer */
87                         CustomData_add_layer(&me->ldata, CD_MDISPS, CD_CALLOC, NULL, me->totloop);
88                 }
89         }
90
91         flags = MULTIRES_ALLOC_PAINT_MASK;
92         if (useRenderParams)
93                 flags |= MULTIRES_USE_RENDER_PARAMS;
94
95         result = multires_make_derived_from_derived(dm, mmd, ob, flags);
96
97         if (result == dm)
98                 return dm;
99
100         if (useRenderParams || !(flag & MOD_APPLY_USECACHE)) {
101                 DerivedMesh *cddm;
102                 
103                 cddm = CDDM_copy(result);
104
105                 /* copy hidden/masks to vertices */
106                 if (!useRenderParams) {
107                         struct MDisps *mdisps;
108                         struct GridPaintMask *grid_paint_mask;
109                         
110                         mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
111                         grid_paint_mask = CustomData_get_layer(&me->ldata, CD_GRID_PAINT_MASK);
112                         
113                         if (mdisps) {
114                                 subsurf_copy_grid_hidden(result, me->mpoly,
115                                                          cddm->getVertArray(cddm),
116                                                          mdisps);
117
118                                 BKE_mesh_flush_hidden_from_verts(cddm->getVertArray(cddm),
119                                                                  cddm->getLoopArray(cddm),
120                                                                  cddm->getEdgeArray(cddm),
121                                                                  cddm->getNumEdges(cddm),
122                                                                  cddm->getPolyArray(cddm),
123                                                                  cddm->getNumPolys(cddm));
124                         }
125                         if (grid_paint_mask) {
126                                 float *paint_mask = CustomData_add_layer(&cddm->vertData,
127                                                                          CD_PAINT_MASK,
128                                                                          CD_CALLOC, NULL,
129                                                                          cddm->getNumVerts(cddm));
130
131                                 subsurf_copy_grid_paint_mask(result, me->mpoly,
132                                                              paint_mask, grid_paint_mask);
133                         }
134                 }
135
136                 result->release(result);
137                 result = cddm;
138         }
139
140         return result;
141 }
142
143
144 ModifierTypeInfo modifierType_Multires = {
145         /* name */              "Multires",
146         /* structName */        "MultiresModifierData",
147         /* structSize */        sizeof(MultiresModifierData),
148         /* type */              eModifierTypeType_Constructive,
149         /* flags */             eModifierTypeFlag_AcceptsMesh |
150                                 eModifierTypeFlag_SupportsMapping |
151                                 eModifierTypeFlag_RequiresOriginalData,
152
153         /* copyData */          copyData,
154         /* deformVerts */       NULL,
155         /* deformMatrices */    NULL,
156         /* deformVertsEM */     NULL,
157         /* deformMatricesEM */  NULL,
158         /* applyModifier */     applyModifier,
159         /* applyModifierEM */   NULL,
160         /* initData */          initData,
161         /* requiredDataMask */  NULL,
162         /* freeData */          NULL,
163         /* isDisabled */        NULL,
164         /* updateDepgraph */    NULL,
165         /* dependsOnTime */     NULL,
166         /* dependsOnNormals */  NULL,
167         /* foreachObjectLink */ NULL,
168         /* foreachIDLink */     NULL,
169         /* foreachTexLink */    NULL,
170 };