[Bf-blender-cvs] [1af42b70d13] greasepencil-object: Minimize Alpha stroke issue

Antonio Vazquez noreply at git.blender.org
Fri Jun 30 14:42:19 CEST 2017


Commit: 1af42b70d13dc3d19d75609f96606cc768effe39
Author: Antonio Vazquez
Date:   Fri Jun 30 14:41:59 2017 +0200
Branches: greasepencil-object
https://developer.blender.org/rB1af42b70d13dc3d19d75609f96606cc768effe39

Minimize Alpha stroke issue

Here we need is OIT (Order Independant Transparency) which is still an open issue in the realtime field (and also for Eevee). The best OIT method I know of is somewhat complex and not very precise (still not 1:1 with the ordered version). It involves more framebuffers and performance cost.

I may need to implement that in the draw manager in the future so that all engines can use it.

Thanks to Clement Foucault for the support provided.

===================================================================

M	source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h

===================================================================

diff --git a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
index 31a39a96a32..c79bbc7418d 100644
--- a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
+++ b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
@@ -72,11 +72,24 @@ tGPencilObjectCache *gpencil_object_cache_allocate(tGPencilObjectCache *cache, i
 /* add a gpencil object to cache to defer drawing */
 void gpencil_object_cache_add(tGPencilObjectCache *cache, Object *ob, int *gp_cache_used)
 {
+	const DRWContextState *draw_ctx = DRW_context_state_get();
+	RegionView3D *rv3d = draw_ctx->rv3d;
+
 	/* save object */
 	cache[*gp_cache_used].ob = ob;
 	cache[*gp_cache_used].init_grp = 0;
 	cache[*gp_cache_used].end_grp = -1;
 
+	/* calculate zdepth from point of view */
+	float zdepth = 0.0;
+	if (rv3d->is_persp) {
+		zdepth = ED_view3d_calc_zfac(rv3d, ob->loc, NULL);
+	}
+	else {
+		zdepth = -dot_v3v3(rv3d->viewinv[2], ob->loc);
+	}
+	cache[*gp_cache_used].zdepth = zdepth;
+
 	/* increase slots used in cache */
 	++*gp_cache_used;
 }
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index 874ec91be78..1e3a8df2379 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -235,6 +235,17 @@ static void GPENCIL_cache_finish(void *vedata)
 	MEM_SAFE_FREE(stl->g_data->gpd_in_cache);
 }
 
+/* helper function to sort inverse gpencil objects using qsort */
+static int gpencil_object_cache_compare_zdepth(const void *a1, const void *a2)
+{
+	const tGPencilObjectCache *ps1 = a1, *ps2 = a2;
+
+	if (ps1->zdepth < ps2->zdepth) return 1;
+	else if (ps1->zdepth > ps2->zdepth) return -1;
+
+	return 0;
+}
+
 static void GPENCIL_draw_scene(void *vedata)
 {
 	GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
@@ -247,7 +258,11 @@ static void GPENCIL_draw_scene(void *vedata)
 
 	/* Draw all pending objects */
 	if (stl->g_data->gp_cache_used > 0) {
-		
+
+		/* sort by zdepth */
+		qsort(stl->g_data->gp_object_cache, stl->g_data->gp_cache_used, 
+			sizeof(tGPencilObjectCache), gpencil_object_cache_compare_zdepth);
+
 		/* attach temp textures */
 		DRW_framebuffer_texture_attach(fbl->temp_color_fb, e_data.temp_fbcolor_depth_tx, 0, 0);
 		DRW_framebuffer_texture_attach(fbl->temp_color_fb, e_data.temp_fbcolor_color_tx, 0, 0);
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index ce92893d9cf..26f281e4f37 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -40,6 +40,7 @@ struct tGPspoint;
 typedef struct tGPencilObjectCache {
 	struct Object *ob;
 	int init_grp, end_grp;
+	float zdepth;
 } tGPencilObjectCache;
 
   /* *********** LISTS *********** */




More information about the Bf-blender-cvs mailing list