Przeglądaj źródła

Added 2D to 3D DebugLines

undefined
Benjamin ‘Touky’ Huet Sam Hocevar <sam@hocevar.net> 10 lat temu
rodzic
commit
dbb1d02aa6
5 zmienionych plików z 182 dodań i 66 usunięć
  1. +121
    -52
      src/debug/lines.cpp
  2. +6
    -3
      src/gpu/line.lolfx
  3. +31
    -0
      src/lol/debug/lines.h
  4. +22
    -11
      src/scene.cpp
  5. +2
    -0
      src/scene.h

+ 121
- 52
src/debug/lines.cpp Wyświetl plik

@@ -56,41 +56,123 @@ void Debug::DrawSetup(float new_time, int new_mask, float segment_size, vec4 col
Debug::DrawSetupColor(color);
}

//Draw stuff
void Debug::DrawLine(vec3 a, vec3 b)
//Screen to world conversion
vec3 Debug::WorldToScreen(vec3 pos)
{
Debug::DrawLine(a, b, g_scene->GetLineColor());
return Debug::WorldToScreen(vec4(pos, 1.f));
}
void Debug::DrawLine(vec3 a, vec3 b, vec4 color)
{ g_scene->AddLine(a, b, color); }

//--
void Debug::DrawBox(box3 a)
{ DrawBox(a.A, a.B, g_scene->GetLineColor()); }
void Debug::DrawBox(box3 a, vec4 color)
vec3 Debug::WorldToScreen(vec3 pos, mat4 view_proj)
{
return Debug::WorldToScreen(vec4(pos, 1.f), view_proj);
}
vec3 Debug::WorldToScreen(vec4 pos)
{
DrawBox(a.A, a.B, color);
if (!g_scene || !g_scene->GetCamera())
return vec3::zero;
mat4 const view_proj = g_scene->GetCamera()->GetProjection() * g_scene->GetCamera()->GetView();
return Debug::WorldToScreen(pos, view_proj);
}
vec3 Debug::WorldToScreen(vec4 pos, mat4 view_proj)
{
vec4 screen_pos = view_proj * pos;
return (screen_pos / screen_pos.w).xyz;
}

//--
void Debug::DrawBox(vec3 a, vec3 b)
{ Debug::DrawBox(a, b, g_scene->GetLineColor()); }
void Debug::DrawBox(vec3 a, vec3 b, vec4 color)
vec3 Debug::ScreenToWorld(vec2 pos, float z)
{ return Debug::ScreenToWorld(vec3(pos, z)); }
vec3 Debug::ScreenToWorld(vec3 pos)
{
Debug::DrawBox(a, b, mat4::identity, color);
if (!g_scene || !g_scene->GetCamera())
return vec3::zero;
mat4 const inv_view_proj = inverse(g_scene->GetCamera()->GetProjection() * g_scene->GetCamera()->GetView());
return Debug::ScreenToWorld(pos, inv_view_proj);
}
vec3 Debug::ScreenToWorld(vec2 pos, mat4 inv_view_proj, float z)
{ return Debug::ScreenToWorld(vec3(pos, z), inv_view_proj); }
vec3 Debug::ScreenToWorld(vec3 pos, mat4 inv_view_proj)
{
vec4 screen_pos = inv_view_proj * vec4(pos, 1.f);
return (screen_pos / screen_pos.w).xyz;
}
vec3 Debug::ScreenToWorld(vec3 pos, mat4 view, mat4 proj)
{
vec4 screen_pos = inverse(proj) * vec4(pos, 1.f);
return (inverse(view) * (screen_pos / screen_pos.w)).xyz;
}

