Adding bounds overlap tests.

This commit is contained in:
Бранимир Караџић
2019-02-03 19:47:33 -08:00
parent b9710b02e3
commit f6f6a22119
8 changed files with 1036 additions and 432 deletions

View File

@@ -553,24 +553,24 @@ public:
Sphere lightPosRadius;
float lightTime = time * m_lightAnimationSpeed * (bx::sin(light/float(m_numLights) * bx::kPiHalf ) * 0.5f + 0.5f);
lightPosRadius.m_center.x = bx::sin( ( (lightTime + light*0.47f) + bx::kPiHalf*1.37f ) )*offset;
lightPosRadius.m_center.y = bx::cos( ( (lightTime + light*0.69f) + bx::kPiHalf*1.49f ) )*offset;
lightPosRadius.m_center.z = bx::sin( ( (lightTime + light*0.37f) + bx::kPiHalf*1.57f ) )*2.0f;
lightPosRadius.m_radius = 2.0f;
lightPosRadius.center.x = bx::sin( ( (lightTime + light*0.47f) + bx::kPiHalf*1.37f ) )*offset;
lightPosRadius.center.y = bx::cos( ( (lightTime + light*0.69f) + bx::kPiHalf*1.49f ) )*offset;
lightPosRadius.center.z = bx::sin( ( (lightTime + light*0.37f) + bx::kPiHalf*1.57f ) )*2.0f;
lightPosRadius.radius = 2.0f;
Aabb aabb;
toAabb(aabb, lightPosRadius);
const bx::Vec3 box[8] =
{
{ aabb.m_min.x, aabb.m_min.y, aabb.m_min.z },
{ aabb.m_min.x, aabb.m_min.y, aabb.m_max.z },
{ aabb.m_min.x, aabb.m_max.y, aabb.m_min.z },
{ aabb.m_min.x, aabb.m_max.y, aabb.m_max.z },
{ aabb.m_max.x, aabb.m_min.y, aabb.m_min.z },
{ aabb.m_max.x, aabb.m_min.y, aabb.m_max.z },
{ aabb.m_max.x, aabb.m_max.y, aabb.m_min.z },
{ aabb.m_max.x, aabb.m_max.y, aabb.m_max.z },
{ aabb.min.x, aabb.min.y, aabb.min.z },
{ aabb.min.x, aabb.min.y, aabb.max.z },
{ aabb.min.x, aabb.max.y, aabb.min.z },
{ aabb.min.x, aabb.max.y, aabb.max.z },
{ aabb.max.x, aabb.min.y, aabb.min.z },
{ aabb.max.x, aabb.min.y, aabb.max.z },
{ aabb.max.x, aabb.max.y, aabb.min.z },
{ aabb.max.x, aabb.max.y, aabb.max.z },
};
bx::Vec3 xyz = bx::mulH(box[0], vp);

View File

