Kstars

polynode.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
3 SPDX-License-Identifier: GPL-2.0-or-later
4*/
5
6#include "polynode.h"
7
8#include <QPolygon>
9#include <QSGFlatColorMaterial>
10#include <QSGGeometry>
11#include <QSGGeometryNode>
12
13extern "C" {
14#include "libtess/tessellate.h"
15}
16
17PolyNode::PolyNode() : m_geometryNode(new QSGGeometryNode), m_material(new QSGFlatColorMaterial)
18{
20 m_geometryNode->setGeometry(m_geometry);
21 m_geometryNode->setFlag(QSGNode::OwnsGeometry);
22
23 m_geometryNode->setOpaqueMaterial(m_material);
24 m_geometryNode->setFlag(QSGNode::OwnsMaterial);
25
26 appendChildNode(m_geometryNode);
27}
28
29void PolyNode::setColor(QColor color)
30{
31 if (color != m_material->color())
32 {
33 m_material->setColor(color);
34 m_geometryNode->markDirty(QSGNode::DirtyMaterial);
35 }
36}
37
39{
40 if (width != m_geometry->lineWidth())
41 {
42 m_geometry->setLineWidth(width);
43 m_geometryNode->markDirty(QSGNode::DirtyGeometry);
44 }
45}
46
47void PolyNode::updateGeometry(const QPolygonF &polygon, bool filled)
48{
49 if (!filled)
50 {
51 m_geometry->setDrawingMode(GL_LINE_STRIP);
52 int size = polygon.size();
53 m_geometry->allocate(size);
54
55 QSGGeometry::Point2D *vertex = m_geometry->vertexDataAsPoint2D();
56
57 for (int i = 0; i < size; ++i)
58 {
59 vertex[i].x = polygon[i].x();
60 vertex[i].y = polygon[i].y();
61 }
62 }
63 else
64 {
65 m_geometry->setDrawingMode(GL_TRIANGLES);
66
67 double *coordinates_out;
68 int *tris_out;
69 int nverts, ntris, i;
70 QPolygonF pol = polygon;
71 int polySize = pol.size() * 2;
72 std::vector<double> vertices_array(polySize);
73
74 for (int i = 0; i < polySize; i += 2)
75 {
76 vertices_array[i] = pol[i / 2].x();
77 vertices_array[i + 1] = pol[i / 2].y();
78 }
79
80 const double *contours_array[] = { &vertices_array[0], &vertices_array[0] + polySize };
81 int contours_size = 2;
82
83 tessellate(&coordinates_out, &nverts, &tris_out, &ntris, contours_array, contours_array + contours_size);
84
85 m_geometry->allocate(3 * ntris);
86 QSGGeometry::Point2D *vertex = m_geometry->vertexDataAsPoint2D();
87
88 for (i = 0; i < 3 * ntris; ++i)
89 {
90 //int tris = tris_out[i];
91 vertex[i].x = coordinates_out[tris_out[i] * 2];
92 vertex[i].y = coordinates_out[tris_out[i] * 2 + 1];
93 }
94 free(coordinates_out);
95 if (tris_out)
96 free(tris_out);
97 }
98 m_geometryNode->markDirty(QSGNode::DirtyGeometry);
99}
void updateGeometry(const QPolygonF &polygon, bool filled)
Update the geometry of polygon.
Definition polynode.cpp:47
void setLineWidth(int width)
Set thickness of border line.
Definition polynode.cpp:38
PolyNode()
Initialize geometry and material.
Definition polynode.cpp:17
qsizetype size() const const
void setGeometry(QSGGeometry *geometry)
const QColor & color() const const
void setColor(const QColor &color)
void allocate(int vertexCount, int indexCount)
const AttributeSet & defaultAttributes_Point2D()
float lineWidth() const const
void setDrawingMode(unsigned int mode)
void setLineWidth(float width)
Point2D * vertexDataAsPoint2D()
void setOpaqueMaterial(QSGMaterial *material)
void appendChildNode(QSGNode *node)
void markDirty(DirtyState bits)
void setFlag(Flag f, bool enabled)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 12 2024 12:00:41 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.