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