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 | /**
|
---|
8 | * @file mesh.hh
|
---|
9 | * @author Kornel Kisielewicz
|
---|
10 | * @brief mesh
|
---|
11 | */
|
---|
12 |
|
---|
13 | #ifndef NV_MESH_HH
|
---|
14 | #define NV_MESH_HH
|
---|
15 |
|
---|
16 | #include <nv/common.hh>
|
---|
17 | #include <nv/string.hh>
|
---|
18 | #include <nv/types.hh>
|
---|
19 | #include <unordered_map>
|
---|
20 | #include <vector>
|
---|
21 |
|
---|
22 | namespace nv
|
---|
23 | {
|
---|
24 | class vertex_attribute_base
|
---|
25 | {
|
---|
26 | public:
|
---|
27 | vertex_attribute_base( const string& name, datatype attrtype )
|
---|
28 | : m_name( name ), m_type( attrtype ) {}
|
---|
29 | const string& get_name() const { return m_name; }
|
---|
30 | datatype get_type() const { return m_type; }
|
---|
31 | virtual datatype get_base_type() const = 0;
|
---|
32 | virtual size_t get_components() const = 0;
|
---|
33 | virtual size_t get_count() const = 0;
|
---|
34 | virtual size_t get_size() const = 0;
|
---|
35 | virtual void* get_data() const = 0;
|
---|
36 | virtual ~vertex_attribute_base() {};
|
---|
37 | private:
|
---|
38 | string m_name;
|
---|
39 | datatype m_type;
|
---|
40 | };
|
---|
41 |
|
---|
42 | template< typename T >
|
---|
43 | class vertex_attribute : public vertex_attribute_base
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | typedef T value_type;
|
---|
47 | typedef typename datatype_traits<T>::base_type base_type;
|
---|
48 | typedef std::vector<T> list;
|
---|
49 |
|
---|
50 | vertex_attribute( const string& name ) : vertex_attribute_base( name, type_to_enum<T>::type ) {}
|
---|
51 | const list& get() const { return m_list; }
|
---|
52 | list& get() { return m_list; }
|
---|
53 | virtual datatype get_base_type() const { return type_to_enum<base_type>::type; };
|
---|
54 | virtual size_t get_components() const { return datatype_traits<T>::size; };
|
---|
55 | virtual size_t get_count() const { return m_list.size(); }
|
---|
56 | virtual size_t get_size() const { return m_list.size() * sizeof(T); }
|
---|
57 | virtual void* get_data() const { return (void*)m_list.data(); }
|
---|
58 | private:
|
---|
59 | list m_list;
|
---|
60 | };
|
---|
61 |
|
---|
62 |
|
---|
63 | class mesh
|
---|
64 | {
|
---|
65 | public:
|
---|
66 | typedef std::unordered_map< std::string, vertex_attribute_base* > map;
|
---|
67 |
|
---|
68 | mesh( primitive p = TRIANGLES, culling::order_type c = culling::CCW )
|
---|
69 | : m_map(), m_indices(), m_primitive( p ), m_order( c ) {}
|
---|
70 | template <typename T>
|
---|
71 | vertex_attribute<T>* add_attribute( const string& attr )
|
---|
72 | {
|
---|
73 | vertex_attribute<T>* result = new vertex_attribute<T>( attr );
|
---|
74 | m_map[ attr ] = result;
|
---|
75 | return result;
|
---|
76 | }
|
---|
77 | template <typename T>
|
---|
78 | vertex_attribute<T>* add_indices()
|
---|
79 | {
|
---|
80 | if ( m_indices ) delete m_indices; // error?
|
---|
81 | m_indices = new vertex_attribute<T>( attr );
|
---|
82 | return m_indices;
|
---|
83 | }
|
---|
84 |
|
---|
85 | vertex_attribute_base* get_attribute( const string& attr )
|
---|
86 | {
|
---|
87 | map::iterator i = m_map.find( attr );
|
---|
88 | return i != m_map.end() ? i->second : nullptr;
|
---|
89 | }
|
---|
90 |
|
---|
91 | template <typename T>
|
---|
92 | vertex_attribute<T>* get_attribute( const string& attr )
|
---|
93 | {
|
---|
94 | map::iterator i = m_map.find( attr );
|
---|
95 | if ( i != m_map.end() && i->second->get_type() != type_to_enum<T>() )
|
---|
96 | {
|
---|
97 | return ((vertex_attribute<T>*)(i->second));
|
---|
98 | }
|
---|
99 | return nullptr;
|
---|
100 | }
|
---|
101 |
|
---|
102 | vertex_attribute_base* get_indices() { return m_indices; }
|
---|
103 |
|
---|
104 | template <typename T>
|
---|
105 | vertex_attribute<T>* get_indices( const string& attr )
|
---|
106 | {
|
---|
107 | return m_indices->get_type() == type_to_enum<T>() ? ((vertex_attribute<T>*)(m_indices)); : nullptr;
|
---|
108 | }
|
---|
109 |
|
---|
110 | const map& get_attributes() const { return m_map; }
|
---|
111 | primitive get_primitive() const { return m_primitive; }
|
---|
112 | bool has_indices() const { return m_indices != nullptr; }
|
---|
113 |
|
---|
114 | ~mesh()
|
---|
115 | {
|
---|
116 | delete m_indices;
|
---|
117 | for ( auto v : m_map )
|
---|
118 | {
|
---|
119 | delete v.second;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | private:
|
---|
123 | map m_map;
|
---|
124 | vertex_attribute_base* m_indices;
|
---|
125 | primitive m_primitive;
|
---|
126 | culling::order_type m_order;
|
---|
127 |
|
---|
128 | };
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | #endif // NV_MESH_HH
|
---|
134 |
|
---|