From 56db3517f958db6f3626be2f155a00431b492471 Mon Sep 17 00:00:00 2001 From: Irlan Date: Tue, 4 Jun 2019 20:56:51 -0300 Subject: [PATCH] Draw solid stuff inside b3World --- examples/testbed/framework/draw.cpp | 171 +------------------------ examples/testbed/framework/draw.h | 14 +- examples/testbed/framework/test.cpp | 2 +- examples/testbed/tests/collide_test.h | 8 +- examples/testbed/tests/distance_test.h | 4 +- examples/testbed/tests/shape_cast.h | 10 +- include/bounce/common/draw.h | 24 ++-- include/bounce/dynamics/world.h | 11 +- src/bounce/dynamics/draw_world.cpp | 135 +++++++++++++++++-- src/bounce/rope/rope.cpp | 4 +- 10 files changed, 165 insertions(+), 218 deletions(-) diff --git a/examples/testbed/framework/draw.cpp b/examples/testbed/framework/draw.cpp index f9a5fed..fe1e305 100644 --- a/examples/testbed/framework/draw.cpp +++ b/examples/testbed/framework/draw.cpp @@ -302,16 +302,16 @@ void Draw::DrawSolidCircle(const b3Vec3& normal, const b3Vec3& center, float32 r void Draw::DrawSphere(const b3Vec3& center, float32 radius, const b3Color& color) { b3Transform xf; - xf.SetIdentity(); + xf.rotation.SetIdentity(); xf.position = center; m_wire->DrawSphere(radius, color, xf); } -void Draw::DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Color& color) +void Draw::DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Mat33& rotation, const b3Color& color) { b3Transform xf; - xf.SetIdentity(); + xf.rotation = rotation; xf.position = center; m_solid->DrawSphere(radius, color, xf); @@ -529,171 +529,6 @@ void Draw::DrawString(const b3Color& color, const char* text, ...) va_end(args); } -void Draw::DrawSolidSphere(const b3SphereShape* s, const b3Color& c, const b3Transform& xf) -{ - b3Transform xfc; - xfc.rotation = xf.rotation; - xfc.position = xf * s->m_center; - m_solid->DrawSphere(s->m_radius, c, xfc); -} - -void Draw::DrawSolidCapsule(const b3CapsuleShape* s, const b3Color& c, const b3Transform& xf) -{ - b3Vec3 c1 = s->m_centers[0]; - b3Vec3 c2 = s->m_centers[1]; - float32 height = b3Length(c1 - c2); - float32 radius = s->m_radius; - - { - b3Transform xfc; - xfc.rotation = xf.rotation; - xfc.position = xf * c1; - m_solid->DrawSphere(radius, c, xfc); - } - - if (height > 0.0f) - { - { - b3Mat33 R; - R.y = (1.0f / height) * (c1 - c2); - R.z = b3Perp(R.y); - R.x = b3Cross(R.y, R.z); - - b3Transform xfc; - xfc.position = xf * (0.5f * (c1 + c2)); - xfc.rotation = xf.rotation * R; - - m_solid->DrawCylinder(radius, height, c, xfc); - } - - { - b3Transform xfc; - xfc.rotation = xf.rotation; - xfc.position = xf * c2; - m_solid->DrawSphere(radius, c, xfc); - } - } -} - -void Draw::DrawSolidHull(const b3HullShape* s, const b3Color& c, const b3Transform& xf) -{ - const b3Hull* hull = s->m_hull; - - for (u32 i = 0; i < hull->faceCount; ++i) - { - const b3Face* face = hull->GetFace(i); - const b3HalfEdge* begin = hull->GetEdge(face->edge); - - b3Vec3 n = xf.rotation * hull->planes[i].normal; - - const b3HalfEdge* edge = hull->GetEdge(begin->next); - do - { - u32 i1 = begin->origin; - u32 i2 = edge->origin; - const b3HalfEdge* next = hull->GetEdge(edge->next); - u32 i3 = next->origin; - - b3Vec3 p1 = xf * hull->vertices[i1]; - b3Vec3 p2 = xf * hull->vertices[i2]; - b3Vec3 p3 = xf * hull->vertices[i3]; - - m_triangles->Vertex(p1, c, n); - m_triangles->Vertex(p2, c, n); - m_triangles->Vertex(p3, c, n); - - edge = next; - } while (hull->GetEdge(edge->next) != begin); - } -} - -void Draw::DrawSolidMesh(const b3MeshShape* s, const b3Color& c, const b3Transform& xf) -{ - const b3Mesh* mesh = s->m_mesh; - for (u32 i = 0; i < mesh->triangleCount; ++i) - { - const b3Triangle* t = mesh->triangles + i; - - b3Vec3 p1 = xf * mesh->vertices[t->v1]; - b3Vec3 p2 = xf * mesh->vertices[t->v2]; - b3Vec3 p3 = xf * mesh->vertices[t->v3]; - - b3Vec3 n1 = b3Cross(p2 - p1, p3 - p1); - n1.Normalize(); - - m_triangles->Vertex(p1, c, n1); - m_triangles->Vertex(p2, c, n1); - m_triangles->Vertex(p3, c, n1); - - b3Vec3 n2 = -n1; - - m_triangles->Vertex(p1, c, n2); - m_triangles->Vertex(p3, c, n2); - m_triangles->Vertex(p2, c, n2); - } -} - -void Draw::DrawSolidShape(const b3Shape* s, const b3Color& c, const b3Transform& xf) -{ - switch (s->GetType()) - { - case e_sphereShape: - { - DrawSolidSphere((b3SphereShape*)s, c, xf); - break; - } - case e_capsuleShape: - { - DrawSolidCapsule((b3CapsuleShape*)s, c, xf); - break; - } - case e_hullShape: - { - DrawSolidHull((b3HullShape*)s, c, xf); - break; - } - case e_meshShape: - { - DrawSolidMesh((b3MeshShape*)s, c, xf); - break; - } - default: - { - break; - } - } -} - -void Draw::DrawSolidShapes(const b3World& world) -{ - for (b3Body* b = world.GetBodyList().m_head; b; b = b->GetNext()) - { - b3Color c; - if (b->IsAwake() == false) - { - c = b3Color(0.5f, 0.25f, 0.25f, 1.0f); - } - else if (b->GetType() == e_staticBody) - { - c = b3Color(0.5f, 0.5f, 0.5f, 1.0f); - } - else if (b->GetType() == e_dynamicBody) - { - c = b3Color(1.0f, 0.5f, 0.5f, 1.0f); - } - else - { - c = b3Color(0.5f, 0.5f, 1.0f, 1.0f); - } - - b3Transform xf = b->GetTransform(); - for (b3Shape* s = b->GetShapeList().m_head; s; s = s->GetNext()) - { - DrawSolidShape(s, c, xf); - } - } -} - void Draw::Flush() { m_triangles->Flush(); diff --git a/examples/testbed/framework/draw.h b/examples/testbed/framework/draw.h index cd89acb..4f5bda4 100644 --- a/examples/testbed/framework/draw.h +++ b/examples/testbed/framework/draw.h @@ -85,7 +85,7 @@ public: void DrawSphere(const b3Vec3& center, float32 radius, const b3Color& color); - void DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Color& color); + void DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Mat33& rotation, const b3Color& color); void DrawCapsule(const b3Vec3& p1, const b3Vec3& p2, float32 radius, const b3Color& color); @@ -105,18 +105,6 @@ public: void DrawString(const b3Color& color, const char* string, ...); - void DrawSolidSphere(const b3SphereShape* s, const b3Color& c, const b3Transform& xf); - - void DrawSolidCapsule(const b3CapsuleShape* s, const b3Color& c, const b3Transform& xf); - - void DrawSolidHull(const b3HullShape* s, const b3Color& c, const b3Transform& xf); - - void DrawSolidMesh(const b3MeshShape* s, const b3Color& c, const b3Transform& xf); - - void DrawSolidShape(const b3Shape* s, const b3Color& c, const b3Transform& xf); - - void DrawSolidShapes(const b3World& world); - void Flush(); private: friend struct DrawPoints; diff --git a/examples/testbed/framework/test.cpp b/examples/testbed/framework/test.cpp index d6dc8b0..fe3ff77 100644 --- a/examples/testbed/framework/test.cpp +++ b/examples/testbed/framework/test.cpp @@ -88,7 +88,7 @@ void Test::Step() if (g_settings->drawTriangles) { - g_draw->DrawSolidShapes(m_world); + m_world.DrawSolid(); } if (g_settings->drawStats) diff --git a/examples/testbed/tests/collide_test.h b/examples/testbed/tests/collide_test.h index 560df43..f76d704 100644 --- a/examples/testbed/tests/collide_test.h +++ b/examples/testbed/tests/collide_test.h @@ -50,13 +50,13 @@ public: g_draw->DrawSegment(pw, pw + wm.points[i].normal, b3Color_white); } - m_world.DrawShape(m_xfA, m_shapeA); - m_world.DrawShape(m_xfB, m_shapeB); + m_world.DrawShape(m_xfA, m_shapeA, b3Color_black); + m_world.DrawShape(m_xfB, m_shapeB, b3Color_black); g_draw->Flush(); - g_draw->DrawSolidShape(m_shapeA, b3Color(1.0f, 1.0f, 1.0f, 0.25f), m_xfA); - g_draw->DrawSolidShape(m_shapeB, b3Color(1.0f, 1.0f, 1.0f, 0.25f), m_xfB); + m_world.DrawSolidShape(m_xfA, m_shapeA, b3Color(1.0f, 1.0f, 1.0f, 0.25f)); + m_world.DrawSolidShape(m_xfB, m_shapeB, b3Color(1.0f, 1.0f, 1.0f, 0.25f)); g_draw->DrawString(b3Color_white, "Left/Right/Up/Down Arrow - Translate shape"); g_draw->DrawString(b3Color_white, "X/Y/Z - Rotate shape"); diff --git a/examples/testbed/tests/distance_test.h b/examples/testbed/tests/distance_test.h index e5ea4ac..4f659c7 100644 --- a/examples/testbed/tests/distance_test.h +++ b/examples/testbed/tests/distance_test.h @@ -68,8 +68,8 @@ public: g_draw->DrawTransform(m_xfA); g_draw->DrawTransform(m_xfB); - m_world.DrawShape(m_xfA, &m_shapeA); - m_world.DrawShape(m_xfB, &m_shapeB); + m_world.DrawShape(m_xfA, &m_shapeA, b3Color_black); + m_world.DrawShape(m_xfB, &m_shapeB, b3Color_black); g_draw->DrawString(b3Color_white, "Left/Right/Up/Down Arrow - Translate shape"); g_draw->DrawString(b3Color_white, "X/Y/Z - Rotate shape"); diff --git a/examples/testbed/tests/shape_cast.h b/examples/testbed/tests/shape_cast.h index 392ce0c..f08e083 100644 --- a/examples/testbed/tests/shape_cast.h +++ b/examples/testbed/tests/shape_cast.h @@ -30,11 +30,11 @@ public: g_draw->DrawTransform(m_xfA); g_draw->DrawTransform(m_xfB); - m_world.DrawShape(m_xfA, &m_shapeA); - m_world.DrawShape(m_xfB, &m_shapeB); + m_world.DrawShape(m_xfA, &m_shapeA, b3Color_black); + m_world.DrawShape(m_xfB, &m_shapeB, b3Color_black); - g_draw->DrawSolidShape(&m_shapeA, b3Color_white, m_xfA); - g_draw->DrawSolidShape(&m_shapeB, b3Color_white, m_xfB); + m_world.DrawSolidShape(m_xfA, &m_shapeA, b3Color_white); + m_world.DrawSolidShape(m_xfB, &m_shapeB, b3Color_white); b3Vec3 translationB = -100.0f * b3Vec3_x; g_draw->DrawSegment(m_xfB.position, m_xfB.position + translationB, b3Color_white); @@ -53,7 +53,7 @@ public: xfB.rotation = m_xfB.rotation; xfB.position = m_xfB.position + out.t * translationB; - m_world.DrawShape(xfB, &m_shapeB); + m_world.DrawShape(xfB, &m_shapeB, b3Color_black); } } diff --git a/include/bounce/common/draw.h b/include/bounce/common/draw.h index c1a3ce7..9030f44 100644 --- a/include/bounce/common/draw.h +++ b/include/bounce/common/draw.h @@ -95,24 +95,24 @@ public : // Draw a solid circle with center, normal, and radius. virtual void DrawSolidCircle(const b3Vec3& normal, const b3Vec3& center, float32 radius, const b3Color& color) = 0; - // Draw a sphere with center and radius. - virtual void DrawSphere(const b3Vec3& center, float32 radius, const b3Color& color) = 0; - - // Draw a solid sphere with center and radius. - virtual void DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Color& color) = 0; - - // Draw a capsule with segment and radius. - virtual void DrawCapsule(const b3Vec3& p1, const b3Vec3& p2, float32 radius, const b3Color& color) = 0; - - // Draw a solid capsule with segment and radius. - virtual void DrawSolidCapsule(const b3Vec3& p1, const b3Vec3& p2, float32 radius, const b3Color& color) = 0; - // Draw a plane with center, normal and radius. virtual void DrawPlane(const b3Vec3& center, const b3Vec3& normal, float32 radius, const b3Color& color) = 0; // Draw a solid plane with center, normal and radius. virtual void DrawSolidPlane(const b3Vec3& center, const b3Vec3& normal, float32 radius, const b3Color& color) = 0; + // Draw a sphere with center, and radius. + virtual void DrawSphere(const b3Vec3& center, float32 radius, const b3Color& color) = 0; + + // Draw a solid sphere with center, radius, and rotation. + virtual void DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Mat33& rotation, const b3Color& color) = 0; + + // Draw a capsule with segment, and radius. + virtual void DrawCapsule(const b3Vec3& p1, const b3Vec3& p2, float32 radius, const b3Color& color) = 0; + + // Draw a solid capsule with segment, and radius. + virtual void DrawSolidCapsule(const b3Vec3& p1, const b3Vec3& p2, float32 radius, const b3Color& color) = 0; + // Draw a AABB. virtual void DrawAABB(const b3AABB3& aabb, const b3Color& color) = 0; diff --git a/include/bounce/dynamics/world.h b/include/bounce/dynamics/world.h index d33a5a8..a60d72d 100644 --- a/include/bounce/dynamics/world.h +++ b/include/bounce/dynamics/world.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -121,9 +122,15 @@ public: // Draw the entities in this world. void Draw() const; + // Draw solid the entities in this world. + void DrawSolid() const; + // Draw a shape. - void DrawShape(const b3Transform& xf, const b3Shape* shape) const; -private : + void DrawShape(const b3Transform& xf, const b3Shape* shape, const b3Color& color) const; + + // Draw solid a shape. + void DrawSolidShape(const b3Transform& xf, const b3Shape* shape, const b3Color& color) const; +private: enum b3Flags { e_shapeAddedFlag = 0x0001, diff --git a/src/bounce/dynamics/draw_world.cpp b/src/bounce/dynamics/draw_world.cpp index 1d197c8..802b1e4 100644 --- a/src/bounce/dynamics/draw_world.cpp +++ b/src/bounce/dynamics/draw_world.cpp @@ -51,7 +51,7 @@ void b3World::Draw() const const b3Transform& xf = b->GetTransform(); for (b3Shape* s = b->m_shapeList.m_head; s; s = s->m_next) { - DrawShape(xf, s); + DrawShape(xf, s, b3Color_black); } } } @@ -142,16 +142,15 @@ void b3World::Draw() const } } -void b3World::DrawShape(const b3Transform& xf, const b3Shape* shape) const +void b3World::DrawShape(const b3Transform& xf, const b3Shape* shape, const b3Color& color) const { - b3Color wireColor(0.0f, 0.0f, 0.0f); switch (shape->GetType()) { case e_sphereShape: { const b3SphereShape* sphere = (b3SphereShape*)shape; b3Vec3 p = xf * sphere->m_center; - b3Draw_draw->DrawPoint(p, 4.0f, wireColor); + b3Draw_draw->DrawPoint(p, 4.0f, color); break; } case e_capsuleShape: @@ -159,9 +158,9 @@ void b3World::DrawShape(const b3Transform& xf, const b3Shape* shape) const const b3CapsuleShape* capsule = (b3CapsuleShape*)shape; b3Vec3 p1 = xf * capsule->m_centers[0]; b3Vec3 p2 = xf * capsule->m_centers[1]; - b3Draw_draw->DrawPoint(p1, 4.0f, wireColor); - b3Draw_draw->DrawPoint(p2, 4.0f, wireColor); - b3Draw_draw->DrawSegment(p1, p2, wireColor); + b3Draw_draw->DrawPoint(p1, 4.0f, color); + b3Draw_draw->DrawPoint(p2, 4.0f, color); + b3Draw_draw->DrawSegment(p1, p2, color); break; } case e_hullShape: @@ -176,7 +175,7 @@ void b3World::DrawShape(const b3Transform& xf, const b3Shape* shape) const b3Vec3 p1 = xf * hull->vertices[edge->origin]; b3Vec3 p2 = xf * hull->vertices[twin->origin]; - b3Draw_draw->DrawSegment(p1, p2, wireColor); + b3Draw_draw->DrawSegment(p1, p2, color); } break; } @@ -192,7 +191,7 @@ void b3World::DrawShape(const b3Transform& xf, const b3Shape* shape) const b3Vec3 p2 = xf * mesh->vertices[t->v2]; b3Vec3 p3 = xf * mesh->vertices[t->v3]; - b3Draw_draw->DrawTriangle(p1, p2, p3, wireColor); + b3Draw_draw->DrawTriangle(p1, p2, p3, color); } break; } @@ -201,4 +200,122 @@ void b3World::DrawShape(const b3Transform& xf, const b3Shape* shape) const break; } }; +} + +void b3World::DrawSolid() const +{ + for (b3Body* b = m_bodyList.m_head; b; b = b->GetNext()) + { + b3Color c; + if (b->IsAwake() == false) + { + c = b3Color(0.5f, 0.25f, 0.25f, 1.0f); + } + else if (b->GetType() == e_staticBody) + { + c = b3Color(0.5f, 0.5f, 0.5f, 1.0f); + } + else if (b->GetType() == e_dynamicBody) + { + c = b3Color(1.0f, 0.5f, 0.5f, 1.0f); + } + else + { + c = b3Color(0.5f, 0.5f, 1.0f, 1.0f); + } + + b3Transform xf = b->GetTransform(); + for (b3Shape* s = b->GetShapeList().m_head; s; s = s->GetNext()) + { + DrawSolidShape(xf, s, c); + } + } +} + +void b3World::DrawSolidShape(const b3Transform& xf, const b3Shape* shape, const b3Color& color) const +{ + switch (shape->GetType()) + { + case e_sphereShape: + { + const b3SphereShape* sphere = (b3SphereShape*)shape; + + b3Vec3 center = xf * sphere->m_center; + + b3Draw_draw->DrawSolidSphere(center, sphere->m_radius, xf.rotation, color); + + break; + } + case e_capsuleShape: + { + const b3CapsuleShape* capsule = (b3CapsuleShape*)shape; + + b3Vec3 c1 = xf * capsule->m_centers[0]; + b3Vec3 c2 = xf * capsule->m_centers[1]; + + b3Draw_draw->DrawSolidCapsule(c1, c2, capsule->m_radius, color); + + break; + } + case e_hullShape: + { + const b3HullShape* hullShape = (b3HullShape*)shape; + + const b3Hull* hull = hullShape->m_hull; + + for (u32 i = 0; i < hull->faceCount; ++i) + { + const b3Face* face = hull->GetFace(i); + const b3HalfEdge* begin = hull->GetEdge(face->edge); + + b3Vec3 n = xf.rotation * hull->planes[i].normal; + + const b3HalfEdge* edge = hull->GetEdge(begin->next); + do + { + u32 i1 = begin->origin; + u32 i2 = edge->origin; + const b3HalfEdge* next = hull->GetEdge(edge->next); + u32 i3 = next->origin; + + b3Vec3 p1 = xf * hull->vertices[i1]; + b3Vec3 p2 = xf * hull->vertices[i2]; + b3Vec3 p3 = xf * hull->vertices[i3]; + + b3Draw_draw->DrawSolidTriangle(n, p1, p2, p3, color); + + edge = next; + } while (hull->GetEdge(edge->next) != begin); + } + + break; + } + case e_meshShape: + { + const b3MeshShape* meshShape = (b3MeshShape*)shape; + + const b3Mesh* mesh = meshShape->m_mesh; + for (u32 i = 0; i < mesh->triangleCount; ++i) + { + const b3Triangle* t = mesh->triangles + i; + + b3Vec3 p1 = xf * mesh->vertices[t->v1]; + b3Vec3 p2 = xf * mesh->vertices[t->v2]; + b3Vec3 p3 = xf * mesh->vertices[t->v3]; + + b3Vec3 n1 = b3Cross(p2 - p1, p3 - p1); + n1.Normalize(); + b3Draw_draw->DrawSolidTriangle(n1, p1, p2, p3, color); + + b3Vec3 n2 = -n1; + b3Draw_draw->DrawSolidTriangle(n2, p3, p2, p1, color); + } + + break; + } + default: + { + break; + } + }; } \ No newline at end of file diff --git a/src/bounce/rope/rope.cpp b/src/bounce/rope/rope.cpp index 38824d7..d74582d 100644 --- a/src/bounce/rope/rope.cpp +++ b/src/bounce/rope/rope.cpp @@ -480,7 +480,7 @@ void b3Rope::Draw() const b3RopeBody* b = m_links; b3Draw_draw->DrawTransform(b->m_X); - b3Draw_draw->DrawSolidSphere(b->m_X.position, 0.2f, b3Color_green); + b3Draw_draw->DrawSolidSphere(b->m_X.position, 0.2f, b->m_X.rotation, b3Color_green); } for (u32 i = 1; i < m_count; ++i) @@ -498,6 +498,6 @@ void b3Rope::Draw() const b3Draw_draw->DrawPoint(X_J0.position, 5.0f, b3Color_red); b3Draw_draw->DrawTransform(b->m_X); - b3Draw_draw->DrawSolidSphere(b->m_X.position, 0.2f, b3Color_green); + b3Draw_draw->DrawSolidSphere(b->m_X.position, 0.2f, b->m_X.rotation, b3Color_green); } } \ No newline at end of file