1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file primitive_manipulator.c
* \ingroup wm
*
* \name Primitive Manipulator
*
* 3D Manipulator
*
* \brief Manipulator with primitive drawing type (plane, cube, etc.).
* Currently only plane primitive supported without own handling, use with operator only.
*/
#include "BIF_gl.h"
#include "BKE_context.h"
#include "BLI_math.h"
#include "DNA_view3d_types.h"
#include "DNA_manipulator_types.h"
#include "GPU_immediate.h"
#include "GPU_matrix.h"
#include "GPU_select.h"
#include "MEM_guardedalloc.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_manipulator_library.h"
/* own includes */
#include "manipulator_library_intern.h"
/* PrimitiveManipulator->flag */
enum {
PRIM_UP_VECTOR_SET = (1 << 0),
};
typedef struct PrimitiveManipulator {
wmManipulator manipulator;
float direction[3];
float up[3];
int style;
int flag;
} PrimitiveManipulator;
static float verts_plane[4][3] = {
{-1, -1, 0},
{ 1, -1, 0},
{ 1, 1, 0},
{-1, 1, 0},
};
/* -------------------------------------------------------------------- */
static void manipulator_primitive_draw_geom(
const float col_inner[4], const float col_outer[4], const int style)
{
float (*verts)[3];
unsigned int vert_count = 0;
if (style == ED_MANIPULATOR_PRIMITIVE_STYLE_PLANE) {
verts = verts_plane;
vert_count = ARRAY_SIZE(verts_plane);
}
if (vert_count > 0) {
unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_F32, 3, KEEP_FLOAT);
immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
wm_manipulator_vec_draw(col_inner, verts, vert_count, pos, PRIM_TRIANGLE_FAN);
wm_manipulator_vec_draw(col_outer, verts, vert_count, pos, PRIM_LINE_LOOP);
immUnbindProgram();
}
}
static void manipulator_primitive_draw_intern(
PrimitiveManipulator *prim, const bool UNUSED(select),
const bool highlight)
{
float col_inner[4], col_outer[4];
float rot[3][3];
float mat[4][4];
if (prim->flag & PRIM_UP_VECTOR_SET) {
copy_v3_v3(rot[2], prim->direction);
copy_v3_v3(rot[1], prim->up);
cross_v3_v3v3(rot[0], prim->up, prim->direction);
}
else {
const float up[3] = {0.0f, 0.0f, 1.0f};
rotation_between_vecs_to_mat3(rot, up, prim->direction);
}
copy_m4_m3(mat, rot);
copy_v3_v3(mat[3], prim->manipulator.origin);
mul_mat3_m4_fl(mat, prim->manipulator.scale);
gpuPushMatrix();
gpuMultMatrix(mat);
manipulator_color_get(&prim->manipulator, highlight, col_outer);
copy_v4_v4(col_inner, col_outer);
col_inner[3] *= 0.5f;
glEnable(GL_BLEND);
gpuTranslate3fv(prim->manipulator.offset);
manipulator_primitive_draw_geom(col_inner, col_outer, prim->style);
glDisable(GL_BLEND);
gpuPopMatrix();
if (prim->manipulator.interaction_data) {
ManipulatorInteraction *inter = prim->manipulator.interaction_data;
copy_v4_fl(col_inner, 0.5f);
copy_v3_fl(col_outer, 0.5f);
col_outer[3] = 0.8f;
copy_m4_m3(mat, rot);
copy_v3_v3(mat[3], inter->init_origin);
mul_mat3_m4_fl(mat, inter->init_scale);
gpuPushMatrix();
gpuMultMatrix(mat);
glEnable(GL_BLEND);
gpuTranslate3fv(prim->manipulator.offset);
manipulator_primitive_draw_geom(col_inner, col_outer, prim->style);
glDisable(GL_BLEND);
gpuPopMatrix();
}
}
static void manipulator_primitive_render_3d_intersect(
const bContext *UNUSED(C), wmManipulator *manipulator,
int selectionbase)
{
GPU_select_load_id(selectionbase);
manipulator_primitive_draw_intern((PrimitiveManipulator *)manipulator, true, false);
}
static void manipulator_primitive_draw(const bContext *UNUSED(C), wmManipulator *manipulator)
{
manipulator_primitive_draw_intern(
(PrimitiveManipulator *)manipulator, false,
(manipulator->state & WM_MANIPULATOR_STATE_HIGHLIGHT));
}
static void manipulator_primitive_invoke(
bContext *UNUSED(C), wmManipulator *manipulator, const wmEvent *UNUSED(event))
{
ManipulatorInteraction *inter = MEM_callocN(sizeof(ManipulatorInteraction), __func__);
copy_v3_v3(inter->init_origin, manipulator->origin);
inter->init_scale = manipulator->scale;
manipulator->interaction_data = inter;
}
/* -------------------------------------------------------------------- */
/** \name Primitive Manipulator API
*
* \{ */
wmManipulator *ED_manipulator_primitive3d_new(wmManipulatorGroup *mgroup, const char *name, const int style)
{
const wmManipulatorType *mpt = WM_manipulatortype_find("MANIPULATOR_WT_primitive3d", false);
PrimitiveManipulator *prim = (PrimitiveManipulator *)WM_manipulator_new(mpt, mgroup, name);
const float dir_default[3] = {0.0f, 0.0f, 1.0f};
prim->manipulator.flag |= WM_MANIPULATOR_DRAW_ACTIVE;
prim->style = style;
/* defaults */
copy_v3_v3(prim->direction, dir_default);
return (wmManipulator *)prim;
}
/**
* Define direction the primitive will point towards
*/
void ED_manipulator_primitive3d_set_direction(wmManipulator *manipulator, const float direction[3])
{
PrimitiveManipulator *prim = (PrimitiveManipulator *)manipulator;
normalize_v3_v3(prim->direction, direction);
}
/**
* Define up-direction of the primitive manipulator
*/
void ED_manipulator_primitive3d_set_up_vector(wmManipulator *manipulator, const float direction[3])
{
PrimitiveManipulator *prim = (PrimitiveManipulator *)manipulator;
if (direction) {
normalize_v3_v3(prim->up, direction);
prim->flag |= PRIM_UP_VECTOR_SET;
}
else {
prim->flag &= ~PRIM_UP_VECTOR_SET;
}
}
static void MANIPULATOR_WT_primitive3d(wmManipulatorType *wt)
{
/* identifiers */
wt->idname = "MANIPULATOR_WT_primitive3d";
/* api callbacks */
wt->draw = manipulator_primitive_draw;
wt->draw_select = manipulator_primitive_render_3d_intersect;
wt->invoke = manipulator_primitive_invoke;
wt->size = sizeof(PrimitiveManipulator);
}
void ED_manipulatortypes_primitive_3d(void)
{
WM_manipulatortype_append(MANIPULATOR_WT_primitive3d);
}
/** \} */ // Primitive Manipulator API
|