[Bf-blender-cvs] [45c287d] openvdb: Visualization: compute normals for voxel box drawing.

Kévin Dietrich noreply at git.blender.org
Tue Jun 23 11:30:58 CEST 2015


Commit: 45c287d0091785dc822a5f9a0802a5ec33614723
Author: Kévin Dietrich
Date:   Mon Jun 22 21:49:17 2015 +0200
Branches: openvdb
https://developer.blender.org/rB45c287d0091785dc822a5f9a0802a5ec33614723

Visualization: compute normals for voxel box drawing.

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

M	intern/openvdb/intern/openvdb_render.cpp

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

diff --git a/intern/openvdb/intern/openvdb_render.cpp b/intern/openvdb/intern/openvdb_render.cpp
index 2a39287..05cd6a5 100644
--- a/intern/openvdb/intern/openvdb_render.cpp
+++ b/intern/openvdb/intern/openvdb_render.cpp
@@ -51,8 +51,26 @@ static void add_point(std::vector<vertex> *vertices, std::vector<vertex> *colors
 	colors->push_back(col);
 }
 
+static inline vertex get_normal(
+        const openvdb::Vec3f &v1, const openvdb::Vec3f &v2,
+        const openvdb::Vec3f &v3, const openvdb::Vec3f &v4)
+{
+	vertex nor;
+	openvdb::Vec3f n1, n2;
+
+	n1 = v1 - v3;
+	n2 = v2 - v4;
+
+	nor.x = n1[1] * n2[2] - n1[2] * n2[1];
+	nor.y = n1[2] * n2[0] - n1[0] * n2[2];
+	nor.z = n1[0] * n2[1] - n1[1] * n2[0];
+
+	return nor;
+}
+
 static void add_box(std::vector<vertex> *vertices,
                     std::vector<vertex> *colors,
+                    std::vector<vertex> *normals,
                     const openvdb::Vec3f &min,
                     const openvdb::Vec3f &max,
                     const openvdb::Vec3f &color,
@@ -102,6 +120,45 @@ static void add_box(std::vector<vertex> *vertices,
 		points.push_back(corners[5]);
 		points.push_back(corners[6]);
 		points.push_back(corners[2]);
+
+		if (normals) {
+			int i;
+			vertex nor = get_normal(corners[0], corners[1], corners[2], corners[3]);
+
+			for (i = 0; i < 4; ++i) {
+				normals->push_back(nor);
+			}
+
+			nor = get_normal(corners[7], corners[6], corners[5], corners[1]);
+
+			for (i = 0; i < 4; ++i) {
+				normals->push_back(nor);
+			}
+
+			nor = get_normal(corners[4], corners[5], corners[1], corners[0]);
+
+			for (i = 0; i < 4; ++i) {
+				normals->push_back(nor);
+			}
+
+			nor = get_normal(corners[3], corners[2], corners[6], corners[7]);
+
+			for (i = 0; i < 4; ++i) {
+				normals->push_back(nor);
+			}
+
+			nor = get_normal(corners[3], corners[7], corners[4], corners[0]);
+
+			for (i = 0; i < 4; ++i) {
+				normals->push_back(nor);
+			}
+
+			nor = get_normal(corners[1], corners[5], corners[6], corners[2]);
+
+			for (i = 0; i < 4; ++i) {
+				normals->push_back(nor);
+			}
+		}
 	}
 	else {
 		points.push_back(corners[0]);
@@ -189,7 +246,7 @@ void OpenVDBPrimitive_draw_tree(OpenVDBPrimitive *vdb_prim, const bool draw_root
 			color = node_color[3];
 		}
 
-		add_box(&vertices, &colors, wmin, wmax, color, false);
+		add_box(&vertices, &colors, NULL, wmin, wmax, color, false);
 	}
 
 	glDisable(GL_CULL_FACE);
@@ -213,7 +270,7 @@ void OpenVDBPrimitive_draw_values(OpenVDBPrimitive *vdb_prim, float tolerance, f
 	using namespace openvdb::math;
 
 	FloatGrid::Ptr grid = gridPtrCast<FloatGrid>(vdb_prim->getGridPtr());
-	std::vector<vertex> vertices, colors;
+	std::vector<vertex> vertices, colors, normals;
 
 	size_t i = 0;
 	float fac = static_cast<float>(lod) * 0.01f;
@@ -223,6 +280,7 @@ void OpenVDBPrimitive_draw_values(OpenVDBPrimitive *vdb_prim, float tolerance, f
 
 	vertices.reserve(num_points);
 	colors.reserve(num_points);
+	normals.reserve(num_points);
 
 	if (draw_box) {
 		CoordBBox bbox;
@@ -255,7 +313,7 @@ void OpenVDBPrimitive_draw_values(OpenVDBPrimitive *vdb_prim, float tolerance, f
 				wmax = grid->indexToWorld(max);
 
 				color = isNegative(value) ? voxel_color[1] : voxel_color[0];
-				add_box(&vertices, &colors, wmin, wmax, color, true);
+				add_box(&vertices, &colors, &normals, wmin, wmax, color, true);
 			}
 
 			i++;
@@ -282,6 +340,10 @@ void OpenVDBPrimitive_draw_values(OpenVDBPrimitive *vdb_prim, float tolerance, f
 	glPointSize(point_size);
 	glEnableClientState(GL_VERTEX_ARRAY);
 	glEnableClientState(GL_COLOR_ARRAY);
+
+	if (draw_box)
+		glEnableClientState(GL_NORMAL_ARRAY);
+
 	glEnable(GL_LIGHTING);
 	glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
 	glEnable(GL_COLOR_MATERIAL);
@@ -289,10 +351,15 @@ void OpenVDBPrimitive_draw_values(OpenVDBPrimitive *vdb_prim, float tolerance, f
 
 	glVertexPointer(3, GL_FLOAT, 0, &vertices[0]);
 	glColorPointer(3, GL_FLOAT, 0, &colors[0]);
+	glNormalPointer(GL_FLOAT, 0, &normals[0]);
 	glDrawArrays(draw_box ? GL_QUADS : GL_POINTS, 0, vertices.size());
 
 	glDisableClientState(GL_VERTEX_ARRAY);
 	glDisableClientState(GL_COLOR_ARRAY);
+
+	if (draw_box)
+		glDisableClientState(GL_NORMAL_ARRAY);
+
 	glDisable(GL_COLOR_MATERIAL);
 	glDisable(GL_LIGHTING);
 }




More information about the Bf-blender-cvs mailing list