//--
void Debug::DrawBox(box3 a, mat4 transform)
{ DrawBox(a.A, a.B, transform, g_scene->GetLineColor()); }
void Debug::DrawBox(box3 a, mat4 transform, vec4 color)
//Draw stuff in World
//-- LINE
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); }
void Debug::DrawLine(vec3 a, vec3 b, vec4 color) { g_scene->AddLine(a, b, color); }
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
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); }
void Debug::DrawArrow(vec3 a, vec3 b, vec3 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), g_scene->GetLineColor()); }
void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, float az) { Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), g_scene->GetLineColor(), az, b.z); }
void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, float az, float bz) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), g_scene->GetLineColor(), az, bz); }
void Debug::DrawArrow(vec3 a, vec3 b, vec3 s, vec4 color)
{
vec3 z = s.x * normalize(b - a);
vec3 x = s.z * orthonormal(b - a);
vec3 y = s.y * cross(normalize(x), normalize(z));

Debug::DrawLine(a, b, color);
Debug::DrawLine(b, b - z + x, color);
Debug::DrawLine(b, b - z - x, color);
Debug::DrawLine(b, b - z + y, color);
Debug::DrawLine(b, b - z - y, color);

Debug::DrawLine(b - z + x, b - z + y, color);
Debug::DrawLine(b - z + x, b - z - y, color);
Debug::DrawLine(b - z - x, b - z + y, color);
Debug::DrawLine(b - z - x, b - z - y, color);

Debug::DrawLine(b - z + x, b - z - x, color);
Debug::DrawLine(b - z + y, b - z - y, color);
}
void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, vec4 color, float az)
{
DrawBox(a.A, a.B, transform, color);
vec3 bn = Debug::WorldToScreen(b);
DrawArrow(a, bn.xy, s, color, az, bn.z);
}
void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az, float bz)
{
vec3 an = vec3(a, az);
vec3 bn = vec3(b, bz);
vec3 z = s.x * normalize(bn - an);
vec3 x = s.z * orthonormal(bn - an);
vec3 y = s.y * cross(normalize(x), normalize(z));

Debug::DrawLine(a, b, color, az, bz);
Debug::DrawLine(b, b - (z + x).xy, color, bz, bz - (z + x).z);
Debug::DrawLine(b, b - (z - x).xy, color, bz, bz - (z - x).z);
Debug::DrawLine(b, b - (z + y).xy, color, bz, bz - (z + y).z);
Debug::DrawLine(b, b - (z - y).xy, color, bz, bz - (z - y).z);

Debug::DrawLine(b - (z + x).xy, b - (z + y).xy, color, bz - (z + x).z, bz - (z + y).z);
Debug::DrawLine(b - (z + x).xy, b - (z - y).xy, color, bz - (z + x).z, bz - (z - y).z);
Debug::DrawLine(b - (z - x).xy, b - (z + y).xy, color, bz - (z - x).z, bz - (z + y).z);
Debug::DrawLine(b - (z - x).xy, b - (z - y).xy, color, bz - (z - x).z, bz - (z - y).z);

Debug::DrawLine(b - (z + x).xy, b - (z - x).xy, color, bz - (z + x).z, bz - (z - x).z);
Debug::DrawLine(b - (z + y).xy, b - (z - y).xy, color, bz - (z + y).z, bz - (z - y).z);
}

//--
void Debug::DrawBox(vec3 a, vec3 b, mat4 transform)
{ Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); }
//-- BOX
void Debug::DrawBox(box3 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(vec3 a, vec3 b) { Debug::DrawBox(a, b, g_scene->GetLineColor()); }
void Debug::DrawBox(vec3 a, vec3 b, vec4 color) { Debug::DrawBox(a, b, mat4::identity, color); }
void Debug::DrawBox(box3 a, mat4 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(vec3 a, vec3 b, mat4 transform) { Debug::DrawBox(a, b, transform, g_scene->GetLineColor()); }
void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color)
{
vec4 v[8];
@@ -112,19 +194,16 @@ void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color)
}
}

//--
void Debug::DrawCircle(vec3 a, vec3 n)
{ Debug::DrawCircle(a, n, g_scene->GetLineColor()); }
//-- CIRCLE
void Debug::DrawCircle(vec3 a, vec3 n) { Debug::DrawCircle(a, n, g_scene->GetLineColor()); }
void Debug::DrawCircle(vec3 a, vec3 x, vec3 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(vec3 a, vec3 x, vec3 y)
{ Debug::DrawCircle(a, x, y, g_scene->GetLineColor()); }
void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color)
{
float size = F_PI * 2.f * lol::max(length(x), length(y));
@@ -143,41 +222,33 @@ void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color)
}
}

