Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsgcurvestrokenode.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
8
10{
11 setFlag(OwnsGeometry, true);
13 updateMaterial();
14}
15
16void QSGCurveStrokeNode::QSGCurveStrokeNode::updateMaterial()
17{
18 m_material.reset(new QSGCurveStrokeMaterial(this));
19 setMaterial(m_material.data());
20}
21
22// Take the start, control and end point of a curve and return the points A, B, C
23// representing the curve as Q(s) = A*s*s + B*s + C
24std::array<QVector2D, 3> QSGCurveStrokeNode::curveABC(const std::array<QVector2D, 3> &p)
25{
26 QVector2D a = p[0] - 2*p[1] + p[2];
27 QVector2D b = 2*p[1] - 2*p[0];
28 QVector2D c = p[0];
29
30 return {a, b, c};
31}
32
33// Curve from p[0] to p[2] with control point p[1]
34void QSGCurveStrokeNode::appendTriangle(const std::array<QVector2D, 3> &v,
35 const std::array<QVector2D, 3> &p,
36 const std::array<QVector2D, 3> &n)
37{
38 auto abc = curveABC(p);
39
40 int currentVertex = m_uncookedVertexes.count();
41
42 for (int i = 0; i < 3; ++i) {
43 m_uncookedVertexes.append( { v[i].x(), v[i].y(),
44 abc[0].x(), abc[0].y(), abc[1].x(), abc[1].y(), abc[2].x(), abc[2].y(),
45 n[i].x(), n[i].y() } );
46 }
47 m_uncookedIndexes << currentVertex << currentVertex + 1 << currentVertex + 2;
48}
49
50// Straight line from p0 to p1
51void QSGCurveStrokeNode::appendTriangle(const std::array<QVector2D, 3> &v,
52 const std::array<QVector2D, 2> &p,
53 const std::array<QVector2D, 3> &n)
54{
55 // We could reduce this to a linear equation by setting A to (0,0).
56 // However, then we cannot use the cubic solution and need an additional
57 // code path in the shader. The following formulation looks more complicated
58 // but allows to always use the cubic solution.
59 auto A = p[1] - p[0];
60 auto B = QVector2D(0., 0.);
61 auto C = p[0];
62
63 int currentVertex = m_uncookedVertexes.count();
64
65// for (auto v : QList<QPair<QVector2D, QVector2D>>({{v0, n0}, {v1, n1}, {v2, n2}})) {
66 for (int i = 0; i < 3; ++i) {
67 m_uncookedVertexes.append( { v[i].x(), v[i].y(),
68 A.x(), A.y(), B.x(), B.y(), C.x(), C.y(),
69 n[i].x(), n[i].y() } );
70 }
71 m_uncookedIndexes << currentVertex << currentVertex + 1 << currentVertex + 2;
72}
73
75{
77 if (g->indexType() != QSGGeometry::UnsignedIntType) {
78 g = new QSGGeometry(attributes(),
79 m_uncookedVertexes.size(),
80 m_uncookedIndexes.size(),
83 } else {
84 g->allocate(m_uncookedVertexes.size(), m_uncookedIndexes.size());
85 }
86
87 g->setDrawingMode(QSGGeometry::DrawTriangles);
88 memcpy(g->vertexData(),
89 m_uncookedVertexes.constData(),
90 g->vertexCount() * g->sizeOfVertex());
91 memcpy(g->indexData(),
92 m_uncookedIndexes.constData(),
93 g->indexCount() * g->sizeOfIndex());
94
95 m_uncookedIndexes.clear();
96 m_uncookedVertexes.clear();
97}
98
111
const QSGGeometry * geometry() const
Returns this node's geometry.
Definition qsgnode.h:160
void setGeometry(QSGGeometry *geometry)
Sets the geometry of this node to geometry.
Definition qsgnode.cpp:764
QVector< quint32 > m_uncookedIndexes
QVector< StrokeVertex > m_uncookedVertexes
static const QSGGeometry::AttributeSet & attributes()
void cookGeometry() override
void appendTriangle(const std::array< QVector2D, 3 > &v, const std::array< QVector2D, 3 > &p, const std::array< QVector2D, 3 > &n)
The QSGGeometry class provides low-level storage for graphics primitives in the \l{Qt Quick Scene Gra...
Definition qsggeometry.h:15
@ OwnsGeometry
Definition qsgnode.h:57
void setFlag(Flag, bool=true)
Sets the flag f on this node if enabled is true; otherwise clears the flag.
Definition qsgnode.cpp:586
The QVector2D class represents a vector or vertex in 2D space.
Definition qvectornd.h:31
Combined button and popup list for selecting options.
static struct AttrInfo attrs[]
n varying highp vec2 A
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLboolean GLboolean g
GLfloat n
GLint y
const GLubyte * c
GLfloat GLfloat p
[1]
The QSGGeometry::AttributeSet describes how the vertices in a QSGGeometry are built up.
Definition qsggeometry.h:73
The QSGGeometry::Attribute describes a single vertex attribute in a QSGGeometry.
Definition qsggeometry.h:58
static Attribute createWithAttributeType(int pos, int tupleSize, int primitiveType, AttributeType attributeType)
Creates a new QSGGeometry::Attribute for attribute register pos with tupleSize.