source: trunk/nv/interface/mesh.hh @ 148

Last change on this file since 148 was 148, checked in by epyon, 12 years ago
  • md3 loader - work in progress
File size: 3.6 KB
RevLine 
[71]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>
[73]19#include <nv/interface/context.hh>
[71]20#include <unordered_map>
21#include <vector>
22
23namespace 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;
[121]37                virtual ~vertex_attribute_base() {}
[71]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; }
[121]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; }
[71]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
[121]69                mesh( primitive p = TRIANGLES )
70                        : m_map(), m_indices(), m_primitive( p ) {}
[71]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?
[148]82                        vertex_attribute<T>* result = new vertex_attribute<T>("");
83                        m_indices = result;
84                        return result;
[71]85                }
86
87                vertex_attribute_base* get_attribute( const string& attr )
88                {
89                        map::iterator i = m_map.find( attr );
90                        return i != m_map.end() ? i->second : nullptr;
91                }
92
93                template <typename T>
94                vertex_attribute<T>* get_attribute( const string& attr )
95                {
96                        map::iterator i = m_map.find( attr );
[142]97                        if ( i != m_map.end() && i->second->get_type() == type_to_enum<T>::type )
[71]98                        {
99                                return ((vertex_attribute<T>*)(i->second));
100                        }
101                        return nullptr;
102                }
103
[73]104                const vertex_attribute_base* get_indices() const { return m_indices; }
[71]105                vertex_attribute_base* get_indices() { return m_indices; }
106
107                template <typename T>
[73]108                vertex_attribute<T>* get_indices()
[71]109                {
[111]110                        return m_indices->get_type() == type_to_enum<T>() ? ((vertex_attribute<T>*)(m_indices)) : nullptr;
[71]111                }
112
113                const map& get_attributes() const { return m_map; }
114                primitive get_primitive() const { return m_primitive; }
115                bool has_indices() const { return m_indices != nullptr; }
116
117                ~mesh()
118                {
119                        delete m_indices;
[101]120                        for ( auto& v : m_map )
[71]121                        {
122                                delete v.second;
123                        }
124                }
125        private:
126                map                    m_map;
127                vertex_attribute_base* m_indices;
128                primitive              m_primitive;
129        };
130
131}
132
133
134#endif // NV_MESH_HH
135
Note: See TracBrowser for help on using the repository browser.