//--
void Debug::DrawSphere(vec3 a, float s)
{ Debug::DrawSphere(a, s, g_scene->GetLineColor()); }
void Debug::DrawSphere(vec3 a, float s, vec4 color)
{
Debug::DrawSphere(a, vec3::axis_x * s, vec3::axis_y * s, vec3::axis_z * s, color);
}

//--
void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z)
{ Debug::DrawSphere(a, x, y, z, g_scene->GetLineColor()); }
//-- SPHERE
void Debug::DrawSphere(vec3 a, float s) { Debug::DrawSphere(a, s, g_scene->GetLineColor()); }
void Debug::DrawSphere(vec3 a, float s, vec4 color) { Debug::DrawSphere(a, vec3::axis_x * s, vec3::axis_y * s, vec3::axis_z * s, color); }
void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z) { Debug::DrawSphere(a, x, y, z, g_scene->GetLineColor()); }
void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, vec4 color)
{
Debug::DrawCircle(a, x, y, color);
Debug::DrawCircle(a, x, (y + z) * .707f, color);
Debug::DrawCircle(a, x, (y - z) * .707f, color);
Debug::DrawCircle(a, x, z, color);
Debug::DrawCircle(a, z, x, color);
Debug::DrawCircle(a, z, (x + y) * .707f, color);
Debug::DrawCircle(a, z, (x - y) * .707f, color);
Debug::DrawCircle(a, y, z, color);
Debug::DrawCircle(a, y, (z + x) * .707f, color);
Debug::DrawCircle(a, y, (z - x) * .707f, color);
}

//--
void Debug::DrawCapsule(vec3 a, float s, vec3 h)
{ Debug::DrawCapsule(a, s, h, g_scene->GetLineColor()); }
//-- CAPSULE
void Debug::DrawCapsule(vec3 a, float s, vec3 h) { Debug::DrawCapsule(a, s, h, g_scene->GetLineColor()); }
void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h) { Debug::DrawCapsule(a, x, y, z, h, g_scene->GetLineColor()); }
void Debug::DrawCapsule(vec3 a, float s, vec3 h, vec4 color)
{
vec3 x = orthonormal(h) * s;
vec3 y = cross(normalize(h), normalize(x)) * s;
Debug::DrawCapsule(a, x, y, normalize(h) * s, h, color);
}

//--
void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h)
{ Debug::DrawCapsule(a, x, y, z, h, g_scene->GetLineColor()); }
void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color)
{
float size = F_PI * 2.f * lol::max(length(x), length(y));
@@ -204,9 +275,9 @@ void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color)
}
}

//--
void Debug::DrawViewProj(mat4 view, mat4 proj)
{ Debug::DrawViewProj(view, proj, g_scene->GetLineColor()); }
//-- VIEW PROJ
void Debug::DrawViewProj(mat4 view, mat4 proj) { Debug::DrawViewProj(view, proj, g_scene->GetLineColor()); }
void Debug::DrawViewProj(mat4 view_proj) { Debug::DrawViewProj(view_proj, g_scene->GetLineColor()); }
void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color)
{
mat4 const view_proj = proj * view;
@@ -231,8 +302,6 @@ void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color)
}

