[Bf-blender-cvs] [f5f93bc576b] greasepencil-object: WIP: new method to calculate scale of the stroke thickness

Antonio Vazquez noreply at git.blender.org
Wed May 3 16:31:45 CEST 2017


Commit: f5f93bc576babca2c8ef303ce357042e6b1db7de
Author: Antonio Vazquez
Date:   Wed May 3 16:31:23 2017 +0200
Branches: greasepencil-object
https://developer.blender.org/rBf5f93bc576babca2c8ef303ce357042e6b1db7de

WIP: new method to calculate scale of the stroke thickness

The previous scale method was not valid because only was affected by viewport, but not by GP object or Stroke location. Now, the pixel size is calculated depending of zdepth and used to scale the stroke thickness.

Maybe there are other ways to do it, but this approach works, so we keep it before finding better methods

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

M	source/blender/draw/engines/gpencil/gpencil_draw.c
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
M	source/blender/draw/engines/gpencil/shaders/gpencil_stroke_geom.glsl

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

diff --git a/source/blender/draw/engines/gpencil/gpencil_draw.c b/source/blender/draw/engines/gpencil/gpencil_draw.c
index b318a20606f..d35f0ce9e33 100644
--- a/source/blender/draw/engines/gpencil/gpencil_draw.c
+++ b/source/blender/draw/engines/gpencil/gpencil_draw.c
@@ -48,22 +48,38 @@
 #include "UI_resources.h"
 
 /* set stroke point to vbo */
