[Bf-blender-cvs] [583373f049] blender2.8: OpenGL: Object bound drawing.

Clément Foucault noreply at git.blender.org
Mon Mar 6 20:52:13 CET 2017


Commit: 583373f04976e919db11c4a119d74ea8bca14a0b
Author: Clément Foucault
Date:   Mon Mar 6 20:55:58 2017 +0100
Branches: blender2.8
https://developer.blender.org/rB583373f04976e919db11c4a119d74ea8bca14a0b

OpenGL: Object bound drawing.

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

M	source/blender/editors/include/BIF_glutil.h
M	source/blender/editors/screen/glutil.c
M	source/blender/editors/space_view3d/drawarmature.c
M	source/blender/editors/space_view3d/drawobject.c
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/editors/space_view3d/view3d_intern.h
M	source/blender/gpu/GPU_batch.h
M	source/blender/gpu/intern/gpu_batch.c

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

diff --git a/source/blender/editors/include/BIF_glutil.h b/source/blender/editors/include/BIF_glutil.h
index c3188b3793..1a1d759064 100644
--- a/source/blender/editors/include/BIF_glutil.h
+++ b/source/blender/editors/include/BIF_glutil.h
@@ -112,7 +112,8 @@ void imm_cpack(unsigned int x);
 * \param slices Specifies the number of subdivisions around the z axis.
 * \param stacks Specifies the number of subdivisions along the z axis.
 */
-void imm_cylinder(unsigned int pos, unsigned int nor, float base, float top, float height, int slices, int stacks);
+void imm_cylinder_nor(unsigned int pos, unsigned int nor, float base, float top, float height, int slices, int stacks);
+void imm_cylinder_wire(unsigned int pos, float base, float top, float height, int slices, int stacks);
 
 /**
  * Returns a float value as obtained by glGetFloatv.
diff --git a/source/blender/editors/screen/glutil.c b/source/blender/editors/screen/glutil.c
index dd06f3952a..ed8ac261c4 100644
--- a/source/blender/editors/screen/glutil.c
+++ b/source/blender/editors/screen/glutil.c
@@ -164,7 +164,7 @@ void imm_cpack(unsigned int x)
 		(((x) >> 16) & 0xFF));
 }
 
-void imm_cylinder(unsigned int pos, unsigned int nor, float base, float top, float height, int slices, int stacks)
+void imm_cylinder_nor(unsigned int pos, unsigned int nor, float base, float top, float height, int slices, int stacks)
 {
 	immBegin(GL_TRIANGLES, 6 * slices * stacks);
 	for (int i = 0; i < slices; ++i) {
@@ -215,6 +215,43 @@ void imm_cylinder(unsigned int pos, unsigned int nor, float base, float top, flo
 	immEnd();
 }
 
+void imm_cylinder_wire(unsigned int pos, float base, float top, float height, int slices, int stacks)
+{
+	immBegin(GL_LINES, 6 * slices * stacks);
+	for (int i = 0; i < slices; ++i) {
+		const float angle1 = 2 * M_PI * ((float)i / (float)slices);
+		const float angle2 = 2 * M_PI * ((float)(i+1) / (float)slices);
+		const float cos1 = cosf(angle1);
+		const float sin1 = sinf(angle1);
+		const float cos2 = cosf(angle2);
+		const float sin2 = sinf(angle2);
+
+		for (int j = 0; j < stacks; ++j) {
+			float fac1 = (float)j / (float)stacks;
+			float fac2 = (float)(j+1) / (float)stacks;
+			float r1 = base * (1.f - fac1) + top * fac1;
+			float r2 = base * (1.f - fac2) + top * fac2;
+			float h1 = height * ((float)j / (float)stacks);
+			float h2 = height * ((float)(j+1) / (float)stacks);
+
+			float v1[3] = {r1 * cos2, r1 * sin2, h1};
+			float v2[3] = {r2 * cos2, r2 * sin2, h2};
+			float v3[3] = {r2 * cos1, r2 * sin1, h2};
+			float v4[3] = {r1 * cos1, r1 * sin1, h1};
+
+			immVertex3fv(pos, v1);
+			immVertex3fv(pos, v2);
+			
+			immVertex3fv(pos, v2);
+			immVertex3fv(pos, v3);
+
+			immVertex3fv(pos, v1);
+			immVertex3fv(pos, v4);
+		}
+	}
+	immEnd();
+}
+
 float glaGetOneFloat(int param)
 {
 	GLfloat v;
diff --git a/source/blender/editors/space_view3d/drawarmature.c b/source/blender/editors/space_view3d/drawarmature.c
index 752a2a6423..18a558cf48 100644
--- a/source/blender/editors/space_view3d/drawarmature.c
+++ b/source/blender/editors/space_view3d/drawarmature.c
@@ -1131,7 +1131,7 @@ static void draw_sphere_bone(const short dt, int armflag, int boneflag, short co
 		immUniform3fv("light", light_vec);
 
 		gpuTranslate3f(0.0f, 0.0f, head);
-		imm_cylinder(pos, nor, size2, size1, length - head - tail, 16, 1);
+		imm_cylinder_nor(pos, nor, size2, size1, length - head - tail, 16, 1);
 
 		immUnbindProgram();
 		
diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c
index e897ced0ae..f2f25016c8 100644
--- a/source/blender/editors/space_view3d/drawobject.c
+++ b/source/blender/editors/space_view3d/drawobject.c
@@ -110,9 +110,7 @@
 #include "view3d_intern.h"  /* bad level include */
 
 /* prototypes */
