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

Last change on this file since 239 was 239, checked in by epyon, 11 years ago
  • massive update of mesh handling
  • universal mesh handling routines
  • removed a lot of legacy code
  • significantly streamlined MD5 loading
  • all tests updated to new features
File size: 4.4 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_data.hh>
18#include <nv/interface/vertex_buffer.hh>
19#include <nv/interface/texture2d.hh>
20#include <nv/interface/image_data.hh>
21
22namespace nv
23{
24        class window;
25        class program;
26
27        class device
28        {
29        public:
30                virtual window* create_window( uint16 width, uint16 height, bool fullscreen ) = 0;
31                virtual program* create_program( const string& vs_source, const string& fs_source ) = 0;
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;
34                virtual vertex_array* create_vertex_array() = 0;
35                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
36                virtual texture2d* create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data = nullptr ) = 0;
37                virtual uint32 get_ticks() = 0;
38                virtual void delay( uint32 ms ) = 0;
39
40                template < typename VTX, slot SLOT >
41                void add_vertex_buffer_impl( vertex_array*, vertex_buffer*, const std::false_type& )
42                {
43                }
44
45                template < typename VTX, slot SLOT >
46                void add_vertex_buffer_impl( vertex_array* va, vertex_buffer* vb, const std::true_type& )
47                {
48                        typedef vertex_slot_info< VTX, SLOT > vinfo;
49                        typedef datatype_traits< typename vinfo::value_type > dt_traits;
50                        va->add_vertex_buffer( SLOT, vb, type_to_enum< dt_traits::base_type >::type, dt_traits::size, vinfo::offset, sizeof( VTX ), false );
51                }
52
53                template < typename VTX, slot SLOT >
54                void add_vertex_buffer( vertex_array* va, vertex_buffer* vb )
55                {
56                        add_vertex_buffer_impl< VTX, SLOT >( va, vb, std::integral_constant< bool, vertex_has_slot< VTX, SLOT >::value >() );
57                }
58
59
60                template < typename VTX >
61                vertex_array* create_vertex_array( const VTX* v, size_t count, buffer_hint hint )
62                {
63                        vertex_array*  va = create_vertex_array();
64                        vertex_buffer* vb = create_vertex_buffer( hint, count * sizeof( VTX ), v );
65                        add_vertex_buffer< VTX, slot::POSITION >  ( va, vb );
66                        add_vertex_buffer< VTX, slot::TEXCOORD >  ( va, vb );
67                        add_vertex_buffer< VTX, slot::NORMAL   >  ( va, vb );
68                        add_vertex_buffer< VTX, slot::TANGENT >   ( va, vb );
69                        add_vertex_buffer< VTX, slot::BONEINDEX > ( va, vb );
70                        add_vertex_buffer< VTX, slot::BONEWEIGHT >( va, vb );
71                        add_vertex_buffer< VTX, slot::COLOR >     ( va, vb );
72                        return va;
73                }
74
75                template < typename VTX >
76                vertex_array* create_vertex_array( const std::vector< VTX >& data, buffer_hint hint )
77                {
78                        return create_vertex_array( data.data(), data.size(), hint );
79                }
80
81                template < typename VTX, typename IDX >
82                vertex_array* create_vertex_array( const VTX* v, size_t vcount, const IDX* i, size_t icount, buffer_hint hint )
83                {
84                        vertex_array* va = create_vertex_array( v, vcount, hint );
85                        index_buffer* ib = create_index_buffer( hint, icount * sizeof( IDX ), i );
86                        va->set_index_buffer( ib, type_to_enum< IDX >::type, true );
87                        return va;
88                }
89
90                template < typename VTX, typename IDX >
91                vertex_array* create_vertex_array( const std::vector< VTX >& data, const std::vector< IDX >& idata, buffer_hint hint )
92                {
93                        return create_vertex_array( data.data(), data.size(), idata.data(), idata.size(), hint );
94                }
95
96                // TODO: HINTS ARE DIFFERENT!
97                vertex_array* create_vertex_array( const mesh_data* data, buffer_hint hint )
98                {
99                        vertex_array*  va = create_vertex_array();
100                        for ( uint32 ch = 0; ch < data->get_channel_data().size(); ++ch )
101                        {
102                                const mesh_raw_channel* channel = data->get_channel_data()[ch];
103                                vertex_buffer* vb = create_vertex_buffer( hint, channel->size, channel->data );
104                                va->add_vertex_buffers( vb, channel );
105                        }
106                        if ( data->get_index_channel() != nullptr )
107                        {
108                                const mesh_raw_index_channel* index = data->get_index_channel();
109                                index_buffer* ib = create_index_buffer( hint, index->size, index->data );
110                                va->set_index_buffer( ib, index->etype, true );
111                        }
112                        return va;
113                }
114
115
116                virtual ~device() {}
117        };
118
119} // namespace nv
120
121
122#endif // NV_DEVICE_HH
Note: See TracBrowser for help on using the repository browser.