diff --git a/examples/testbed/tests/convex_hull.h b/examples/testbed/tests/convex_hull.h index 91fdeb4..d9b210e 100644 --- a/examples/testbed/tests/convex_hull.h +++ b/examples/testbed/tests/convex_hull.h @@ -40,6 +40,22 @@ public: void KeyDown(int button) { + if (button == GLFW_KEY_LEFT) + { + if (m_selection > 0) + { + m_selection -= 1; + } + } + + if (button == GLFW_KEY_RIGHT) + { + if (m_selection < m_hull.faceCount - 1) + { + m_selection += 1; + } + } + if (button == GLFW_KEY_G) { Generate(); @@ -65,60 +81,114 @@ public: m_points[m_count++] = p; } + + m_hull.Set(m_points, m_count); + assert(m_hull.faceCount > 0); + m_selection = 0; } void Step() { - b3QHull hull; - hull.Set(m_points, m_count); + Generate(); + + for (u32 i = 0; i < m_hull.vertexCount; ++i) + { + b3Color solidColor(1.0f, 1.0f, 1.0f, 0.75f); + g_draw->DrawPoint(m_hull.vertices[i], 4.0f, solidColor); + } for (u32 i = 0; i < m_count; ++i) { - g_draw->DrawPoint(m_points[i], 4.0f, b3Color_green); + b3Color solidColor(1.0f, 1.0f, 1.0f, 0.25f); + g_draw->DrawPoint(m_points[i], 4.0f, solidColor); } - for (u32 i = 0; i < hull.edgeCount; i += 2) + for (u32 i = 0; i < m_hull.edgeCount; i += 2) { - const b3HalfEdge* edge = hull.GetEdge(i); - const b3HalfEdge* twin = hull.GetEdge(i + 1); + const b3HalfEdge* edge = m_hull.GetEdge(i); + const b3HalfEdge* twin = m_hull.GetEdge(i + 1); - b3Vec3 v1 = hull.GetVertex(edge->origin); - b3Vec3 v2 = hull.GetVertex(twin->origin); + b3Vec3 v1 = m_hull.GetVertex(edge->origin); + b3Vec3 v2 = m_hull.GetVertex(twin->origin); - g_draw->DrawSegment(v1, v2, b3Color_black); + //g_draw->DrawSegment(v1, v2, b3Color_black); } g_draw->Flush(); - for (u32 i = 0; i < hull.faceCount; ++i) { - const b3Face* face = hull.GetFace(i); - const b3HalfEdge* begin = hull.GetEdge(face->edge); + const b3Face* face = m_hull.GetFace(m_selection); + b3Vec3 n = m_hull.GetPlane(m_selection).normal; - b3Vec3 n = hull.GetPlane(i).normal; + b3Vec3 c; + c.SetZero(); + + u32 vn = 0; + + const b3HalfEdge* begin = m_hull.GetEdge(face->edge); + const b3HalfEdge* edge = begin; + do + { + u32 i1 = edge->origin; + b3Vec3 v1 = m_hull.GetVertex(i1); + + const b3HalfEdge* twin = m_hull.GetEdge(edge->twin); + u32 i2 = twin->origin; + b3Vec3 v2 = m_hull.GetVertex(i2); - const b3HalfEdge* edge = hull.GetEdge(begin->next); + c += v1; + ++vn; + + b3Color solidColor(1.0f, 0.0f, 0.0f, 1.0f); + g_draw->DrawSegment(v1, v2, solidColor); + + g_draw->DrawString(b3Color_white, v1, "v%d", vn); + + edge = m_hull.GetEdge(edge->next); + } while (edge != begin); + + c /= float32(vn); + g_draw->DrawSegment(c, c + n, b3Color_white); + } + + g_draw->Flush(); + + for (u32 i = 0; i < m_hull.faceCount; ++i) + { + const b3Face* face = m_hull.GetFace(i); + + b3Vec3 n = m_hull.GetPlane(i).normal; + + const b3HalfEdge* begin = m_hull.GetEdge(face->edge); + const b3HalfEdge* edge = begin; do { u32 i1 = begin->origin; u32 i2 = edge->origin; - const b3HalfEdge* next = hull.GetEdge(edge->next); + const b3HalfEdge* next = m_hull.GetEdge(edge->next); u32 i3 = next->origin; - b3Vec3 v1 = hull.GetVertex(i1); - b3Vec3 v2 = hull.GetVertex(i2); - b3Vec3 v3 = hull.GetVertex(i3); + b3Vec3 v1 = m_hull.GetVertex(i1); + b3Vec3 v2 = m_hull.GetVertex(i2); + b3Vec3 v3 = m_hull.GetVertex(i3); - b3Color solidColor(1.0f, 1.0f, 1.0f, 0.5f); + b3Color solidColor(1.0f, 1.0f, 1.0f, 0.25f); + + if (i == m_selection) + { + solidColor.a = 1.0f; + } + g_draw->DrawSolidTriangle(n, v1, v2, v3, solidColor); edge = next; - } while (hull.GetEdge(edge->next) != begin); + } while (edge != begin); } g_draw->Flush(); g_draw->DrawString(b3Color_white, "G - Generate a random convex hull"); + g_draw->DrawString(b3Color_white, "Left/Right Arrow - Select previous/next convex hull face"); } static Test* Create() @@ -126,6 +196,8 @@ public: return new ConvexHull(); } + b3QHull m_hull; + u32 m_selection; u32 m_count; b3Vec3 m_points[e_count]; };