Merge branch 'blender2.7'
[blender.git] / source / blender / editors / mesh / editmesh_preselect_elem.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  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/mesh/editmesh_preselect_elem.c
22  *  \ingroup edmesh
23  */
24
25 #include "MEM_guardedalloc.h"
26
27 #include "BLI_stack.h"
28 #include "BLI_math.h"
29
30 #include "BKE_context.h"
31 #include "BKE_editmesh.h"
32
33 #include "GPU_immediate.h"
34 #include "GPU_matrix.h"
35 #include "GPU_state.h"
36
37 #include "DNA_object_types.h"
38
39 #include "ED_mesh.h"
40 #include "ED_view3d.h"
41
42 /* -------------------------------------------------------------------- */
43 /** \name Mesh Element Pre-Select
44  * Public API:
45  *
46  * #EDBM_preselect_elem_create
47  * #EDBM_preselect_elem_destroy
48  * #EDBM_preselect_elem_clear
49  * #EDBM_preselect_elem_draw
50  * #EDBM_preselect_elem_update_from_single
51  *
52  * \{ */
53
54 static void vcos_get(BMVert *v, float r_co[3], const float (*coords)[3])
55 {
56         if (coords) {
57                 copy_v3_v3(r_co, coords[BM_elem_index_get(v)]);
58         }
59         else {
60                 copy_v3_v3(r_co, v->co);
61         }
62 }
63
64 static void vcos_get_pair(BMVert *v[2], float r_cos[2][3], const float (*coords)[3])
65 {
66         if (coords) {
67                 for (int j = 0; j < 2; j++) {
68                         copy_v3_v3(r_cos[j], coords[BM_elem_index_get(v[j])]);
69                 }
70         }
71         else {
72                 for (int j = 0; j < 2; j++) {
73                         copy_v3_v3(r_cos[j], v[j]->co);
74                 }
75         }
76 }
77
78 struct EditMesh_PreSelElem {
79         float (*edges)[2][3];
80         int     edges_len;
81
82         float (*verts)[3];
83         int     verts_len;
84 };
85
86 struct EditMesh_PreSelElem *EDBM_preselect_elem_create(void)
87 {
88         struct EditMesh_PreSelElem *psel = MEM_callocN(sizeof(*psel), __func__);
89         return psel;
90 }
91
92 void EDBM_preselect_elem_destroy(
93         struct EditMesh_PreSelElem *psel)
94 {
95         EDBM_preselect_elem_clear(psel);
96         MEM_freeN(psel);
97 }
98
99 void EDBM_preselect_elem_clear(
100         struct EditMesh_PreSelElem *psel)
101 {
102         MEM_SAFE_FREE(psel->edges);
103         psel->edges_len = 0;
104
105         MEM_SAFE_FREE(psel->verts);
106         psel->verts_len = 0;
107 }
108
109 void EDBM_preselect_elem_draw(
110         struct EditMesh_PreSelElem *psel, const float matrix[4][4])
111 {
112         if ((psel->edges_len == 0) && (psel->verts_len == 0)) {
113                 return;
114         }
115
116         GPU_depth_test(false);
117
118         GPU_matrix_push();
119         GPU_matrix_mul(matrix);
120
121         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
122
123         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
124         immUniformColor3ub(255, 0, 255);
125
126         if (psel->edges_len > 0) {
127                 immBegin(GPU_PRIM_LINES, psel->edges_len * 2);
128
129                 for (int i = 0; i < psel->edges_len; i++) {
130                         immVertex3fv(pos, psel->edges[i][0]);
131                         immVertex3fv(pos, psel->edges[i][1]);
132                 }
133
134                 immEnd();
135         }
136
137         if (psel->verts_len > 0) {
138                 GPU_point_size(3.0f);
139
140                 immBegin(GPU_PRIM_POINTS, psel->verts_len);
141
142                 for (int i = 0; i < psel->verts_len; i++) {
143                         immVertex3fv(pos, psel->verts[i]);
144                 }
145
146                 immEnd();
147         }
148
149         immUnbindProgram();
150
151         GPU_matrix_pop();
152
153         /* Reset default */
154         GPU_depth_test(true);
155 }
156
157 static void view3d_preselect_mesh_elem_update_from_vert(
158         struct EditMesh_PreSelElem *psel,
159         BMesh *UNUSED(bm), BMVert *eve, const float (*coords)[3])
160 {
161         float (*verts)[3] = MEM_mallocN(sizeof(*psel->verts), __func__);
162         vcos_get(eve, verts[0], coords);
163         psel->verts = verts;
164         psel->verts_len = 1;
165 }
166
167 static void view3d_preselect_mesh_elem_update_from_edge(
168         struct EditMesh_PreSelElem *psel,
169         BMesh *UNUSED(bm), BMEdge *eed, const float (*coords)[3])
170 {
171         float (*edges)[2][3] = MEM_mallocN(sizeof(*psel->edges), __func__);
172         vcos_get_pair(&eed->v1, edges[0], coords);
173         psel->edges = edges;
174         psel->edges_len = 1;
175 }
176
177 static void view3d_preselect_mesh_elem_update_from_face(
178         struct EditMesh_PreSelElem *psel,
179         BMesh *UNUSED(bm), BMFace *efa, const float (*coords)[3])
180 {
181         float (*edges)[2][3] = MEM_mallocN(sizeof(*psel->edges) * efa->len, __func__);
182         BMLoop *l_iter, *l_first;
183         l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
184         int i = 0;
185         do {
186                 vcos_get_pair(&l_iter->e->v1, edges[i++], coords);
187         } while ((l_iter = l_iter->next) != l_first);
188         psel->edges = edges;
189         psel->edges_len = efa->len;
190 }
191
192 void EDBM_preselect_elem_update_from_single(
193         struct EditMesh_PreSelElem *psel,
194         BMesh *bm, BMElem *ele,
195         const float (*coords)[3])
196 {
197         EDBM_preselect_elem_clear(psel);
198
199         if (coords) {
200                 BM_mesh_elem_index_ensure(bm, BM_VERT);
201         }
202
203         switch (ele->head.htype) {
204                 case BM_VERT:
205                         view3d_preselect_mesh_elem_update_from_vert(psel, bm, (BMVert *)ele, coords);
206                         break;
207                 case BM_EDGE:
208                         view3d_preselect_mesh_elem_update_from_edge(psel, bm, (BMEdge *)ele, coords);
209                         break;
210                 case BM_FACE:
211                         view3d_preselect_mesh_elem_update_from_face(psel, bm, (BMFace *)ele, coords);
212                         break;
213                 default:
214                         BLI_assert(0);
215         }
216 }
217
218 /** \} */