1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include "nv/gfx/debug_draw.hh"
|
---|
8 |
|
---|
9 | #include "nv/interface/device.hh"
|
---|
10 | #include "nv/core/logging.hh"
|
---|
11 |
|
---|
12 | static const char *nv_debug_draw_vertex_shader = R"(
|
---|
13 | #version 120
|
---|
14 | attribute vec3 nv_position;
|
---|
15 | attribute vec3 nv_color;
|
---|
16 | varying vec3 v_color;
|
---|
17 | uniform mat4 nv_m_mvp;
|
---|
18 | void main(void)
|
---|
19 | {
|
---|
20 | gl_Position = nv_m_mvp * vec4 (nv_position, 1.0);
|
---|
21 | v_color = nv_color;
|
---|
22 | };
|
---|
23 | )";
|
---|
24 | static const char *nv_debug_draw_fragment_shader = R"(
|
---|
25 | #version 120
|
---|
26 | varying vec3 v_color;
|
---|
27 | out vec4 o_frag_color;
|
---|
28 | void main(void)
|
---|
29 | {
|
---|
30 | o_frag_color = vec4( v_color, 1.0 );
|
---|
31 | }
|
---|
32 | )";
|
---|
33 |
|
---|
34 | nv::debug_data::debug_data( context* a_context )
|
---|
35 | : m_context( a_context ), m_program(), m_va()
|
---|
36 | {
|
---|
37 | m_program = m_context->get_device()->create_program( nv_debug_draw_vertex_shader, nv_debug_draw_fragment_shader );
|
---|
38 | m_buffer_size = 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void nv::debug_data::update()
|
---|
42 | {
|
---|
43 | if ( !m_va.is_valid() || m_data.size() > m_buffer_size )
|
---|
44 | {
|
---|
45 | if ( m_va.is_valid() ) m_context->release( m_va );
|
---|
46 | m_buffer_size = nv::max( m_data.size(), 4096U, m_buffer_size );
|
---|
47 | m_vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, nv::STREAM_DRAW, m_buffer_size * sizeof( debug_vtx ) );
|
---|
48 | vertex_array_desc va_desc;
|
---|
49 | va_desc.add_vertex_buffers< debug_vtx >( m_vb, true );
|
---|
50 | m_va = m_context->create_vertex_array( va_desc );
|
---|
51 | }
|
---|
52 | m_context->update( m_vb, m_data.data(), 0, m_data.size()* sizeof( debug_vtx ) );
|
---|
53 | }
|
---|
54 |
|
---|
55 | void nv::debug_data::reset()
|
---|
56 | {
|
---|
57 | m_data.clear();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void nv::debug_data::push_line( const vec3& a, const vec3& b, const vec3& color )
|
---|
61 | {
|
---|
62 | m_data.emplace_back( a, color );
|
---|
63 | m_data.emplace_back( b, color );
|
---|
64 | }
|
---|
65 |
|
---|
66 | void nv::debug_data::push_aabox( const vec3& a, const vec3& b, const vec3& color )
|
---|
67 | {
|
---|
68 | vec3 corners[8] =
|
---|
69 | {
|
---|
70 | vec3( a.x, a.y, a.z ), vec3( b.x, a.y, a.z ), vec3( b.x, a.y, b.z ), vec3( a.x, a.y, b.z ),
|
---|
71 | vec3( a.x, b.y, a.z ), vec3( b.x, b.y, a.z ), vec3( b.x, b.y, b.z ), vec3( a.x, b.y, b.z ),
|
---|
72 | };
|
---|
73 |
|
---|
74 | push_line( corners[0], corners[1], color );
|
---|
75 | push_line( corners[1], corners[2], color );
|
---|
76 | push_line( corners[2], corners[3], color );
|
---|
77 | push_line( corners[3], corners[0], color );
|
---|
78 | push_line( corners[0], corners[4], color );
|
---|
79 | push_line( corners[1], corners[5], color );
|
---|
80 | push_line( corners[2], corners[6], color );
|
---|
81 | push_line( corners[3], corners[7], color );
|
---|
82 | push_line( corners[4], corners[5], color );
|
---|
83 | push_line( corners[5], corners[6], color );
|
---|
84 | push_line( corners[6], corners[7], color );
|
---|
85 | push_line( corners[7], corners[4], color );
|
---|
86 | }
|
---|
87 |
|
---|
88 | nv::debug_data::~debug_data()
|
---|
89 | {
|
---|
90 | m_context->release( m_va );
|
---|
91 | m_context->get_device()->release( m_program );
|
---|
92 | }
|
---|