source: trunk/src/gfx/debug_draw.cc @ 474

Last change on this file since 474 was 469, checked in by epyon, 10 years ago
  • stl/short_string - minor fix
  • stl/utility - max for 3 arguments
  • stl/math length_sq
  • interface/context - proper handling of buffer ownage in va's
  • wx/canvas - sleep added
File size: 2.7 KB
Line 
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
12static const char *nv_debug_draw_vertex_shader = R"(
13#version 120
14attribute vec3 nv_position;
15attribute vec3 nv_color;
16varying vec3 v_color;
17uniform mat4 nv_m_mvp;
18void main(void)
19{
20        gl_Position = nv_m_mvp * vec4 (nv_position, 1.0);
21        v_color     = nv_color;
22};
23)";
24static const char *nv_debug_draw_fragment_shader = R"(
25#version 120
26varying vec3 v_color;
27void main(void)
28{
29        gl_FragColor = vec4( v_color, 1.0 );
30}
31)";
32
33nv::debug_data::debug_data( context* a_context )
34        : m_context( a_context ), m_program(), m_va()
35{
36        m_program = m_context->get_device()->create_program( nv_debug_draw_vertex_shader, nv_debug_draw_fragment_shader );
37        m_buffer_size = 0;
38}
39
40void nv::debug_data::update()
41{
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        }
54}
55
56void nv::debug_data::reset()
57{
58        m_data.clear();
59}
60
61void 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
67void 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
89nv::debug_data::~debug_data()
90{
91        m_context->release( m_va );
92        m_context->get_device()->release( m_program );
93}
Note: See TracBrowser for help on using the repository browser.