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