-#ifdef WITH_GAMEENGINE
 static void imm_draw_box(const float vec[8][3], bool solid, unsigned pos);
-#endif
 
 /* Workaround for sequencer scene render mode.
  *
@@ -4251,7 +4249,7 @@ static void draw_mesh_fancy(Scene *scene, SceneLayer *sl, ARegion *ar, View3D *v
 
 	if (dt == OB_BOUNDBOX) {
 		if (((v3d->flag2 & V3D_RENDER_OVERRIDE) && v3d->drawtype >= OB_WIRE) == 0)
-			draw_bounding_volume(ob, ob->boundtype);
+			draw_bounding_volume(ob, ob->boundtype, ob_wire_col);
 	}
 	else if ((no_faces && no_edges) ||
 	         ((!is_obact || (ob->mode == OB_MODE_OBJECT)) && object_is_halo(scene, ob)))
@@ -4672,7 +4670,7 @@ static void draw_mesh_fancy_new(Scene *scene, SceneLayer *sl, ARegion *ar, View3
 
 	if (dt == OB_BOUNDBOX) {
 		if (((v3d->flag2 & V3D_RENDER_OVERRIDE) && v3d->drawtype >= OB_WIRE) == 0)
-			draw_bounding_volume(ob, ob->boundtype);
+			draw_bounding_volume(ob, ob->boundtype, ob_wire_col);
 	}
 	else if ((no_faces && no_edges) ||
 	         ((!is_obact || (ob->mode == OB_MODE_OBJECT)) && object_is_halo(scene, ob)))
@@ -7773,7 +7771,6 @@ static void draw_box(const float vec[8][3], bool solid)
 	glDisableClientState(GL_VERTEX_ARRAY);
 }
 
-#ifdef WITH_GAMEENGINE
 static void imm_draw_box(const float vec[8][3], bool solid, unsigned pos)
 {
 	static const GLubyte quad_indices[24] = {0,1,2,3,7,6,5,4,4,5,1,0,3,2,6,7,3,7,4,0,1,5,6,2};
@@ -7797,14 +7794,13 @@ static void imm_draw_box(const float vec[8][3], bool solid, unsigned pos)
 	}
 	immEnd();
 }
-#endif
 
-static void draw_bb_quadric(BoundBox *bb, char type, bool around_origin)
+static void imm_draw_bb(BoundBox *bb, char type, bool around_origin, const unsigned char ob_wire_col[4])
 {
 	float size[3], cent[3];
-	GLUquadricObj *qobj = gluNewQuadric();
-	
-	gluQuadricDrawStyle(qobj, GLU_SILHOUETTE);
+	Batch *sphere = Batch_get_sphere_wire(0);
+	Batch_set_builtin_program(sphere, GPU_SHADER_3D_UNIFORM_COLOR);
+	if (ob_wire_col) Batch_Uniform4f(sphere, "color", ob_wire_col[0]/255.0f, ob_wire_col[1]/255.0f, ob_wire_col[2]/255.0f, 1.0f);
 	
 	BKE_boundbox_calc_size_aabb(bb, size);
 
@@ -7815,40 +7811,53 @@ static void draw_bb_quadric(BoundBox *bb, char type, bool around_origin)
 		BKE_boundbox_calc_center_aabb(bb, cent);
 	}
 	
-	glPushMatrix();
+	gpuMatrixBegin3D_legacy();
+	gpuPushMatrix();
+
+	unsigned int pos = add_attrib(immVertexFormat(), "pos", GL_FLOAT, 3, KEEP_FLOAT);
+	immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+	if (ob_wire_col) immUniformColor3ubv(ob_wire_col);
+
 	if (type == OB_BOUND_SPHERE) {
 		float scale = MAX3(size[0], size[1], size[2]);
-		glTranslate3fv(cent);
-		glScalef(scale, scale, scale);
-		gluSphere(qobj, 1.0, 8, 5);
+		gpuTranslate3fv(cent);
+		gpuRotateAxis(90, 'X');
+		gpuScaleUniform(scale);
+		Batch_draw(sphere);
 	}
 	else if (type == OB_BOUND_CYLINDER) {
 		float radius = size[0] > size[1] ? size[0] : size[1];
-		glTranslatef(cent[0], cent[1], cent[2] - size[2]);
-		glScalef(radius, radius, 2.0f * size[2]);
-		gluCylinder(qobj, 1.0, 1.0, 1.0, 8, 1);
+		gpuTranslate3f(cent[0], cent[1], cent[2] - size[2]);
+		gpuScale3f(radius, radius, 2.0f * size[2]);
+		imm_cylinder_wire(pos, 1.0f, 1.0f, 1.0f, 8, 1);
 	}
 	else if (type == OB_BOUND_CONE) {
 		float radius = size[0] > size[1] ? size[0] : size[1];
-		glTranslatef(cent[0], cent[1], cent[2] - size[2]);
-		glScalef(radius, radius, 2.0f * size[2]);
-		gluCylinder(qobj, 1.0, 0.0, 1.0, 8, 1);
+		gpuTranslate3f(cent[0], cent[1], cent[2] - size[2]);
+		gpuScale3f(radius, radius, 2.0f * size[2]);
+		imm_cylinder_wire(pos, 1.0f, 0.0f, 1.0f, 8, 1);
+
 	}
 	else if (type == OB_BOUND_CAPSULE) {
 		float radius = size[0] > size[1] ? size[0] : size[1];
 		float length = size[2] > radius ? 2.0f * (size[2] - radius) : 0.0f;
-		glTranslatef(cent[0], cent[1], cent[2] - length * 0.5f);
-		gluCylinder(qobj, radius, radius, length, 8, 1);
-		gluSphere(qobj, radius, 8, 4);
-		glTranslatef(0.0, 0.0, length);
-		gluSphere(qobj, radius, 8, 4);
+		gpuTranslate3f(cent[0], cent[1], cent[2] - length * 0.5f);
+		imm_cylinder_wire(pos, radius, radius, length, 8, 1);
+
+		gpuRotateAxis(90, 'X');
+		gpuScaleUniform(radius);
+		Batch_draw(sphere);
+
+		gpuTranslate3f(0.0f, length / radius, 0.0f);
+		Batch_draw(sphere);
 	}
-	glPopMatrix();
-	
-	gluDeleteQuadric(qobj);
+	gpuPopMatrix();
+	gpuMatrixEnd();
+
+	immUnbindProgram();
 }
 
-void draw_bounding_volume(Object *ob, char type)
+void draw_bounding_volume(Object *ob, char type, const unsigned char ob_wire_col[4])
 {
 	BoundBox  bb_local;
 	BoundBox *bb = NULL;
@@ -7878,12 +7887,16 @@ void draw_bounding_volume(Object *ob, char type)
 	
 	if (bb == NULL)
 		return;
-	
+
 	if (ob->gameflag & OB_BOUNDS) { /* bounds need to be drawn around origin for game engine */
 
 		if (type == OB_BOUND_BOX) {
 			float vec[8][3], size[3];
-			
+
+			unsigned int pos = add_attrib(immVertexFormat(), "pos", GL_FLOAT, 3, KEEP_FLOAT);
+			immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+			if (ob_wire_col) immUniformColor3ubv(ob_wire_col);
+
 			BKE_boundbox_calc_size_aabb(bb, size);
 			
 			vec[0][0] = vec[1][0] = vec[2][0] = vec[3][0] = -size[0];
@@ -7893,18 +7906,28 @@ void draw_bounding_volume(Object *ob, char type)
 			vec[0][2] = vec[3][2] = vec[4][2] = vec[7][2] = -size[2];
 			vec[1][2] = vec[2][2] = vec[5][2] = vec[6][2] = +size[2];
 			
-			draw_box(vec, false);
+			imm_draw_box(vec, false, pos);
+
+			immUnbindProgram();
 		}
 		else {
-			draw_bb_quadric(bb, type, true);
+			imm_draw_bb(bb, type, true, ob_wire_col);
 		}
 	}
 	else {
-		if (type == OB_BOUND_BOX)
-			draw_box(bb->vec, false);
+		if (type == OB_BOUND_BOX) {
+			unsigned int pos = add_attrib(immVertexFormat(), "pos", GL_FLOAT, 3, KEEP_FLOAT);
+			immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+			if (ob_wire_col) immUniformColor3ubv(ob_wire_col);
+
+			imm_draw_box(bb->vec, false, pos);
+
+			immUnbindProgram();
+		}
 		else
-			draw_bb_quadric(bb, type, false);
+			imm_draw_bb(bb, type, false, ob_wire_col);
 	}
+
 }
 
 static void drawtexspace(Object *ob)
@@ -8190,7 +8213,7 @@ static void draw_object_matcap_check(View3D *v3d, Object *ob)
 	v3d->flag2 |= V3D_SHOW_SOLID_MATCAP;
 }
 
-void draw_rigidbody_shape(Object *ob)
+void draw_rigidbody_shape(Object *ob, const unsigned char ob_wire_col[4])
 {
 	BoundBox *bb = NULL;
 	float size[3], vec[8][3];
@@ -8216,16 +8239,16 @@ void draw_rigidbody_shape(Object *ob)
 			draw_box(vec, false);
 			break;
 		case RB_SHAPE_SPHERE:
-			draw_bb_quadric(bb, OB_BOUND_SPHERE

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list