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