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