[395] | 1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
[237] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[237] | 6 |
|
---|
| 7 | #include "nv/gfx/debug_draw.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/interface/device.hh"
|
---|
[469] | 10 | #include "nv/core/logging.hh"
|
---|
[237] | 11 |
|
---|
[346] | 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 | void main(void)
|
---|
| 28 | {
|
---|
| 29 | gl_FragColor = vec4( v_color, 1.0 );
|
---|
| 30 | }
|
---|
| 31 | )";
|
---|
[237] | 32 |
|
---|
[313] | 33 | nv::debug_data::debug_data( context* a_context )
|
---|
| 34 | : m_context( a_context ), m_program(), m_va()
|
---|
[237] | 35 | {
|
---|
[313] | 36 | m_program = m_context->get_device()->create_program( nv_debug_draw_vertex_shader, nv_debug_draw_fragment_shader );
|
---|
[469] | 37 | m_buffer_size = 0;
|
---|
[237] | 38 | }
|
---|
| 39 |
|
---|
| 40 | void nv::debug_data::update()
|
---|
| 41 | {
|
---|
[469] | 42 | if ( !m_va.is_valid() || m_data.size() > m_buffer_size )
|
---|
| 43 | {
|
---|
| 44 | if ( m_va.is_valid() ) m_context->release( m_va );
|
---|
| 45 | m_buffer_size = nv::max( m_data.size(), 4096U, m_buffer_size );
|
---|
| 46 | m_va = m_context->create_vertex_array();
|
---|
| 47 | m_vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, nv::STREAM_DRAW, m_buffer_size * sizeof( debug_vtx ), m_data.data() );
|
---|
| 48 | m_context->add_vertex_buffers< debug_vtx >( m_va, m_vb, true );
|
---|
| 49 | }
|
---|
| 50 | else
|
---|
| 51 | {
|
---|
| 52 | m_context->update( m_vb, m_data.data(), 0, m_data.size()* sizeof( debug_vtx ) );
|
---|
| 53 | }
|
---|
[237] | 54 | }
|
---|
| 55 |
|
---|
| 56 | void nv::debug_data::reset()
|
---|
| 57 | {
|
---|
| 58 | m_data.clear();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void nv::debug_data::push_line( const vec3& a, const vec3& b, const vec3& color )
|
---|
| 62 | {
|
---|
| 63 | m_data.emplace_back( a, color );
|
---|
| 64 | m_data.emplace_back( b, color );
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void nv::debug_data::push_aabox( const vec3& a, const vec3& b, const vec3& color )
|
---|
| 68 | {
|
---|
| 69 | vec3 corners[8] =
|
---|
| 70 | {
|
---|
| 71 | 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 ),
|
---|
| 72 | 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 ),
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | push_line( corners[0], corners[1], color );
|
---|
| 76 | push_line( corners[1], corners[2], color );
|
---|
| 77 | push_line( corners[2], corners[3], color );
|
---|
| 78 | push_line( corners[3], corners[0], color );
|
---|
| 79 | push_line( corners[0], corners[4], color );
|
---|
| 80 | push_line( corners[1], corners[5], color );
|
---|
| 81 | push_line( corners[2], corners[6], color );
|
---|
| 82 | push_line( corners[3], corners[7], color );
|
---|
| 83 | push_line( corners[4], corners[5], color );
|
---|
| 84 | push_line( corners[5], corners[6], color );
|
---|
| 85 | push_line( corners[6], corners[7], color );
|
---|
| 86 | push_line( corners[7], corners[4], color );
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | nv::debug_data::~debug_data()
|
---|
| 90 | {
|
---|
[313] | 91 | m_context->release( m_va );
|
---|
| 92 | m_context->get_device()->release( m_program );
|
---|
[237] | 93 | }
|
---|