Cleanup: move clipping shader lib & define into struct
[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         if (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) {
97                 DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
98         }
99 }
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         const DRWContextState *draw_ctx = DRW_context_state_get();
108
109
110         if (!stl->g_data) {
111                 /* Alloc transient pointers */
112                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
113         }
114
115         {
116                 /* Create a pass */
117                 DRWState state = (
118                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
119                         DRW_STATE_BLEND | DRW_STATE_WIRE);
120                 psl->pass = DRW_pass_create("My Pass", state);
121
122                 /* Create a shadingGroup using a function in draw_common.c or custom one */
123                 stl->g_data->group = shgroup_instance_mball_handles(psl->pass, draw_ctx->sh_cfg);
124         }
125 }
126
127 /* Add geometry to shadingGroups. Execute for each objects */
128 static void EDIT_METABALL_cache_populate(void *vedata, Object *ob)
129 {
130         //EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
131         EDIT_METABALL_StorageList *stl = ((EDIT_METABALL_Data *)vedata)->stl;
132
133         if (ob->type == OB_MBALL) {
134                 const DRWContextState *draw_ctx = DRW_context_state_get();
135                 DRWShadingGroup *group = stl->g_data->group;
136
137                 if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
138                         MetaBall *mb = ob->data;
139
140                         const float *color;
141                         const float col_radius[3]           = {0.63, 0.19, 0.19}; /* 0x3030A0 */
142                         const float col_radius_select[3]    = {0.94, 0.63, 0.63}; /* 0xA0A0F0 */
143                         const float col_stiffness[3]        = {0.19, 0.63, 0.19}; /* 0x30A030 */
144                         const float col_stiffness_select[3] = {0.63, 0.94, 0.63}; /* 0xA0F0A0 */
145
146                         const bool is_select = DRW_state_is_select();
147
148                         float draw_scale_xform[3][4]; /* Matrix of Scale and Translation */
149                         {
150                                 float scamat[3][3];
151                                 copy_m3_m4(scamat, ob->obmat);
152                                 /* Get the normalized inverse matrix to extract only
153                                  * the scale of Scamat */
154                                 float iscamat[3][3];
155                                 invert_m3_m3(iscamat, scamat);
156                                 normalize_m3(iscamat);
157                                 mul_m3_m3_post(scamat, iscamat);
158
159                                 copy_v3_v3(draw_scale_xform[0], scamat[0]);
160                                 copy_v3_v3(draw_scale_xform[1], scamat[1]);
161                                 copy_v3_v3(draw_scale_xform[2], scamat[2]);
162                         }
163
164                         int selection_id = ob->select_color;
165                         for (MetaElem *ml = mb->editelems->first; ml != NULL; ml = ml->next, selection_id += 0x10000) {
166                                 float world_pos[3];
167                                 mul_v3_m4v3(world_pos, ob->obmat, &ml->x);
168                                 draw_scale_xform[0][3] = world_pos[0];
169                                 draw_scale_xform[1][3] = world_pos[1];
170                                 draw_scale_xform[2][3] = world_pos[2];
171
172                                 float draw_stiffness_radius = ml->rad * atanf(ml->s) / (float)M_PI_2;
173
174                                 if ((ml->flag & SELECT) && (ml->flag & MB_SCALE_RAD)) color = col_radius_select;
175                                 else color = col_radius;
176
177                                 if (is_select) {
178                                         DRW_select_load_id(selection_id | MBALLSEL_RADIUS);
179                                 }
180
181                                 DRW_shgroup_call_dynamic_add(group, draw_scale_xform, &ml->rad, color);
182
183                                 if ((ml->flag & SELECT) && !(ml->flag & MB_SCALE_RAD)) color = col_stiffness_select;
184                                 else color = col_stiffness;
185
186                                 if (is_select) {
187                                         DRW_select_load_id(selection_id | MBALLSEL_STIFF);
188                                 }
189
190                                 DRW_shgroup_call_dynamic_add(group, draw_scale_xform, &draw_stiffness_radius, color);
191                         }
192                 }
193         }
194 }
195
196 /* Draw time ! Control rendering pipeline from here */
197 static void EDIT_METABALL_draw_scene(void *vedata)
198 {
199         EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
200         /* render passes on default framebuffer. */
201         DRW_draw_pass(psl->pass);
202
203         /* If you changed framebuffer, double check you rebind
204          * the default one with its textures attached before finishing */
205
206         DRW_state_clip_planes_reset();
207 }
208
209 /* Cleanup when destroying the engine.
210  * This is not per viewport ! only when quitting blender.
211  * Mostly used for freeing shaders */
212 static void EDIT_METABALL_engine_free(void)
213 {
214         // DRW_SHADER_FREE_SAFE(custom_shader);
215 }
216
217 static const DrawEngineDataSize EDIT_METABALL_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_METABALL_Data);
218
219 DrawEngineType draw_engine_edit_metaball_type = {
220         NULL, NULL,
221         N_("EditMetaballMode"),
222         &EDIT_METABALL_data_size,
223         &EDIT_METABALL_engine_init,
224         &EDIT_METABALL_engine_free,
225         &EDIT_METABALL_cache_init,
226         &EDIT_METABALL_cache_populate,
227         NULL,
228         NULL, /* draw_background but not needed by mode engines */
229         &EDIT_METABALL_draw_scene,
230         NULL,
231         NULL,
232 };