VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Mesh.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MESH_H
18 #define MESH_H
19 
20 #include "Attribute.h"
21 #include "AttributeArray.h"
22 #include "AttributeSet.h"
23 #include "RendererCommon.h"
24 
36 class Mesh {
37 public:
49  Mesh(int vertexCount, const AttributeSetPtr& attributes);
50  ~Mesh();
51 
55  int getVertexCount() const { return mVertexCount; }
56 
60  void bind();
64  void unbind();
70  void upload();
74  void dispose();
81  bool isUploaded() const { return mBufferValid; }
82 
86  const AttributeSetPtr& getAttributes() const { return mAttributes; }
91  bool hasAttributeArray(const char* name) const;
97  const Attribute& getAttribute(const char* name) const;
103  int findAttributeIndex(const char* name) const;
107  void* getBuffer() const { return mBuffer; }
117  void setBuffer(void* buffer, int vertexCount);
126  void allocateBuffer(int vertexCount);
131  int getDrawVertexCount() const { return mDrawVertexCount; }
141  void setDrawVertexCount(int drawVertexCount);
142 
154  template<typename T> const AttributeArray<T> getAttributeArray(const char* name) const {
155  return getAttributeGenericArray(name).getAsType<T>();
156  }
169  template<typename T> AttributeArray<T> getAttributeArray(const char* name) {
170  return getAttributeGenericArray(name).getAsType<T>();
171  }
172 
173 private:
174  void checkUpload();
175  void uploadData();
176  const AttributeArray<void*> getAttributeGenericArray(const char* name) const;
177  AttributeArray<void*> getAttributeGenericArray(const char* name);
178 
179  std::unique_ptr<GLbyte> mOwnedBuffer;
180  int mVertexCount = 0;
181  int mDrawVertexCount = 0;
182  bool mBufferValid = false;
183  bool mBufferUploaded = false;
184  GLuint mBufferId = 0;
185  void* mBuffer = nullptr;
186  AttributeSetPtr mAttributes;
187 };
188 
189 #endif // MESH_H
bool isUploaded() const
Returns whether or not an associated GL buffer object exists and contains uploaded data...
Definition: Mesh.h:81
const Attribute & getAttribute(const char *name) const
Returns the Attribute definition with the given name, or an empty Attribute if it does not exist...
Header declaring and including types common to renderer classes such as Vector2. Also includes GL hea...
void upload()
Uploads the current vertex buffer's data into a GL buffer object.
AttributeArray< T > getAttributeArray(const char *name)
Returns the values of the vertex Attribute with the given name as a mutable AttributeArray.
Definition: Mesh.h:169
void setDrawVertexCount(int drawVertexCount)
Sets the number of vertices to draw when this Mesh is rendered.
void unbind()
Unbinds this vertex buffer.
void bind()
Bind this vertex buffer, uploading it if necessary.
void * getBuffer() const
Returns a raw pointer to the current vertex buffer.
Definition: Mesh.h:107
Mesh composed of vertices, and attributes for each vertex.
Definition: Mesh.h:36
const AttributeSetPtr & getAttributes() const
Returns the list of vertex Attributes as an AttributeSet.
Definition: Mesh.h:86
bool hasAttributeArray(const char *name) const
Returns whether or not vertices in this Mesh have an Attribute of the given name. ...
int getVertexCount() const
Returns the length of the current vertex buffer, in number of vertices.
Definition: Mesh.h:55
The definition of a vertex attribute, such as a position vector, or a texture coordinate.
Definition: Attribute.h:31
int getDrawVertexCount() const
Returns the number of vertices that will be drawn when this Mesh is rendered.
Definition: Mesh.h:131
const AttributeArray< T > getAttributeArray(const char *name) const
Returns the values of the vertex Attribute with the given name as an AttributeArray.
Definition: Mesh.h:154
std::shared_ptr< AttributeSet > AttributeSetPtr
Shared pointer typedef for AttributeSet.
Definition: PointerDeclarations.h:63
void setBuffer(void *buffer, int vertexCount)
Sets the current vertex buffer to be a user managed buffer.
Mesh(int vertexCount, const AttributeSetPtr &attributes)
Construct a mesh with the given vertex attributes and vertex count.
void allocateBuffer(int vertexCount)
Sets the current buffer to be a Mesh managed buffer.
int findAttributeIndex(const char *name) const
Returns the index of the Attribute with the given name.
void dispose()
Frees this Mesh's managed buffer, and GL buffer object, if allocated.
A helper class to access and iterate over an interleaved vertex attribute array.
Definition: AttributeArray.h:31