[Bf-blender-cvs] [f2ef00f] GPU_data_request: improve grid floor drawing function

Mike Erwin noreply at git.blender.org
Fri Apr 10 03:33:56 CEST 2015


Commit: f2ef00f80e5a8bcc543e2488684a5bfb2c691b56
Author: Mike Erwin
Date:   Thu Apr 9 21:33:07 2015 -0400
Branches: GPU_data_request
https://developer.blender.org/rBf2ef00f80e5a8bcc543e2488684a5bfb2c691b56

improve grid floor drawing function

Maintain visual hierarchy of the grid:
- emphasized lines draw atop normal lines
- axes draw atop all lines (same as before)

Draw axes only once, not twice.

Return early if nothing to draw.

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

M	source/blender/editors/space_view3d/view3d_draw.c

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

diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c
index 6589ffd..2da5e11 100644
--- a/source/blender/editors/space_view3d/view3d_draw.c
+++ b/source/blender/editors/space_view3d/view3d_draw.c
@@ -552,75 +552,129 @@ static void drawfloor(Scene *scene, View3D *v3d, const char **grid_unit)
 
 static void drawfloor_new(Scene *scene, View3D *v3d, const char **grid_unit)
 {
-	unsigned char col_grid[3];
-	const int gridlines = v3d->gridlines / 2;
-	const float grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
-	const float grid = gridlines * grid_scale;
+	/* draw only if there is something to draw */
+	if (v3d->gridflag & (V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_Z)) {
+		/* draw how many lines?
+		 * trunc(v3d->gridlines / 2) * 4
+		 * + 2 for xy axes (possibly with special colors)
+		 * + 1 for z axis (the only line not in xy plane)
+		 * even v3d->gridlines are honored, odd rounded down */
+		const int gridlines = v3d->gridlines / 2;
+		const float grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
+		const float grid = gridlines * grid_scale;
 
-	UI_GetThemeColor3ubv(TH_GRID, col_grid);
+		const bool show_floor = (v3d->gridflag & V3D_SHOW_FLOOR) && gridlines >= 1;
 
-	/* draw the Y axis and/or grid lines */
-	if ((v3d->gridflag & V3D_SHOW_FLOOR) && (v3d->gridlines >= 3)) {
-		const int sublines = v3d->gridsubdiv;
-		float vert[4][2] = {{0.0f}};
-		unsigned char col_bg[3];
-		unsigned char col_grid_emphasise[3], col_grid_light[3];
-		int a;
-		int prev_emphasise = -1;
+		unsigned char col_grid[3], col_axis[3];
 
-		UI_GetThemeColor3ubv(TH_BACK, col_bg);
+		UI_GetThemeColor3ubv(TH_GRID, col_grid);
 
-		/* emphasise division lines lighter instead of darker, if background is darker than grid */
-		UI_GetColorPtrShade3ubv(col_grid, col_grid_light, 10);
-		UI_GetColorPtrShade3ubv(col_grid, col_grid_emphasise,
-		                        (((col_grid[0] + col_grid[1] + col_grid[2]) + 30) >
-		                         (col_bg[0] + col_bg[1] + col_bg[2])) ? 20 : -10);
+		if (show_floor) {
+			const int sublines = v3d->gridsubdiv;
+			int a;
 
-		/* set fixed axis */
-		vert[0][0] = vert[2][1] = grid;
-		vert[1][0] = vert[3][1] = -grid;
+			unsigned char col_bg[3], col_grid_emphasise[3], col_grid_light[3];
 
-		glEnableClientState(GL_VERTEX_ARRAY);
-		glVertexPointer(2, GL_FLOAT, 0, vert);
+			glDepthFunc(GL_ALWAYS); /* draw lines in order given */
+			glBegin(GL_LINES);
 
-		for (a = -gridlines; a <= gridlines; a++) {
-			const float line = a * grid_scale;
-			const int is_emphasise = (a % sublines) == 0;
+			/* draw normal grid lines */
+			UI_GetColorPtrShade3ubv(col_grid, col_grid_light, 10);
+			glColor3ubv(col_grid_light);
 
-			if (is_emphasise != prev_emphasise) {
-				glColor3ubv(is_emphasise ? col_grid_emphasise : col_grid_light);
-				prev_emphasise = is_emphasise;
+			for (a = 1; a <= gridlines; a++) {
+				/* skip emphasised divider lines */
+				if (a % sublines != 0) {
+					const float line = a * grid_scale;
+
+					glVertex2f(-grid, -line);
+					glVertex2f(+grid, -line);
+					glVertex2f(-grid, +line);
+					glVertex2f(+grid, +line);
+
+					glVertex2f(-line, -grid);
+					glVertex2f(-line, +grid);
+					glVertex2f(+line, -grid);
+					glVertex2f(+line, +grid);
+				}
 			}
 
-			/* set variable axis */
-			vert[0][1] = vert[1][1] = vert[2][0] = vert[3][0] = line;
+			/* draw emphasised grid lines */
+			UI_GetThemeColor3ubv(TH_BACK, col_bg);
+			/* emphasise division lines lighter instead of darker, if background is darker than grid */
+			UI_GetColorPtrShade3ubv(col_grid, col_grid_emphasise,
+			                        (col_grid[0] + col_grid[1] + col_grid[2] + 30 >
+			                         col_bg[0] + col_bg[1] + col_bg[2]) ? 20 : -10);
+			glColor3ubv(col_grid_emphasise);
+
+			for (a = sublines; a <= gridlines; a += sublines) {
+				const float line = a * grid_scale;
+
+				glVertex2f(-grid, -line);
+				glVertex2f(+grid, -line);
+				glVertex2f(-grid, +line);
+				glVertex2f(+grid, +line);
+
+				glVertex2f(-line, -grid);
+				glVertex2f(-line, +grid);
+				glVertex2f(+line, -grid);
+				glVertex2f(+line, +grid);
+			}
 
-			glDrawArrays(GL_LINES, 0, 4);
-		}
+			/* draw X axis */
+			if (v3d->gridflag & V3D_SHOW_X) {
+				UI_make_axis_color(col_grid, col_axis, 'X');
+				glColor3ubv(col_axis);
+			}
+			else
+				glColor3ubv(col_grid_emphasise);
+			glVertex2f(-grid, 0.0f);
+			glVertex2f(+grid, 0.0f);
+
+			/* draw Y axis */
+			if (v3d->gridflag & V3D_SHOW_Y) {
+				UI_make_axis_color(col_grid, col_axis, 'Y');
+				glColor3ubv(col_axis);
+			}
+			else
+				glColor3ubv(col_grid_emphasise);
+			glVertex2f(0.0f, -grid);
+			glVertex2f(0.0f, +grid);
 
-		glDisableClientState(GL_VERTEX_ARRAY);
-	}
+			glEnd(); /* done with XY plane */
 
-	/* draw the axis lines */
-	if (v3d->gridflag & (V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_Z)) {
-		int axis;
-		glBegin(GL_LINES);
-		for (axis = 0; axis < 3; axis++) {
-			if (v3d->gridflag & (V3D_SHOW_X << axis)) {
-				float vert[3];
-				unsigned char tcol[3];
+			glDepthFunc(GL_LESS); /* restore default */
 
-				UI_make_axis_color(col_grid, tcol, 'X' + axis);
-				glColor3ubv(tcol);
+			/* maybe draw Z axis */
+			if (v3d->gridflag & V3D_SHOW_Z) {
+				UI_make_axis_color(col_grid, col_axis, 'Z');
+				glColor3ubv(col_axis);
 
-				zero_v3(vert);
-				vert[axis] = grid;
-				glVertex3fv(vert);
-				vert[axis] = -grid;
-				glVertex3fv(vert);
+				glBegin(GL_LINES);
+				glVertex3f(0.0f, 0.0f, -grid);
+				glVertex3f(0.0f, 0.0f, +grid);
+				glEnd();
 			}
 		}
-		glEnd();
+		else {
+			/* draw just the axis lines */
+			int axis;
+			glBegin(GL_LINES);
+			for (axis = 0; axis < 3; axis++) {
+				if (v3d->gridflag & (V3D_SHOW_X << axis)) {
+					float vert[3] = {0.0f};
+
+					UI_make_axis_color(col_grid, col_axis, 'X' + axis);
+					glColor3ubv(col_axis);
+
+					vert[axis] = grid;
+					glVertex3fv(vert);
+					vert[axis] = -grid;
+					glVertex3fv(vert);
+				}
+			}
+			glEnd();
+		}
 	}
 }




More information about the Bf-blender-cvs mailing list