Cleanup: use shorter name for shader config
[blender.git] / source / blender / draw / modes / edit_metaball_mode.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  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file \ingroup draw
20  */
21
22 #include "DRW_render.h"
23
24 #include "DNA_meta_types.h"
25
26 #include "BKE_object.h"
27
28 #include "ED_mball.h"
29
30 /* If builtin shaders are needed */
31 #include "GPU_shader.h"
32
33 #include "draw_common.h"
34
35
36 /* *********** LISTS *********** */
37 /* All lists are per viewport specific datas.
38  * They are all free when viewport changes engines
39  * or is free itself. Use EDIT_METABALL_engine_init() to
40  * initialize most of them and EDIT_METABALL_cache_init()
41  * for EDIT_METABALL_PassList */
42
43 typedef struct EDIT_METABALL_PassList {
44         /* Declare all passes here and init them in
45          * EDIT_METABALL_cache_init().
46          * Only contains (DRWPass *) */
47         struct DRWPass *pass;
48 } EDIT_METABALL_PassList;
49
50 typedef struct EDIT_METABALL_FramebufferList {
51         /* Contains all framebuffer objects needed by this engine.
52          * Only contains (GPUFrameBuffer *) */
53         struct GPUFrameBuffer *fb;
54 } EDIT_METABALL_FramebufferList;
55
56 typedef struct EDIT_METABALL_TextureList {
57         /* Contains all framebuffer textures / utility textures
58          * needed by this engine. Only viewport specific textures
59          * (not per object). Only contains (GPUTexture *) */
60         struct GPUTexture *texture;
61 } EDIT_METABALL_TextureList;
62
63 typedef struct EDIT_METABALL_StorageList {
64         /* Contains any other memory block that the engine needs.
65          * Only directly MEM_(m/c)allocN'ed blocks because they are
66          * free with MEM_freeN() when viewport is freed.
67          * (not per object) */
68         // struct CustomStruct *block;
69         struct EDIT_METABALL_PrivateData *g_data;
70 } EDIT_METABALL_StorageList;
71
72 typedef struct EDIT_METABALL_Data {
73         /* Struct returned by DRW_viewport_engine_data_ensure.
74          * If you don't use one of these, just make it a (void *) */
75         // void *fbl;
76         void *engine_type; /* Required */
77         EDIT_METABALL_FramebufferList *fbl;
78         EDIT_METABALL_TextureList *txl;
79         EDIT_METABALL_PassList *psl;
80         EDIT_METABALL_StorageList *stl;
81 } EDIT_METABALL_Data;
82
83 /* *********** STATIC *********** */
84
85 typedef struct EDIT_METABALL_PrivateData {
86         /* This keeps the references of the shading groups for
87          * easy access in EDIT_METABALL_cache_populate() */
88         DRWShadingGroup *group;
89 } EDIT_METABALL_PrivateData; /* Transient data */
90
91 /* *********** FUNCTIONS *********** */
92
93 static void EDIT_METABALL_engine_init(void *UNUSED(vedata))
94 {
95         const DRWContextState *draw_ctx = DRW_context_state_get();
96         const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
97         if (is_clip) {
98                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
99         }
100 }
101
102 /* Here init all passes and shading groups
103  * Assume that all Passes are NULL */
104 static void EDIT_METABALL_cache_init(void *vedata)
105 {
106         EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
107         EDIT_METABALL_StorageList *stl = ((EDIT_METABALL_Data *)vedata)->stl;
108         const DRWContextState *draw_ctx = DRW_context_state_get();
109
110
111         if (!stl->g_data) {
112                 /* Alloc transient pointers */
113                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
114         }
115
116         {
117                 /* Create a pass */
118                 DRWState state = (
119                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
120                         DRW_STATE_BLEND | DRW_STATE_WIRE);
121                 psl->pass = DRW_pass_create("My Pass", state);
122
123                 /* Create a shadingGroup using a function in draw_common.c or custom one */
124                 stl->g_data->group = shgroup_instance_mball_handles(psl->pass, draw_ctx->sh_cfg);
125         }
126 }
127
128 /* Add geometry to shadingGroups. Execute for each objects */
129 static void EDIT_METABALL_cache_populate(void *vedata, Object *ob)
130 {
131         //EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
132         EDIT_METABALL_StorageList *stl = ((EDIT_METABALL_Data *)vedata)->stl;
133
134         if (ob->type == OB_MBALL) {
135                 const DRWContextState *draw_ctx = DRW_context_state_get();
136                 DRWShadingGroup *group = stl->g_data->group;
137
138                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
139                         MetaBall *mb = ob->data;
140
141                         const float *color;
142                         const float col_radius[3]           = {0.63, 0.19, 0.19}; /* 0x3030A0 */
143                         const float col_radius_select[3]    = {0.94, 0.63, 0.63}; /* 0xA0A0F0 */
144                         const float col_stiffness[3]        = {0.19, 0.63, 0.19}; /* 0x30A030 */
145                         const float col_stiffness_select[3] = {0.63, 0.94, 0.63}; /* 0xA0F0A0 */
146
147                         const bool is_select = DRW_state_is_select();
148
149                         float draw_scale_xform[3][4]; /* Matrix of Scale and Translation */
150                         {
151                                 float scamat[3][3];
152                                 copy_m3_m4(scamat, ob->obmat);
153                                 /* Get the normalized inverse matrix to extract only
154                                  * the scale of Scamat */
155                                 float iscamat[3][3];
156                                 invert_m3_m3(iscamat, scamat);
157                                 normalize_m3(iscamat);
158                                 mul_m3_m3_post(scamat, iscamat);
159
160                                 copy_v3_v3(draw_scale_xform[0], scamat[0]);
161                                 copy_v3_v3(draw_scale_xform[1], scamat[1]);
162                                 copy_v3_v3(draw_scale_xform[2], scamat[2]);
163                         }
164
165                         int selection_id = ob->select_color;
166                         for (MetaElem *ml = mb->editelems->first; ml != NULL; ml = ml->next, selection_id += 0x10000) {
167                                 float world_pos[3];
168                                 mul_v3_m4v3(world_pos, ob->obmat, &ml->x);
169                                 draw_scale_xform[0][3] = world_pos[0];
170                                 draw_scale_xform[1][3] = world_pos[1];
171                                 draw_scale_xform[2][3] = world_pos[2];
172
173                                 float draw_stiffness_radius = ml->rad * atanf(ml->s) / (float)M_PI_2;
174
175                                 if ((ml->flag & SELECT) && (ml->flag & MB_SCALE_RAD)) color = col_radius_select;
176                                 else color = col_radius;
177
178                                 if (is_select) {
179                                         DRW_select_load_id(selection_id | MBALLSEL_RADIUS);
180                                 }
181
182                                 DRW_shgroup_call_dynamic_add(group, draw_scale_xform, &ml->rad, color);
183
184                                 if ((ml->flag & SELECT) && !(ml->flag & MB_SCALE_RAD)) color = col_stiffness_select;
185                                 else color = col_stiffness;
186
187                                 if (is_select) {
188                                         DRW_select_load_id(selection_id | MBALLSEL_STIFF);
189                                 }
190
191                                 DRW_shgroup_call_dynamic_add(group, draw_scale_xform, &draw_stiffness_radius, color);
192                         }
193                 }
194         }
195 }
196
197 /* Draw time ! Control rendering pipeline from here */
198 static void EDIT_METABALL_draw_scene(void *vedata)
199 {
200         EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
201         /* render passes on default framebuffer. */
202         DRW_draw_pass(psl->pass);
203
204         /* If you changed framebuffer, double check you rebind
205          * the default one with its textures attached before finishing */
206
207         DRW_state_clip_planes_reset();
208 }
209
210 /* Cleanup when destroying the engine.
211  * This is not per viewport ! only when quitting blender.
212  * Mostly used for freeing shaders */
213 static void EDIT_METABALL_engine_free(void)
214 {
215         // DRW_SHADER_FREE_SAFE(custom_shader);
216 }
217
218 static const DrawEngineDataSize EDIT_METABALL_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_METABALL_Data);
219
220 DrawEngineType draw_engine_edit_metaball_type = {
221         NULL, NULL,
222         N_("EditMetaballMode"),
223         &EDIT_METABALL_data_size,
224         &EDIT_METABALL_engine_init,
225         &EDIT_METABALL_engine_free,
226         &EDIT_METABALL_cache_init,
227         &EDIT_METABALL_cache_populate,
228         NULL,
229         NULL, /* draw_background but not needed by mode engines */
230         &EDIT_METABALL_draw_scene,
231         NULL,
232         NULL,
233 };