Debian Codegraph @

avogadro/rendering/glrenderer.h

raw
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_GLRENDERER_H
7 #define AVOGADRO_RENDERING_GLRENDERER_H
8
9 #include "avogadrorenderingexport.h"
10
11 #include "bufferobject.h"
12 #include "camera.h"
13 #include "primitive.h"
14 #include "scene.h"
15 #include "shader.h"
16 #include "shaderprogram.h"
17 #include "solidpipeline.h"
18 #include "volumegeometry.h"
19
20 #include <avogadro/core/array.h>
21
22 #include <map>
23 #include <string> // For member variables.
24 #include <vector>
25
26 namespace Avogadro {
27 namespace Rendering {
28 class GeometryNode;
29 class TextRenderStrategy;
30
31 /**
32 * @class GLRenderer glrenderer.h <avogadro/rendering/glrenderer.h>
33 * @brief Render the scene using OpenGL calls.
34 * @author Marcus D. Hanwell
35 */
36
37 class AVOGADRORENDERING_EXPORT GLRenderer
38 {
39 public:
40 GLRenderer();
41 ~GLRenderer();
42
43 /** Initialize the OpenGL context for rendering. */
44 void initialize();
45
46 /** Resize the context in response to window management events. */
47 void resize(int width, int height);
48
49 /** Set the ratio of physical to logical pixels. */
50 void setPixelRatio(float ratio);
51
52 /** Take care of rendering the scene, requires that the context is current. */
53 void render();
54
55 /** Reset the view to fit the entire scene. */
56 void resetCamera();
57
58 /**
59 * Reset the scene geometry, this should be done when the scene geometry has
60 * changed in order to ensure correct clipping.
61 */
62 void resetGeometry();
63
64 /** Return the primitives under the display coordinate (x,y), mapped by depth.
65 */
66 std::multimap<float, Identifier> hits(int x, int y) const;
67
68 /** Return the top primitive under the display coordinate (x,y).
69 */
70 Identifier hit(int x, int y) const;
71
72 /** Return the depth of provided ray - geometry hit test.
73 */
74 float hit(const Vector3f& rayOrigin, const Vector3f& rayEnd,
75 const Vector3f& rayDirection) const;
76
77 /** Return the primitives in the rectangular area provided. */
78 Core::Array<Identifier> hits(int x1, int y1, int x2, int y2) const;
79
80 /** Check whether the GL context is valid and supports required features.
81 * \sa error() to get more information if the context is not valid.
82 */
83 bool isValid() const { return m_valid; }
84
85 /** Get the error message if the context is not valid. Empty if valid. */
86 std::string error() const { return m_error; }
87
88 /** Get the camera for this renderer. */
89 const Camera& camera() const;
90 Camera& camera();
91
92 /** Get the overlay camera. */
93 const Camera& overlayCamera() const;
94 Camera& overlayCamera();
95
96 /** Get the scene for this renderer. */
97 const Scene& scene() const { return m_scene; }
98 Scene& scene() { return m_scene; }
99
100 /** Get the solid pipeline for this renderer. */
101 const SolidPipeline& solidPipeline() const { return m_solidPipeline; }
102 SolidPipeline& solidPipeline() { return m_solidPipeline; }
103
104
105 const VolumeGeometry& volume() const { return m_volume; }
106 VolumeGeometry& volume() { return m_volume; }
107 /**
108 * Get/set the text rendering strategy for this object. The renderer takes
109 * ownership of the strategy object. @{
110 */
111 const TextRenderStrategy* textRenderStrategy() const;
112 TextRenderStrategy* textRenderStrategy();
113 void setTextRenderStrategy(TextRenderStrategy* tren);
114 /** @} */
115
116 std::array<float, 6> m_perspectiveFrustum; // L, R, B, T, N, F (planes order)
117 std::array<float, 6> m_orthographicFrustum; // L, R, B, T, N, F (planes order)
118 #ifdef _3DCONNEXION
119 bool m_drawIcon;
120 void* m_iconData;
121 uint32_t m_iconWidth;
122 uint32_t m_iconHeight;
123 Eigen::Vector3f m_iconPosition;
124 #endif
125
126 private:
127 /**
128 * Apply the projection matrix.
129 */
130 void applyProjection();
131
132 /**
133 * @brief Detect hits in a group node.
134 */
135 std::multimap<float, Identifier> hits(const GroupNode* group,
136 const Vector3f& rayOrigin,
137 const Vector3f& rayEnd,
138 const Vector3f& rayDirection) const;
139
140 /**
141 * @brief Detect hits in a geometry node.
142 */
143 std::multimap<float, Identifier> hits(const GeometryNode* geometry,
144 const Vector3f& rayOrigin,
145 const Vector3f& rayEnd,
146 const Vector3f& rayDirection) const;
147
148 Core::Array<Identifier> hits(const GroupNode* group,
149 const Frustrum& frustrum) const;
150
151 bool m_valid;
152 std::string m_error;
153 Camera m_camera;
154 Camera m_overlayCamera;
155 Scene m_scene;
156 TextRenderStrategy* m_textRenderStrategy;
157 SolidPipeline m_solidPipeline;
158 VolumeGeometry m_volume;
159
160 Vector3f m_center;
161 float m_radius;
162 };
163
164 inline const Camera& GLRenderer::camera() const
165 {
166 return m_camera;
167 }
168
169 inline Camera& GLRenderer::camera()
170 {
171 return m_camera;
172 }
173
174 inline const Camera& GLRenderer::overlayCamera() const
175 {
176 return m_camera;
177 }
178
179 inline Camera& GLRenderer::overlayCamera()
180 {
181 return m_camera;
182 }
183
184 inline const TextRenderStrategy* GLRenderer::textRenderStrategy() const
185 {
186 return m_textRenderStrategy;
187 }
188
189 inline TextRenderStrategy* GLRenderer::textRenderStrategy()
190 {
191 return m_textRenderStrategy;
192 }
193
194 inline Identifier GLRenderer::hit(int x, int y) const
195 {
196 std::multimap<float, Identifier> results = hits(x, y);
197 if (results.size())
198 return results.begin()->second;
199 return Identifier();
200 }
201
202 } // namespace Rendering
203 } // namespace Avogadro
204
205 #endif // AVOGADRO_RENDERING_GLRENDERER_H