Overte C++ Documentation
Asset.h
1 //
2 // Asset.h
3 // libraries/graphics/src/graphics
4 //
5 // Created by Sam Gateau on 08/21/2015.
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_model_Asset_h
12 #define hifi_model_Asset_h
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "Material.h"
18 #include "Geometry.h"
19 
20 namespace graphics {
21 
22 template <class T>
23 class Table {
24 public:
25  typedef std::vector< T > Vector;
26  typedef int ID;
27 
28  static const ID INVALID_ID = 0;
29 
30  typedef size_t Index;
31  enum Version {
32  DRAFT = 0,
33  FINAL,
34  NUM_VERSIONS,
35  };
36 
37  static Version evalVersionFromID(ID id) {
38  if (id <= 0) {
39  return DRAFT;
40  } else {
41  return FINAL;
42  }
43  }
44  static Index evalIndexFromID(ID id) {
45  return Index(id < 0 ? -id : id) - 1;
46  }
47  static ID evalID(Index index, Version version) {
48  return (version == DRAFT ? -int(index + 1) : int(index + 1));
49  }
50 
51  Table() {
52  for (auto e : _elements) {
53  e.resize(0);
54  }
55  }
56  ~Table() {}
57 
58  Index getNumElements() const {
59  return _elements[DRAFT].size();
60  }
61 
62  ID add(const T& element) {
63  for (auto e : _elements) {
64  e.push_back(element);
65  }
66  return evalID(_elements[DRAFT].size(), DRAFT);
67  }
68 
69  void set(ID id, const T& element) {
70  Index index = evalIndexFromID(id);
71  if (index < getNumElements()) {
72  _elements[DRAFT][index] = element;
73  }
74  }
75 
76  const T& get(ID id, const T& element) const {
77  Index index = evalIndexFromID(id);
78  if (index < getNumElements()) {
79  return _elements[DRAFT][index];
80  }
81  return _default;
82  }
83 
84 protected:
85  Vector _elements[NUM_VERSIONS];
86  T _default;
87 };
88 
89 typedef Table< MaterialPointer > MaterialTable;
90 
91 typedef Table< MeshPointer > MeshTable;
92 
93 
94 class Shape {
95 public:
96 
97  MeshTable::ID _meshID{ MeshTable::INVALID_ID };
98  int _partID = 0;
99 
100  MaterialTable::ID _materialID{ MaterialTable::INVALID_ID };
101 };
102 
103 typedef Table< Shape > ShapeTable;
104 
105 class Asset {
106 public:
107 
108 
109  Asset();
110  ~Asset();
111 
112  MeshTable& editMeshes() { return _meshes; }
113  const MeshTable& getMeshes() const { return _meshes; }
114 
115  MaterialTable& editMaterials() { return _materials; }
116  const MaterialTable& getMaterials() const { return _materials; }
117 
118  ShapeTable& editShapes() { return _shapes; }
119  const ShapeTable& getShapes() const { return _shapes; }
120 
121 protected:
122 
123  MeshTable _meshes;
124  MaterialTable _materials;
125  ShapeTable _shapes;
126 
127 };
128 
129 typedef std::shared_ptr< Asset > AssetPointer;
130 
131 };
132 #endif