From 7c9f96e426d0a745abd11665f66b6778ca6462f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Sun, 16 Feb 2014 19:28:07 +0000 Subject: [PATCH] Added Debug::DrawViewProj --- src/debug/lines.cpp | 57 ++++++++++++++++++++++++++++++++++++++++--- src/lol/debug/lines.h | 2 ++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/debug/lines.cpp b/src/debug/lines.cpp index 982a294f..a725e119 100644 --- a/src/debug/lines.cpp +++ b/src/debug/lines.cpp @@ -36,11 +36,62 @@ void Debug::DrawBox(vec3 a, vec3 b, vec4 color) { int j = ((i & 1) << 1) | ((i >> 1) ^ 1); - g_scene->AddLine(v[i], v[i + 4], color); - g_scene->AddLine(v[i], v[j], color); - g_scene->AddLine(v[i + 4], v[j + 4], color); + Debug::DrawLine(v[i], v[i + 4], color); + Debug::DrawLine(v[i], v[j], color); + Debug::DrawLine(v[i + 4], v[j + 4], color); } } +void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color) +{ + mat4 view_proj = proj * view; + + //Pos to center + vec3 p0 = (inverse(view) * vec4(vec3::zero, 1.f)).xyz; + + //Near plane + vec4 p[4] = { inverse(view_proj) * vec4(-1.f, 1.f, -1.f, 1.f), + inverse(view_proj) * vec4( 1.f, 1.f, -1.f, 1.f), + inverse(view_proj) * vec4( 1.f,-1.f, -1.f, 1.f), + inverse(view_proj) * vec4(-1.f,-1.f, -1.f, 1.f) }; + for (int i = 0; i < 4; i++) + p[i] = p[i] / p[i].w; + + //Draw near + for (int i = 0; i < 4; i++) + Debug::DrawLine(p[i].xyz, p0, color); + + Debug::DrawViewProj(view_proj, color); +} + +void Debug::DrawViewProj(mat4 view_proj, vec4 color) +{ + //Near plane + vec4 p[8] = { inverse(view_proj) * vec4(-1.f, 1.f, 1.f, 1.f), + inverse(view_proj) * vec4( 1.f, 1.f, 1.f, 1.f), + inverse(view_proj) * vec4( 1.f,-1.f, 1.f, 1.f), + inverse(view_proj) * vec4(-1.f,-1.f, 1.f, 1.f), + inverse(view_proj) * vec4(-1.f, 1.f,-1.f, 1.f), + inverse(view_proj) * vec4( 1.f, 1.f,-1.f, 1.f), + inverse(view_proj) * vec4( 1.f,-1.f,-1.f, 1.f), + inverse(view_proj) * vec4(-1.f,-1.f,-1.f, 1.f) + }; + for (int i = 0; i < 8; i++) + p[i] = p[i] / p[i].w; + + //Draw near + for (int i = 0; i < 4; i++) + Debug::DrawLine(p[i].xyz, p[(i + 1) % 4].xyz, color); + //Draw far + for (int i = 4; i < 8; i++) + Debug::DrawLine(p[i].xyz, p[(i - 4 + 1) % 4 + 4].xyz, color); + //Draw near to far + for (int i = 0; i < 4; i++) + Debug::DrawLine(p[i].xyz, p[i + 4].xyz, color); + //Draw diagonal + for (int i = 2; i < 6; i++) + Debug::DrawLine(p[i].xyz, p[i + ((i < 4)?(-2):(+2))].xyz, color); +} + } /* namespace lol */ diff --git a/src/lol/debug/lines.h b/src/lol/debug/lines.h index bd47a3eb..cb182e76 100644 --- a/src/lol/debug/lines.h +++ b/src/lol/debug/lines.h @@ -26,6 +26,8 @@ namespace Debug void DrawLine(vec3 a, vec3 b, vec4 color); void DrawBox(vec3 a, vec3 b, vec4 color); +void DrawViewProj(mat4 view_proj, vec4 color); +void DrawViewProj(mat4 view, mat4 proj, vec4 color); } /* namespace Debug */