[Bf-blender-cvs] [1eb49f8a2fc] blender2.8: GPU_immediate_util: add cube drawing function

Campbell Barton noreply at git.blender.org
Tue Sep 26 09:39:33 CEST 2017


Commit: 1eb49f8a2fc33b17542eac950ebdc43aadb0f03e
Author: Campbell Barton
Date:   Tue Sep 26 17:51:47 2017 +1000
Branches: blender2.8
https://developer.blender.org/rB1eb49f8a2fc33b17542eac950ebdc43aadb0f03e

GPU_immediate_util: add cube drawing function

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

M	source/blender/gpu/GPU_immediate_util.h
M	source/blender/gpu/intern/gpu_immediate_util.c

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

diff --git a/source/blender/gpu/GPU_immediate_util.h b/source/blender/gpu/GPU_immediate_util.h
index e225c0569ba..c31d477ff5b 100644
--- a/source/blender/gpu/GPU_immediate_util.h
+++ b/source/blender/gpu/GPU_immediate_util.h
@@ -44,11 +44,13 @@ void imm_draw_disk_partial_fill_2d(
         float radius_inner, float radius_outer, int nsegments, float start, float sweep);
 
 void imm_draw_box_wire_2d(uint pos, float x1, float y1, float x2, float y2);
-
 void imm_draw_box_wire_3d(uint pos, float x1, float y1, float x2, float y2);
 
 void imm_draw_box_checker_2d(float x1, float y1, float x2, float y2);
 
+void imm_draw_cube_fill_3d(uint pos, const float co[3], const float aspect[3]);
+void imm_draw_cube_wire_3d(uint pos, const float co[3], const float aspect[3]);
+
 void imm_draw_cylinder_fill_normal_3d(
         uint pos, uint nor, float base, float top, float height,
         int slices, int stacks);
diff --git a/source/blender/gpu/intern/gpu_immediate_util.c b/source/blender/gpu/intern/gpu_immediate_util.c
index 45230021435..bad878ef4bf 100644
--- a/source/blender/gpu/intern/gpu_immediate_util.c
+++ b/source/blender/gpu/intern/gpu_immediate_util.c
@@ -33,6 +33,39 @@
 #include "GPU_immediate_util.h"
 #include "GPU_matrix.h"
 
+static const float cube_coords[8][3] = {
+	{-1, -1, -1},
+	{-1, -1, +1},
+	{-1, +1, -1},
+	{-1, +1, +1},
+	{+1, -1, -1},
+	{+1, -1, +1},
+	{+1, +1, -1},
+	{+1, +1, +1},
+};
+static const int cube_quad_index[6][4] = {
+	{0, 1, 3, 2},
+	{0, 2, 6, 4},
+	{0, 4, 5, 1},
+	{1, 5, 7, 3},
+	{2, 3, 7, 6},
+	{4, 6, 7, 5},
+};
+static const int cube_line_index[12][2] = {
+	{0, 1},
+	{0, 2},
+	{0, 4},
+	{1, 3},
+	{1, 5},
+	{2, 3},
+	{2, 6},
+	{3, 7},
+	{4, 5},
+	{4, 6},
+	{5, 7},
+	{6, 7},
+};
+
 /**
  * Pack color into 3 bytes
  *
@@ -214,6 +247,43 @@ void imm_draw_box_checker_2d(float x1, float y1, float x2, float y2)
 	immUnbindProgram();
 }
 
+void imm_draw_cube_fill_3d(uint pos, const float co[3], const float aspect[3])
+{
+	float coords[ARRAY_SIZE(cube_coords)][3];
+
+	for (int i = 0; i < ARRAY_SIZE(cube_coords); i++) {
+		madd_v3_v3v3v3(coords[i], co, cube_coords[i], aspect);
+	}
+
+	immBegin(GWN_PRIM_TRIS, ARRAY_SIZE(cube_quad_index) * 3 * 2);
+	for (int i = 0; i < ARRAY_SIZE(cube_quad_index); i++) {
+		immVertex3fv(pos, coords[cube_quad_index[i][0]]);
+		immVertex3fv(pos, coords[cube_quad_index[i][1]]);
+		immVertex3fv(pos, coords[cube_quad_index[i][2]]);
+
+		immVertex3fv(pos, coords[cube_quad_index[i][0]]);
+		immVertex3fv(pos, coords[cube_quad_index[i][2]]);
+		immVertex3fv(pos, coords[cube_quad_index[i][3]]);
+	}
+	immEnd();
+}
+
+void imm_draw_cube_wire_3d(uint pos, const float co[3], const float aspect[3])
+{
+	float coords[ARRAY_SIZE(cube_coords)][3];
+
+	for (int i = 0; i < ARRAY_SIZE(cube_coords); i++) {
+		madd_v3_v3v3v3(coords[i], co, cube_coords[i], aspect);
+	}
+
+	immBegin(GWN_PRIM_LINES, ARRAY_SIZE(cube_line_index) * 2);
+	for (int i = 0; i < ARRAY_SIZE(cube_line_index); i++) {
+		immVertex3fv(pos, coords[cube_line_index[i][0]]);
+		immVertex3fv(pos, coords[cube_line_index[i][1]]);
+	}
+	immEnd();
+}
+
 /**
 * Draw a cylinder. Replacement for gluCylinder.
 * _warning_ : Slow, better use it only if you no other choices.



More information about the Bf-blender-cvs mailing list