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