@@ -477,6 +477,13 @@ void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _
}
}
void translate(Triangle& _inout, bx::Vec3 _pos)
{
_inout.v0 = bx::add(_inout.v0, _pos);
_inout.v1 = bx::add(_inout.v1, _pos);
_inout.v2 = bx::add(_inout.v2, _pos);
}
class ExampleDebugDraw : public entry::AppI
{
public:
@@ -565,10 +572,10 @@ public:
_dde->setColor(0xff0000ff);
const bx::Vec3 tmp = bx::mul(hit.m_normal, 0.7f);
const bx::Vec3 end = bx::add(hit.m_pos, tmp);
const bx::Vec3 tmp = bx::mul(hit.plane.normal, 0.7f);
const bx::Vec3 end = bx::add(hit.pos, tmp);
_dde->drawCone(hit.m_pos, end, 0.1f);
_dde->drawCone(hit.pos, end, 0.1f);
_dde->pop();
@@ -638,7 +645,8 @@ public:
, mtxInvVp
);
const uint32_t selected = 0xff80ffff;
constexpr uint32_t kSelected = 0xff80ffff;
constexpr uint32_t kOverlap = 0xff0000ff;
DebugDrawEncoder dde;
@@ -652,19 +660,19 @@ public:
{ 10.0f, 5.0f, 5.0f },
};
dde.setWireframe(true);
dde.setColor(intersect(&dde, ray, aabb) ? selected : 0xff00ff00);
dde.setColor(intersect(&dde, ray, aabb) ? kSelected : 0xff00ff00);
dde.draw(aabb);
dde.pop();
float time = float(now/freq);
Obb obb;
bx::mtxRotateX(obb.m_mtx, time);
bx::mtxRotateX(obb.mtx, time);
dde.setWireframe(true);
dde.setColor(intersect(&dde, ray, obb) ? selected : 0xffffffff);
dde.setColor(intersect(&dde, ray, obb) ? kSelected : 0xffffffff);
dde.draw(obb);
bx::mtxSRT(obb.m_mtx, 1.0f, 1.0f, 1.0f, time*0.23f, time, 0.0f, 3.0f, 0.0f, 0.0f);
bx::mtxSRT(obb.mtx, 1.0f, 1.0f, 1.0f, time*0.23f, time, 0.0f, 3.0f, 0.0f, 0.0f);
dde.push();
toAabb(aabb, obb);
@@ -674,7 +682,7 @@ public:
dde.pop();
dde.setWireframe(false);
dde.setColor(intersect(&dde, ray, obb) ? selected : 0xffffffff);
dde.setColor(intersect(&dde, ray, obb) ? kSelected : 0xffffffff);
dde.draw(obb);
dde.setColor(0xffffffff);
@@ -702,7 +710,7 @@ public:
dde.setColor(false
|| intersect(&dde, ray, plane)
? selected
? kSelected
: 0xffffffff
);
@@ -713,24 +721,24 @@ public:
dde.push();
Sphere sphere = { { 0.0f, 5.0f, 0.0f }, 1.0f };
dde.setColor(intersect(&dde, ray, sphere) ? selected : 0xfff0c0ff);
dde.setColor(intersect(&dde, ray, sphere) ? kSelected : 0xfff0c0ff);
dde.setWireframe(true);
dde.setLod(3);
dde.draw(sphere);
dde.setWireframe(false);
sphere.m_center.x = -2.0f;
dde.setColor(intersect(&dde, ray, sphere) ? selected : 0xc0ffc0ff);
sphere.center.x = -2.0f;
dde.setColor(intersect(&dde, ray, sphere) ? kSelected : 0xc0ffc0ff);
dde.setLod(2);
dde.draw(sphere);
sphere.m_center.x = -4.0f;
dde.setColor(intersect(&dde, ray, sphere) ? selected : 0xa0f0ffff);
sphere.center.x = -4.0f;
dde.setColor(intersect(&dde, ray, sphere) ? kSelected : 0xa0f0ffff);
dde.setLod(1);
dde.draw(sphere);
sphere.m_center.x = -6.0f;
dde.setColor(intersect(&dde, ray, sphere) ? selected : 0xffc0ff00);
sphere.center.x = -6.0f;
dde.setColor(intersect(&dde, ray, sphere) ? kSelected : 0xffc0ff00);
dde.setLod(0);
dde.draw(sphere);
dde.pop();
@@ -780,7 +788,7 @@ public:
dde.setColor(false
|| intersect(&dde, ray, cone)
|| intersect(&dde, ray, cylinder)
? selected
? kSelected
: 0xffffffff
);
@@ -797,7 +805,7 @@ public:
{ -6.0f, 7.0f, 0.0f },
0.5f
};
dde.setColor(intersect(&dde, ray, capsule) ? selected : 0xffffffff);
dde.setColor(intersect(&dde, ray, capsule) ? kSelected : 0xffffffff);
dde.draw(capsule);
}
dde.pop();
@@ -818,8 +826,8 @@ public:
1.0f
};
cylinder.m_end = bx::mul({ 0.0f, 4.0f, 0.0f }, mtx);
dde.setColor(intersect(&dde, ray, cylinder) ? selected : 0xffffffff);
cylinder.end = bx::mul({ 0.0f, 4.0f, 0.0f }, mtx);
dde.setColor(intersect(&dde, ray, cylinder) ? kSelected : 0xffffffff);
dde.draw(cylinder);
dde.push();
@@ -833,6 +841,228 @@ public:
dde.drawOrb(-11.0f, 0.0f, 0.0f, 1.0f);
dde.push();
{
bool olp;
constexpr float kStepX = 3.0f;
constexpr float kStepZ = 3.0f;
const float px = 0.0f;
const float py = 1.0f;
const float pz = 10.0f;
const float xx = bx::sin(time*0.39f) * 1.03f + px;
const float yy = bx::cos(time*0.79f) * 1.03f + py;
const float zz = bx::cos(time) * 1.03f + pz;
// Sphere ---
{
Sphere sphereA = { { px+kStepX*0.0f, py, pz+kStepZ*0.0f }, 0.5f };
Sphere sphereB = { { xx+kStepX*0.0f, yy, zz+kStepZ*0.0f }, 0.5f };
olp = overlap(sphereA, sphereB);;
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(sphereA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(sphereB);
}
{
Sphere sphereA = { { px+kStepX*1.0f, py, pz+kStepZ*0.0f }, 0.5f };
Aabb aabbB;
toAabb(aabbB, { xx+kStepX*1.0f, yy, zz+kStepZ*0.0f }, { 0.5f, 0.5f, 0.5f });
olp = overlap(sphereA, aabbB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(sphereA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(aabbB);
}
{
Sphere sphereA = { { px+kStepX*2.0f, py, pz+kStepZ*0.0f }, 0.5f };
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*2.0f, 0.0f, kStepZ*0.0f});
bx::Plane planeB;
bx::calcPlane(planeB, triangleB.v0, triangleB.v1, triangleB.v2);
olp = overlap(sphereA, planeB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(sphereA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.drawGrid(planeB.normal, triangleB.v0, 10, 0.3f);
}
{
Sphere sphereA = { { px+kStepX*3.0f, py, pz+kStepZ*0.0f }, 0.5f };
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*3.0f, 0.0f, kStepZ*0.0f});
olp = overlap(sphereA, triangleB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(sphereA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(triangleB);
}
// AABB ---
{
Aabb aabbA, aabbB;
toAabb(aabbA, { px+kStepX*1.0f, py, pz+kStepZ*1.0f }, { 0.5f, 0.5f, 0.5f });
toAabb(aabbB, { xx+kStepX*1.0f, yy, zz+kStepZ*1.0f }, { 0.5f, 0.5f, 0.5f });
olp = overlap(aabbA, aabbB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(aabbA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(aabbB);
}
{
Aabb aabbA;
toAabb(aabbA, { px+kStepX*2.0f, py, pz+kStepZ*1.0f }, { 0.5f, 0.5f, 0.5f });
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*2.0f, 0.0f, kStepZ*1.0f});
bx::Plane planeB;
bx::calcPlane(planeB, triangleB.v0, triangleB.v1, triangleB.v2);
olp = overlap(aabbA, planeB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(aabbA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.drawGrid(planeB.normal, triangleB.v0, 10, 0.3f);
}
{
Aabb aabbA;
toAabb(aabbA, { px+kStepX*3.0f, py, pz+kStepZ*1.0f }, { 0.5f, 0.5f, 0.5f });
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*3.0f, 0.0f, kStepZ*1.0f});
olp = overlap(aabbA, triangleB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(aabbA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(triangleB);
}
// Triangle ---
{
Triangle triangleA =
{
{ px-0.4f, py+0.0f, pz-0.4f },
{ px+0.0f, py-0.3f, pz-0.5f },
{ px+0.0f, py+0.5f, pz+0.3f },
};
translate(triangleA, {kStepX*2.0f, 0.0f, kStepZ*2.0f});
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*2.0f, 0.0f, kStepZ*2.0f});
bx::Plane planeB;
bx::calcPlane(planeB, triangleB.v0, triangleB.v1, triangleB.v2);
olp = overlap(triangleA, planeB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(triangleA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.drawGrid(planeB.normal, triangleB.v0, 10, 0.3f);
}
{
Triangle triangleA =
{
{ px-0.4f, py+0.0f, pz-0.4f },
{ px+0.0f, py-0.3f, pz-0.5f },
{ px+0.0f, py+0.5f, pz+0.3f },
};
translate(triangleA, {kStepX*3.0f, 0.0f, kStepZ*2.0f});
Triangle triangleB =
{
{ xx-0.4f, yy+0.0f, zz-0.4f },
{ xx-0.5f, yy-0.3f, zz+0.0f },
{ xx+0.3f, yy+0.5f, zz+0.0f },
};
translate(triangleB, {kStepX*3.0f, 0.0f, kStepZ*2.0f});
olp = overlap(triangleA, triangleB);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(false);
dde.draw(triangleA);
dde.setColor(olp ? kOverlap : 0xffffffff);
dde.setWireframe(true);
dde.draw(triangleB);
}
}
dde.pop();
dde.end();
// Advance to next frame. Rendering thread will be kicked to

File diff suppressed because it is too large Load Diff

View File

@@ -10,114 +10,112 @@
struct Aabb
{
bx::Vec3 m_min;
bx::Vec3 m_max;
bx::Vec3 min;
bx::Vec3 max;
};
struct Cylinder
{
bx::Vec3 m_pos;
bx::Vec3 m_end;
float m_radius;
bx::Vec3 pos;
bx::Vec3 end;
float radius;
};
struct Capsule
{
bx::Vec3 m_pos;
bx::Vec3 m_end;
float m_radius;
bx::Vec3 pos;
bx::Vec3 end;
float radius;
};
struct Cone
{
bx::Vec3 m_pos;
bx::Vec3 m_end;
float m_radius;
bx::Vec3 pos;
bx::Vec3 end;
float radius;
};
struct Disk
{
bx::Vec3 m_center;
bx::Vec3 m_normal;
float m_radius;
bx::Vec3 center;
bx::Vec3 normal;
float radius;
};
struct Obb
{
float m_mtx[16];
};
struct Ray
{
bx::Vec3 m_pos;
bx::Vec3 m_dir;
float mtx[16];
};
struct Sphere
{
bx::Vec3 m_center;
float m_radius;
bx::Vec3 center;
float radius;
};
struct Tris
struct Triangle
{
bx::Vec3 m_v0;
bx::Vec3 m_v1;
bx::Vec3 m_v2;
bx::Vec3 v0;
bx::Vec3 v1;
bx::Vec3 v2;
};
struct Ray
{
bx::Vec3 pos;
bx::Vec3 dir;
};
struct Hit
{
bx::Vec3 m_pos;
bx::Vec3 m_normal;
float m_dist;
bx::Vec3 pos;
bx::Plane plane;
};
/// Convert axis aligned bounding box to oriented bounding box.
void aabbToObb(Obb& _obb, const Aabb& _aabb);
///
void toAabb(Aabb& _outAabb, const bx::Vec3& _center, const bx::Vec3& _extent);
/// Convert oriented bounding box to axis aligned bounding box.
void toAabb(Aabb& _aabb, const Obb& _obb);
void toAabb(Aabb& _outAabb, const Obb& _obb);
/// Convert sphere to axis aligned bounding box.
void toAabb(Aabb& _aabb, const Sphere& _sphere);
void toAabb(Aabb& _outAabb, const Sphere& _sphere);
/// Convert disk to axis aligned bounding box.
void toAabb(Aabb& _aabb, const Disk& _disk);
void toAabb(Aabb& _outAabb, const Disk& _disk);
/// Convert cylinder to axis aligned bounding box.
void toAabb(Aabb& _aabb, const Cylinder& _cylinder);
void toAabb(Aabb& _outAabb, const Cylinder& _cylinder);
/// Calculate axis aligned bounding box.
void toAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
/// Transform vertices and calculate axis aligned bounding box.
void toAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
/// Expand AABB.
void aabbExpand(Aabb& _aabb, float _factor);
void aabbExpand(Aabb& _outAabb, float _factor);
/// Expand AABB with xyz.
void aabbExpand(Aabb& _aabb, const float* _pos);
void aabbExpand(Aabb& _outAabb, const bx::Vec3& _pos);
/// Calculate surface area of axis aligned bounding box.
float calcAreaAabb(const Aabb& _aabb);
/// Returns 0 is two AABB don't overlap, otherwise returns flags of overlap
/// test.
uint32_t aabbOverlapTest(const Aabb& _aabb0, const Aabb& _aabb1);
/// Convert axis aligned bounding box to oriented bounding box.
void toObb(Obb& _outObb, const Aabb& _aabb);
/// Calculate oriented bounding box.
void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17);
void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17);
/// Calculate maximum bounding sphere.
void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
void calcMaxBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride);
/// Calculate minimum bounding sphere.
void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f);
void calcMinBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f);
/// Returns 6 (near, far, left, right, top, bottom) planes representing frustum planes.
void buildFrustumPlanes(bx::Plane* _planes, const float* _viewProj);
void buildFrustumPlanes(bx::Plane* _outPlanes, const float* _viewProj);
/// Returns point from 3 intersecting planes.
bx::Vec3 intersectPlanes(const bx::Plane& _pa, const bx::Plane& _pb, const bx::Plane& _pc);
@@ -150,6 +148,87 @@ bool intersect(const Ray& _ray, const bx::Plane& _plane, Hit* _hit = NULL);
bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL);
/// Intersect ray / triangle.
bool intersect(const Ray& _ray, const Tris& _triangle, Hit* _hit = NULL);
bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit = NULL);
///
bool overlap(const Sphere& _sphere, const bx::Vec3& _pos);
///
bool overlap(const Sphere& _sphereA, const Sphere& _sphereB);
///
bool overlap(const Sphere& _sphere, const Aabb& _aabb);
///
bool overlap(const Sphere& _sphere, const bx::Plane& _plane);
///
bool overlap(const Sphere& _sphere, const Triangle& _triangle);
///
bool overlap(const Sphere& _sphere, const Cylinder& _cylinder);
///
bool overlap(const Sphere& _sphere, const Capsule& _capsule);
///
bool overlap(const Sphere& _sphere, const Cone& _cone);
///
bool overlap(const Sphere& _sphere, const Disk& _disk);
///
bool overlap(const Sphere& _sphere, const Obb& _obb);
///
bool overlap(const Aabb& _aabb, const bx::Vec3& _pos);
///
bool overlap(const Aabb& _aabbA, const Aabb& _aabbB);
///
bool overlap(const Aabb& _aabb, const bx::Plane& _plane);
///
bool overlap(const Aabb& _aabb, const Triangle& _triangle);
///
bool overlap(const Aabb& _aabb, const Cylinder& _cylinder);
///
bool overlap(const Aabb& _aabb, const Capsule& _capsule);
///
bool overlap(const Aabb& _aabb, const Cone& _cone);
///
bool overlap(const Aabb& _aabb, const Disk& _disk);
///
bool overlap(const Aabb& _aabb, const Obb& _obb);
///
bool overlap(const Triangle& _triangle, const bx::Vec3& _pos);
///
bool overlap(const Triangle& _triangle, const bx::Plane& _plane);
///
bool overlap(const Triangle& _triangleA, const Triangle& _triangleB);
///
bool overlap(const Triangle& _triangle, const Cylinder& _cylinder);
///
bool overlap(const Triangle& _triangle, const Capsule& _capsule);
///
bool overlap(const Triangle& _triangle, const Cone& _cone);
///
bool overlap(const Triangle& _triangle, const Disk& _disk);
///
bool overlap(const Triangle& _triangle, const Obb& _obb);
#endif // BOUNDS_H_HEADER_GUARD

