doxygen: add newline after \file
[blender.git] / source / blender / editors / mesh / editmesh_preselect_elem.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup edmesh
19  */
20
21 #include "MEM_guardedalloc.h"
22
23 #include "BLI_math.h"
24
25 #include "BKE_editmesh.h"
26
27 #include "GPU_immediate.h"
28 #include "GPU_matrix.h"
29 #include "GPU_state.h"
30
31 #include "DNA_object_types.h"
32
33 #include "ED_mesh.h"
34 #include "ED_view3d.h"
35
36 /* -------------------------------------------------------------------- */
37 /** \name Mesh Element Pre-Select
38  * Public API:
39  *
40  * #EDBM_preselect_elem_create
41  * #EDBM_preselect_elem_destroy
42  * #EDBM_preselect_elem_clear
43  * #EDBM_preselect_elem_draw
44  * #EDBM_preselect_elem_update_from_single
45  *
46  * \{ */
47
48 static void vcos_get(BMVert *v, float r_co[3], const float (*coords)[3])
49 {
50         if (coords) {
51                 copy_v3_v3(r_co, coords[BM_elem_index_get(v)]);
52         }
53         else {
54                 copy_v3_v3(r_co, v->co);
55         }
56 }
57
58 static void vcos_get_pair(BMVert *v[2], float r_cos[2][3], const float (*coords)[3])
59 {
60         if (coords) {
61                 for (int j = 0; j < 2; j++) {
62                         copy_v3_v3(r_cos[j], coords[BM_elem_index_get(v[j])]);
63                 }
64         }
65         else {
66                 for (int j = 0; j < 2; j++) {
67                         copy_v3_v3(r_cos[j], v[j]->co);
68                 }
69         }
70 }
71
72 struct EditMesh_PreSelElem {
73         float (*edges)[2][3];
74         int     edges_len;
75
76         float (*verts)[3];
77         int     verts_len;
78 };
79
80 struct EditMesh_PreSelElem *EDBM_preselect_elem_create(void)
81 {
82         struct EditMesh_PreSelElem *psel = MEM_callocN(sizeof(*psel), __func__);
83         return psel;
84 }
85
86 void EDBM_preselect_elem_destroy(
87         struct EditMesh_PreSelElem *psel)
88 {
89         EDBM_preselect_elem_clear(psel);
90         MEM_freeN(psel);
91 }
92
93 void EDBM_preselect_elem_clear(
94         struct EditMesh_PreSelElem *psel)
95 {
96         MEM_SAFE_FREE(psel->edges);
97         psel->edges_len = 0;
98
99         MEM_SAFE_FREE(psel->verts);
100         psel->verts_len = 0;
101 }
102
103 void EDBM_preselect_elem_draw(
104         struct EditMesh_PreSelElem *psel, const float matrix[4][4])
105 {
106         if ((psel->edges_len == 0) && (psel->verts_len == 0)) {
107                 return;
108         }
109
110         GPU_depth_test(false);
111
112         GPU_matrix_push();
113         GPU_matrix_mul(matrix);
114
115         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
116
117         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
118         immUniformColor3ub(255, 0, 255);
119
120         if (psel->edges_len > 0) {
121                 immBegin(GPU_PRIM_LINES, psel->edges_len * 2);
122
123                 for (int i = 0; i < psel->edges_len; i++) {
124                         immVertex3fv(pos, psel->edges[i][0]);
125                         immVertex3fv(pos, psel->edges[i][1]);
126                 }
127
128                 immEnd();
129         }
130
131         if (psel->verts_len > 0) {
132                 GPU_point_size(3.0f);
133
134                 immBegin(GPU_PRIM_POINTS, psel->verts_len);
135
136                 for (int i = 0; i < psel->verts_len; i++) {
137                         immVertex3fv(pos, psel->verts[i]);
138                 }
139
140                 immEnd();
141         }
142
143         immUnbindProgram();
144
145         GPU_matrix_pop();
146
147         /* Reset default */
148         GPU_depth_test(true);
149 }
150
151 static void view3d_preselect_mesh_elem_update_from_vert(
152         struct EditMesh_PreSelElem *psel,
153         BMesh *UNUSED(bm), BMVert *eve, const float (*coords)[3])
154 {
155         float (*verts)[3] = MEM_mallocN(sizeof(*psel->verts), __func__);
156         vcos_get(eve, verts[0], coords);
157         psel->verts = verts;
158         psel->verts_len = 1;
159 }
160
161 static void view3d_preselect_mesh_elem_update_from_edge(
162         struct EditMesh_PreSelElem *psel,
163         BMesh *UNUSED(bm), BMEdge *eed, const float (*coords)[3])
164 {
165         float (*edges)[2][3] = MEM_mallocN(sizeof(*psel->edges), __func__);
166         vcos_get_pair(&eed->v1, edges[0], coords);
167         psel->edges = edges;
168         psel->edges_len = 1;
169 }
170
171 static void view3d_preselect_mesh_elem_update_from_face(
172         struct EditMesh_PreSelElem *psel,
173         BMesh *UNUSED(bm), BMFace *efa, const float (*coords)[3])
174 {
175         float (*edges)[2][3] = MEM_mallocN(sizeof(*psel->edges) * efa->len, __func__);
176         BMLoop *l_iter, *l_first;
177         l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
178         int i = 0;
179         do {
180                 vcos_get_pair(&l_iter->e->v1, edges[i++], coords);
181         } while ((l_iter = l_iter->next) != l_first);
182         psel->edges = edges;
183         psel->edges_len = efa->len;
184 }
185
186 void EDBM_preselect_elem_update_from_single(
187         struct EditMesh_PreSelElem *psel,
188         BMesh *bm, BMElem *ele,
189         const float (*coords)[3])
190 {
191         EDBM_preselect_elem_clear(psel);
192
193         if (coords) {
194                 BM_mesh_elem_index_ensure(bm, BM_VERT);
195         }
196
197         switch (ele->head.htype) {
198                 case BM_VERT:
199                         view3d_preselect_mesh_elem_update_from_vert(psel, bm, (BMVert *)ele, coords);
200                         break;
201                 case BM_EDGE:
202                         view3d_preselect_mesh_elem_update_from_edge(psel, bm, (BMEdge *)ele, coords);
203                         break;
204                 case BM_FACE:
205                         view3d_preselect_mesh_elem_update_from_face(psel, bm, (BMFace *)ele, coords);
206                         break;
207                 default:
208                         BLI_assert(0);
209         }
210 }
211
212 /** \} */