1 /******************************************************************************
2
3 This source file is part of the Avogadro project.
4
5 Copyright 2013 Kitware, Inc.
6
7 This source code is released under the New BSD License, (the "License").
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 ******************************************************************************/
16
17 #ifndef AVOGADRO_RENDERING_DRAWABLE_H
18 #define AVOGADRO_RENDERING_DRAWABLE_H
19
20 #include "avogadrorenderingexport.h"
21
22 #include "avogadrorendering.h"
23 #include "primitive.h"
24
25 #include <avogadro/core/array.h>
26 #include <avogadro/core/vector.h>
27
28 #include <map>
29
30 namespace Avogadro {
31 namespace Rendering {
32
33 class Camera;
34 class GeometryNode;
35 class Visitor;
36
37 /**
38 * @class Drawable drawable.h <avogadro/rendering/drawable.h>
39 * @brief The base class for all drawable geometry and types.
40 * @author Marcus D. Hanwell
41 *
42 * This class provides the common API for drawable objects on the scene. It is
43 * not a Node object, and can only be attached to GeometryNode objects in the
44 * Scene.
45 */
46
47 class AVOGADRORENDERING_EXPORT Drawable
48 {
49 public:
50 Drawable();
51 Drawable(const Drawable& other);
52 virtual ~Drawable();
53
54 Drawable& operator=(Drawable);
55 friend void swap(Drawable& lhs, Drawable& rhs);
56
57 /**
58 * Accept a visit from our friendly visitor.
59 */
60 virtual void accept(Visitor&);
61
62 /**
63 * @brief Get a pointer to the drawable object's parent.
64 * @return Pointer to the parent node, nullptr if no parent.
65 */
parent()66 const GeometryNode* parent() const { return m_parent; }
parent()67 GeometryNode* parent() { return m_parent; }
68
69 /**
70 * @brief Set the visibility of the drawable object.
71 * @param visibility True if the drawable is visible, false if invisible.
72 */
setVisible(bool visibility)73 void setVisible(bool visibility) { m_visible = visibility; }
74
75 /**
76 * @brief Get the current visibility of the drawable.
77 * @return True if visible.
78 */
isVisible()79 bool isVisible() const { return m_visible; }
80
81 /**
82 * The render pass in which this drawable should be rendered.
83 * @sa Rendering::RenderPass
84 * @{
85 */
setRenderPass(RenderPass pass)86 void setRenderPass(RenderPass pass) { m_renderPass = pass; }
renderPass()87 RenderPass renderPass() const { return m_renderPass; }
88 /** @} */
89
90 /**
91 * @brief Render the contents of the drawable.
92 * @param camera The current Camera.
93 */
94 virtual void render(const Camera& camera);
95
96 /**
97 * Get the identifier for the object, this stores the parent Molecule and
98 * the type represented by the geometry.
99 */
identifier()100 Identifier& identifier() { return m_identifier; }
identifier()101 const Identifier& identifier() const { return m_identifier; }
102
103 /**
104 * Return the primitives that are hit by the ray.
105 * @param rayOrigin Origin of the ray.
106 * @param rayEnd End point of the ray.
107 * @param rayDirection Normalized direction of the ray.
108 * @return Sorted collection of primitives that were hit.
109 */
110 virtual std::multimap<float, Identifier> hits(
111 const Vector3f& rayOrigin, const Vector3f& rayEnd,
112 const Vector3f& rayDirection) const;
113
114 /**
115 * Return the primitives within the supplied area.
116 * @param f The frustrum defining the area highlighted.
117 * @return Collection of primitives in the area.
118 */
119 virtual Core::Array<Identifier> areaHits(const Frustrum& f) const;
120
121 /**
122 * Clear the contents of the node.
123 */
124 virtual void clear();
125
126 protected:
127 friend class GeometryNode;
128
129 /**
130 * @brief Set the parent node for the node.
131 * @param parent The parent, a value of nullptr denotes no parent node.
132 */
133 void setParent(GeometryNode* parent);
134
135 GeometryNode* m_parent;
136 bool m_visible;
137 RenderPass m_renderPass;
138 Identifier m_identifier;
139 };
140
141 inline Drawable& Drawable::operator=(Drawable rhs)
142 {
143 using std::swap;
144 swap(*this, rhs);
145 return *this;
146 }
147
swap(Drawable & lhs,Drawable & rhs)148 inline void swap(Drawable& lhs, Drawable& rhs)
149 {
150 using std::swap;
151 swap(lhs.m_parent, rhs.m_parent);
152 swap(lhs.m_visible, rhs.m_visible);
153 swap(lhs.m_renderPass, rhs.m_renderPass);
154 swap(lhs.m_identifier, rhs.m_identifier);
155 }
156
157 } // End namespace Rendering
158 } // End namespace Avogadro
159
160 #endif // AVOGADRO_RENDERING_SPHERENODE_H
161