| 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_SPHEREGEOMETRY_H |
| 7 |
#define AVOGADRO_RENDERING_SPHEREGEOMETRY_H |
| 8 |
|
| 9 |
#include "drawable.h" |
| 10 |
|
| 11 |
#include <avogadro/core/array.h> |
| 12 |
#include <avogadro/core/vector.h> |
| 13 |
|
| 14 |
namespace Avogadro { |
| 15 |
namespace Rendering { |
| 16 |
|
| 17 |
struct SphereColor |
| 18 |
{ |
| 19 |
SphereColor(Vector3f centre, float r, Vector3ub c) |
| 20 |
: center(centre), radius(r), color(c) |
| 21 |
{ |
| 22 |
} |
| 23 |
Vector3f center; |
| 24 |
float radius; |
| 25 |
Vector3ub color; |
| 26 |
}; |
| 27 |
|
| 28 |
/** |
| 29 |
* @class SphereGeometry spheregeometry.h <avogadro/rendering/spheregeometry.h> |
| 30 |
* @brief The SphereGeometry class contains one or more spheres. |
| 31 |
* @author Marcus D. Hanwell |
| 32 |
* |
| 33 |
* This Drawable is capable of storing the geometry for one or more spheres. |
| 34 |
* A sphere is defined by a center point, a radius and a color. If the |
| 35 |
* spheres are not a densely packed one-to-one mapping with the objects indices |
| 36 |
* they can also optionally use an identifier that will point to some numeric |
| 37 |
* ID for the purposes of picking. |
| 38 |
*/ |
| 39 |
|
| 40 |
class AVOGADRORENDERING_EXPORT SphereGeometry : public Drawable |
| 41 |
{ |
| 42 |
public: |
| 43 |
SphereGeometry(); |
| 44 |
SphereGeometry(const SphereGeometry& other); |
| 45 |
~SphereGeometry() override; |
| 46 |
|
| 47 |
SphereGeometry& operator=(SphereGeometry); |
| 48 |
friend void swap(SphereGeometry& lhs, SphereGeometry& rhs); |
| 49 |
|
| 50 |
/** |
| 51 |
* Accept a visit from our friendly visitor. |
| 52 |
*/ |
| 53 |
void accept(Visitor&) override; |
| 54 |
|
| 55 |
/** |
| 56 |
* @brief Update the VBOs, IBOs etc ready for rendering. |
| 57 |
*/ |
| 58 |
void update(); |
| 59 |
|
| 60 |
/** |
| 61 |
* @brief Render the sphere geometry. |
| 62 |
* @param camera The current camera to be used for rendering. |
| 63 |
*/ |
| 64 |
void render(const Camera& camera) override; |
| 65 |
|
| 66 |
/** |
| 67 |
* Return the primitives that are hit by the ray. |
| 68 |
* @param rayOrigin Origin of the ray. |
| 69 |
* @param rayEnd End point of the ray. |
| 70 |
* @param rayDirection Normalized direction of the ray. |
| 71 |
* @return Sorted collection of primitives that were hit. |
| 72 |
*/ |
| 73 |
std::multimap<float, Identifier> hits( |
| 74 |
const Vector3f& rayOrigin, const Vector3f& rayEnd, |
| 75 |
const Vector3f& rayDirection) const override; |
| 76 |
|
| 77 |
/** |
| 78 |
* Return the primitives within the supplied frustrum. |
| 79 |
*/ |
| 80 |
Core::Array<Identifier> areaHits(const Frustrum& f) const override; |
| 81 |
|
| 82 |
/** |
| 83 |
* Set the opacity of the spheres in this group. |
| 84 |
*/ |
| 85 |
void setOpacity(float o) |
| 86 |
{ |
| 87 |
m_opacity = o; |
| 88 |
if (o < 1.0f) |
| 89 |
setRenderPass(TranslucentPass); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Add a sphere to the geometry object. |
| 94 |
*/ |
| 95 |
void addSphere(const Vector3f& position, const Vector3ub& color, float radius, |
| 96 |
size_t index = MaxIndex); |
| 97 |
|
| 98 |
/** |
| 99 |
* Get a reference to the spheres. |
| 100 |
*/ |
| 101 |
Core::Array<SphereColor>& spheres() { return m_spheres; } |
| 102 |
const Core::Array<SphereColor>& spheres() const { return m_spheres; } |
| 103 |
|
| 104 |
/** |
| 105 |
* Clear the contents of the node. |
| 106 |
*/ |
| 107 |
void clear() override; |
| 108 |
|
| 109 |
/** |
| 110 |
* Get the number of spheres in the node object. |
| 111 |
*/ |
| 112 |
size_t size() const { return m_spheres.size(); } |
| 113 |
|
| 114 |
private: |
| 115 |
Core::Array<SphereColor> m_spheres; |
| 116 |
Core::Array<size_t> m_indices; |
| 117 |
|
| 118 |
bool m_dirty; |
| 119 |
|
| 120 |
float m_opacity = 1.0f; |
| 121 |
|
| 122 |
class Private; |
| 123 |
Private* d; |
| 124 |
}; |
| 125 |
|
| 126 |
inline SphereGeometry& SphereGeometry::operator=(SphereGeometry other) |
| 127 |
{ |
| 128 |
using std::swap; |
| 129 |
swap(*this, other); |
| 130 |
return *this; |
| 131 |
} |
| 132 |
|
| 133 |
inline void swap(SphereGeometry& lhs, SphereGeometry& rhs) |
| 134 |
{ |
| 135 |
using std::swap; |
| 136 |
swap(static_cast<Drawable&>(lhs), static_cast<Drawable&>(rhs)); |
| 137 |
swap(lhs.m_spheres, rhs.m_spheres); |
| 138 |
swap(lhs.m_indices, rhs.m_indices); |
| 139 |
lhs.m_dirty = rhs.m_dirty = true; |
| 140 |
} |
| 141 |
|
| 142 |
} // End namespace Rendering |
| 143 |
} // End namespace Avogadro |
| 144 |
|
| 145 |
#endif // AVOGADRO_RENDERING_SPHEREGEOMETRY_H |