DebugShape& DebugDrawer::DrawSphere(const Sphere& sphere) { // To access the camera's position for the horizon disc calculation use mApplication->mCamera.mTranslation DebugShape& shape = GetNewShape(); int it; std::vector<LineSegment> segments[4]; segments[0] = makeCircle(sphere.mCenter, Math::Vector3(1.0f, 0.0f, 0.0f), sphere.mRadius); segments[1] = makeCircle(sphere.mCenter, Math::Vector3(0.0f, 1.0f, 0.0f), sphere.mRadius); segments[2] = makeCircle(sphere.mCenter, Math::Vector3(0.0f, 0.0f, 1.0f), sphere.mRadius); if(mApplication) { //horizon circle Math::Vector3 dVec = sphere.mCenter - mApplication->mCamera.mTranslation; Math::Vector3 dVecNorm = dVec.Normalized(); float radSquared = sphere.mRadius * sphere.mRadius; float lLen = Math::Sqrt(dVec.LengthSq() - radSquared); float rPrime = (sphere.mRadius * lLen) / dVec.Length(); float z = radSquared - (rPrime * rPrime); Math::Vector3 newCenter = mApplication->mCamera.mTranslation + (dVec - (dVecNorm * z)); segments[3] = makeCircle(newCenter, dVecNorm, rPrime); } for(int i = 0; i < 4; ++i) { shape.mSegments.insert(shape.mSegments.end(), segments[i].begin(), segments[i].end()); } return shape; }
bool BarycentricCoordinates(const Vector3& point, const Vector3& a, const Vector3& b, float& u, float& v, float epsilon) { if(a != b) { Math::Vector3 ba = a - b; float baLen = ba.Length(); Math::Vector3 normal = ba / baLen; u = normal.Dot(point - b) / baLen; v = 1.0f - u; if(u == Math::Clamp(u, -epsilon, 1.0f + epsilon)) { if(v == Math::Clamp(v, -epsilon, 1.0f + epsilon)) { return true; } } } return false; }