[32] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 | /**
|
---|
| 7 | * @file device.hh
|
---|
| 8 | * @author Kornel Kisielewicz epyon@chaosforge.org
|
---|
| 9 | * @brief Device class
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | #ifndef NV_DEVICE_HH
|
---|
| 13 | #define NV_DEVICE_HH
|
---|
| 14 |
|
---|
| 15 | #include <nv/common.hh>
|
---|
| 16 | #include <nv/string.hh>
|
---|
[73] | 17 | #include <nv/interface/mesh.hh>
|
---|
[42] | 18 | #include <nv/interface/vertex_buffer.hh>
|
---|
[45] | 19 | #include <nv/interface/texture2d.hh>
|
---|
[90] | 20 | #include <nv/interface/image_data.hh>
|
---|
[32] | 21 |
|
---|
| 22 | namespace nv
|
---|
| 23 | {
|
---|
| 24 | class window;
|
---|
| 25 | class program;
|
---|
| 26 |
|
---|
| 27 | class device
|
---|
| 28 | {
|
---|
| 29 | public:
|
---|
[228] | 30 | virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0;
|
---|
[39] | 31 | virtual program* create_program( const string& vs_source, const string& fs_source ) = 0;
|
---|
[153] | 32 | virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
|
---|
| 33 | virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
|
---|
[44] | 34 | virtual vertex_array* create_vertex_array() = 0;
|
---|
[90] | 35 | virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
|
---|
[70] | 36 | virtual texture2d* create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data = nullptr ) = 0;
|
---|
[92] | 37 | virtual uint32 get_ticks() = 0;
|
---|
| 38 | virtual void delay( uint32 ms ) = 0;
|
---|
[73] | 39 |
|
---|
[236] | 40 | virtual vertex_array* create_vertex_array( const mesh_pack* m, const attribute_map* am, buffer_hint hint )
|
---|
[73] | 41 | {
|
---|
| 42 | vertex_array* result = create_vertex_array();
|
---|
[101] | 43 | for ( auto& attr : m->get_attributes() )
|
---|
[73] | 44 | {
|
---|
| 45 | // TODO : error checking
|
---|
| 46 | vertex_buffer* vb = create_vertex_buffer( hint, attr.second->get_size(), attr.second->get_data() );
|
---|
| 47 | result->add_vertex_buffer( am->at( attr.first )->get_location(), vb, attr.second->get_base_type(), attr.second->get_components() );
|
---|
| 48 | }
|
---|
| 49 | if ( m->has_indices() )
|
---|
| 50 | {
|
---|
| 51 | const vertex_attribute_base* i = m->get_indices();
|
---|
| 52 | index_buffer* vb = create_index_buffer( hint, i->get_size(), i->get_data() );
|
---|
[116] | 53 | result->set_index_buffer( vb, i->get_base_type(), true );
|
---|
[73] | 54 | }
|
---|
| 55 | return result;
|
---|
| 56 | }
|
---|
[237] | 57 |
|
---|
| 58 | template < typename VTX, slot SLOT >
|
---|
| 59 | void add_vertex_buffer_impl( vertex_array*, vertex_buffer*, const std::false_type& )
|
---|
| 60 | {
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | template < typename VTX, slot SLOT >
|
---|
| 64 | void add_vertex_buffer_impl( vertex_array* va, vertex_buffer* vb, const std::true_type& )
|
---|
| 65 | {
|
---|
| 66 | typedef vertex_slot_info< VTX, SLOT > vinfo;
|
---|
| 67 | typedef datatype_traits< typename vinfo::value_type > dt_traits;
|
---|
| 68 | va->add_vertex_buffer( SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | template < typename VTX, slot SLOT >
|
---|
| 72 | void add_vertex_buffer( vertex_array* va, vertex_buffer* vb )
|
---|
| 73 | {
|
---|
| 74 | add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() );
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | template < typename VTX >
|
---|
| 79 | vertex_array* create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
|
---|
| 80 | {
|
---|
| 81 | vertex_array* va = create_vertex_array();
|
---|
| 82 | vertex_buffer* vb = create_vertex_buffer( hint, count * sizeof( VTX ), v );
|
---|
| 83 | add_vertex_buffer< VTX, slot::POSITION > ( va, vb );
|
---|
| 84 | add_vertex_buffer< VTX, slot::TEXCOORD > ( va, vb );
|
---|
| 85 | add_vertex_buffer< VTX, slot::NORMAL > ( va, vb );
|
---|
| 86 | add_vertex_buffer< VTX, slot::TANGENT > ( va, vb );
|
---|
| 87 | add_vertex_buffer< VTX, slot::BONEINDEX > ( va, vb );
|
---|
| 88 | add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb );
|
---|
| 89 | add_vertex_buffer< VTX, slot::COLOR > ( va, vb );
|
---|
| 90 | return va;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | template < typename VTX >
|
---|
| 94 | vertex_array* create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
|
---|
| 95 | {
|
---|
| 96 | return create_vertex_array( data.data(), data.size(), hint );
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | template < typename VTX, typename IDX >
|
---|
| 100 | vertex_array* create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
|
---|
| 101 | {
|
---|
| 102 | vertex_array* va = create_vertex_array( v, vcount, hint );
|
---|
| 103 | index_buffer* ib = create_index_buffer( hint, icount * sizeof( IDX ), i );
|
---|
| 104 | va->set_index_buffer( ib, type_to_enum< IDX >::type, true );
|
---|
| 105 | return va;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | template < typename VTX, typename IDX >
|
---|
| 109 | vertex_array* create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
|
---|
| 110 | {
|
---|
| 111 | return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint );
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[121] | 114 | virtual ~device() {}
|
---|
[32] | 115 | };
|
---|
| 116 |
|
---|
| 117 | } // namespace nv
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | #endif // NV_DEVICE_HH
|
---|