merge with/from trunk at r35190
[blender.git] / source / blender / modifiers / intern / MOD_subsurf.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 /** \file blender/modifiers/intern/MOD_subsurf.c
34  *  \ingroup modifiers
35  */
36
37
38 #include "stddef.h"
39
40 #include "DNA_scene_types.h"
41 #include "DNA_object_types.h"
42
43 #include "BLI_utildefines.h"
44
45
46 #include "BKE_cdderivedmesh.h"
47 #include "BKE_scene.h"
48 #include "BKE_subsurf.h"
49
50 #include "MOD_modifiertypes.h"
51
52 #include "CCGSubSurf.h"
53
54 static void initData(ModifierData *md)
55 {
56         SubsurfModifierData *smd = (SubsurfModifierData*) md;
57
58         smd->levels = 1;
59         smd->renderLevels = 2;
60         smd->flags |= eSubsurfModifierFlag_SubsurfUv;
61 }
62
63 static void copyData(ModifierData *md, ModifierData *target)
64 {
65         SubsurfModifierData *smd = (SubsurfModifierData*) md;
66         SubsurfModifierData *tsmd = (SubsurfModifierData*) target;
67
68         tsmd->flags = smd->flags;
69         tsmd->levels = smd->levels;
70         tsmd->renderLevels = smd->renderLevels;
71         tsmd->subdivType = smd->subdivType;
72 }
73
74 static void freeData(ModifierData *md)
75 {
76         SubsurfModifierData *smd = (SubsurfModifierData*) md;
77
78         if(smd->mCache) {
79                 ccgSubSurf_free(smd->mCache);
80         }
81         if(smd->emCache) {
82                 ccgSubSurf_free(smd->emCache);
83         }
84 }
85
86 static int isDisabled(ModifierData *md, int useRenderParams)
87 {
88         SubsurfModifierData *smd = (SubsurfModifierData*) md;
89         int levels= (useRenderParams)? smd->renderLevels: smd->levels;
90
91         return get_render_subsurf_level(&md->scene->r, levels) == 0;
92 }
93
94 static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
95                                                 DerivedMesh *derivedData,
96                                                 int useRenderParams,
97                                                 int isFinalCalc)
98 {
99         SubsurfModifierData *smd = (SubsurfModifierData*) md;
100         DerivedMesh *result;
101
102         result = subsurf_make_derived_from_derived(derivedData, smd,
103                         useRenderParams, NULL, isFinalCalc, 0);
104         
105         if(useRenderParams || !isFinalCalc) {
106                 DerivedMesh *cddm= CDDM_copy(result, 0);
107                 result->release(result);
108                 result= cddm;
109         }
110
111         return result;
112 }
113
114 static DerivedMesh *applyModifierEM(ModifierData *md, Object *UNUSED(ob),
115                                                 struct EditMesh *UNUSED(editData),
116                                                 DerivedMesh *derivedData)
117 {
118         SubsurfModifierData *smd = (SubsurfModifierData*) md;
119         DerivedMesh *result;
120
121         result = subsurf_make_derived_from_derived(derivedData, smd, 0,
122                         NULL, 0, 1);
123
124         return result;
125 }
126
127
128 ModifierTypeInfo modifierType_Subsurf = {
129         /* name */              "Subsurf",
130         /* structName */        "SubsurfModifierData",
131         /* structSize */        sizeof(SubsurfModifierData),
132         /* type */              eModifierTypeType_Constructive,
133         /* flags */             eModifierTypeFlag_AcceptsMesh
134                                                         | eModifierTypeFlag_SupportsMapping
135                                                         | eModifierTypeFlag_SupportsEditmode
136                                                         | eModifierTypeFlag_EnableInEditmode
137                                                         | eModifierTypeFlag_AcceptsCVs,
138
139         /* copyData */          copyData,
140         /* deformVerts */       0,
141         /* deformMatrices */    0,
142         /* deformVertsEM */     0,
143         /* deformMatricesEM */  0,
144         /* applyModifier */     applyModifier,
145         /* applyModifierEM */   applyModifierEM,
146         /* initData */          initData,
147         /* requiredDataMask */  0,
148         /* freeData */          freeData,
149         /* isDisabled */        isDisabled,
150         /* updateDepgraph */    0,
151         /* dependsOnTime */     0,
152         /* dependsOnNormals */  0,
153         /* foreachObjectLink */ 0,
154         /* foreachIDLink */     0,
155 };