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