Debian Codegraph @

avogadro/rendering/glrenderer.cpp

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 #include "glrenderer.h"
7
8 #include "avogadrogl.h"
9
10 #include "geometrynode.h"
11 #include "glrendervisitor.h"
12 #include "shader.h"
13 #include "shaderprogram.h"
14 #include "textlabel2d.h"
15 #include "textlabel3d.h"
16 #include "textrenderstrategy.h"
17 #include "visitor.h"
18
19 #include <avogadro/core/matrix.h>
20
21 #include <iostream>
22
23 namespace Avogadro::Rendering {
24
25 using Core::Array;
26
27 GLRenderer::GLRenderer()
28 : m_valid(false), m_textRenderStrategy(nullptr), m_center(Vector3f::Zero()),
29 m_radius(20.0)
30 #ifdef _3DCONNEXION
31 ,
32 m_drawIcon(false), m_iconData(nullptr), m_iconWidth(0u), m_iconHeight(0u),
33 m_iconPosition(Eigen::Vector3f::Zero())
34 #endif
35 {
36 m_overlayCamera.setIdentity();
37
38 float aspectRatio = static_cast<float>(m_camera.width()) /
39 static_cast<float>(m_camera.height());
40 float distance = m_camera.distance(m_center);
41 float offset = distance + m_radius;
42 m_perspectiveFrustum = {
43 -aspectRatio, aspectRatio, -1.0f, 1.0f, 2.0f, offset
44 };
45 m_orthographicFrustum = {
46 -5.0f * aspectRatio, 5.0f * aspectRatio, -5.0f, 5.0f, -offset, offset
47 };
48 }
49
50 GLRenderer::~GLRenderer()
51 {
52 delete m_textRenderStrategy;
53 }
54
55 void GLRenderer::initialize()
56 {
57 GLenum result = glewInit();
58 m_valid = (result == GLEW_OK || result == GLEW_ERROR_NO_GLX_DISPLAY);
59 if (!m_valid) {
60 m_error += "GLEW could not be initialized.\n";
61 return;
62 }
63
64 if (!GLEW_VERSION_2_0) {
65 m_error += "GL version 2.0 is not supported by your graphics driver.\n";
66 m_valid = false;
67 return;
68 }
69
70 m_solidPipeline.initialize();
71 }
72
73 void GLRenderer::resize(int width, int height)
74 {
75 if (!m_valid)
76 return;
77
78 // m_volume.resize(width, height);
79 glViewport(0, 0, static_cast<GLint>(width), static_cast<GLint>(height));
80 m_camera.setViewport(width, height);
81 m_overlayCamera.setViewport(width, height);
82 m_solidPipeline.resize(width, height);
83 }
84
85 void GLRenderer::setPixelRatio(float ratio)
86 {
87 m_solidPipeline.setPixelRatio(ratio);
88 }
89
90 void GLRenderer::render()
91 {
92 if (!m_valid)
93 return;
94
95 Vector4ub c = m_scene.backgroundColor();
96 glClearColor(c[0] / 255.0f, c[1] / 255.0f, c[2] / 255.0f, c[3] / 255.0f);
97 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
98 applyProjection();
99
100 GLRenderVisitor visitor(m_camera, m_textRenderStrategy);
101 // Setup for solid geometry
102 // m_volume.begin()
103 m_solidPipeline.begin();
104 visitor.setRenderPass(SolidPass);
105 glEnable(GL_DEPTH_TEST);
106 glDisable(GL_BLEND);
107 m_scene.rootNode().accept(visitor);
108 m_solidPipeline.end();
109 m_solidPipeline.adjustOffset(m_camera);
110
111 // Setup for opaque geometry
112 visitor.setRenderPass(OpaquePass);
113 m_scene.rootNode().accept(visitor);
114
115 // Setup for transparent geometry
116 visitor.setRenderPass(TranslucentPass);
117 glEnable(GL_BLEND);
118 // nvidia drivers have a bug where they don't like blending
119 // so on Mac we can use this (they don't use nvidia)
120 #ifdef __APPLE__
121 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
122 #else
123 // Thanks to Giuseppe D'Angelo for a related comment:
124 // https://bugreports.qt.io/browse/QTBUG-36739
125 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
126 #endif
127 m_scene.rootNode().accept(visitor);
128
129 // Setup for 3d overlay rendering
130 visitor.setRenderPass(Overlay3DPass);
131 glClear(GL_DEPTH_BUFFER_BIT);
132 m_scene.rootNode().accept(visitor);
133
134 // Setup for 2d overlay rendering
135 visitor.setRenderPass(Overlay2DPass);
136 visitor.setCamera(m_overlayCamera);
137 glDisable(GL_DEPTH_TEST);
138 m_scene.rootNode().accept(visitor);
139
140 #ifdef _3DCONNEXION
141 if (m_drawIcon && (m_iconData != nullptr)) {
142 glPushMatrix();
143 Eigen::Vector4f pivotPosition =
144 m_camera.projection().matrix() * m_camera.modelView().matrix() *
145 Eigen::Vector4f(m_iconPosition.x(), m_iconPosition.y(),
146 m_iconPosition.z(), 1.0);
147 pivotPosition /= pivotPosition.w();
148 glRasterPos3d(pivotPosition.x(), pivotPosition.y(), pivotPosition.z());
149 glPixelZoom(1.0f, -1.0f);
150 glBitmap(0.0f, 0.0f, 0.0f, 0.0f, -static_cast<float>(m_iconWidth >> 1),
151 static_cast<float>(m_iconHeight >> 1), NULL);
152 glDrawPixels(m_iconWidth, m_iconHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
153 m_iconData);
154 glPopMatrix();
155 }
156 #endif
157 }
158
159 void GLRenderer::resetCamera()
160 {
161 resetGeometry();
162 m_camera.setFocus(m_center);
163 m_camera.setIdentity();
164 m_camera.translate(-m_center);
165 m_camera.preTranslate(-2.22f * m_radius * Vector3f::UnitZ());
166
167 if (m_camera.projectionType() == Orthographic) {
168 // The base orthographic frustum has half-height of 5.0
169 // We want the scene radius to fit comfortably with some padding
170 float baseHalfHeight = 5.0f;
171 float optimalScale = m_radius / baseHalfHeight;
172 m_camera.setOrthographicScale(optimalScale);
173 }
174 }
175
176 void GLRenderer::resetGeometry()
177 {
178 m_scene.setDirty(true);
179 if (m_camera.focus()(0) != m_camera.focus()(0) ||
180 m_camera.focus() == m_center)
181 m_camera.setFocus(m_scene.center());
182 m_center = m_scene.center();
183 m_radius = m_scene.radius();
184 }
185
186 void GLRenderer::setTextRenderStrategy(TextRenderStrategy* tren)
187 {
188 if (tren != m_textRenderStrategy) {
189 // Force all labels to be regenerated on the next render:
190 class ResetTextLabelVisitor : public Visitor
191 {
192 public:
193 void visit(Node&) override { return; }
194 void visit(GroupNode&) override { return; }
195 void visit(GeometryNode&) override { return; }
196 void visit(Drawable&) override { return; }
197 void visit(SphereGeometry&) override { return; }
198 void visit(AmbientOcclusionSphereGeometry&) override { return; }
199 void visit(CurveGeometry&) override { return; }
200 void visit(CylinderGeometry&) override { return; }
201 void visit(MeshGeometry&) override { return; }
202 void visit(Texture2D&) { return; }
203 void visit(TextLabel2D& l) override { l.resetTexture(); }
204 void visit(TextLabel3D& l) override { l.resetTexture(); }
205 void visit(LineStripGeometry&) override { return; }
206 } labelResetter;
207
208 m_scene.rootNode().accept(labelResetter);
209
210 delete m_textRenderStrategy;
211 m_textRenderStrategy = tren;
212 }
213 }
214
215 void GLRenderer::applyProjection()
216 {
217 float distance = m_camera.distance(m_center);
218 float aspectRatio = static_cast<float>(m_camera.width()) /
219 static_cast<float>(m_camera.height());
220 if (m_camera.projectionType() == Perspective) {
221 m_perspectiveFrustum[0] = m_perspectiveFrustum[2] * aspectRatio;
222 m_perspectiveFrustum[1] = m_perspectiveFrustum[3] * aspectRatio;
223 m_perspectiveFrustum[5] = distance + m_radius;
224 m_camera.calculatePerspective(
225 m_perspectiveFrustum[0], m_perspectiveFrustum[1], m_perspectiveFrustum[2],
226 m_perspectiveFrustum[3], m_perspectiveFrustum[4],
227 m_perspectiveFrustum[5]);
228 } else {
229 // Renders the orthographic projection of the molecule
230 m_orthographicFrustum[0] = m_orthographicFrustum[2] * aspectRatio;
231 m_orthographicFrustum[1] = m_orthographicFrustum[3] * aspectRatio;
232 m_orthographicFrustum[5] = distance + m_radius;
233 m_orthographicFrustum[4] = -m_orthographicFrustum[5];
234 m_camera.calculateOrthographic(m_orthographicFrustum[0], // L
235 m_orthographicFrustum[1], // R
236 m_orthographicFrustum[2], // B
237 m_orthographicFrustum[3], // T
238 m_orthographicFrustum[4], // N
239 m_orthographicFrustum[5]); // F
240 }
241 m_overlayCamera.calculateOrthographic(
242 0.f, static_cast<float>(m_overlayCamera.width()), 0.f,
243 static_cast<float>(m_overlayCamera.height()), -1.f, 1.f);
244 }
245
246 std::multimap<float, Identifier> GLRenderer::hits(
247 const GroupNode* group, const Vector3f& rayOrigin, const Vector3f& rayEnd,
248 const Vector3f& rayDirection) const
249 {
250 std::multimap<float, Identifier> result;
251 if (!group)
252 return result;
253
254 for (auto it : group->children()) {
255 std::multimap<float, Identifier> loopHits;
256 const Node* itNode = it.node;
257 const auto* childGroup = dynamic_cast<const GroupNode*>(itNode);
258 if (childGroup) {
259 loopHits = hits(childGroup, rayOrigin, rayEnd, rayDirection);
260 result.insert(loopHits.begin(), loopHits.end());
261 continue;
262 }
263 const auto* childGeometry = itNode->cast<GeometryNode>();
264 if (childGeometry) {
265 loopHits = hits(childGeometry, rayOrigin, rayEnd, rayDirection);
266 result.insert(loopHits.begin(), loopHits.end());
267 continue;
268 }
269 }
270 return result;
271 }
272
273 std::multimap<float, Identifier> GLRenderer::hits(
274 const GeometryNode* geometry, const Vector3f& rayOrigin,
275 const Vector3f& rayEnd, const Vector3f& rayDirection) const
276 {
277 if (!geometry)
278 return std::multimap<float, Identifier>();
279 return geometry->hits(rayOrigin, rayEnd, rayDirection);
280 }
281
282 std::multimap<float, Identifier> GLRenderer::hits(int x, int y) const
283 {
284 // Our ray:
285 const Vector3f origin(m_camera.unProject(
286 Vector3f(static_cast<float>(x), static_cast<float>(y), 0.f)));
287 const Vector3f end(m_camera.unProject(
288 Vector3f(static_cast<float>(x), static_cast<float>(y), 1.f)));
289 const Vector3f direction((end - origin).normalized());
290
291 return hits(&m_scene.rootNode(), origin, end, direction);
292 }
293
294 Array<Identifier> GLRenderer::hits(const GroupNode* group,
295 const Frustrum& f) const
296 {
297 Array<Identifier> result;
298
299 for (auto it : group->children()) {
300 Array<Identifier> loopHits;
301 const Node* itNode = it.node;
302 const auto* childGroup = dynamic_cast<const GroupNode*>(itNode);
303 if (childGroup) {
304 loopHits = hits(childGroup, f);
305 result.insert(result.end(), loopHits.begin(), loopHits.end());
306 continue;
307 }
308 const auto childGeometry = itNode->cast<GeometryNode>();
309 if (childGeometry) {
310 loopHits = childGeometry->areaHits(f);
311 result.insert(result.end(), loopHits.begin(), loopHits.end());
312 continue;
313 }
314 }
315
316 return result;
317 }
318
319 float GLRenderer::hit(const Vector3f& rayOrigin, const Vector3f& rayEnd,
320 const Vector3f& rayDirection) const
321 {
322 std::multimap<float, Identifier> results =
323 hits(&m_scene.rootNode(), rayOrigin, rayEnd, rayDirection);
324 if (results.size())
325 return results.begin()->first;
326 return std::numeric_limits<float>::max();
327 }
328
329 Array<Identifier> GLRenderer::hits(int x1, int y1, int x2, int y2) const
330 {
331 // Figure out where the corners of our rectangle are.
332 Frustrum f;
333
334 f.points[0] = m_camera.unProject(
335 Vector3f(static_cast<float>(x1), static_cast<float>(y1), 0.f));
336 f.points[1] = m_camera.unProject(
337 Vector3f(static_cast<float>(x1), static_cast<float>(y1), 1.f));
338 f.points[2] = m_camera.unProject(
339 Vector3f(static_cast<float>(x1), static_cast<float>(y2), 0.f));
340 f.points[3] = m_camera.unProject(
341 Vector3f(static_cast<float>(x1), static_cast<float>(y2), 1.f));
342 f.points[4] = m_camera.unProject(
343 Vector3f(static_cast<float>(x2), static_cast<float>(y2), 0.f));
344 f.points[5] = m_camera.unProject(
345 Vector3f(static_cast<float>(x2), static_cast<float>(y2), 1.f));
346 f.points[6] = m_camera.unProject(
347 Vector3f(static_cast<float>(x2), static_cast<float>(y1), 0.f));
348 f.points[7] = m_camera.unProject(
349 Vector3f(static_cast<float>(x2), static_cast<float>(y1), 1.f));
350
351 if (m_camera.projectionType() == Perspective) {
352 f.planes[0] = (f.points[0] - f.points[1]).cross(f.points[2] - f.points[3]);
353 f.planes[1] = (f.points[2] - f.points[3]).cross(f.points[4] - f.points[5]);
354 f.planes[2] = (f.points[4] - f.points[5]).cross(f.points[6] - f.points[7]);
355 f.planes[3] = (f.points[6] - f.points[7]).cross(f.points[0] - f.points[1]);
356 } else {
357 // Orthographic: planes are parallel, defined by edges and view direction
358
359 // Left plane: normal points inward (to the right)
360 Vector3f edge01 = (f.points[1] - f.points[0]).normalized();
361 Vector3f edge02 = (f.points[2] - f.points[0]).normalized();
362 f.planes[0] = edge01.cross(edge02).normalized();
363
364 // Bottom plane: normal points inward (upward)
365 Vector3f edge23 = (f.points[3] - f.points[2]).normalized();
366 Vector3f edge24 = (f.points[4] - f.points[2]).normalized();
367 f.planes[1] = edge23.cross(edge24).normalized();
368
369 // Right plane: normal points inward (to the left)
370 Vector3f edge45 = (f.points[5] - f.points[4]).normalized();
371 Vector3f edge46 = (f.points[6] - f.points[4]).normalized();
372 f.planes[2] = edge45.cross(edge46).normalized();
373
374 // Top plane: normal points inward (downward)
375 Vector3f edge67 = (f.points[7] - f.points[6]).normalized();
376 Vector3f edge60 = (f.points[0] - f.points[6]).normalized();
377 f.planes[3] = edge67.cross(edge60).normalized();
378 }
379
380 return hits(&m_scene.rootNode(), f);
381 }
382
383 } // namespace Avogadro::Rendering