//--
void Debug::DrawViewProj(mat4 view_proj)
{ Debug::DrawViewProj(view_proj, g_scene->GetLineColor()); }
void Debug::DrawViewProj(mat4 view_proj, vec4 color)
{
//Near plane


+ 6
- 3
src/gpu/line.lolfx Wyświetl plik

@@ -2,7 +2,7 @@

#version 130

attribute vec3 in_Position;
attribute vec4 in_Position;
attribute vec4 in_Color;
varying vec4 pass_Color;

@@ -11,8 +11,11 @@ uniform mat4 u_view;

void main()
{
gl_Position = u_projection * u_view
* vec4(in_Position, 1.0);
if (in_Position.w > 0.5)
gl_Position = vec4(in_Position.xyz, 1.0);
else
gl_Position = u_projection * u_view
* vec4(in_Position.xyz, 1.0);
pass_Color = in_Color;
}



+ 31
- 0
src/lol/debug/lines.h Wyświetl plik

@@ -33,9 +33,36 @@ void DrawSetupSegment(float segment_size);
void DrawSetupColor(vec4 color);
void DrawSetup(float new_time, int new_mask, float segment_size, vec4 color);

//Screen to world conversion
vec3 WorldToScreen(vec3 pos);
vec3 WorldToScreen(vec3 pos, mat4 view_proj);
vec3 WorldToScreen(vec4 pos);
vec3 WorldToScreen(vec4 pos, mat4 view_proj);
vec3 ScreenToWorld(vec2 pos, float z=-1.f);
vec3 ScreenToWorld(vec3 pos);
vec3 ScreenToWorld(vec2 pos, mat4 inv_view_proj, float z=-1.f);
vec3 ScreenToWorld(vec3 pos, mat4 inv_view_proj);
vec3 ScreenToWorld(vec3 pos, mat4 view, mat4 proj);

//Draw stuff in World
//-- LINE
void DrawLine(vec3 a, vec3 b);
void DrawLine(vec2 a, vec3 b, float az=-1.f);
void DrawLine(vec2 a, vec2 b, float az=-1.f, float bz=-1.f);
void DrawLine(vec3 a, vec3 b, vec4 color);
void DrawLine(vec2 a, vec3 b, vec4 color, float az=-1.f);
void DrawLine(vec2 a, vec2 b, vec4 color, float az=-1.f, float bz=-1.f);
//-- ARROW
void DrawArrow(vec3 a, vec3 b, vec2 s);
void DrawArrow(vec2 a, vec3 b, vec2 s, float az=-1.f);
void DrawArrow(vec2 a, vec2 b, vec2 s, float az=-1.f, float bz=-1.f);
void DrawArrow(vec3 a, vec3 b, vec3 s);
void DrawArrow(vec2 a, vec3 b, vec3 s, float az=-1.f);
void DrawArrow(vec2 a, vec2 b, vec3 s, float az=-1.f, float bz=-1.f);
void DrawArrow(vec3 a, vec3 b, vec3 s, vec4 color);
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(box3 a, vec4 color);
void DrawBox(vec3 a, vec3 b);
@@ -44,18 +71,22 @@ void DrawBox(box3 a, mat4 transform);
void DrawBox(box3 a, mat4 transform, vec4 color);
void DrawBox(vec3 a, vec3 b, mat4 transform);
void DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color);
//-- CIRCLE
void DrawCircle(vec3 a, vec3 n);
void DrawCircle(vec3 a, vec3 n, vec4 color);
void DrawCircle(vec3 a, vec3 x, vec3 y);
void DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color);
//-- SPHERE
void DrawSphere(vec3 a, float s);
void DrawSphere(vec3 a, float s, vec4 color);
void DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z);
void DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, vec4 color);
//-- CAPSULE
void DrawCapsule(vec3 a, float s, vec3 h);
void DrawCapsule(vec3 a, float s, vec3 h, vec4 color);
void DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h);
void DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color);
//-- VIEW PROJ
void DrawViewProj(mat4 view_proj);
void DrawViewProj(mat4 view_proj, vec4 color);
void DrawViewProj(mat4 view, mat4 proj);


+ 22
- 11
src/scene.cpp Wyświetl plik

@@ -60,11 +60,11 @@ private:
Array<Primitive> m_primitives;

