DRW: Support edit-metaball clipping
authorCampbell Barton <ideasman42@gmail.com>
Wed, 6 Feb 2019 13:07:30 +0000 (00:07 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 6 Feb 2019 13:07:30 +0000 (00:07 +1100)
source/blender/draw/intern/draw_common.c
source/blender/draw/intern/draw_common.h
source/blender/draw/modes/edit_metaball_mode.c
source/blender/draw/modes/object_mode.c
source/blender/draw/modes/shaders/object_mball_handles_vert.glsl

index 01e3264..73371d5 100644 (file)
@@ -696,13 +696,18 @@ DRWShadingGroup *shgroup_instance_bone_envelope_solid(DRWPass *pass, bool transp
        return grp;
 }
 
-DRWShadingGroup *shgroup_instance_mball_handles(DRWPass *pass)
+DRWShadingGroup *shgroup_instance_mball_handles(DRWPass *pass, eGPUShaderConfig shader_cfg)
 {
-       COMMON_Shaders *sh_data = &g_shaders[GPU_SHADER_CFG_DEFAULT];
+       COMMON_Shaders *sh_data = &g_shaders[shader_cfg];
        if (sh_data->mball_handles == NULL) {
-               sh_data->mball_handles = DRW_shader_create(
-                       datatoc_object_mball_handles_vert_glsl, NULL,
-                       datatoc_gpu_shader_flat_color_frag_glsl, NULL);
+               bool is_clip = (shader_cfg == GPU_SHADER_CFG_CLIPPED);
+               const char *world_clip_lib_or_empty = is_clip ? datatoc_gpu_shader_cfg_world_clip_lib_glsl : "";
+               const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
+               sh_data->mball_handles = GPU_shader_create_from_arrays({
+                       .vert = (const char *[]){world_clip_lib_or_empty, datatoc_object_mball_handles_vert_glsl, NULL},
+                       .frag = (const char *[]){datatoc_gpu_shader_flat_color_frag_glsl, NULL},
+                       .defs = (const char *[]){world_clip_def_or_empty, NULL},
+               });
        }
 
        DRW_shgroup_instance_format(g_formats.instance_mball_handles, {
@@ -716,7 +721,9 @@ DRWShadingGroup *shgroup_instance_mball_handles(DRWPass *pass)
                DRW_cache_screenspace_circle_get(),
                g_formats.instance_mball_handles);
        DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
-
+       if (shader_cfg == GPU_SHADER_CFG_CLIPPED) {
+               DRW_shgroup_world_clip_planes_from_rv3d(grp, DRW_context_state_get()->rv3d);
+       }
        return grp;
 }
 
index 3a5237d..af24080 100644 (file)
@@ -140,7 +140,7 @@ struct DRWShadingGroup *shgroup_instance_outline(struct DRWPass *pass, struct GP
 struct DRWShadingGroup *shgroup_camera_instance(struct DRWPass *pass, struct GPUBatch *geom, eGPUShaderConfig shader_cfg);
 struct DRWShadingGroup *shgroup_distance_lines_instance(struct DRWPass *pass, struct GPUBatch *geom, eGPUShaderConfig shader_cfg);
 struct DRWShadingGroup *shgroup_spot_instance(struct DRWPass *pass, struct GPUBatch *geom, eGPUShaderConfig shader_cfg);
-struct DRWShadingGroup *shgroup_instance_mball_handles(struct DRWPass *pass);
+struct DRWShadingGroup *shgroup_instance_mball_handles(struct DRWPass *pass, eGPUShaderConfig shader_cfg);
 struct DRWShadingGroup *shgroup_instance_bone_axes(struct DRWPass *pass);
 struct DRWShadingGroup *shgroup_instance_bone_envelope_distance(struct DRWPass *pass);
 struct DRWShadingGroup *shgroup_instance_bone_envelope_outline(struct DRWPass *pass);
index d6f5307..a61745b 100644 (file)
@@ -90,12 +90,23 @@ typedef struct EDIT_METABALL_PrivateData {
 
 /* *********** FUNCTIONS *********** */
 
+static void EDIT_METABALL_engine_init(void *UNUSED(vedata))
+{
+       const DRWContextState *draw_ctx = DRW_context_state_get();
+       const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
+       if (is_clip) {
+               DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
+       }
+}
+
 /* Here init all passes and shading groups
  * Assume that all Passes are NULL */
 static void EDIT_METABALL_cache_init(void *vedata)
 {
        EDIT_METABALL_PassList *psl = ((EDIT_METABALL_Data *)vedata)->psl;
        EDIT_METABALL_StorageList *stl = ((EDIT_METABALL_Data *)vedata)->stl;
+       const DRWContextState *draw_ctx = DRW_context_state_get();
+
 
        if (!stl->g_data) {
                /* Alloc transient pointers */
@@ -110,7 +121,7 @@ static void EDIT_METABALL_cache_init(void *vedata)
                psl->pass = DRW_pass_create("My Pass", state);
 
                /* Create a shadingGroup using a function in draw_common.c or custom one */
-               stl->g_data->group = shgroup_instance_mball_handles(psl->pass);
+               stl->g_data->group = shgroup_instance_mball_handles(psl->pass, draw_ctx->shader_cfg);
        }
 }
 
@@ -192,6 +203,8 @@ static void EDIT_METABALL_draw_scene(void *vedata)
 
        /* If you changed framebuffer, double check you rebind
         * the default one with its textures attached before finishing */
+
+       DRW_state_clip_planes_reset();
 }
 
 /* Cleanup when destroying the engine.
@@ -208,7 +221,7 @@ DrawEngineType draw_engine_edit_metaball_type = {
        NULL, NULL,
        N_("EditMetaballMode"),
        &EDIT_METABALL_data_size,
-       NULL,
+       &EDIT_METABALL_engine_init,
        &EDIT_METABALL_engine_free,
        &EDIT_METABALL_cache_init,
        &EDIT_METABALL_cache_populate,
index 561998b..7b5b83c 100644 (file)
@@ -1298,7 +1298,7 @@ static void OBJECT_cache_init(void *vedata)
                DRW_shgroup_state_disable(sgl->points_dupli_select, DRW_STATE_BLEND);
 
                /* Metaballs Handles */
-               sgl->mball_handle = shgroup_instance_mball_handles(sgl->non_meshes);
+               sgl->mball_handle = shgroup_instance_mball_handles(sgl->non_meshes, draw_ctx->shader_cfg);
 
                /* Lamps */
                /* TODO
index 294a95d..a2bd08b 100644 (file)
@@ -5,6 +5,7 @@
 
 
 uniform mat4 ViewProjectionMatrix;
+uniform mat4 ModelMatrix;
 uniform vec3 screen_vecs[2];
 
 /* ---- Instantiated Attrs ---- */
@@ -31,4 +32,8 @@ void main()
 
        gl_Position = ViewProjectionMatrix * world_pos;
        finalColor = vec4(color, 1.0);
+
+#ifdef USE_WORLD_CLIP_PLANES
+       world_clip_planes_calc_clip_distance((ModelMatrix * world_pos).xyz);
+#endif
 }