diff --git a/src/ViewportWidget.cpp b/src/ViewportWidget.cpp index 0774467..fc3c953 100644 --- a/src/ViewportWidget.cpp +++ b/src/ViewportWidget.cpp @@ -30,6 +30,13 @@ #include #include +#include +#include +#include +#include +#include +#include + struct PntComparator { bool operator()(const gp_Pnt& a, const gp_Pnt& b) const { // A tolerance is needed for floating point comparisons @@ -649,6 +656,44 @@ void ViewportWidget::drawSketch(const SketchFeature* sketch) } } + // Draw faces + if (!sketch->shape().IsNull()) { + BRepMesh_IncrementalMesh(sketch->shape(), 0.1); // Linear deflection + + QVector faceVertices; + TopExp_Explorer explorer(sketch->shape(), TopAbs_FACE); + for (; explorer.More(); explorer.Next()) { + TopoDS_Face face = TopoDS::Face(explorer.Current()); + TopLoc_Location location; + Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation(face, location); + + if (!triangulation.IsNull()) { + const TColgp_Array1OfPnt& nodes = triangulation->Nodes(); + const Poly_Array1OfTriangle& triangles = triangulation->Triangles(); + + for (int i = 1; i <= triangles.Length(); ++i) { + const Poly_Triangle& triangle = triangles(i); + gp_Pnt p1 = nodes(triangle.Value(1)).Transformed(location); + gp_Pnt p2 = nodes(triangle.Value(2)).Transformed(location); + gp_Pnt p3 = nodes(triangle.Value(3)).Transformed(location); + faceVertices << p1.X() << p1.Y() << p1.Z(); + faceVertices << p2.X() << p2.Y() << p2.Z(); + faceVertices << p3.X() << p3.Y() << p3.Z(); + } + } + } + + if (!faceVertices.isEmpty()) { + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + m_shaderProgram->setUniformValue(m_colorLoc, QVector4D(0.5f, 0.5f, 1.0f, 0.5f)); + m_vbo.bind(); + m_vbo.allocate(faceVertices.constData(), faceVertices.size() * sizeof(GLfloat)); + glDrawArrays(GL_TRIANGLES, 0, faceVertices.size() / 3); + glDisable(GL_BLEND); + } + } + glEnable(GL_DEPTH_TEST); }