View File

@@ -1420,46 +1420,46 @@ struct DebugDrawEncoderImpl
const Attrib& attrib = m_attrib[m_stack];
if (attrib.m_wireframe)
{
moveTo(_aabb.m_min.x, _aabb.m_min.y, _aabb.m_min.z);
lineTo(_aabb.m_max.x, _aabb.m_min.y, _aabb.m_min.z);
lineTo(_aabb.m_max.x, _aabb.m_max.y, _aabb.m_min.z);
lineTo(_aabb.m_min.x, _aabb.m_max.y, _aabb.m_min.z);
moveTo(_aabb.min.x, _aabb.min.y, _aabb.min.z);
lineTo(_aabb.max.x, _aabb.min.y, _aabb.min.z);
lineTo(_aabb.max.x, _aabb.max.y, _aabb.min.z);
lineTo(_aabb.min.x, _aabb.max.y, _aabb.min.z);
close();
moveTo(_aabb.m_min.x, _aabb.m_min.y, _aabb.m_max.z);
lineTo(_aabb.m_max.x, _aabb.m_min.y, _aabb.m_max.z);
lineTo(_aabb.m_max.x, _aabb.m_max.y, _aabb.m_max.z);
lineTo(_aabb.m_min.x, _aabb.m_max.y, _aabb.m_max.z);
moveTo(_aabb.min.x, _aabb.min.y, _aabb.max.z);
lineTo(_aabb.max.x, _aabb.min.y, _aabb.max.z);
lineTo(_aabb.max.x, _aabb.max.y, _aabb.max.z);
lineTo(_aabb.min.x, _aabb.max.y, _aabb.max.z);
close();
moveTo(_aabb.m_min.x, _aabb.m_min.y, _aabb.m_min.z);
lineTo(_aabb.m_min.x, _aabb.m_min.y, _aabb.m_max.z);
moveTo(_aabb.min.x, _aabb.min.y, _aabb.min.z);
lineTo(_aabb.min.x, _aabb.min.y, _aabb.max.z);
moveTo(_aabb.m_max.x, _aabb.m_min.y, _aabb.m_min.z);
lineTo(_aabb.m_max.x, _aabb.m_min.y, _aabb.m_max.z);
moveTo(_aabb.max.x, _aabb.min.y, _aabb.min.z);
lineTo(_aabb.max.x, _aabb.min.y, _aabb.max.z);
moveTo(_aabb.m_min.x, _aabb.m_max.y, _aabb.m_min.z);
lineTo(_aabb.m_min.x, _aabb.m_max.y, _aabb.m_max.z);
moveTo(_aabb.min.x, _aabb.max.y, _aabb.min.z);
lineTo(_aabb.min.x, _aabb.max.y, _aabb.max.z);
moveTo(_aabb.m_max.x, _aabb.m_max.y, _aabb.m_min.z);
lineTo(_aabb.m_max.x, _aabb.m_max.y, _aabb.m_max.z);
moveTo(_aabb.max.x, _aabb.max.y, _aabb.min.z);
lineTo(_aabb.max.x, _aabb.max.y, _aabb.max.z);
}
else
{
Obb obb;
aabbToObb(obb, _aabb);
draw(Mesh::Cube, obb.m_mtx, 1, false);
toObb(obb, _aabb);
draw(Mesh::Cube, obb.mtx, 1, false);
}
}
void draw(const Cylinder& _cylinder, bool _capsule)
{
drawCylinder(_cylinder.m_pos, _cylinder.m_end, _cylinder.m_radius, _capsule);
drawCylinder(_cylinder.pos, _cylinder.end, _cylinder.radius, _capsule);
}
void draw(const Disk& _disk)
{
drawCircle(_disk.m_normal, _disk.m_center, _disk.m_radius, 0.0f);
drawCircle(_disk.normal, _disk.center, _disk.radius, 0.0f);
}
void draw(const Obb& _obb)
@@ -1467,7 +1467,7 @@ struct DebugDrawEncoderImpl
const Attrib& attrib = m_attrib[m_stack];
if (attrib.m_wireframe)
{
pushTransform(_obb.m_mtx, 1);
pushTransform(_obb.mtx, 1);
moveTo(-1.0f, -1.0f, -1.0f);
lineTo( 1.0f, -1.0f, -1.0f);
@@ -1497,7 +1497,7 @@ struct DebugDrawEncoderImpl
}
else
{
draw(Mesh::Cube, _obb.m_mtx, 1, false);
draw(Mesh::Cube, _obb.mtx, 1, false);
}
}
@@ -1506,15 +1506,15 @@ struct DebugDrawEncoderImpl
const Attrib& attrib = m_attrib[m_stack];
float mtx[16];
bx::mtxSRT(mtx
, _sphere.m_radius
, _sphere.m_radius
, _sphere.m_radius
, _sphere.radius
, _sphere.radius
, _sphere.radius
, 0.0f
, 0.0f
, 0.0f
, _sphere.m_center.x
, _sphere.m_center.y
, _sphere.m_center.z
, _sphere.center.x
, _sphere.center.y
, _sphere.center.z
);
uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
? uint8_t(Mesh::SphereMaxLod)
@@ -1523,6 +1523,29 @@ struct DebugDrawEncoderImpl
draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe);
}
void draw(const Triangle& _triangle)
{
Attrib& attrib = m_attrib[m_stack];
if (attrib.m_wireframe)
{
moveTo(_triangle.v0);
lineTo(_triangle.v1);
lineTo(_triangle.v2);
close();
}
else
{
BX_STATIC_ASSERT(sizeof(DdVertex) == sizeof(bx::Vec3), "");
uint64_t old = attrib.m_state;
attrib.m_state &= ~BGFX_STATE_CULL_MASK;
draw(false, 3, reinterpret_cast<const DdVertex*>(&_triangle.v0.x), 0, NULL);
attrib.m_state = old;
}
}
void setUParams(const Attrib& _attrib, bool _wireframe)
{
const float flip = 0 == (_attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f;
@@ -1643,11 +1666,15 @@ struct DebugDrawEncoderImpl
bgfx::allocTransientIndexBuffer(&tib, numIndices);
bx::memCopy(tib.data, _indices, numIndices * sizeof(uint16_t) );
}
m_encoder->setIndexBuffer(&tib);
}
m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
bgfx::ProgramHandle program = s_dds.m_program[wireframe ? Program::FillMesh : Program::FillLitMesh];
bgfx::ProgramHandle program = s_dds.m_program[wireframe
? Program::FillMesh
: Program::FillLitMesh
];
m_encoder->submit(m_viewId, program);
}
}
@@ -1958,11 +1985,11 @@ struct DebugDrawEncoderImpl
draw(Mesh::Enum(Mesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe);
Sphere sphere;
sphere.m_center = _from;
sphere.m_radius = _radius;
sphere.center = _from;
sphere.radius = _radius;
draw(sphere);
sphere.m_center = _to;
sphere.center = _to;
draw(sphere);
}
else
@@ -2035,10 +2062,10 @@ struct DebugDrawEncoderImpl
const uint32_t num = (_size/2)*2+1;
const float halfExtent = float(_size/2);
const bx::Vec3 umin = bx::mul(udir, -halfExtent);
const bx::Vec3 umax = bx::mul(udir, halfExtent);
const bx::Vec3 vmin = bx::mul(vdir, -halfExtent);
const bx::Vec3 vmax = bx::mul(vdir, halfExtent);
const bx::Vec3 umin = bx::mul(udir, -halfExtent);
const bx::Vec3 umax = bx::mul(udir, halfExtent);
const bx::Vec3 vmin = bx::mul(vdir, -halfExtent);
const bx::Vec3 vmax = bx::mul(vdir, halfExtent);
bx::Vec3 xs = bx::add(_center, bx::add(umin, vmin) );
bx::Vec3 xe = bx::add(_center, bx::add(umax, vmin) );
@@ -2457,9 +2484,14 @@ void DebugDrawEncoder::draw(const Sphere& _sphere)
DEBUG_DRAW_ENCODER(draw(_sphere) );
}
void DebugDrawEncoder::draw(const Triangle& _triangle)
{
DEBUG_DRAW_ENCODER(draw(_triangle) );
}
void DebugDrawEncoder::draw(const Cone& _cone)
{
DEBUG_DRAW_ENCODER(drawCone(_cone.m_pos, _cone.m_end, _cone.m_radius) );
DEBUG_DRAW_ENCODER(drawCone(_cone.pos, _cone.end, _cone.radius) );
}
void DebugDrawEncoder::draw(GeometryHandle _handle)

