@@ -102,7 +102,7 @@ vec3 Debug::ScreenToWorld(vec3 pos, mat4 view, mat4 proj) | |||
} | |||
//Draw stuff in World | |||
//-- LINE | |||
//-- LINE: 3D -2D - 3D_to_2D | |||
void Debug::DrawLine(vec3 a, vec3 b) { Debug::DrawLine(a, b, g_scene->GetLineColor()); } | |||
void Debug::DrawLine(vec2 a, vec3 b, float az) { Debug::DrawLine(a, b, g_scene->GetLineColor(), az); } | |||
void Debug::DrawLine(vec2 a, vec2 b, float az, float bz) { Debug::DrawLine(a, b, g_scene->GetLineColor(), az, bz); } | |||
@@ -110,7 +110,7 @@ void Debug::DrawLine(vec3 a, vec3 b, vec4 color) { g_scene->A | |||
void Debug::DrawLine(vec2 a, vec3 b, vec4 color, float az) { g_scene->AddLine(a, b, color, az); } | |||
void Debug::DrawLine(vec2 a, vec2 b, vec4 color, float az, float bz){ g_scene->AddLine(a, b, color, az, bz); } | |||
//-- ARROW | |||
//-- ARROW: 3D -2D - 3D_to_2D | |||
void Debug::DrawArrow(vec3 a, vec3 b, vec2 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y)); } | |||
void Debug::DrawArrow(vec2 a, vec3 b, vec2 s, float az) { Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), az, b.z); } | |||
void Debug::DrawArrow(vec2 a, vec2 b, vec2 s, float az, float bz) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), az, bz); } | |||
@@ -165,14 +165,24 @@ void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az, float bz) | |||
Debug::DrawLine(b - (z + y).xy, b - (z - y).xy, color, bz - (z + y).z, bz - (z - y).z); | |||
} | |||
//-- BOX | |||
//-- BOX: 3D -2D - 3D_to_2D | |||
void Debug::DrawBox(box3 a) { Debug::DrawBox(a.A, a.B, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(box2 a) { Debug::DrawBox(a.A, a.B, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(box3 a, vec4 color) { Debug::DrawBox(a.A, a.B, color); } | |||
void Debug::DrawBox(box2 a, vec4 color) { Debug::DrawBox(a.A, a.B, color); } | |||
void Debug::DrawBox(vec3 a, vec3 b) { Debug::DrawBox(a, b, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(vec2 a, vec2 b) { Debug::DrawBox(a, b, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(vec2 a, float s) { Debug::DrawBox(a, s, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(vec3 a, vec3 b, vec4 color) { Debug::DrawBox(a, b, mat4::identity, color); } | |||
void Debug::DrawBox(vec2 a, vec2 b, vec4 color) { Debug::DrawBox(a, b, mat2::identity, color); } | |||
void Debug::DrawBox(vec2 a, float s, vec4 color) { Debug::DrawBox(a, s, mat2::identity, color); } | |||
void Debug::DrawBox(box3 a, mat4 transform) { Debug::DrawBox(a.A, a.B, transform, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(box2 a, mat2 transform) { Debug::DrawBox(a.A, a.B, transform, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(box3 a, mat4 transform, vec4 color) { Debug::DrawBox(a.A, a.B, transform, color); } | |||
void Debug::DrawBox(box2 a, mat2 transform, vec4 color) { Debug::DrawBox(a.A, a.B, transform, color); } | |||
void Debug::DrawBox(vec3 a, vec3 b, mat4 transform) { Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(vec2 a, vec2 b, mat2 transform) { Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(vec2 a, float s, mat2 transform) { Debug::DrawBox(a, s, transform, g_scene->GetLineColor()); } | |||
void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color) | |||
{ | |||
vec4 v[8]; | |||
@@ -193,16 +203,44 @@ void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color) | |||
Debug::DrawLine((transform * v[i + 4]).xyz, (transform * v[j + 4]).xyz, color); | |||
} | |||
} | |||
void Debug::DrawBox(vec2 a, vec2 b, mat2 transform, vec4 color) | |||
{ | |||
vec2 v[4]; | |||
v[0] = a; | |||
v[1] = vec2(a.x, b.y); | |||
v[2] = b; | |||
v[3] = vec2(b.x, a.y); | |||
int i = 0; | |||
Debug::DrawLine((transform * v[0]).xy, (transform * v[1]).xy, color); | |||
Debug::DrawLine((transform * v[1]).xy, (transform * v[2]).xy, color); | |||
Debug::DrawLine((transform * v[2]).xy, (transform * v[3]).xy, color); | |||
Debug::DrawLine((transform * v[3]).xy, (transform * v[0]).xy, color); | |||
} | |||
void Debug::DrawBox(vec2 a, float s, mat2 transform, vec4 color) | |||
{ | |||
vec2 b = s * vec2(1.f, g_renderer->GetXYRatio()); | |||
Debug::DrawBox(a - b, a + b, transform, color); | |||
} | |||
//-- CIRCLE | |||
void Debug::DrawCircle(vec2 a, float s) { Debug::DrawCircle(a, s * vec2(1.f, g_renderer->GetXYRatio()), g_scene->GetLineColor()); } | |||
void Debug::DrawCircle(vec3 a, vec3 n) { Debug::DrawCircle(a, n, g_scene->GetLineColor()); } | |||
void Debug::DrawCircle(vec2 a, vec2 s) { Debug::DrawCircle(a, s * vec2(1.f, g_renderer->GetXYRatio()), g_scene->GetLineColor()); } | |||
void Debug::DrawCircle(vec3 a, vec3 x, vec3 y) { Debug::DrawCircle(a, x, y, g_scene->GetLineColor()); } | |||
void Debug::DrawCircle(vec2 a, vec2 x, vec2 y) { Debug::DrawCircle(a, x, y, g_scene->GetLineColor()); } | |||
void Debug::DrawCircle(vec3 a, vec3 n, vec4 color) | |||
{ | |||
vec3 x = orthogonal(n); | |||
vec3 y = cross(normalize(n), normalize(x)) * length(n); | |||
DrawCircle(a, x, y, color); | |||
} | |||
void Debug::DrawCircle(vec2 a, vec2 s, vec4 color) | |||
{ | |||
vec2 x = vec2::axis_x * s.x; | |||
vec2 y = vec2::axis_y * s.y; | |||
DrawCircle(a, x, y, color); | |||
} | |||
//-- | |||
void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color) | |||
{ | |||
@@ -221,6 +259,24 @@ void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color) | |||
Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color); | |||
} | |||
} | |||
//-- | |||
void Debug::DrawCircle(vec2 a, vec2 x, vec2 y, vec4 color) | |||
{ | |||
float size = F_PI * 2.f * lol::max(length(x), length(y)); | |||
int segment_nb = lol::max(1, (int)((size * .25f) / g_scene->GetLineSegmentSize())); | |||
for (int i = 0; i < segment_nb; i++) | |||
{ | |||
float a0 = (((float)i) / (float)segment_nb) * F_PI_2; | |||
float a1 = (((float)i + 1) / (float)segment_nb) * F_PI_2; | |||
vec2 p0 = vec2(lol::cos(a0), lol::sin(a0)); | |||
vec2 p1 = vec2(lol::cos(a1), lol::sin(a1)); | |||
Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, color); | |||
Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, color); | |||
Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, color); | |||
Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color); | |||
} | |||
} | |||
//-- SPHERE | |||
void Debug::DrawSphere(vec3 a, float s) { Debug::DrawSphere(a, s, g_scene->GetLineColor()); } | |||
@@ -249,6 +249,22 @@ ibox2 Renderer::GetViewport() const | |||
return m_data->m_viewport; | |||
} | |||
float Renderer::GetXYRatio() const | |||
{ | |||
ibox2 a = GetViewport(); | |||
box2 b(a.A, a.B); | |||
vec2 s = b.B - b.A; | |||
return s.x / s.y; | |||
} | |||
float Renderer::GetYXRatio() const | |||
{ | |||
ibox2 a = GetViewport(); | |||
box2 b(a.A, a.B); | |||
vec2 s = b.B - b.A; | |||
return s.y / s.x; | |||
} | |||
/* | |||
* Clear color | |||
*/ | |||
@@ -133,6 +133,38 @@ public: | |||
return m_data[n]; | |||
} | |||
inline Element& operator[](uint32_t n) | |||
{ | |||
/* Allow array[0] even if size is zero so that people can | |||
* always use &array[0] to get a pointer to the data. */ | |||
ASSERT(n >= 0); | |||
ASSERT((uint32_t)n < (uint32_t)m_count || (!n && !m_count)); | |||
return m_data[n]; | |||
} | |||
inline Element const& operator[](uint32_t n) const | |||
{ | |||
ASSERT(n >= 0); | |||
ASSERT(n < m_count || (!n && !m_count)); | |||
return m_data[n]; | |||
} | |||
inline Element& operator[](uint64_t n) | |||
{ | |||
/* Allow array[0] even if size is zero so that people can | |||
* always use &array[0] to get a pointer to the data. */ | |||
ASSERT(n >= 0); | |||
ASSERT((uint64_t)n < (uint64_t)m_count || (!n && !m_count)); | |||
return m_data[n]; | |||
} | |||
inline Element const& operator[](uint64_t n) const | |||
{ | |||
ASSERT(n >= 0); | |||
ASSERT(n < m_count || (!n && !m_count)); | |||
return m_data[n]; | |||
} | |||
inline Element& Last() | |||
{ | |||
ASSERT(m_count > 0); | |||
@@ -128,7 +128,7 @@ public: | |||
using namespace std; | |||
char const *tmp = strchr(C(), token); | |||
return tmp ? (int)(intptr_t)(tmp - C()) : -1; | |||
return tmp ? (int)(intptr_t)(tmp - C()) : INDEX_NONE; | |||
} | |||
int IndexOf(char const* token) const | |||
@@ -136,7 +136,12 @@ public: | |||
using namespace std; | |||
char const *tmp = strstr(C(), token); | |||
return tmp ? (int)(intptr_t)(tmp - C()) : -1; | |||
return tmp ? (int)(intptr_t)(tmp - C()) : INDEX_NONE; | |||
} | |||
bool Contains(String const &s) const | |||
{ | |||
return IndexOf(s.C()) != INDEX_NONE; | |||
} | |||
int LastIndexOf(char token) const | |||
@@ -144,7 +149,7 @@ public: | |||
using namespace std; | |||
char const *tmp = strrchr(C(), token); | |||
return tmp ? (int)(intptr_t)(tmp - C()) : -1; | |||
return tmp ? (int)(intptr_t)(tmp - C()) : INDEX_NONE; | |||
} | |||
int Replace(const char old_token, const char new_token, bool all_occurence=false) | |||
@@ -206,6 +211,14 @@ public: | |||
&& memcmp(C() + Count() - s.Count(), s.C(), s.Count()) == 0; | |||
} | |||
bool IsAlpha() | |||
{ | |||
for (int i = 0; i < m_count; i++) | |||
if (m_data[i] != '\0' && (m_data[i] < '0' || '9' < m_data[i])) | |||
return false; | |||
return true; | |||
} | |||
inline String operator +(String const &s) const | |||
{ | |||
String ret(*this); | |||
@@ -64,18 +64,35 @@ void DrawArrow(vec2 a, vec3 b, vec3 s, vec4 color, float az=-1.f); | |||
void DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az=-1.f, float bz=-1.f); | |||
//-- BOX | |||
void DrawBox(box3 a); | |||
void DrawBox(box2 a); | |||
void DrawBox(box3 a, vec4 color); | |||
void DrawBox(box2 a, vec4 color); | |||
void DrawBox(vec3 a, vec3 b); | |||
void DrawBox(vec2 a, vec2 b); | |||
void DrawBox(vec2 a, float s); | |||
void DrawBox(vec3 a, vec3 b, vec4 color); | |||
void DrawBox(vec2 a, vec2 b, vec4 color); | |||
void DrawBox(vec2 a, float s, vec4 color); | |||
void DrawBox(box3 a, mat4 transform); | |||
void DrawBox(box2 a, mat2 transform); | |||
void DrawBox(box3 a, mat4 transform, vec4 color); | |||
void DrawBox(box2 a, mat2 transform, vec4 color); | |||
void DrawBox(vec3 a, vec3 b, mat4 transform); | |||
void DrawBox(vec2 a, vec2 b, mat2 transform); | |||
void DrawBox(vec2 a, float s, mat2 transform); | |||
void DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color); | |||
void DrawBox(vec2 a, vec2 b, mat2 transform, vec4 color); | |||
void DrawBox(vec2 a, float s, mat2 transform, vec4 color); | |||
//-- CIRCLE | |||
void DrawCircle(vec2 a, float s); | |||
void DrawCircle(vec3 a, vec3 n); | |||
void DrawCircle(vec2 a, vec2 s); | |||
void DrawCircle(vec3 a, vec3 n, vec4 color); | |||
void DrawCircle(vec2 a, vec2 s, vec4 color); | |||
void DrawCircle(vec3 a, vec3 x, vec3 y); | |||
void DrawCircle(vec2 a, vec2 x, vec2 y); | |||
void DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color); | |||
void DrawCircle(vec2 a, vec2 x, vec2 y, vec4 color); | |||
//-- SPHERE | |||
void DrawSphere(vec3 a, float s); | |||
void DrawSphere(vec3 a, float s, vec4 color); | |||
@@ -177,6 +177,8 @@ public: | |||
public: | |||
void SetViewport(ibox2 viewport); | |||
ibox2 GetViewport() const; | |||
float GetXYRatio() const; | |||
float GetYXRatio() const; | |||
void SetClearColor(vec4 color); | |||
vec4 GetClearColor() const; | |||
@@ -153,12 +153,32 @@ template <typename T> struct Box3 | |||
class TestEpsilon | |||
{ | |||
private: | |||
static float g_test_epsilon; | |||
float m_epsilon; | |||
float m_value; | |||
public: | |||
static inline float Get() { return g_test_epsilon; } | |||
static inline void Set(float epsilon=.0001f) { g_test_epsilon = lol::max(epsilon, .0f); } | |||
TestEpsilon() { m_value = 0.f; m_epsilon = .0001f; } | |||
static float Get(); | |||
static void Set(float epsilon=.0001f); | |||
static const TestEpsilon& F(float value); | |||
private: | |||
float Minus() const; | |||
float Plus() const; | |||
public: | |||
bool operator==(float value) const; | |||
bool operator!=(float value) const; | |||
bool operator<(float value) const; | |||
bool operator<=(float value) const; | |||
bool operator>(float value) const; | |||
bool operator>=(float value) const; | |||
}; | |||
bool operator==(float value, const TestEpsilon& epsilon); | |||
bool operator!=(float value, const TestEpsilon& epsilon); | |||
bool operator<(float value, const TestEpsilon& epsilon); | |||
bool operator<=(float value, const TestEpsilon& epsilon); | |||
bool operator>(float value, const TestEpsilon& epsilon); | |||
bool operator>=(float value, const TestEpsilon& epsilon); | |||
//-- | |||
static inline bool TestAABBVsAABB(box2 const &b1, box2 const &b2) | |||
{ | |||
vec2 dist = 0.5f * (b1.A - b2.A + b1.B - b2.B); | |||
@@ -25,7 +25,24 @@ using namespace std; | |||
namespace lol | |||
{ | |||
//Test epsilon stuff | |||
float TestEpsilon::g_test_epsilon = .0001f; | |||
TestEpsilon g_test_epsilon; | |||
float TestEpsilon::Get() { return g_test_epsilon.m_epsilon; } | |||
void TestEpsilon::Set(float epsilon) { g_test_epsilon.m_epsilon = lol::max(epsilon, .0f); } | |||
const TestEpsilon& TestEpsilon::F(float value) { g_test_epsilon.m_value = value; return g_test_epsilon; } | |||
float TestEpsilon::Minus()const { return m_value - m_epsilon; } | |||
float TestEpsilon::Plus() const { return m_value + m_epsilon; } | |||
bool TestEpsilon::operator==(float value)const { return (Minus() <= value && value <= Plus()); } | |||
bool TestEpsilon::operator!=(float value)const { return (value < Minus() || Plus() < value); } | |||
bool TestEpsilon::operator<(float value) const { return (value < Minus()); } | |||
bool TestEpsilon::operator<=(float value)const { return (value <= Plus()); } | |||
bool TestEpsilon::operator>(float value) const { return (value > Plus()); } | |||
bool TestEpsilon::operator>=(float value)const { return (value >= Minus()); } | |||
bool operator==(float value, const TestEpsilon& epsilon) { return epsilon == value; } | |||
bool operator!=(float value, const TestEpsilon& epsilon) { return epsilon != value; } | |||
bool operator<(float value, const TestEpsilon& epsilon) { return epsilon < value; } | |||
bool operator<=(float value, const TestEpsilon& epsilon) { return epsilon <= value; } | |||
bool operator>(float value, const TestEpsilon& epsilon) { return epsilon > value; } | |||
bool operator>=(float value, const TestEpsilon& epsilon) { return epsilon >= value; } | |||
// Line/triangle : sets isec_p as the intersection point & return true if ok. | |||
bool TestRayVsTriangle(vec3 const &ray_point, vec3 const &ray_dir, | |||