/* Old API <P0, P1, COLOR, TIME, MASK> */
float m_new_line_time;
int m_new_line_mask;
float m_new_line_segment_size;
vec4 m_new_line_color;
Array<vec3, vec3, vec4, float, int> m_lines;
float m_new_line_time;
int m_new_line_mask;
float m_new_line_segment_size;
vec4 m_new_line_color;
Array<vec3, vec3, vec4, float, int, bool, bool> m_lines;
int m_debug_mask;
Shader *m_line_shader;
VertexDeclaration *m_line_vdecl;
@@ -98,7 +98,7 @@ Scene::Scene(ivec2 size)
VertexStream<vec2>(VertexUsage::TexCoord));

data->m_line_shader = 0;
data->m_line_vdecl = new VertexDeclaration(VertexStream<vec3,vec4>(VertexUsage::Position, VertexUsage::Color));
data->m_line_vdecl = new VertexDeclaration(VertexStream<vec4,vec4>(VertexUsage::Position, VertexUsage::Color));

data->m_debug_mask = 1;

@@ -194,7 +194,17 @@ vec4 Scene::GetLineColor() { return data->m_new_line_color; }

void Scene::AddLine(vec3 a, vec3 b, vec4 color)
{
data->m_lines.Push(a, b, color, data->m_new_line_time, data->m_new_line_mask);
data->m_lines.Push(a, b, color, data->m_new_line_time, data->m_new_line_mask, false, false);
}

void Scene::AddLine(vec2 a, vec3 b, vec4 color, float az)
{
data->m_lines.Push(vec3(a, az), b, color, data->m_new_line_time, data->m_new_line_mask, true, false);
}

void Scene::AddLine(vec2 a, vec2 b, vec4 color, float az, float bz)
{
data->m_lines.Push(vec3(a, az), vec3(b, bz), color, data->m_new_line_time, data->m_new_line_mask, true, true);
}

void Scene::AddLight(Light *l)
@@ -350,16 +360,17 @@ void Scene::RenderLines(float seconds) // XXX: rename to Blit()
if (!data->m_line_shader)
data->m_line_shader = Shader::Create(LOLFX_RESOURCE_NAME(line));

Array<vec3, vec4, vec3, vec4> buff;
Array<vec4, vec4, vec4, vec4> buff;
buff.Resize(linecount);
int real_linecount = 0;
mat4 const inv_view_proj = inverse(g_scene->GetCamera()->GetProjection() * g_scene->GetCamera()->GetView());
for (int i = 0; i < linecount; i++)
{
if (data->m_lines[i].m5 & data->m_debug_mask)
{
buff[real_linecount].m1 = data->m_lines[i].m1;
buff[real_linecount].m1 = vec4(data->m_lines[i].m1, (float)data->m_lines[i].m6);
buff[real_linecount].m2 = data->m_lines[i].m3;
buff[real_linecount].m3 = data->m_lines[i].m2;
buff[real_linecount].m3 = vec4(data->m_lines[i].m2, (float)data->m_lines[i].m7);
buff[real_linecount].m4 = data->m_lines[i].m3;
real_linecount++;
}
@@ -370,7 +381,7 @@ void Scene::RenderLines(float seconds) // XXX: rename to Blit()
linecount--;
}
}
int vb_size = (sizeof(vec3) + sizeof(vec4)) * 2 * real_linecount;
int vb_size = sizeof(vec4) * 4 * real_linecount;
VertexBuffer *vb = new VertexBuffer(vb_size);
float *vertex = (float *)vb->Lock(0, 0);
memcpy(vertex, buff.Data(), vb_size);


+ 2
- 0
src/scene.h Wyświetl plik

@@ -60,6 +60,8 @@ public:
void SetLineColor(vec4 new_color=vec4(1.f));
vec4 GetLineColor();
void AddLine(vec3 a, vec3 b, vec4 color);
void AddLine(vec2 a, vec3 b, vec4 color, float az=-1.f);
void AddLine(vec2 a, vec2 b, vec4 color, float az=-1.f, float bz=-1.f);

void AddLight(Light *light);
Array<Light *> const &GetLights() const;


Ładowanie…
Anuluj
Zapisz