source: trunk/nv/interface/device.hh @ 238

Last change on this file since 238 was 238, checked in by epyon, 11 years ago
  • mesh data interface and usage
  • new wavefront importer (old pending removal)
  • updates to test projects
File size: 5.5 KB
Line 
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>
17#include <nv/interface/mesh.hh>
18#include <nv/interface/mesh_data.hh>
19#include <nv/interface/vertex_buffer.hh>
20#include <nv/interface/texture2d.hh>
21#include <nv/interface/image_data.hh>
22
23namespace nv
24{
25        class window;
26        class program;
27
28        class device
29        {
30        public:
31                virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0;
32                virtual program* create_program( const string& vs_source, const string& fs_source ) = 0;
33                virtual vertex_buffer* create_vertex_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
34                virtual index_buffer* create_index_buffer( buffer_hint hint, size_t size, const void* source = nullptr ) = 0;
35                virtual vertex_array* create_vertex_array() = 0;
36                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
37                virtual texture2d* create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data = nullptr ) = 0;
38                virtual uint32 get_ticks() = 0;
39                virtual void delay( uint32 ms ) = 0;
40
41                virtual vertex_array* create_vertex_array( const mesh_pack* m, const attribute_map* am, buffer_hint hint )
42                {
43                        vertex_array* result = create_vertex_array();
44                        for ( auto& attr : m->get_attributes() )
45                        {
46                                // TODO : error checking
47                                vertex_buffer* vb = create_vertex_buffer( hint, attr.second->get_size(), attr.second->get_data() );
48                                result->add_vertex_buffer( am->at( attr.first )->get_location(), vb, attr.second->get_base_type(), attr.second->get_components() );
49                        }
50                        if ( m->has_indices() )
51                        {
52                                const vertex_attribute_base* i = m->get_indices();
53                                index_buffer* vb = create_index_buffer( hint, i->get_size(), i->get_data() );
54                                result->set_index_buffer( vb, i->get_base_type(), true );
55                        }
56                        return result;
57                }
58
59                template < typename VTX, slot SLOT >
60                void add_vertex_buffer_impl( vertex_array*, vertex_buffer*, const std::false_type& )
61                {
62                }
63
64                template < typename VTX, slot SLOT >
65                void add_vertex_buffer_impl( vertex_array* va, vertex_buffer* vb, const std::true_type& )
66                {
67                        typedef vertex_slot_info< VTX, SLOT > vinfo;
68                        typedef datatype_traits< typename vinfo::value_type > dt_traits;
69                        va->add_vertex_buffer( SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
70                }
71
72                template < typename VTX, slot SLOT >
73                void add_vertex_buffer( vertex_array* va, vertex_buffer* vb )
74                {
75                        add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() );
76                }
77
78
79                template < typename VTX >
80                vertex_array* create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
81                {
82                        vertex_array*  va = create_vertex_array();
83                        vertex_buffer* vb = create_vertex_buffer( hint, count * sizeof( VTX ), v );
84                        add_vertex_buffer< VTX, slot::POSITION >  ( va, vb );
85                        add_vertex_buffer< VTX, slot::TEXCOORD >  ( va, vb );
86                        add_vertex_buffer< VTX, slot::NORMAL   >  ( va, vb );
87                        add_vertex_buffer< VTX, slot::TANGENT >   ( va, vb );
88                        add_vertex_buffer< VTX, slot::BONEINDEX > ( va, vb );
89                        add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb );
90                        add_vertex_buffer< VTX, slot::COLOR >     ( va, vb );
91                        return va;
92                }
93
94                template < typename VTX >
95                vertex_array* create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
96                {
97                        return create_vertex_array( data.data(), data.size(), hint );
98                }
99
100                template < typename VTX, typename IDX >
101                vertex_array* create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
102                {
103                        vertex_array* va = create_vertex_array( v, vcount, hint );
104                        index_buffer* ib = create_index_buffer( hint, icount * sizeof( IDX ), i );
105                        va->set_index_buffer( ib, type_to_enum< IDX >::type, true );
106                        return va;
107                }
108
109                template < typename VTX, typename IDX >
110                vertex_array* create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
111                {
112                        return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint );
113                }
114
115                // TODO: HINTS ARE DIFFERENT!
116                vertex_array* create_vertex_array( const mesh_data* data, buffer_hint hint )
117                {
118                        vertex_array*  va = create_vertex_array();
119                        for ( uint32 ch = 0; ch < data->get_channel_data().size(); ++ch )
120                        {
121                                const mesh_raw_channel* channel = data->get_channel_data()[ch];
122                                vertex_buffer* vb = create_vertex_buffer( hint, channel->size, channel->data );
123                                for ( uint32 s = 0; s < channel->desc.count; ++s )
124                                {
125                                        const vertex_descriptor_slot& slot = channel->desc.slots[s];
126                                        const datatype_info&          info = get_datatype_info(slot.etype);
127                                        va->add_vertex_buffer( slot.vslot, vb, info.base , info.elements, slot.offset, channel->desc.size, false );
128                                }
129                        }
130                        if ( data->get_index_channel() != nullptr )
131                        {
132                                const mesh_raw_index_channel* index = data->get_index_channel();
133                                index_buffer* ib = create_index_buffer( hint, index->size, index->data );
134                                va->set_index_buffer( ib, index->etype, true );
135                        }
136                        return va;
137                }
138
139
140                virtual ~device() {}
141        };
142
143} // namespace nv
144
145
146#endif // NV_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.