~ubuntu-branches/ubuntu/trusty/blender/trusty-proposed

« back to all changes in this revision

Viewing changes to source/blender/editors/sculpt_paint/paint_vertex_proj.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2013-08-14 10:43:49 UTC
  • mfrom: (14.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20130814104349-t1d5mtwkphp12dyj
Tags: 2.68a-3
* Upload to unstable
* debian/: python3.3 Depends simplified
  - debian/control: python3.3 Depends dropped
    for blender-data package
  - 0001-blender_thumbnailer.patch refreshed
* debian/control: libavcodec b-dep versioning dropped

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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) 2013 Blender Foundation.
 
19
 * All rights reserved.
 
20
 *
 
21
 *
 
22
 * Contributor(s): Blender Foundation
 
23
 *
 
24
 * ***** END GPL LICENSE BLOCK *****
 
25
 */
 
26
 
 
27
/** \file blender/editors/sculpt_paint/paint_vertex_proj.c
 
28
 *  \ingroup edsculpt
 
29
 *
 
30
 * Utility functions for getting vertex locations while painting
 
31
 * (since they may be instanced multiple times in a DerivedMesh)
 
32
 */
 
33
 
 
34
#include "MEM_guardedalloc.h"
 
35
 
 
36
#include "BLI_math.h"
 
37
 
 
38
#include "DNA_mesh_types.h"
 
39
#include "DNA_object_types.h"
 
40
 
 
41
#include "BKE_DerivedMesh.h"
 
42
#include "BKE_context.h"
 
43
 
 
44
#include "ED_screen.h"
 
45
#include "ED_view3d.h"
 
46
 
 
47
#include "paint_intern.h"  /* own include */
 
48
 
 
49
 
 
50
/* Opaque Structs for internal use */
 
51
 
 
52
/* stored while painting */
 
53
struct VertProjHandle {
 
54
        DMCoNo *vcosnos;
 
55
 
 
56
        bool use_update;
 
57
 
 
58
        /* use for update */
 
59
        float *dists;
 
60
 
 
61
        Object *ob;
 
62
        Scene *scene;
 
63
};
 
64
 
 
65
/* only for passing to the callbacks */
 
66
struct VertProjUpdate {
 
67
        struct VertProjHandle *vp_handle;
 
68
 
 
69
        /* runtime */
 
70
        ARegion *ar;
 
71
        const float *mval_fl;
 
72
};
 
73
 
 
74
 
 
75
/* -------------------------------------------------------------------- */
 
76
/* Internal Init */
 
77
 
 
78
static void vpaint_proj_dm_map_cosnos_init__map_cb(void *userData, int index, const float co[3],
 
79
                                                   const float no_f[3], const short no_s[3])
 
80
{
 
81
        struct VertProjHandle *vp_handle = userData;
 
82
        DMCoNo *co_no = &vp_handle->vcosnos[index];
 
83
 
 
84
        /* check if we've been here before (normal should not be 0) */
 
85
        if (!is_zero_v3(co_no->no)) {
 
86
                /* remember that multiple dm verts share the same source vert */
 
87
                vp_handle->use_update = true;
 
88
                return;
 
89
        }
 
90
 
 
91
        copy_v3_v3(co_no->co, co);
 
92
        if (no_f) {
 
93
                copy_v3_v3(co_no->no, no_f);
 
94
        }
 
95
        else {
 
96
                normal_short_to_float_v3(co_no->no, no_s);
 
97
        }
 
98
}
 
99
 
 
100
static void vpaint_proj_dm_map_cosnos_init(Scene *scene, Object *ob,
 
101
                                           struct VertProjHandle *vp_handle)
 
102
{
 
103
        Mesh *me = ob->data;
 
104
        DerivedMesh *dm;
 
105
 
 
106
        dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH | CD_MASK_ORIGINDEX);
 
107
 
 
108
        if (dm->foreachMappedVert) {
 
109
                memset(vp_handle->vcosnos, 0, sizeof(DMCoNo) * me->totvert);
 
110
                dm->foreachMappedVert(dm, vpaint_proj_dm_map_cosnos_init__map_cb, vp_handle);
 
111
        }
 
112
        else {
 
113
                DMCoNo *v_co_no = vp_handle->vcosnos;
 
114
                int a;
 
115
                for (a = 0; a < me->totvert; a++, v_co_no++) {
 
116
                        dm->getVertCo(dm, a, v_co_no->co);
 
117
                        dm->getVertNo(dm, a, v_co_no->no);
 
118
                }
 
119
        }
 
120
 
 
121
        dm->release(dm);
 
122
}
 
123
 
 
124
 
 
125
/* -------------------------------------------------------------------- */
 
126
/* Internal Update */
 
127
 
 
128
/* Same as init but take mouse location into account */
 
129
 
 
130
static void vpaint_proj_dm_map_cosnos_update__map_cb(void *userData, int index, const float co[3],
 
131
                                                     const float no_f[3], const short no_s[3])
 