View File

@@ -137,6 +137,9 @@ struct DebugDrawEncoder
///
void draw(const Sphere& _sphere);
///
void draw(const Triangle& _triangle);
///
void draw(const Cone& _cone);

View File

@@ -375,32 +375,37 @@ namespace ps
const bx::Vec3 vdir = { _mtxView[1]*scale, _mtxView[5]*scale, _mtxView[9]*scale };
PosColorTexCoord0Vertex* vertex = &_outVertices[current*4];
bx::store(&vertex->m_x, bx::sub(bx::sub(pos, udir), vdir) );
aabbExpand(aabb, &vertex->m_x);
const bx::Vec3 ul = bx::sub(bx::sub(pos, udir), vdir);
bx::store(&vertex->m_x, ul);
aabbExpand(aabb, ul);
vertex->m_abgr = abgr;
vertex->m_u = _uv[0];
vertex->m_v = _uv[1];
vertex->m_blend = blend;
++vertex;
bx::store(&vertex->m_x, bx::sub(bx::add(pos, udir), vdir) );
aabbExpand(aabb, &vertex->m_x);
const bx::Vec3 ur = bx::sub(bx::add(pos, udir), vdir);
bx::store(&vertex->m_x, ur);
aabbExpand(aabb, ur);
vertex->m_abgr = abgr;
vertex->m_u = _uv[2];
vertex->m_v = _uv[1];
vertex->m_blend = blend;
++vertex;
bx::store(&vertex->m_x, bx::add(bx::add(pos, udir), vdir) );
aabbExpand(aabb, &vertex->m_x);
const bx::Vec3 br = bx::add(bx::add(pos, udir), vdir);
bx::store(&vertex->m_x, br);
aabbExpand(aabb, br);
vertex->m_abgr = abgr;
vertex->m_u = _uv[2];
vertex->m_v = _uv[3];
vertex->m_blend = blend;
++vertex;
bx::store(&vertex->m_x, bx::add(bx::sub(pos, udir), vdir) );
aabbExpand(aabb, &vertex->m_x);
const bx::Vec3 bl = bx::add(bx::sub(pos, udir), vdir);
bx::store(&vertex->m_x, bl);
aabbExpand(aabb, bl);
vertex->m_abgr = abgr;
vertex->m_u = _uv[0];
vertex->m_v = _uv[3];

