[Bf-blender-cvs] [938a26be8fe] greasepencil-refactor: Gpencil: Refactor: Support per layer random color in wireframe overlay
Clément Foucault
noreply at git.blender.org
Thu Jan 9 22:10:11 CET 2020
Commit: 938a26be8fed899cbd256399710708887c5d5127
Author: Clément Foucault
Date: Thu Jan 9 22:09:43 2020 +0100
Branches: greasepencil-refactor
https://developer.blender.org/rB938a26be8fed899cbd256399710708887c5d5127
Gpencil: Refactor: Support per layer random color in wireframe overlay
===================================================================
M source/blender/draw/engines/gpencil/gpencil_engine.c
M source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl
M source/blender/draw/intern/draw_cache_impl_gpencil.c
===================================================================
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index 438b84b08b0..4c2a2cb2167 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -72,6 +72,7 @@ void GPENCIL_engine_init(void *ved)
GPENCIL_FramebufferList *fbl = vedata->fbl;
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
+ const DRWContextState *ctx = DRW_context_state_get();
if (!stl->pd) {
stl->pd = MEM_callocN(sizeof(GPENCIL_PrivateData), "GPENCIL_PrivateData");
@@ -102,7 +103,8 @@ void GPENCIL_engine_init(void *ved)
stl->pd->tobjects.last = NULL;
stl->pd->sbuffer_tobjects.first = NULL;
stl->pd->sbuffer_tobjects.last = NULL;
- stl->pd->draw_depth_only = !DRW_state_is_fbo();
+ stl->pd->draw_depth_only = !DRW_state_is_fbo() ||
+ (ctx->v3d && ctx->v3d->shading.type == OB_WIRE);
stl->pd->scene_depth_tx = stl->pd->draw_depth_only ? txl->dummy_texture : dtxl->depth;
stl->pd->scene_fb = dfbl->default_fb;
stl->pd->is_render = true; /* TODO */
@@ -119,7 +121,6 @@ void GPENCIL_engine_init(void *ved)
gpencil_light_ambient_add(stl->pd->shadeless_light_pool, (float[3]){1.0f, 1.0f, 1.0f});
- const DRWContextState *ctx = DRW_context_state_get();
World *world = ctx->scene->world;
if (world != NULL) {
gpencil_light_ambient_add(stl->pd->global_light_pool, &world->horr);
@@ -689,6 +690,11 @@ static void GPENCIL_draw_scene_depth_only(void *ved)
{
GPENCIL_Data *vedata = (GPENCIL_Data *)ved;
GPENCIL_PrivateData *pd = vedata->stl->pd;
+ DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
+
+ if (DRW_state_is_fbo()) {
+ GPU_framebuffer_bind(dfbl->depth_only_fb);
+ }
for (GPENCIL_tObject *ob = pd->tobjects.first; ob; ob = ob->next) {
for (GPENCIL_tLayer *layer = ob->layers.first; layer; layer = layer->next) {
@@ -696,7 +702,16 @@ static void GPENCIL_draw_scene_depth_only(void *ved)
}
}
+ if (DRW_state_is_fbo()) {
+ GPU_framebuffer_bind(dfbl->default_fb);
+ }
+
pd->gp_object_pool = pd->gp_layer_pool = pd->gp_vfx_pool = NULL;
+
+ /* Free temp stroke buffers. */
+ if (pd->sbuffer_gpd) {
+ DRW_cache_gpencil_sbuffer_clear(pd->obact);
+ }
}
static void GPENCIL_draw_object(GPENCIL_Data *vedata, GPENCIL_tObject *ob)
diff --git a/source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl b/source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl
index 9a6476ceae9..422d3cd1cf7 100644
--- a/source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl
+++ b/source/blender/draw/engines/overlay/shaders/wireframe_vert.glsl
@@ -9,6 +9,8 @@ in vec3 pos;
in vec3 nor;
in float wd; /* wiredata */
+in float layer_rand; /* gpencil only */
+
flat out vec2 edgeStart;
#ifndef SELECT_EDGES
@@ -81,7 +83,7 @@ void wire_object_color_get(out vec3 rim_col, out vec3 wire_col)
rim_col = wire_col = ObjectColor.rgb * 0.5;
}
else {
- float hue = ObjectInfo.z;
+ float hue = fract(ObjectInfo.z + layer_rand);
vec3 hsv = vec3(hue, 0.75, 0.8);
rim_col = wire_col = hsv_to_rgb(hsv);
}
diff --git a/source/blender/draw/intern/draw_cache_impl_gpencil.c b/source/blender/draw/intern/draw_cache_impl_gpencil.c
index 4a28c3e9413..d50af0dde90 100644
--- a/source/blender/draw/intern/draw_cache_impl_gpencil.c
+++ b/source/blender/draw/intern/draw_cache_impl_gpencil.c
@@ -21,8 +21,6 @@
* \ingroup draw
*/
-#include "BLI_polyfill_2d.h"
-
#include "DNA_gpencil_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_screen_types.h"
@@ -38,6 +36,9 @@
#include "DEG_depsgraph_query.h"
+#include "BLI_polyfill_2d.h"
+#include "BLI_hash.h"
+
#include "draw_cache.h"
#include "draw_cache_impl.h"
@@ -207,7 +208,7 @@ static GPUVertFormat *gpencil_stroke_format(void)
/* MUST match the format below. */
typedef struct gpEditVert {
- int vflag;
+ uint vflag;
float weight;
} gpEditVert;
@@ -221,6 +222,20 @@ static GPUVertFormat *gpencil_edit_stroke_format(void)
return &format;
}
+/* MUST match the format below. */
+typedef struct gpExtraDataVert {
+ float layer_rand;
+} gpExtraDataVert;
+
+static GPUVertFormat *gpencil_extra_data_format(void)
+{
+ static GPUVertFormat format = {0};
+ if (format.attr_len == 0) {
+ GPU_vertformat_attr_add(&format, "layer_rand", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
+ }
+ return &format;
+}
+
/** \} */
/* -------------------------------------------------------------------- */
@@ -229,10 +244,15 @@ static GPUVertFormat *gpencil_edit_stroke_format(void)
typedef struct gpIterData {
bGPdata *gpd;
- gpStrokeVert *verts;
+ union {
+ gpStrokeVert *verts;
+ gpExtraDataVert *verts_extra;
+ };
GPUIndexBufBuilder ibo;
int vert_len;
int tri_len;
+ int layer_id;
+ float layer_rand;
} gpIterData;
static GPUVertBuf *gpencil_dummy_buffer_get(void)
@@ -393,20 +413,31 @@ GPUBatch *DRW_cache_gpencil_fills_get(Object *ob, int cfra)
return cache->fill_batch;
}
+static void gp_layer_index_cb(bGPDlayer *gpl,
+ bGPDframe *UNUSED(gpf),
+ bGPDstroke *UNUSED(gps),
+ void *thunk)
+{
+ gpIterData *iter = (gpIterData *)thunk;
+ iter->layer_id++;
+ iter->layer_rand = BLI_hash_int_01(BLI_hash_string(gpl->info));
+}
+
static void gp_lines_indices_cb(bGPDlayer *UNUSED(gpl),
bGPDframe *UNUSED(gpf),
bGPDstroke *gps,
void *thunk)
{
- GPUIndexBufBuilder *builder = (GPUIndexBufBuilder *)thunk;
+ gpIterData *iter = (gpIterData *)thunk;
int pts_len = gps->totpoints + gpencil_stroke_is_cyclic(gps);
int start = gps->runtime.stroke_start + 1;
int end = start + pts_len;
for (int i = start; i < end; i++) {
- GPU_indexbuf_add_generic_vert(builder, i);
+ GPU_indexbuf_add_generic_vert(&iter->ibo, i);
+ iter->verts_extra[i].layer_rand = iter->layer_rand;
}
- GPU_indexbuf_add_primitive_restart(builder);
+ GPU_indexbuf_add_primitive_restart(&iter->ibo);
}
GPUBatch *DRW_cache_gpencil_face_wireframe_get(Object *ob)
@@ -419,16 +450,31 @@ GPUBatch *DRW_cache_gpencil_face_wireframe_get(Object *ob)
if (cache->lines_batch == NULL) {
GPUVertBuf *vbo = cache->vbo;
- GPUIndexBufBuilder ibo_builder;
- GPU_indexbuf_init_ex(&ibo_builder, GPU_PRIM_LINE_STRIP, vbo->vertex_len, vbo->vertex_len);
+
+ gpIterData iter = {
+ .gpd = ob->data,
+ .verts = NULL,
+ .ibo = {0},
+ .layer_id = 0,
+ };
+
+ /* Create VBO containing layer index. */
+ GPUVertFormat *format = gpencil_extra_data_format();
+ GPUVertBuf *vbo_layer = GPU_vertbuf_create_with_format(format);
+ GPU_vertbuf_data_alloc(vbo_layer, vbo->vertex_len);
+ iter.verts_extra = (gpExtraDataVert *)vbo_layer->data;
+
+ GPU_indexbuf_init_ex(&iter.ibo, GPU_PRIM_LINE_STRIP, vbo->vertex_len, vbo->vertex_len);
/* IMPORTANT: Keep in sync with gpencil_edit_batches_ensure() */
bool do_onion = true;
- BKE_gpencil_visible_stroke_iter(ob, NULL, gp_lines_indices_cb, &ibo_builder, do_onion, cfra);
+ BKE_gpencil_visible_stroke_iter(
+ ob, gp_layer_index_cb, gp_lines_indices_cb, &iter, do_onion, cfra);
- GPUIndexBuf *ibo = GPU_indexbuf_build(&ibo_builder);
+ GPUIndexBuf *ibo = GPU_indexbuf_build(&iter.ibo);
cache->lines_batch = GPU_batch_create_ex(GPU_PRIM_LINE_STRIP, vbo, ibo, GPU_BATCH_OWNS_INDEX);
+ GPU_batch_vertbuf_add_ex(cache->lines_batch, vbo_layer, true);
}
return cache->lines_batch;
}
More information about the Bf-blender-cvs
mailing list