| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_RENDERING_MESHGEOMETRY_H |
| 7 |
#define AVOGADRO_RENDERING_MESHGEOMETRY_H |
| 8 |
|
| 9 |
#include "drawable.h" |
| 10 |
#include "packedvertex.h" |
| 11 |
|
| 12 |
#include <avogadro/core/array.h> |
| 13 |
|
| 14 |
namespace Avogadro { |
| 15 |
namespace Rendering { |
| 16 |
|
| 17 |
/** |
| 18 |
* @class MeshGeometry meshgeometry.h <avogadro/rendering/meshgeometry.h> |
| 19 |
* @brief The MeshGeometry is used for triangle mesh geometry. |
| 20 |
* @author Marcus D. Hanwell |
| 21 |
*/ |
| 22 |
|
| 23 |
class AVOGADRORENDERING_EXPORT MeshGeometry : public Drawable |
| 24 |
{ |
| 25 |
public: |
| 26 |
static const unsigned int InvalidIndex; |
| 27 |
|
| 28 |
MeshGeometry(); |
| 29 |
MeshGeometry(const MeshGeometry& other); |
| 30 |
~MeshGeometry() override; |
| 31 |
|
| 32 |
MeshGeometry& operator=(MeshGeometry); |
| 33 |
friend void swap(MeshGeometry& lhs, MeshGeometry& rhs); |
| 34 |
|
| 35 |
/** |
| 36 |
* Accept a visit from our friendly visitor. |
| 37 |
*/ |
| 38 |
void accept(Visitor&) override; |
| 39 |
|
| 40 |
/** |
| 41 |
* @brief Render the mesh geometry. |
| 42 |
* @param camera The current camera to be used for rendering. |
| 43 |
*/ |
| 44 |
void render(const Camera& camera) override; |
| 45 |
|
| 46 |
/** |
| 47 |
* Add vertices to the object. Note that this just adds vertices to the |
| 48 |
* object. Use addTriangles with size_t indices to actually draw them. |
| 49 |
* @param vertices The 3D vertex points to add to the drawable. |
| 50 |
* @param normals The normal direction at the vertex. |
| 51 |
* @param colors Vertex color. If not specified, use the current color() and |
| 52 |
* opacity(). If the 3 component color is set, the current opacity() is used. |
| 53 |
* @note All arrays must be the same length, or this function call will fail, |
| 54 |
* returning InvalidIndex. |
| 55 |
* @return The index of the first vertex added by this call, used to specify |
| 56 |
* element arrays for the actual triangles. |
| 57 |
* @{ |
| 58 |
*/ |
| 59 |
unsigned int addVertices(const Core::Array<Vector3f>& vertices, |
| 60 |
const Core::Array<Vector3f>& normals, |
| 61 |
const Core::Array<Vector4ub>& colors); |
| 62 |
unsigned int addVertices(const Core::Array<Vector3f>& vertices, |
| 63 |
const Core::Array<Vector3f>& normals, |
| 64 |
const Core::Array<Vector3ub>& colors); |
| 65 |
unsigned int addVertices(const Core::Array<Vector3f>& vertices, |
| 66 |
const Core::Array<Vector3f>& normals); |
| 67 |
/** @} */ |
| 68 |
|
| 69 |
/** |
| 70 |
* Add triangles to the mesh. Triangles are specified as 3-tuples of vertex |
| 71 |
* indices. Must call addVertices first, and use the return value to obtain |
| 72 |
* the valid index range. |
| 73 |
* @{ |
| 74 |
*/ |
| 75 |
void addTriangle(size_t index1, size_t index2, size_t index3); |
| 76 |
void addTriangles(const Core::Array<unsigned int>& indices); |
| 77 |
/** @} */ |
| 78 |
|
| 79 |
/** |
| 80 |
* Clear the contents of the node. |
| 81 |
*/ |
| 82 |
void clear() override; |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the number of vertices. |
| 86 |
*/ |
| 87 |
size_t vertexCount() const { return m_vertices.size(); } |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the number of vertices. |
| 91 |
*/ |
| 92 |
size_t indexCount() const { return m_indices.size(); } |
| 93 |
|
| 94 |
/** |
| 95 |
* Get the number of triangles. |
| 96 |
*/ |
| 97 |
size_t triangleCount() const { return m_indices.size() / 3; } |
| 98 |
|
| 99 |
/** |
| 100 |
* The default color of the mesh. This is used to set the color of new |
| 101 |
* vertices when no explicit vertex color is specified. |
| 102 |
* @{ |
| 103 |
*/ |
| 104 |
void setColor(const Vector3ub& c) { m_color = c; } |
| 105 |
Vector3ub color() const { return m_color; } |
| 106 |
/** @} */ |
| 107 |
|
| 108 |
/** |
| 109 |
* The default opacity of the mesh. This is used when either no explicit |
| 110 |
* vertex color is specified, or a three component color is used. |
| 111 |
* @{ |
| 112 |
*/ |
| 113 |
void setOpacity(unsigned char opacity_) { m_opacity = opacity_; } |
| 114 |
unsigned char opacity() const { return m_opacity; } |
| 115 |
/** @} */ |
| 116 |
|
| 117 |
Core::Array<PackedVertex> vertices() { return m_vertices; } |
| 118 |
Core::Array<unsigned int> triangles() { return m_indices; } |
| 119 |
|
| 120 |
private: |
| 121 |
/** |
| 122 |
* @brief Update the VBOs, IBOs etc ready for rendering. |
| 123 |
*/ |
| 124 |
void update(); |
| 125 |
|
| 126 |
Core::Array<PackedVertex> m_vertices; |
| 127 |
Core::Array<unsigned int> m_indices; |
| 128 |
Vector3ub m_color; |
| 129 |
unsigned char m_opacity; |
| 130 |
|
| 131 |
bool m_dirty; |
| 132 |
|
| 133 |
class Private; |
| 134 |
Private* d; |
| 135 |
}; |
| 136 |
|
| 137 |
inline MeshGeometry& MeshGeometry::operator=(MeshGeometry other) |
| 138 |
{ |
| 139 |
using std::swap; |
| 140 |
swap(*this, other); |
| 141 |
return *this; |
| 142 |
} |
| 143 |
|
| 144 |
inline void swap(MeshGeometry& lhs, MeshGeometry& rhs) |
| 145 |
{ |
| 146 |
using std::swap; |
| 147 |
swap(static_cast<Drawable&>(lhs), static_cast<Drawable&>(rhs)); |
| 148 |
swap(lhs.m_vertices, rhs.m_vertices); |
| 149 |
swap(lhs.m_indices, rhs.m_indices); |
| 150 |
swap(lhs.m_color, rhs.m_color); |
| 151 |
swap(lhs.m_opacity, rhs.m_opacity); |
| 152 |
lhs.m_dirty = rhs.m_dirty = true; |
| 153 |
} |
| 154 |
|
| 155 |
} // End namespace Rendering |
| 156 |
} // End namespace Avogadro |
| 157 |
|
| 158 |
#endif // AVOGADRO_RENDERING_MESHGEOMETRY_H |