Overte C++ Documentation
Geometry.h
1 //
2 // Geometry.h
3 // libraries/graphics/src/graphics
4 //
5 // Created by Sam Gateau on 12/5/2014.
6 // Copyright 2014 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_Geometry_h
12 #define hifi_model_Geometry_h
13 
14 #include <glm/glm.hpp>
15 
16 #include <AABox.h>
17 
18 #include <gpu/Forward.h>
19 #include <gpu/Resource.h>
20 #include <gpu/Stream.h>
21 
22 namespace graphics {
23 typedef gpu::BufferView::Index Index;
24 typedef gpu::BufferView BufferView;
25 typedef AABox Box;
26 typedef std::vector< Box > Boxes;
27 typedef glm::vec3 Vec3;
28 
29 class Mesh;
30 using MeshPointer = std::shared_ptr< Mesh >;
31 
32 class Mesh {
33 public:
34  const static Index PRIMITIVE_RESTART_INDEX = -1;
35 
36  typedef gpu::BufferView BufferView;
37  typedef std::vector< BufferView > BufferViews;
38 
39  typedef gpu::Stream::Slot Slot;
40  typedef gpu::Stream::Format VertexFormat;
41  typedef std::map< Slot, BufferView > BufferViewMap;
42 
43  typedef graphics::Vec3 Vec3;
44 
45  Mesh();
46  Mesh(const Mesh& mesh);
47  Mesh& operator= (const Mesh& mesh) = default;
48  virtual ~Mesh();
49 
50  // Vertex buffer
51  void setVertexBuffer(const BufferView& buffer);
52  const BufferView& getVertexBuffer() const { return _vertexBuffer; }
53  size_t getNumVertices() const { return _vertexBuffer.getNumElements(); }
54  bool hasVertexData() const { return _vertexBuffer._buffer.get() != nullptr; }
55 
56  // Attribute Buffers
57  size_t getNumAttributes() const { return _attributeBuffers.size(); }
58  void addAttribute(Slot slot, const BufferView& buffer);
59  void removeAttribute(Slot slot);
60  const BufferView getAttributeBuffer(int attrib) const;
61 
62  // Force vertex stream and Vertex format
63  void setVertexFormatAndStream(const gpu::Stream::FormatPointer& vf, const gpu::BufferStreamPointer& vbs);
64 
65  // Stream format
66  const gpu::Stream::FormatPointer getVertexFormat() const { return _vertexFormat; }
67 
68  // BufferStream on the mesh vertices and attributes matching the vertex format
69  const gpu::BufferStream& getVertexStream() const { return _vertexStream; }
70 
71  // Index Buffer
72  using Indices16 = std::vector<int16_t>;
73  using Indices32 = std::vector<int32_t>;
74 
75  void setIndexBuffer(const BufferView& buffer);
76  const BufferView& getIndexBuffer() const { return _indexBuffer; }
77  size_t getNumIndices() const { return _indexBuffer.getNumElements(); }
78 
79  // Access vertex position value
80  const Vec3& getPos(Index index) const { return _vertexBuffer.get<Vec3>(index); }
81 
82  enum Topology {
83  POINTS = 0,
84  LINES,
85  LINE_STRIP,
86  TRIANGLES,
87  TRIANGLE_STRIP,
88  QUADS, // NOTE: These must be translated to triangles before rendering
89  QUAD_STRIP,
90 
91  NUM_TOPOLOGIES,
92  };
93 
94  // Subpart of a mesh, describing the toplogy of the surface
95  class Part {
96  public:
97  Index _startIndex;
98  Index _numIndices;
99  Index _baseVertex;
100  Topology _topology;
101 
102  Part() :
103  _startIndex(0),
104  _numIndices(0),
105  _baseVertex(0),
106  _topology(TRIANGLES)
107  {}
108  Part(Index startIndex, Index numIndices, Index baseVertex, Topology topology) :
109  _startIndex(startIndex),
110  _numIndices(numIndices),
111  _baseVertex(baseVertex),
112  _topology(topology)
113  {}
114  };
115 
116  void setPartBuffer(const BufferView& buffer);
117  const BufferView& getPartBuffer() const { return _partBuffer; }
118  size_t getNumParts() const { return _partBuffer.getNumElements(); }
119 
120  // evaluate the bounding box of A part
121  Box evalPartBound(int partNum) const;
122  // evaluate the bounding boxes of the parts in the range [start, end]
123  // the returned box is the bounding box of ALL the evaluated parts bound.
124  Box evalPartsBound(int partStart, int partEnd) const;
125 
126  static gpu::Primitive topologyToPrimitive(Topology topo) { return static_cast<gpu::Primitive>(topo); }
127 
128  // create a copy of this mesh after passing its vertices, normals, and indexes though the provided functions
129  MeshPointer map(std::function<glm::vec3(glm::vec3)> vertexFunc,
130  std::function<glm::vec3(glm::vec3)> colorFunc,
131  std::function<glm::vec3(glm::vec3)> normalFunc,
132  std::function<uint32_t(uint32_t)> indexFunc) const;
133 
134  void forEach(std::function<void(glm::vec3)> vertexFunc,
135  std::function<void(glm::vec3)> colorFunc,
136  std::function<void(glm::vec3)> normalFunc,
137  std::function<void(uint32_t)> indexFunc);
138 
139 
140  static MeshPointer createIndexedTriangles_P3F(uint32_t numVertices, uint32_t numTriangles, const glm::vec3* vertices = nullptr, const uint32_t* indices = nullptr);
141 
142  std::string modelName;
143  std::string displayName;
144 
145  gpu::BufferPointer getColorBuffer() const { return _colorBuffer; }
146 
147 protected:
148 
149  gpu::Stream::FormatPointer _vertexFormat;
150  gpu::BufferStream _vertexStream;
151 
152  BufferView _vertexBuffer;
153  BufferViewMap _attributeBuffers;
154 
155  BufferView _indexBuffer;
156 
157  BufferView _partBuffer;
158 
159  gpu::BufferPointer _colorBuffer { std::make_shared<gpu::Buffer>() };
160 
161  void evalVertexFormat();
162  void evalVertexStream();
163 
164 };
165 
166 
167 class Geometry {
168 public:
169 
170  Geometry();
171  Geometry(const Geometry& geometry);
172  ~Geometry();
173 
174  void setMesh(const MeshPointer& mesh);
175  const MeshPointer& getMesh() const { return _mesh; }
176 
177 protected:
178 
179  MeshPointer _mesh;
180  BufferView _boxes;
181 
182 };
183 
184 };
185 
186 #endif
Provides the Vec3 scripting interface.
Definition: Vec3.h:80