View File

@@ -59,14 +59,7 @@ namespace stl = tinystl;
#include "bounds.h"
struct Vector3
{
float x;
float y;
float z;
};
typedef std::vector<Vector3> Vector3Array;
typedef std::vector<bx::Vec3> bx::Vec3Array;
struct Index3
{
@@ -79,12 +72,12 @@ struct Index3
typedef stl::unordered_map<uint64_t, Index3> Index3Map;
struct Triangle
struct TriIndices
{
uint64_t m_index[3];
};
typedef std::vector<Triangle> TriangleArray;
typedef std::vector<TriIndices> TriangleArray;
struct Group
{
@@ -486,9 +479,9 @@ int main(int _argc, const char* _argv[])
// https://en.wikipedia.org/wiki/Wavefront_.obj_file
Vector3Array positions;
Vector3Array normals;
Vector3Array texcoords;
bx::Vec3Array positions;
bx::Vec3Array normals;
bx::Vec3Array texcoords;
Index3Map indexMap;
TriangleArray triangles;
GroupArray groups;
@@ -519,8 +512,8 @@ int main(int _argc, const char* _argv[])
}
else if (0 == bx::strCmp(argv[0], "f") )
{
Triangle triangle;
bx::memSet(&triangle, 0, sizeof(Triangle) );
TriIndices triangle;
bx::memSet(&triangle, 0, sizeof(TriIndices) );
const int numNormals = (int)normals.size();
const int numTexcoords = (int)texcoords.size();
@@ -537,7 +530,7 @@ int main(int _argc, const char* _argv[])
}
else
{
index.m_vbc = 0;
index.m_vbc = 0;
}
{
@@ -638,7 +631,7 @@ int main(int _argc, const char* _argv[])
if (0 == bx::strCmp(argv[0], "vn") )
{
Vector3 normal;
bx::Vec3 normal;
bx::fromString(&normal.x, argv[1]);
bx::fromString(&normal.y, argv[2]);
bx::fromString(&normal.z, argv[3]);
@@ -656,7 +649,7 @@ int main(int _argc, const char* _argv[])
}
else if (0 == bx::strCmp(argv[0], "vt") )
{
Vector3 texcoord;
bx::Vec3 texcoord;
texcoord.y = 0.0f;
texcoord.z = 0.0f;
@@ -699,7 +692,7 @@ int main(int _argc, const char* _argv[])
py *= invW;
pz *= invW;
Vector3 pos;
bx::Vec3 pos;
pos.x = px;
pos.y = py;
pos.z = pz;
@@ -953,7 +946,7 @@ int main(int _argc, const char* _argv[])
material = groupIt->m_material;
}
Triangle& triangle = triangles[tri];
TriIndices& triangle = triangles[tri];
for (uint32_t edge = 0; edge < 3; ++edge)
{
uint64_t hash = triangle.m_index[edge];