| 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 "overlayaxes.h" |
| 7 |
|
| 8 |
#include <avogadro/rendering/arrowgeometry.h> |
| 9 |
#include <avogadro/rendering/avogadrogl.h> |
| 10 |
#include <avogadro/rendering/camera.h> |
| 11 |
#include <avogadro/rendering/geometrynode.h> |
| 12 |
#include <avogadro/rendering/groupnode.h> |
| 13 |
#include <avogadro/rendering/scene.h> |
| 14 |
#include <avogadro/qtopengl/glwidget.h> |
| 15 |
|
| 16 |
#include <avogadro/core/vector.h> |
| 17 |
|
| 18 |
using Avogadro::Vector3f; |
| 19 |
using Avogadro::Vector3ub; |
| 20 |
using Avogadro::Rendering::ArrowGeometry; |
| 21 |
using Avogadro::Rendering::Camera; |
| 22 |
using Avogadro::Rendering::GeometryNode; |
| 23 |
using Avogadro::Rendering::GroupNode; |
| 24 |
using Eigen::Affine3f; |
| 25 |
|
| 26 |
namespace { |
| 27 |
|
| 28 |
// Arrow geometry class that overrides the camera used in rendering |
| 29 |
// to display in a corner of the screen. |
| 30 |
class OverlayArrowGeometry : public ArrowGeometry |
| 31 |
{ |
| 32 |
public: |
| 33 |
OverlayArrowGeometry() { setRenderPass(Avogadro::Rendering::Overlay3DPass); } |
| 34 |
~OverlayArrowGeometry() override {} |
| 35 |
|
| 36 |
void render(const Camera& camera) override; |
| 37 |
}; |
| 38 |
|
| 39 |
void OverlayArrowGeometry::render(const Camera& camera) |
| 40 |
{ |
| 41 |
// Swap in a new viewport/camera for the overlay |
| 42 |
// Keep the rotation, lose the translation: |
| 43 |
Affine3f mv(camera.modelView()); |
| 44 |
mv.matrix().block<3, 1>(0, 3) = Vector3f::Zero(); |
| 45 |
|
| 46 |
// Save the actual viewport - works better on high resolution screens |
| 47 |
GLint viewport[4]; |
| 48 |
glGetIntegerv(GL_VIEWPORT, viewport); |
| 49 |
|
| 50 |
// The largest window dimension, used to scale the axes |
| 51 |
const int maxDim = std::max(viewport[2], viewport[3]); |
| 52 |
|
| 53 |
Camera arrowCamera(camera); |
| 54 |
arrowCamera.setViewport(maxDim / 10, maxDim / 10); |
| 55 |
arrowCamera.setModelView(mv); |
| 56 |
// Reset orthographic scale so axes are constant size regardless of zoom |
| 57 |
arrowCamera.setOrthographicScale(1.0f); |
| 58 |
arrowCamera.calculateOrthographic(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f); |
| 59 |
|
| 60 |
glViewport(static_cast<GLint>(10), static_cast<GLsizei>(10), |
| 61 |
static_cast<GLint>(arrowCamera.width()), |
| 62 |
static_cast<GLsizei>(arrowCamera.height())); |
| 63 |
|
| 64 |
ArrowGeometry::render(arrowCamera); |
| 65 |
|
| 66 |
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); |
| 67 |
} |
| 68 |
|
| 69 |
} // namespace |
| 70 |
|
| 71 |
namespace Avogadro::QtPlugins { |
| 72 |
|
| 73 |
class OverlayAxes::RenderImpl |
| 74 |
{ |
| 75 |
public: |
| 76 |
RenderImpl(); |
| 77 |
~RenderImpl(); |
| 78 |
|
| 79 |
OverlayArrowGeometry* arrows; |
| 80 |
}; |
| 81 |
|
| 82 |
OverlayAxes::RenderImpl::RenderImpl() : arrows(new OverlayArrowGeometry) |
| 83 |
{ |
| 84 |
// Use thicker arrows for the overlay (closer to original mesh-based axes) |
| 85 |
arrows->setRadiusScale(3.0f); |
| 86 |
// Add the three axes arrows (origin to unit axis, each with its own color) |
| 87 |
const Vector3f origin(0.f, 0.f, 0.f); |
| 88 |
arrows->addSingleArrow(origin, Vector3f(1.f, 0.f, 0.f), Vector3ub(255, 0, 0)); |
| 89 |
arrows->addSingleArrow(origin, Vector3f(0.f, 1.f, 0.f), Vector3ub(0, 255, 0)); |
| 90 |
arrows->addSingleArrow(origin, Vector3f(0.f, 0.f, 1.f), Vector3ub(0, 0, 255)); |
| 91 |
} |
| 92 |
|
| 93 |
OverlayAxes::RenderImpl::~RenderImpl() |
| 94 |
{ |
| 95 |
delete arrows; |
| 96 |
} |
| 97 |
|
| 98 |
OverlayAxes::OverlayAxes(QObject* p) : ScenePlugin(p), m_render(new RenderImpl) |
| 99 |
{ |
| 100 |
m_layerManager = QtGui::PluginLayerManager(m_name); |
| 101 |
} |
| 102 |
|
| 103 |
OverlayAxes::~OverlayAxes() {} |
| 104 |
|
| 105 |
void OverlayAxes::process(const QtGui::Molecule&, Rendering::GroupNode& node) |
| 106 |
{ |
| 107 |
auto* geo = new GeometryNode; |
| 108 |
// Since our geometry doesn't change, we just make a copy of the pre-built |
| 109 |
// set of axes. |
| 110 |
geo->addDrawable(new OverlayArrowGeometry(*m_render->arrows)); |
| 111 |
node.addChild(geo); |
| 112 |
} |
| 113 |
|
| 114 |
} // namespace Avogadro::QtPlugins |