| 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_LINESTRIPGEOMETRY_H |
| 7 |
#define AVOGADRO_RENDERING_LINESTRIPGEOMETRY_H |
| 8 |
|
| 9 |
#include "drawable.h" |
| 10 |
|
| 11 |
#include <avogadro/core/array.h> |
| 12 |
|
| 13 |
namespace Avogadro { |
| 14 |
namespace Rendering { |
| 15 |
|
| 16 |
/** |
| 17 |
* @class LineStripGeometry linestripgeometry.h |
| 18 |
* <avogadro/rendering/linestripgeometry.h> |
| 19 |
* @brief The LineStripGeometry class is used to store sets of line strips. |
| 20 |
*/ |
| 21 |
|
| 22 |
class AVOGADRORENDERING_EXPORT LineStripGeometry : public Drawable |
| 23 |
{ |
| 24 |
public: |
| 25 |
struct PackedVertex |
| 26 |
{ // 16 bytes total: |
| 27 |
Vector3f vertex; // 12 bytes |
| 28 |
Vector4ub color; // 4 bytes |
| 29 |
|
| 30 |
PackedVertex(const Vector3f& v, const Vector4ub& c) : vertex(v), color(c) {} |
| 31 |
static int vertexOffset() { return 0; } |
| 32 |
static int colorOffset() { return static_cast<int>(sizeof(Vector3f)); } |
| 33 |
}; |
| 34 |
|
| 35 |
static const size_t InvalidIndex; |
| 36 |
|
| 37 |
LineStripGeometry(); |
| 38 |
LineStripGeometry(const LineStripGeometry& other); |
| 39 |
~LineStripGeometry() override; |
| 40 |
|
| 41 |
LineStripGeometry& operator=(LineStripGeometry); |
| 42 |
friend void swap(LineStripGeometry& lhs, LineStripGeometry& rhs); |
| 43 |
|
| 44 |
/** |
| 45 |
* Accept a visit from our friendly visitor. |
| 46 |
*/ |
| 47 |
void accept(Visitor&) override; |
| 48 |
|
| 49 |
/** |
| 50 |
* @brief Render the line strips. |
| 51 |
* @param camera The current camera to be used for rendering. |
| 52 |
*/ |
| 53 |
void render(const Camera& camera) override; |
| 54 |
|
| 55 |
/** |
| 56 |
* Clear the contents of the node. |
| 57 |
*/ |
| 58 |
void clear() override; |
| 59 |
|
| 60 |
/** |
| 61 |
* Add a complete line strip to the object. |
| 62 |
* @param vertices The 3D vertices which will be connected to form the line |
| 63 |
* strip. |
| 64 |
* @param color Vertex color. If not specified, use the current color() and |
| 65 |
* opacity(). If the 3 component color is set, the current opacity() is used. |
| 66 |
* @param lineWidth The width of the line strip. |
| 67 |
* @note All arrays must be the same length, or this function call will fail, |
| 68 |
* returning InvalidIndex. |
| 69 |
* @return The index of the first vertex added by this call. |
| 70 |
* @{ |
| 71 |
*/ |
| 72 |
size_t addLineStrip(const Core::Array<Vector3f>& vertices, |
| 73 |
const Core::Array<Vector4ub>& color, float lineWidth); |
| 74 |
size_t addLineStrip(const Core::Array<Vector3f>& vertices, |
| 75 |
const Core::Array<Vector3ub>& color, float lineWidth); |
| 76 |
size_t addLineStrip(const Core::Array<Vector3f>& vertices, |
| 77 |
const Vector3ub& color, float lineWidth); |
| 78 |
size_t addLineStrip(const Core::Array<Vector3f>& vertices, float lineWidth); |
| 79 |
/** @} */ |
| 80 |
|
| 81 |
/** |
| 82 |
* The default color of the lines. This is used to set the color of new |
| 83 |
* vertices when no explicit vertex color is specified. |
| 84 |
* @{ |
| 85 |
*/ |
| 86 |
void setColor(const Vector3ub& c) { m_color = c; } |
| 87 |
Vector3ub color() const { return m_color; } |
| 88 |
/** @} */ |
| 89 |
|
| 90 |
/** |
| 91 |
* The default opacity of the lines. This is used when either no explicit |
| 92 |
* vertex color is specified, or a three component color is used. |
| 93 |
* @{ |
| 94 |
*/ |
| 95 |
void setOpacity(unsigned char opacity_) { m_opacity = opacity_; } |
| 96 |
unsigned char opacity() const { return m_opacity; } |
| 97 |
/** @} */ |
| 98 |
|
| 99 |
/** The vertex array. */ |
| 100 |
Core::Array<PackedVertex> vertices() const { return m_vertices; } |
| 101 |
|
| 102 |
private: |
| 103 |
/** |
| 104 |
* @brief Update the VBOs, IBOs etc ready for rendering. |
| 105 |
*/ |
| 106 |
void update(); |
| 107 |
|
| 108 |
Core::Array<PackedVertex> m_vertices; |
| 109 |
Core::Array<unsigned int> m_lineStarts; |
| 110 |
Core::Array<float> m_lineWidths; |
| 111 |
|
| 112 |
Vector3ub m_color; |
| 113 |
unsigned char m_opacity; |
| 114 |
|
| 115 |
bool m_dirty; |
| 116 |
|
| 117 |
class Private; |
| 118 |
Private* d; |
| 119 |
}; |
| 120 |
|
| 121 |
inline LineStripGeometry& LineStripGeometry::operator=(LineStripGeometry other) |
| 122 |
{ |
| 123 |
using std::swap; |
| 124 |
swap(*this, other); |
| 125 |
return *this; |
| 126 |
} |
| 127 |
|
| 128 |
inline void swap(LineStripGeometry& lhs, LineStripGeometry& rhs) |
| 129 |
{ |
| 130 |
using std::swap; |
| 131 |
swap(static_cast<Drawable&>(lhs), static_cast<Drawable&>(rhs)); |
| 132 |
swap(lhs.m_vertices, rhs.m_vertices); |
| 133 |
swap(lhs.m_lineStarts, rhs.m_lineStarts); |
| 134 |
swap(lhs.m_lineWidths, rhs.m_lineWidths); |
| 135 |
swap(lhs.m_color, rhs.m_color); |
| 136 |
swap(lhs.m_opacity, rhs.m_opacity); |
| 137 |
lhs.m_dirty = rhs.m_dirty = true; |
| 138 |
} |
| 139 |
|
| 140 |
} // End namespace Rendering |
| 141 |
} // End namespace Avogadro |
| 142 |
|
| 143 |
#endif // AVOGADRO_RENDERING_LINESTRIPGEOMETRY_H |