-static void gpencil_set_stroke_point(VertexBuffer *vbo, const bGPDspoint *pt, int idx,
+static void gpencil_set_stroke_point(RegionView3D *rv3d, VertexBuffer *vbo, float matrix[4][4], const bGPDspoint *pt, int idx,
 						    unsigned int pos_id, unsigned int color_id,
 							unsigned int thickness_id, short thickness,
 	                        const float ink[4], bool inverse)
 {
 	float fpt[3];
+	float viewfpt[3];
+	copy_v3_v3(fpt, &pt->x);
+	
+	copy_v3_v3(viewfpt, &pt->x);
+	mul_m4_v3(matrix, viewfpt);
+
+	const float defaultpixsize = rv3d->pixsize * U.pixelsize;
+	const float pixsize = ED_view3d_pixel_size(rv3d, viewfpt);
+	float scale_thickness;
+	if (rv3d->is_persp) {
+		scale_thickness = (defaultpixsize / pixsize); 
+		/* need a factor to mimmic old glLine size, 10.0f works fine */
+		scale_thickness *= 10.0f;
+	}
+	else {
+		scale_thickness = (1.0f / pixsize) / 100.0f;
+	}
 
 	float alpha = ink[3] * pt->strength;
 	CLAMP(alpha, GPENCIL_STRENGTH_MIN, 1.0f);
 	float col[4] = { ink[0], ink[1], ink[2], alpha };
 	VertexBuffer_set_attrib(vbo, color_id, idx, col);
 
-	float thick = max_ff(pt->pressure * thickness, 1.0f);
+	float thick = max_ff(pt->pressure * thickness * scale_thickness, 1.0f);
 	VertexBuffer_set_attrib(vbo, thickness_id, idx, &thick);
 	
-	copy_v3_v3(fpt, &pt->x);
 	if (inverse) {
 		mul_v3_fl(fpt, -1.0f);
 	}
@@ -98,8 +114,11 @@ Batch *gpencil_get_point_geom(bGPDspoint *pt, short thickness, const float ink[4
 }
 
 /* create batch geometry data for stroke shader */
-Batch *gpencil_get_stroke_geom(bGPDstroke *gps, short thickness, const float ink[4])
+Batch *gpencil_get_stroke_geom(bGPDframe *gpf, bGPDstroke *gps, short thickness, const float ink[4])
 {
+	const DRWContextState *draw_ctx = DRW_context_state_get();
+	RegionView3D *rv3d = draw_ctx->rv3d;
+
 	bGPDspoint *points = gps->points;
 	int totpoints = gps->totpoints;
 	/* if cyclic needs more vertex */
@@ -122,27 +141,27 @@ Batch *gpencil_get_stroke_geom(bGPDstroke *gps, short thickness, const float ink
 	for (int i = 0; i < totpoints; i++, pt++) {
 		/* first point for adjacency (not drawn) */
 		if (i == 0) {
-			gpencil_set_stroke_point(vbo, pt, idx, pos_id, color_id, thickness_id, thickness, ink, true);
+			gpencil_set_stroke_point(rv3d, vbo, gpf->matrix, pt, idx, pos_id, color_id, thickness_id, thickness, ink, true);
 			++idx;
 		}
 		/* set point */
-		gpencil_set_stroke_point(vbo, pt, idx, pos_id, color_id, thickness_id, thickness, ink, false);
+		gpencil_set_stroke_point(rv3d, vbo, gpf->matrix, pt, idx, pos_id, color_id, thickness_id, thickness, ink, false);
 		++idx;
 	}
 
 	if (gps->flag & GP_STROKE_CYCLIC && totpoints > 2) {
 		/* draw line to first point to complete the cycle */
-		gpencil_set_stroke_point(vbo, &points[0], idx, pos_id, color_id, thickness_id, thickness, ink, false);
+		gpencil_set_stroke_point(rv3d, vbo, gpf->matrix, &points[0], idx, pos_id, color_id, thickness_id, thickness, ink, false);
 		++idx;
 		/* now add adjacency points using 2nd & 3rd point to get smooth transition */
-		gpencil_set_stroke_point(vbo, &points[1], idx, pos_id, color_id, thickness_id, thickness, ink, false);
+		gpencil_set_stroke_point(rv3d, vbo, gpf->matrix, &points[1], idx, pos_id, color_id, thickness_id, thickness, ink, false);
 		++idx;
-		gpencil_set_stroke_point(vbo, &points[2], idx, pos_id, color_id, thickness_id, thickness, ink, false);
+		gpencil_set_stroke_point(rv3d, vbo, gpf->matrix, &points[2], idx, pos_id, color_id, thickness_id, thickness, ink, false);
 		++idx;
 	}
 	/* last adjacency point (not drawn) */
 	else {
-		gpencil_set_stroke_point(vbo, &points[totpoints - 1], idx, pos_id, color_id, thickness_id, thickness, ink, true);
+		gpencil_set_stroke_point(rv3d, vbo, gpf->matrix, &points[totpoints - 1], idx, pos_id, color_id, thickness_id, thickness, ink, true);
 	}
 
 	return Batch_create(PRIM_LINE_STRIP_ADJACENCY, vbo, NULL);
@@ -227,12 +246,13 @@ Batch *gpencil_get_buffer_point_geom(bGPdata *gpd, short thickness)
 }
 
 /* create batch geometry data for current buffer stroke shader */
-Batch *gpencil_get_buffer_stroke_geom(bGPdata *gpd, short thickness)
+Batch *gpencil_get_buffer_stroke_geom(bGPdata *gpd, float matrix[4][4], short thickness)
 {
 	const DRWContextState *draw_ctx = DRW_context_state_get();
 	Scene *scene = draw_ctx->scene;
 	View3D *v3d = draw_ctx->v3d;
 	ARegion *ar = draw_ctx->ar;
+	RegionView3D *rv3d = draw_ctx->rv3d;
 
 	tGPspoint *points = gpd->sbuffer;
 	int totpoints = gpd->sbuffer_size;
@@ -258,16 +278,16 @@ Batch *gpencil_get_buffer_stroke_geom(bGPdata *gpd, short thickness)
 
 		/* first point for adjacency (not drawn) */
 		if (i == 0) {
-			gpencil_set_stroke_point(vbo, &pt, idx, pos_id, color_id, thickness_id, thickness, gpd->scolor, true);
+			gpencil_set_stroke_point(rv3d, vbo, matrix, &pt, idx, pos_id, color_id, thickness_id, thickness, gpd->scolor, true);
 			++idx;
 		}
 		/* set point */
-		gpencil_set_stroke_point(vbo, &pt, idx, pos_id, color_id, thickness_id, thickness, gpd->scolor, false);
+		gpencil_set_stroke_point(rv3d, vbo, matrix, &pt, idx, pos_id, color_id, thickness_id, thickness, gpd->scolor, false);
 		++idx;
 	}
 
 	/* last adjacency point (not drawn) */
-	gpencil_set_stroke_point(vbo, &pt, idx, pos_id, color_id, thickness_id, thickness, gpd->scolor, true);
+	gpencil_set_stroke_point(rv3d, vbo, matrix, &pt, idx, pos_id, color_id, thickness_id, thickness, gpd->scolor, true);
 
 	return Batch_create(PRIM_LINE_STRIP_ADJACENCY, vbo, NULL);
 }
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index 317ae8b50f6..19a87f01d58 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -29,11 +29,8 @@
 #include "BKE_gpencil.h"
 #include "BKE_image.h"
 #include "ED_gpencil.h"
-#include "ED_view3d.h"
 
 #include "DNA_gpencil_types.h"
-#include "DNA_screen_types.h"
-#include "DNA_view3d_types.h"
 
  /* If builtin shaders are needed */
 #include "GPU_shader.h"
@@ -111,7 +108,6 @@ static struct {
 	struct GPUShader *gpencil_stroke_sh;
 	struct GPUShader *gpencil_volumetric_sh;
 	struct GPUShader *gpencil_drawing_fill_sh;
-	float scale;
 } e_data = {NULL}; /* Engine data */
 
 /* *********** FUNCTIONS *********** */
@@ -227,58 +223,14 @@ static DRWShadingGroup *GPENCIL_shgroup_point_volumetric_create(GPENCIL_Data *ve
 	return grp;
 }
 
-/* calculate scale of viewport */
-static float get_view_scale(ARegion *ar, View3D *v3d)
-{
-	RegionView3D *rv3d = ar->regiondata;
-
-	double fx = rv3d->persmat[3][0];
-	double fy = rv3d->persmat[3][1];
-	double fw = rv3d->persmat[3][3];
-
-	const double wx = 0.5 * ar->winx;  /* use double precision to avoid rounding errors */
-	const double wy = 0.5 * ar->winy;
-
-	double x = wx * fx / fw;
-	double y = wy * fy / fw;
-
-	double vec4[4] = { 1.0, 1.0, 0.0, 1.0 };
-	mul_m4_v4d(rv3d->persmat, vec4);
-	fx = vec4[0];
-	fy = vec4[1];
-	fw = vec4[3];
-
-	double dx = fabs(x - wx * fx / fw);
-	if (dx == 0) dx = fabs(y - wy * fy / fw);
-
-	x += wx;
-	y += wy;
-
-	/* apply a factor to get more thick lines */
-	dx = dx / 100.0f;
-
-	if (dx < 0.0001f) {
-		dx = 0.0001f;
-	}
-
-	return dx;
-}
-
 /* create shading group for strokes */
 static DRWShadingGroup *GPENCIL_shgroup_stroke_create(GPENCIL_Data *vedata, DRWPass *pass, PaletteColor *palcolor)
 {
 	GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
-	const DRWContextState *draw_ctx = DRW_context_state_get();
-	ARegion *ar = draw_ctx->ar;
-	View3D *v3d = draw_ctx->v3d;
 	const float *viewport_size = DRW_viewport_size_get();
 
-	/* TODO: need a better way to detect the scale factor */
-	e_data.scale = get_view_scale(ar, v3d);
-
 	DRWShadingGroup *grp = DRW_shgroup_create(e_data.gpencil_stroke_sh, pass);
 	DRW_shgroup_uniform_vec2(grp, "Viewport", viewport_size, 1);
-	DRW_shgroup_uniform_float(grp, "scale", &e_data.scale, 1);
 
 	return grp;
 }
@@ -460,7 +412,7 @@ static void gpencil_draw_strokes(void *vedata, ToolSettings *ts, Object *ob,
 		short sthickness = gps->thickness + gpl->thickness;
 		if (sthickness > 0) {
 			if (gps->totpoints > 1) {
-				struct Batch *stroke_geom = gpencil_get_stroke_geom(gps, sthickness, ink);
+				struct Batch *stroke_geom = gpencil_get_stroke_geom(gpf, gps, sthickness, ink);
 				DRW_shgroup_call_add(strokegrp, stroke_geom, gpf->matrix);
 			}
 			else if (gps->totpoints == 1) {
@@ -521,7 +473,7 @@ static void gpencil_draw_buffer_strokes(void *vedata, ToolSettings *ts, bGPdata
 			}
 			else {
 				/* use unit matrix because the buffer is in screen space and does not need conversion */
-				struct Batch *drawing_stroke_geom = gpencil_get_buffer_stroke_geom(gpd, lthick);
+				struct Batch *drawing_stroke_geom = gpencil_get_buffer_stroke_geom(gpd, matrix, lthick);
 				DRW_shgroup_call_add(stl->g_data->shgrps_drawing_stroke, drawing_stroke_geom, matrix);
 
 				if ((gpd->sbuffer_size >= 3) && ((gpd->sfill[3] > GPENCIL_ALPHA_OPACITY_THRESH) || (gpd->bfill_style > 0))) {
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index 2320d98fc0d..a9f3ce88a3a 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -29,10 +29,10 @@
 struct Batch;
 
 struct Batch *gpencil_get_point_geom(struct bGPDspoint *pt, short thickness, const float ink[4]);
-struct Batch *gpencil_get_stroke_geom(struct bGPDstroke *gps, short thickness, const float ink[4]);
+struct Batch *gpencil_get_stroke_geom(struct bGPDframe *gpf, struct bGPDstroke *gps, short thickness, const float ink[4]);
 struct Batch *gpencil_get_fill_geom(struct bGPDstroke *gps, const float color[4]);
 struct Batch *gpencil_get_edit_geom(struct bGPDstroke *gps, float alpha, short dflag);
-struct Batch *gpencil_get_buffer_stroke_geom(struct bGPdata *gpd, sh

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list