Blender  V2.93
primitive3d_gizmo.c
Go to the documentation of this file.
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 
28 #include "MEM_guardedalloc.h"
29 
30 #include "BLI_math.h"
31 
32 #include "DNA_view3d_types.h"
33 
34 #include "BKE_context.h"
35 
36 #include "GPU_immediate.h"
37 #include "GPU_matrix.h"
38 #include "GPU_select.h"
39 #include "GPU_state.h"
40 
41 #include "RNA_access.h"
42 #include "RNA_define.h"
43 
44 #include "WM_api.h"
45 #include "WM_types.h"
46 
47 #include "ED_gizmo_library.h"
48 
49 /* own includes */
50 #include "../gizmo_library_intern.h"
51 
52 static float verts_plane[4][3] = {
53  {-1, -1, 0},
54  {1, -1, 0},
55  {1, 1, 0},
56  {-1, 1, 0},
57 };
58 
59 /* -------------------------------------------------------------------- */
60 
61 static void gizmo_primitive_draw_geom(const float col_inner[4],
62  const float col_outer[4],
63  const int draw_style)
64 {
65  float(*verts)[3];
66  uint vert_count = 0;
67 
68  if (draw_style == ED_GIZMO_PRIMITIVE_STYLE_PLANE) {
70  vert_count = ARRAY_SIZE(verts_plane);
71  }
72 
73  if (vert_count > 0) {
76  wm_gizmo_vec_draw(col_inner, verts, vert_count, pos, GPU_PRIM_TRI_FAN);
77  wm_gizmo_vec_draw(col_outer, verts, vert_count, pos, GPU_PRIM_LINE_LOOP);
79  }
80 }
81 
83  const bool UNUSED(select),
84  const bool highlight)
85 {
86  float color_inner[4], color_outer[4];
87  float matrix_final[4][4];
88  const int draw_style = RNA_enum_get(gz->ptr, "draw_style");
89 
90  gizmo_color_get(gz, highlight, color_outer);
91  copy_v4_v4(color_inner, color_outer);
92  color_inner[3] *= 0.5f;
93 
94  WM_gizmo_calc_matrix_final(gz, matrix_final);
95 
97  GPU_matrix_mul(matrix_final);
98 
100  gizmo_primitive_draw_geom(color_inner, color_outer, draw_style);
102 
103  GPU_matrix_pop();
104 
105  if (gz->interaction_data) {
106  GizmoInteraction *inter = gz->interaction_data;
107 
108  copy_v4_fl(color_inner, 0.5f);
109  copy_v3_fl(color_outer, 0.5f);
110  color_outer[3] = 0.8f;
111 
112  GPU_matrix_push();
114 
116  gizmo_primitive_draw_geom(color_inner, color_outer, draw_style);
118 
119  GPU_matrix_pop();
120  }
121 }
122 
123 static void gizmo_primitive_draw_select(const bContext *UNUSED(C), wmGizmo *gz, int select_id)
124 {
125  GPU_select_load_id(select_id);
126  gizmo_primitive_draw_intern(gz, true, false);
127 }
128 
129 static void gizmo_primitive_draw(const bContext *UNUSED(C), wmGizmo *gz)
130 {
132 }
133 
135 {
136  gz->flag |= WM_GIZMO_DRAW_MODAL;
137 }
138 
139 static int gizmo_primitive_invoke(bContext *UNUSED(C), wmGizmo *gz, const wmEvent *UNUSED(event))
140 {
141  GizmoInteraction *inter = MEM_callocN(sizeof(GizmoInteraction), __func__);
142 
144 
145  gz->interaction_data = inter;
146 
147  return OPERATOR_RUNNING_MODAL;
148 }
149 
150 /* -------------------------------------------------------------------- */
155 {
156  /* identifiers */
157  gzt->idname = "GIZMO_GT_primitive_3d";
158 
159  /* api callbacks */
160  gzt->draw = gizmo_primitive_draw;
164 
165  gzt->struct_size = sizeof(wmGizmo);
166 
167  static EnumPropertyItem rna_enum_draw_style[] = {
168  {ED_GIZMO_PRIMITIVE_STYLE_PLANE, "PLANE", 0, "Plane", ""},
169  {0, NULL, 0, NULL, NULL},
170  };
171  RNA_def_enum(gzt->srna,
172  "draw_style",
173  rna_enum_draw_style,
175  "Draw Style",
176  "");
177 }
178 
180 {
182 }
183 
typedef float(TangentPoint)[2]
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void copy_v4_fl(float r[4], float f)
unsigned int uint
Definition: BLI_sys_types.h:83
#define ARRAY_SIZE(arr)
#define UNUSED(x)
@ OPERATOR_RUNNING_MODAL
@ ED_GIZMO_PRIMITIVE_STYLE_PLANE
void immUnbindProgram(void)
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
GPUVertFormat * immVertexFormat(void)
void GPU_matrix_pop(void)
Definition: gpu_matrix.cc:142
#define GPU_matrix_mul(x)
Definition: GPU_matrix.h:223
void GPU_matrix_push(void)
Definition: gpu_matrix.cc:135
@ GPU_PRIM_TRI_FAN
Definition: GPU_primitive.h:41
@ GPU_PRIM_LINE_LOOP
Definition: GPU_primitive.h:39
bool GPU_select_load_id(unsigned int id)
Definition: gpu_select.c:108
@ GPU_SHADER_3D_UNIFORM_COLOR
Definition: GPU_shader.h:200
@ GPU_BLEND_NONE
Definition: GPU_state.h:55
@ GPU_BLEND_ALPHA
Definition: GPU_state.h:57
void GPU_blend(eGPUBlend blend)
Definition: gpu_state.cc:55
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
struct wmGizmo wmGizmo
Definition: WM_api.h:74
@ WM_GIZMO_DRAW_MODAL
@ WM_GIZMO_STATE_HIGHLIGHT
static float verts[][3]
void wm_gizmo_vec_draw(const float color[4], const float(*verts)[3], uint vert_count, uint pos, uint primitive_type)
void gizmo_color_get(const struct wmGizmo *gz, const bool highlight, float r_color[4])
uint pos
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void ED_gizmotypes_primitive_3d(void)
static void gizmo_primitive_draw(const bContext *UNUSED(C), wmGizmo *gz)
static void gizmo_primitive_setup(wmGizmo *gz)
static void gizmo_primitive_draw_intern(wmGizmo *gz, const bool UNUSED(select), const bool highlight)
static float verts_plane[4][3]
static int gizmo_primitive_invoke(bContext *UNUSED(C), wmGizmo *gz, const wmEvent *UNUSED(event))
static void gizmo_primitive_draw_select(const bContext *UNUSED(C), wmGizmo *gz, int select_id)
static void GIZMO_GT_primitive_3d(wmGizmoType *gzt)
static void gizmo_primitive_draw_geom(const float col_inner[4], const float col_outer[4], const int draw_style)
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3771
float init_matrix_final[4][4]
wmGizmoFnDraw draw
wmGizmoFnSetup setup
const char * idname
struct StructRNA * srna
wmGizmoFnInvoke invoke
wmGizmoFnDrawSelect draw_select
void * interaction_data
eWM_GizmoFlagState state
struct PointerRNA * ptr
eWM_GizmoFlag flag
__forceinline const avxb select(const avxb &m, const avxb &t, const avxb &f)
Definition: util_avxb.h:167
void WM_gizmo_calc_matrix_final(const wmGizmo *gz, float r_mat[4][4])
Definition: wm_gizmo.c:601
void WM_gizmotype_append(void(*gtfunc)(struct wmGizmoType *))