From 783db660790e54cebaa2c32868ba4aac3c065145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 28 Sep 2017 21:50:11 -0700 Subject: [PATCH] Fixed AABB hit normal. --- examples/29-debugdraw/debugdraw.cpp | 19 ++++++---- examples/common/bounds.cpp | 15 ++++---- examples/common/debugdraw/debugdraw.cpp | 46 +++++++++++++++---------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/examples/29-debugdraw/debugdraw.cpp b/examples/29-debugdraw/debugdraw.cpp index 149c4d5f0..ce7126242 100644 --- a/examples/29-debugdraw/debugdraw.cpp +++ b/examples/29-debugdraw/debugdraw.cpp @@ -175,6 +175,7 @@ public: { 5.0f, 1.0f, 1.0f }, { 10.0f, 5.0f, 5.0f }, }; + ddSetWireframe(true); ddSetColor(intersect(ray, aabb) ? selected : 0xff00ff00); ddDraw(aabb); ddPop(); @@ -189,9 +190,12 @@ public: bx::mtxSRT(obb.m_mtx, 1.0f, 1.0f, 1.0f, time*0.23f, time, 0.0f, 3.0f, 0.0f, 0.0f); - toAabb(aabb, obb); - ddSetColor(0xff0000ff); - ddDraw(aabb); + ddPush(); + toAabb(aabb, obb); + ddSetWireframe(true); + ddSetColor(0xff0000ff); + ddDraw(aabb); + ddPop(); ddSetWireframe(false); ddSetColor(intersect(ray, obb) ? selected : 0xffffffff); @@ -316,9 +320,12 @@ public: ddSetColor(intersect(ray, cylinder) ? selected : 0xffffffff); ddDraw(cylinder); - toAabb(aabb, cylinder); - ddSetColor(0xff0000ff); - ddDraw(aabb); + ddPush(); + toAabb(aabb, cylinder); + ddSetWireframe(true); + ddSetColor(0xff0000ff); + ddDraw(aabb); + ddPop(); ddPop(); diff --git a/examples/common/bounds.cpp b/examples/common/bounds.cpp index e0c195843..a333b019d 100644 --- a/examples/common/bounds.cpp +++ b/examples/common/bounds.cpp @@ -523,9 +523,9 @@ bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit) if (NULL != _hit) { - _hit->m_normal[0] = float( (min[0] == tmin) - (max[0] == tmin) ); - _hit->m_normal[1] = float( (min[1] == tmin) - (max[1] == tmin) ); - _hit->m_normal[2] = float( (min[2] == tmin) - (max[2] == tmin) ); + _hit->m_normal[0] = float( (t1[0] == tmin) - (t0[0] == tmin) ); + _hit->m_normal[1] = float( (t1[1] == tmin) - (t0[1] == tmin) ); + _hit->m_normal[2] = float( (t1[2] == tmin) - (t0[2] == tmin) ); _hit->m_dist = tmin; getPointAt(_hit->m_pos, _ray, tmin); @@ -779,11 +779,11 @@ bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit) return hit; } - float tmp[3]; - getPointAt(tmp, _ray, tt); + float hitPos[3]; + getPointAt(hitPos, _ray, tt); float point[3]; - bx::vec3Sub(point, tmp, _cone.m_pos); + bx::vec3Sub(point, hitPos, _cone.m_pos); const float hh = bx::vec3Dot(normal, point); @@ -800,12 +800,13 @@ bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit) { _hit->m_dist = tt; - bx::vec3Move(_hit->m_pos, point); + bx::vec3Move(_hit->m_pos, hitPos); const float scale = hh / bx::vec3Dot(point, point); float pointScaled[3]; bx::vec3Mul(pointScaled, point, scale); + float tmp[3]; bx::vec3Sub(tmp, pointScaled, normal); bx::vec3Norm(_hit->m_normal, tmp); } diff --git a/examples/common/debugdraw/debugdraw.cpp b/examples/common/debugdraw/debugdraw.cpp index 8f0a0f159..92ddee58f 100644 --- a/examples/common/debugdraw/debugdraw.cpp +++ b/examples/common/debugdraw/debugdraw.cpp @@ -1103,29 +1103,39 @@ struct DebugDraw void draw(const Aabb& _aabb) { - moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]); - lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]); - lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]); - lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]); - close(); + const Attrib& attrib = m_attrib[m_stack]; + if (attrib.m_wireframe) + { + moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]); + lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]); + lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]); + lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]); + close(); - moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]); - lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]); - lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]); - lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]); - close(); + moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]); + lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]); + lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]); + lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]); + close(); - moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]); - lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]); + moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]); + lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]); - moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]); - lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]); + moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]); + lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]); - moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]); - lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]); + moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]); + lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]); - moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]); - lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]); + moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]); + lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]); + } + else + { + Obb obb; + aabbToObb(obb, _aabb); + draw(Mesh::Cube, obb.m_mtx, 1, false); + } } void draw(const Cylinder& _cylinder, bool _capsule)