132
{
 
133
        struct VertProjUpdate *vp_update = userData;
 
134
        struct VertProjHandle *vp_handle = vp_update->vp_handle;
 
135
 
 
136
        DMCoNo *co_no = &vp_handle->vcosnos[index];
 
137
 
 
138
        /* find closest vertex */
 
139
        {
 
140
                /* first find distance to this vertex */
 
141
                float co_ss[2];  /* screenspace */
 
142
 
 
143
                if (ED_view3d_project_float_object(vp_update->ar,
 
144
                                                   co, co_ss,
 
145
                                                   V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
 
146
                {
 
147
                        const float dist = len_squared_v2v2(vp_update->mval_fl, co_ss);
 
148
                        if (dist > vp_handle->dists[index]) {
 
149
                                /* bail out! */
 
150
                                return;
 
151
                        }
 
152
 
 
153
                        vp_handle->dists[index] = dist;
 
154
                }
 
155
        }
 
156
        /* continue with regular functionality */
 
157
 
 
158
        copy_v3_v3(co_no->co, co);
 
159
        if (no_f) {
 
160
                copy_v3_v3(co_no->no, no_f);
 
161
        }
 
162
        else {
 
163
                normal_short_to_float_v3(co_no->no, no_s);
 
164
        }
 
165
}
 
166
 
 
167
static void vpaint_proj_dm_map_cosnos_update(struct VertProjHandle *vp_handle,
 
168
                                             ARegion *ar, const float mval_fl[2])
 
169
{
 
170
        struct VertProjUpdate vp_update = {vp_handle, ar, mval_fl};
 
171
 
 
172
        Scene *scene = vp_handle->scene;
 
173
        Object *ob = vp_handle->ob;
 
174
        Mesh *me = ob->data;
 
175
        DerivedMesh *dm;
 
176
 
 
177
        /* quick sanity check - we shouldn't have to run this if there are no modifiers */
 
178
        BLI_assert(ob->modifiers.first != NULL);
 
179
 
 
180
        dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH | CD_MASK_ORIGINDEX);
 
181
 
 
182
        /* highly unlikely this will become unavailable once painting starts (perhaps with animated modifiers) */
 
183
        if (LIKELY(dm->foreachMappedVert)) {
 
184
                fill_vn_fl(vp_handle->dists, me->totvert, FLT_MAX);
 
185
 
 
186
                dm->foreachMappedVert(dm, vpaint_proj_dm_map_cosnos_update__map_cb, &vp_update);
 
187
        }
 
188
 
 
189
        dm->release(dm);
 
190
}
 
191
 
 
192
 
 
193
/* -------------------------------------------------------------------- */
 
194
/* Public Functions */
 
195
 
 
196
struct VertProjHandle *ED_vpaint_proj_handle_create(Scene *scene, Object *ob,
 
197
                                                    DMCoNo **r_vcosnos)
 
198
{
 
199
        struct VertProjHandle *vp_handle = MEM_mallocN(sizeof(struct VertProjHandle), __func__);
 
200
        Mesh *me = ob->data;
 
201
 
 
202
        /* setup the handle */
 
203
        vp_handle->vcosnos = MEM_mallocN(sizeof(DMCoNo) * me->totvert, "vertexcosnos map");
 
204
        vp_handle->use_update = false;
 
205
 
 
206
        /* sets 'use_update' if needed */
 
207
        vpaint_proj_dm_map_cosnos_init(scene, ob, vp_handle);
 
208
 
 
209
        if (vp_handle->use_update) {
 
210
                vp_handle->dists = MEM_mallocN(sizeof(float) * me->totvert, __func__);
 
211
 
 
212
                vp_handle->ob = ob;
 
213
                vp_handle->scene = scene;
 
214
        }
 
215
        else {
 
216
                vp_handle->dists = NULL;
 
217
 
 
218
                vp_handle->ob = NULL;
 
219
                vp_handle->scene = NULL;
 
220
        }
 
221
 
 
222
        *r_vcosnos = vp_handle->vcosnos;
 
223
        return vp_handle;
 
224
}
 
225
 
 
226
void  ED_vpaint_proj_handle_update(struct VertProjHandle *vp_handle,
 
227
                                   ARegion *ar, const float mval_fl[2])
 
228
{
 
229
        if (vp_handle->use_update) {
 
230
                vpaint_proj_dm_map_cosnos_update(vp_handle, ar, mval_fl);
 
231
        }
 
232
}
 
233
 
 
234
void  ED_vpaint_proj_handle_free(struct VertProjHandle *vp_handle)
 
235
{
 
236
        if (vp_handle->use_update) {
 
237
                MEM_freeN(vp_handle->dists);
 
238
        }
 
239
 
 
240
        MEM_freeN(vp_handle->vcosnos);
 
241
        MEM_freeN(vp_handle);
 
242
}