2017-03-12 21:16:03 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright 2016, Blender Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Blender Institute
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file blender/draw/modes/edit_metaball_mode.c
|
|
|
|
|
* \ingroup draw
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "DRW_engine.h"
|
|
|
|
|
#include "DRW_render.h"
|
|
|
|
|
|
2017-11-16 15:12:32 -02:00
|
|
|
#include "DNA_meta_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BKE_mball.h"
|
|
|
|
|
|
2017-03-12 21:16:03 +01:00
|
|
|
/* If builtin shaders are needed */
|
|
|
|
|
#include "GPU_shader.h"
|
2017-11-16 15:12:32 -02:00
|
|
|
#include "GPU_select.h"
|
2017-03-12 21:16:03 +01:00
|
|
|
|
|
|
|
|
#include "draw_common.h"
|
|
|
|
|
|
|
|
|
|
#include "draw_mode_engines.h"
|
|
|
|
|
|
2017-03-18 22:14:53 +01:00
|
|
|
/* If needed, contains all global/Theme colors
|
|
|
|
|
* Add needed theme colors / values to DRW_globals_update() and update UBO
|
|
|
|
|
* Not needed for constant color. */
|
|
|
|
|
extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
|
|
|
|
|
extern struct GlobalsUboStorage ts; /* draw_common.c */
|
|
|
|
|
|
2017-03-12 21:16:03 +01:00
|
|
|
/* *********** LISTS *********** */
|
|
|
|
|
/* All lists are per viewport specific datas.
|
|
|
|
|
* They are all free when viewport changes engines
|
|
|
|
|
* or is free itself. Use EDIT_METABALL_engine_init() to
|
|
|
|
|
* initialize most of them and EDIT_METABALL_cache_init()
|
|
|
|
|
* for EDIT_METABALL_PassList */
|
|
|
|
|
|
|
|
|
|
typedef struct EDIT_METABALL_PassList {
|
|
|
|
|
/* Declare all passes here and init them in
|
|
|
|
|
* EDIT_METABALL_cache_init().
|
|
|
|
|
* Only contains (DRWPass *) */
|
|
|
|
|
struct DRWPass *pass;
|
|
|
|
|
} EDIT_METABALL_PassList;
|
|
|
|
|
|
|
|
|
|
typedef struct EDIT_METABALL_FramebufferList {
|
|
|
|
|
/* Contains all framebuffer objects needed by this engine.
|
|
|
|
|
* Only contains (GPUFrameBuffer *) */
|
2017-03-13 11:39:41 +01:00
|
|
|
struct GPUFrameBuffer *fb;
|
2017-03-12 21:16:03 +01:00
|
|
|
} EDIT_METABALL_FramebufferList;
|
|
|
|
|
|
|
|
|
|
typedef struct EDIT_METABALL_TextureList {
|
|
|
|
|
/* Contains all framebuffer textures / utility textures
|
|
|
|
|
* needed by this engine. Only viewport specific textures
|
|
|
|
|
* (not per object). Only contains (GPUTexture *) */
|
2017-03-13 11:39:41 +01:00
|
|
|
struct GPUTexture *texture;
|
2017-03-12 21:16:03 +01:00
|
|
|
} EDIT_METABALL_TextureList;
|
|
|
|
|
|
|
|
|
|
typedef struct EDIT_METABALL_StorageList {
|
|
|
|
|
/* Contains any other memory block that the engine needs.
|
|
|
|
|
* Only directly MEM_(m/c)allocN'ed blocks because they are
|
|
|
|
|
* free with MEM_freeN() when viewport is freed.
|
|
|
|
|
* (not per object) */
|
2017-11-16 15:12:32 -02:00
|
|
|
// struct CustomStruct *block;
|
2017-04-29 16:52:12 +10:00
|
|
|
struct EDIT_METABALL_PrivateData *g_data;
|
2017-03-12 21:16:03 +01:00
|
|
|
} EDIT_METABALL_StorageList;
|
|
|
|
|
|
|
|
|
|
typedef struct EDIT_METABALL_Data {
|
|
|
|
|
/* Struct returned by DRW_viewport_engine_data_get.
|
|
|
|
|
* If you don't use one of these, just make it a (void *) */
|
|
|
|
|
// void *fbl;
|
2017-04-03 19:32:05 +02:00
|
|
|
void *engine_type; /* Required */
|
2017-03-12 21:16:03 +01:00
|
|
|
EDIT_METABALL_FramebufferList *fbl;
|
|
|
|
|
EDIT_METABALL_TextureList *txl;
|
|
|
|
|
EDIT_METABALL_PassList *psl;
|
|
|
|
|
EDIT_METABALL_StorageList *stl;
|
|
|
|
|
} EDIT_METABALL_Data;
|
|
|
|
|
|
|
|
|
|
/* *********** STATIC *********** */
|
|
|
|
|
|
2017-04-29 16:52:12 +10:00
|
|
|
typedef struct EDIT_METABALL_PrivateData {
|
2017-03-18 22:14:53 +01:00
|
|
|
/* This keeps the references of the shading groups for
|
|
|
|
|
* easy access in EDIT_METABALL_cache_populate() */
|
|
|
|
|
DRWShadingGroup *group;
|
2017-04-29 16:52:12 +10:00
|
|
|
} EDIT_METABALL_PrivateData; /* Transient data */
|
2017-03-12 21:16:03 +01:00
|
|
|
|
|
|
|
|
/* *********** FUNCTIONS *********** */
|
|
|
|
|
|
|
|
|
|
/* Here init all passes and shading groups
|
|
|
|
|
* Assume that all Passes are NULL */
|
2017-03-26 19:10:53 +02:00
|
|
|
static void EDIT_METABALL_cache_init(void *vedata)
|
2017-03-12 21:16:03 +01:00
|
|
|
{
|
2017-03-26 19:10:53 +02:00
|
|
|
EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
|
|
|
|
|
EDIT_METABALL_StorageList *stl = ((EDIT_METABALL_Data *)vedata)->stl;
|
2017-03-12 21:16:03 +01:00
|
|
|
|
2017-03-26 20:13:34 +02:00
|
|
|
if (!stl->g_data) {
|
|
|
|
|
/* Alloc transient pointers */
|
2017-04-29 16:52:12 +10:00
|
|
|
stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
|
2017-03-26 20:13:34 +02:00
|
|
|
}
|
2017-03-14 18:40:23 +11:00
|
|
|
|
2017-03-12 21:16:03 +01:00
|
|
|
{
|
|
|
|
|
/* Create a pass */
|
|
|
|
|
DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND | DRW_STATE_WIRE;
|
|
|
|
|
psl->pass = DRW_pass_create("My Pass", state);
|
|
|
|
|
|
|
|
|
|
/* Create a shadingGroup using a function in draw_common.c or custom one */
|
2017-11-16 15:12:32 -02:00
|
|
|
stl->g_data->group = shgroup_instance_mball_helpers(psl->pass, DRW_cache_screenspace_circle_get());
|
2017-03-12 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-16 15:12:32 -02:00
|
|
|
static void EDIT_METABALL_cache_populate_radius_visualization(
|
|
|
|
|
DRWShadingGroup *group, MetaElem *ml, const float scale_xform[3][4],
|
|
|
|
|
const float *radius, const int selection_id)
|
2017-03-12 21:16:03 +01:00
|
|
|
{
|
2017-11-16 15:12:32 -02:00
|
|
|
const float *color;
|
|
|
|
|
static const float col_radius[3] = {0.63, 0.19, 0.19}; /* 0x3030A0 */
|
|
|
|
|
static const float col_radius_select[3] = {0.94, 0.63, 0.63}; /* 0xA0A0F0 */
|
2017-03-12 21:16:03 +01:00
|
|
|
|
2017-11-16 15:12:32 -02:00
|
|
|
if ((ml->flag & SELECT) && (ml->flag & MB_SCALE_RAD)) color = col_radius_select;
|
|
|
|
|
else color = col_radius;
|
2017-03-14 18:40:23 +11:00
|
|
|
|
2017-11-16 15:12:32 -02:00
|
|
|
if (selection_id != -1) {
|
|
|
|
|
ml->selcol1 = selection_id;
|
|
|
|
|
DRW_select_load_id(selection_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DRW_shgroup_call_dynamic_add(group, scale_xform, radius, color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void EDIT_METABALL_cache_populate_stiffness_visualization(
|
|
|
|
|
DRWShadingGroup *group, MetaElem *ml, const float scale_xform[3][4],
|
|
|
|
|
const float *radius, const int selection_id)
|
|
|
|
|
{
|
|
|
|
|
const float *color;
|
|
|
|
|
static const float col_stiffness[3] = {0.19, 0.63, 0.19}; /* 0x30A030 */
|
|
|
|
|
static const float col_stiffness_select[3] = {0.63, 0.94, 0.63}; /* 0xA0F0A0 */
|
2017-03-12 21:16:03 +01:00
|
|
|
|
2017-11-16 15:12:32 -02:00
|
|
|
if ((ml->flag & SELECT) && !(ml->flag & MB_SCALE_RAD)) color = col_stiffness_select;
|
|
|
|
|
else color = col_stiffness;
|
|
|
|
|
|
|
|
|
|
if (selection_id != -1) {
|
|
|
|
|
ml->selcol2 = selection_id;
|
|
|
|
|
DRW_select_load_id(selection_id);
|
2017-03-12 21:16:03 +01:00
|
|
|
}
|
2017-11-16 15:12:32 -02:00
|
|
|
|
|
|
|
|
DRW_shgroup_call_dynamic_add(group, scale_xform, radius, color);
|
2017-03-12 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
2017-11-16 15:12:32 -02:00
|
|
|
/* Add geometry to shadingGroups. Execute for each objects */
|
|
|
|
|
static void EDIT_METABALL_cache_populate(void *vedata, Object *ob)
|
2017-03-12 21:16:03 +01:00
|
|
|
{
|
2017-11-16 15:12:32 -02:00
|
|
|
//EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
|
2017-03-26 19:10:53 +02:00
|
|
|
EDIT_METABALL_StorageList *stl = ((EDIT_METABALL_Data *)vedata)->stl;
|
2017-03-12 21:16:03 +01:00
|
|
|
|
2017-11-16 15:12:32 -02:00
|
|
|
if (ob->type == OB_MBALL) {
|
|
|
|
|
const DRWContextState *draw_ctx = DRW_context_state_get();
|
|
|
|
|
Scene *scene = draw_ctx->scene;
|
|
|
|
|
Object *obedit = scene->obedit;
|
|
|
|
|
DRWShadingGroup *group = stl->g_data->group;
|
|
|
|
|
|
|
|
|
|
if (ob == obedit) {
|
|
|
|
|
MetaBall *mb = ob->data;
|
|
|
|
|
|
|
|
|
|
const bool is_select = DRW_state_is_select();
|
|
|
|
|
|
|
|
|
|
int selection_id = 0;
|
|
|
|
|
|
|
|
|
|
for (MetaElem *ml = mb->editelems->first; ml != NULL; ml = ml->next) {
|
|
|
|
|
BKE_mball_element_calc_display_m3x4(ml->draw_scale_xform, ob->obmat, &ml->x);
|
|
|
|
|
ml->draw_stiffness_radius = ml->rad * atanf(ml->s) / (float)M_PI_2;
|
|
|
|
|
|
|
|
|
|
EDIT_METABALL_cache_populate_radius_visualization(
|
|
|
|
|
group, ml, ml->draw_scale_xform, &ml->rad, is_select ? ++selection_id : -1);
|
|
|
|
|
|
|
|
|
|
EDIT_METABALL_cache_populate_stiffness_visualization(
|
|
|
|
|
group, ml, ml->draw_scale_xform, &ml->draw_stiffness_radius, is_select ? ++selection_id : -1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-12 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Draw time ! Control rendering pipeline from here */
|
2017-03-26 19:10:53 +02:00
|
|
|
static void EDIT_METABALL_draw_scene(void *vedata)
|
2017-03-12 21:16:03 +01:00
|
|
|
{
|
2017-03-26 19:10:53 +02:00
|
|
|
EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
|
2017-11-16 15:12:32 -02:00
|
|
|
/* render passes on default framebuffer. */
|
2017-03-12 21:16:03 +01:00
|
|
|
DRW_draw_pass(psl->pass);
|
|
|
|
|
|
|
|
|
|
/* If you changed framebuffer, double check you rebind
|
|
|
|
|
* the default one with its textures attached before finishing */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Cleanup when destroying the engine.
|
|
|
|
|
* This is not per viewport ! only when quitting blender.
|
|
|
|
|
* Mostly used for freeing shaders */
|
|
|
|
|
static void EDIT_METABALL_engine_free(void)
|
|
|
|
|
{
|
2017-04-13 13:30:53 +10:00
|
|
|
// DRW_SHADER_FREE_SAFE(custom_shader);
|
2017-03-12 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create collection settings here.
|
|
|
|
|
*
|
|
|
|
|
* Be sure to add this function there :
|
|
|
|
|
* source/blender/draw/DRW_engine.h
|
|
|
|
|
* source/blender/blenkernel/intern/layer.c
|
|
|
|
|
* source/blenderplayer/bad_level_call_stubs/stubs.c
|
|
|
|
|
*
|
|
|
|
|
* And relevant collection settings to :
|
|
|
|
|
* source/blender/makesrna/intern/rna_scene.c
|
|
|
|
|
* source/blender/blenkernel/intern/layer.c
|
|
|
|
|
*/
|
2017-03-14 18:40:23 +11:00
|
|
|
#if 0
|
2017-03-12 21:16:03 +01:00
|
|
|
void EDIT_METABALL_collection_settings_create(CollectionEngineSettings *ces)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(ces);
|
|
|
|
|
// BKE_collection_engine_property_add_int(ces, "my_bool_prop", false);
|
|
|
|
|
// BKE_collection_engine_property_add_int(ces, "my_int_prop", 0);
|
|
|
|
|
// BKE_collection_engine_property_add_float(ces, "my_float_prop", 0.0f);
|
|
|
|
|
}
|
2017-03-14 18:40:23 +11:00
|
|
|
#endif
|
2017-03-12 21:16:03 +01:00
|
|
|
|
2017-04-12 19:49:19 +10:00
|
|
|
static const DrawEngineDataSize EDIT_METABALL_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_METABALL_Data);
|
|
|
|
|
|
2017-03-12 21:16:03 +01:00
|
|
|
DrawEngineType draw_engine_edit_metaball_type = {
|
|
|
|
|
NULL, NULL,
|
|
|
|
|
N_("EditMetaballMode"),
|
2017-04-12 19:49:19 +10:00
|
|
|
&EDIT_METABALL_data_size,
|
2017-11-16 15:12:32 -02:00
|
|
|
NULL,
|
2017-03-12 21:16:03 +01:00
|
|
|
&EDIT_METABALL_engine_free,
|
|
|
|
|
&EDIT_METABALL_cache_init,
|
|
|
|
|
&EDIT_METABALL_cache_populate,
|
2017-11-16 15:12:32 -02:00
|
|
|
NULL,
|
2017-03-12 21:16:03 +01:00
|
|
|
NULL, /* draw_background but not needed by mode engines */
|
2017-09-25 20:07:02 +02:00
|
|
|
&EDIT_METABALL_draw_scene,
|
|
|
|
|
NULL,
|
2017-03-12 21:16:03 +01:00
|
|
|
};
|