1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
//! \file CGAL_Lab_example_plugin.cpp
#include <QApplication>
#include <QMainWindow>
#include <QAction>
#include <QVector>
#include <CGAL/Three/Scene_item_rendering_helper.h>
#include <CGAL/Three/Viewer_interface.h>
#include <CGAL/Three/Scene_group_item.h>
#include <CGAL/Three/Triangle_container.h>
//! [itemdeclaration]
// The special Scene_item only for triangles
using namespace CGAL::Three;
typedef Triangle_container Tc;
typedef Viewer_interface VI;
class Scene_triangle_item
: public CGAL::Three::Scene_item_rendering_helper
{
Q_OBJECT
public :
Scene_triangle_item(double ax,double ay, double az,
double bx,double by, double bz,
double cx,double cy, double cz);
// Indicates if rendering mode is supported
bool supportsRenderingMode(RenderingMode m) const override {
return (m == Flat);
}
//Displays the item
void draw(CGAL::Three::Viewer_interface* viewer) const override;
//Specifies that the buffers need to be initialized again.
//Is mostly called after a change of geometry in the data.
void invalidateOpenGLBuffers() override;
//fills the std::vector
void computeElements() const override;
Scene_item* clone() const override {return 0;}
QString toolTip() const override {return QString();}
void compute_bbox() const override { _bbox = Bbox(); }
private:
//contains the data
mutable std::vector<float> vertices;
mutable std::size_t nb_pos;
//Fills the buffers with data. The buffers allow us to give data to the shaders.
void initializeBuffers(Viewer_interface *viewer) const override;
}; //end of class Scene_triangle_item
//! [itemdeclaration]
Scene_triangle_item::Scene_triangle_item(double ax,double ay, double az,
double bx,double by, double bz,
double cx,double cy, double cz)
{
//! [creation]
//Prepare a single TriangleContainer, as we will only use one program.
setTriangleContainer(0, new Tc(VI::PROGRAM_NO_SELECTION,
false));
//! [creation]
//! [computeelements]
//Fills the position vector with data.
nb_pos = 0;
vertices.resize(9);
vertices[0] = ax; vertices[1] = ay; vertices[2] = az;
vertices[3] = bx; vertices[4] = by; vertices[5] = bz;
vertices[6] = cx; vertices[7] = cy; vertices[8] = cz;
nb_pos=vertices.size();
//! [computeelements]
//be sure the data will be computed next draw call
invalidateOpenGLBuffers();
}
//prepare the TriangleContainer with the computed data.
void Scene_triangle_item::computeElements()const
{
//! [allocateelements]
getTriangleContainer(0)->allocate(Tc::Flat_vertices, vertices.data(),
static_cast<int>(vertices.size()
* sizeof(float)));
setBuffersFilled(true);
//! [allocateelements]
}
//! [draw]
void Scene_triangle_item::draw(CGAL::Three::Viewer_interface* viewer) const
{
//Initializes the OpenGL context for `viewer` if needed.
if(!isInit(viewer))
initGL(viewer);
if ( getBuffersFilled() &&
! getBuffersInit(viewer))
{
initializeBuffers(viewer);
setBuffersInit(viewer, true);
}
if(!getBuffersFilled())
{
computeElements();
initializeBuffers(viewer);
}
//set the uniform properties for the TriangleContainer.
//Uniform values are set at each draw call and are defined for the whole item.
//Values per simplex are computed as buffers in ComputeElements() and bound in initializeBuffers().
getTriangleContainer(0)->setColor(this->color());
getTriangleContainer(0)->draw(viewer, true);
}
//! [draw]
//Specifies that the buffers need to be initialized again.
//Is mostly called after a change of geometry in the data.
void Scene_triangle_item::invalidateOpenGLBuffers()
{
setBuffersFilled(false);
getTriangleContainer(0)->reset_vbos(ALL);
}
//! [fillbuffers]
void Scene_triangle_item::initializeBuffers(CGAL::Three::Viewer_interface *viewer)const
{
//Bind the buffers filled in ComputeElements() for the TriangleContainer.
getTriangleContainer(0)->initializeBuffers(viewer);
getTriangleContainer(0)->setFlatDataSize(nb_pos);
//once the buffers are bound, we can clear the vectors to optimize memory consumption
vertices.clear();
vertices.shrink_to_fit();
}
//! [fillbuffers]
#include <CGAL/Three/CGAL_Lab_plugin_helper.h>
//The actual plugin
using namespace CGAL::Three;
class Q_DECL_EXPORT CGAL_Lab_example_plugin :
public QObject,
public CGAL_Lab_plugin_helper
{
//Configures CMake to use MOC correctly
Q_OBJECT
Q_INTERFACES(CGAL::Three::CGAL_Lab_plugin_interface)
Q_PLUGIN_METADATA(IID "com.geometryfactory.CGALLab.PluginInterface/1.0")
public :
// Adds an action to the menu and configures the widget
void init(QMainWindow* mainWindow, CGAL::Three::Scene_interface* scene_interface, Messages_interface*) override{
//get the references
this->scene = scene_interface;
this->mw = mainWindow;
//creates and link the actions
QAction* actionDrawTriangle= new QAction("Draw Triangle", mw);
if(actionDrawTriangle) {
connect(actionDrawTriangle, SIGNAL(triggered()),
this, SLOT(draw_triangle()));
_actions << actionDrawTriangle;
}
}
bool applicable(QAction*) const override
{
return true;
}
QList<QAction*> actions() const override{
return _actions;
}
public Q_SLOTS:
void draw_triangle() {
triangle = new Scene_triangle_item(0, 0, 0,
1, 0, 0,
0.5, 0.5, 0);
triangle->setName(QString("Basic triangle"));
scene->addItem(triangle);
}
private:
CGAL::Three::Scene_item* triangle;
QList<QAction*> _actions;
}; //end of class CGAL_Lab_example_plugin
#include "Example_plugin.moc"
|