From dbb5c077090c7162b30ffde58a0a2547eac6c03f Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Tue, 1 Dec 2015 12:42:20 +0100 Subject: [PATCH] Draw text done. WIP Blending --- .../demo/Polyhedron/Scene_polyhedron_item.cpp | 7 +++ Polyhedron/demo/Polyhedron/Viewer.cpp | 41 +++++++++++++++- Polyhedron/demo/Polyhedron/Viewer.h | 49 +++++++++++++++++++ .../include/CGAL/IO/Polyhedron_scan_OFF.h | 1 + 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp b/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp index f715f405970..45ce88dc13c 100644 --- a/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp @@ -36,6 +36,9 @@ #include #include + +#include + namespace PMP = CGAL::Polygon_mesh_processing; @@ -832,6 +835,7 @@ void Scene_polyhedron_item::set_erase_next_picked_facet(bool b) } void Scene_polyhedron_item::draw(CGAL::Three::Viewer_interface* viewer) const { + if(!are_buffers_filled) { compute_normals_and_vertices(); @@ -862,6 +866,9 @@ void Scene_polyhedron_item::draw(CGAL::Three::Viewer_interface* viewer) const { vaos[Facets]->release(); else vaos[Gouraud_Facets]->release(); + + viewer->qglColor(Qt::black); + viewer->drawText(viewer->width()/2, viewer->height()/2, "CENTER"); } // Points/Wireframe/Flat/Gouraud OpenGL drawing in a display list diff --git a/Polyhedron/demo/Polyhedron/Viewer.cpp b/Polyhedron/demo/Polyhedron/Viewer.cpp index 2e4c47ce637..dcc98330616 100644 --- a/Polyhedron/demo/Polyhedron/Viewer.cpp +++ b/Polyhedron/demo/Polyhedron/Viewer.cpp @@ -806,7 +806,17 @@ void Viewer::drawVisualHints() rendering_program.release(); vao[0].release(); } - +bool has_text = false; + if(has_text) + { + TextRenderer *textRenderer = new TextRenderer(); + for(int i=0; i<3; i++) + { + float x = 30*i, y = 120, z=0; + textRenderer->addText(new TextItem(x,y,z,"Under Testing")); + } + textRenderer->draw(this); + } } void Viewer::resizeGL(int w, int h) @@ -1131,3 +1141,32 @@ void Viewer::wheelEvent(QWheelEvent* e) else QGLViewer::wheelEvent(e); } + +void TextRenderer::draw(CGAL::Three::Viewer_interface *viewer) +{ + QPainter painter(viewer); + QRect rect; + qglviewer::Camera* camera = viewer->camera(); + //fbo = new QOpenGLFramebufferObject(viewer->width(), viewer->height(),QOpenGLFramebufferObject::Depth); + //fbo->bind(); + //viewer->glEnable(GL_DEPTH_TEST); + //viewer->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + Q_FOREACH(TextItem* item, textItems) + { + qglviewer::Vec src(item->position()->x(), item->position()->y(),item->position()->z()); + rect = QRect(camera->projectedCoordinatesOf(src).x-item->width()/2, + camera->projectedCoordinatesOf(src).y-item->height()/2, + item->width(), + item->height()); + painter.setFont(item->font()); + painter.drawText(rect, item->text()); + } + + +} + + void TextRenderer::addText(TextItem *ti) + { + textItems.push_back(ti); + } + diff --git a/Polyhedron/demo/Polyhedron/Viewer.h b/Polyhedron/demo/Polyhedron/Viewer.h index 53ed0360903..b5df436a1c6 100644 --- a/Polyhedron/demo/Polyhedron/Viewer.h +++ b/Polyhedron/demo/Polyhedron/Viewer.h @@ -11,6 +11,8 @@ #include #include +#include +#include // forward declarations class QWidget; @@ -92,6 +94,7 @@ protected: std::vector *normals; std::vector *colors; }; + //! The buffers used to draw the axis system QOpenGLBuffer buffers[3]; //! The VAO used to draw the axis system @@ -133,4 +136,50 @@ protected: double prev_radius; }; // end class Viewer +//!This class holds the properties of each line of text to be rendered. +class TextItem{ +public : + TextItem(float p_x, float p_y, float p_z, QString p_text, QFont font = QFont(), QColor p_color = Qt::black) + :x(p_x), y(p_y), z(p_z), m_text(p_text), m_font(font), color(p_color) + { + QFontMetrics fm(m_font); + _width = fm.width(m_text); + _height = fm.height(); + } + QString text()const {return m_text;} + //!Returns the position of the center of the text, in world coordinates. + QVector3D *position(){return new QVector3D(x,y,z);} + float width(){return _width;} + float height(){return _height;} + QFont font(){return m_font;} +private: + float x; + float y; + float z; + float _width; + float _height; + QString m_text; + QFont m_font; + QColor color; +};//end class TextItem + +//!This class draws all the textItems. +class TextRenderer{ +public: + TextRenderer() + { + textItems.resize(0); + } + /*! + * Projects each textItem from the world coordinates to the Screen coordinates + * and draws it. + */ + void draw(CGAL::Three::Viewer_interface* viewer); + void addText(TextItem* ti); + +private: + std::vector textItems; + QOpenGLFramebufferObject *fbo; + +};//end class TextRenderer #endif // VIEWER_H diff --git a/Polyhedron_IO/include/CGAL/IO/Polyhedron_scan_OFF.h b/Polyhedron_IO/include/CGAL/IO/Polyhedron_scan_OFF.h index 0d23b13f049..d862822e9ea 100644 --- a/Polyhedron_IO/include/CGAL/IO/Polyhedron_scan_OFF.h +++ b/Polyhedron_IO/include/CGAL/IO/Polyhedron_scan_OFF.h @@ -114,6 +114,7 @@ Polyhedron_scan_OFF:: operator()( HDS& target) { scanner.scan_facet_vertex_index( index, i); B.add_vertex_to_facet( index); } + //TO DO : Insert read color B.end_facet(); scanner.skip_to_next_facet( i); }