-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinenode.cpp
170 lines (145 loc) · 5.42 KB
/
linenode.cpp
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
#include "linenode_p.h"
#include <QtGui/QColor>
#include <QtQuick/QSGSimpleMaterial>
class LineShader : public QSGSimpleMaterialShader<LineNode::LineMaterial>
{
QSG_DECLARE_SIMPLE_SHADER(LineShader, LineNode::LineMaterial)
public:
LineShader() {
setShaderSourceFile(QOpenGLShader::Vertex, ":/shaders/LineNode.vsh");
setShaderSourceFile(QOpenGLShader::Fragment, ":/shaders/LineNode.fsh");
}
QList<QByteArray> attributes() const { return QList<QByteArray>() << "pos" << "prevNext"; }
void updateState(const LineNode::LineMaterial *m, const LineNode::LineMaterial *) {
program()->setUniformValue(id_height, m->height);
program()->setUniformValue(id_lineWidth, GLfloat(m->aa ? m->lineWidth * 1.7 : m->lineWidth));
program()->setUniformValue(id_warningBelowMinimum, m->warningMinValue);
program()->setUniformValue(id_warningAboveMaximum, m->warningMaxValue);
program()->setUniformValue(id_fillDirection, m->fillDirection);
program()->setUniformValue(id_normalColor, m->color);
program()->setUniformValue(id_warningMinColor, m->warningMinColor);
program()->setUniformValue(id_warningMaxColor, m->warningMaxColor);
program()->setUniformValue(id_dataScalingTransform, m->dataTransform.toGenericMatrix<2, 2>());
program()->setUniformValue(id_dataOffset, m->dataTransform.column(3).toVector2D());
program()->setUniformValue(id_aa, m->fillDirection == 0 ? m->aa : 0);
//qDebug() << "colors" << m->color << m->warningMinColor << m->warningMaxColor;
}
void resolveUniforms() {
id_height = program()->uniformLocation("height");
id_lineWidth = program()->uniformLocation("lineWidth");
id_warningBelowMinimum = program()->uniformLocation("warningBelowMinimum");
id_warningAboveMaximum = program()->uniformLocation("warningAboveMaximum");
id_fillDirection = program()->uniformLocation("fillDirection");
id_normalColor = program()->uniformLocation("normalColor");
id_warningMinColor = program()->uniformLocation("warningMinColor");
id_warningMaxColor = program()->uniformLocation("warningMaxColor");
id_dataScalingTransform = program()->uniformLocation("dataScalingTransform");
id_dataOffset = program()->uniformLocation("dataOffset");
id_aa = program()->uniformLocation("aa");
}
private:
int id_height;
int id_lineWidth;
int id_warningBelowMinimum;
int id_warningAboveMaximum;
int id_fillDirection;
int id_normalColor;
int id_warningMinColor;
int id_warningMaxColor;
int id_dataScalingTransform;
int id_dataOffset;
int id_aa;
};
static const QSGGeometry::AttributeSet &attributes()
{
static QSGGeometry::Attribute attr[] = {
QSGGeometry::Attribute::create(0, 4, GL_FLOAT),
QSGGeometry::Attribute::create(1, 4, GL_FLOAT)
};
static QSGGeometry::AttributeSet set = { 2, 8 * sizeof(float), attr };
return set;
}
LineNode::LineNode()
: m_geometry(attributes(), 0)
{
setGeometry(&m_geometry);
m_material = LineShader::createMaterial();
m_material->setFlag(QSGMaterial::Blending);
setMaterial(m_material);
setFlag(OwnsMaterial);
}
void LineNode::updateGeometry(const QRectF &bounds, const QVector<LineVertex> *v, qreal timeScale)
{
float vscale = bounds.height() / (m_maxValue - m_minValue);
// TODO only adjust the x offset each frame
QMatrix4x4 &matrix = m_material->state()->dataTransform;
matrix.setToIdentity();
matrix.scale(timeScale, -vscale);
matrix.translate(bounds.width() / timeScale - v->last().x, -m_maxValue);
//qDebug() << v->size() << timeScale << vscale << matrix << v->first().x << v->first().y << v->last().x << v->last().y;
m_geometry.setDrawingMode(m_wireframe ? GL_LINE_STRIP : GL_TRIANGLE_STRIP);
if (m_geometry.vertexCount() != v->size())
m_geometry.allocate(v->size());
// TODO limit on left side to stay in bounds
memcpy(m_geometry.vertexData(), v->constData(), sizeof(LineVertex) * v->size());
markDirty(QSGNode::DirtyGeometry);
}
void LineNode::setHeight(float height)
{
m_material->state()->height = height;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setLineWidth(float width)
{
m_material->state()->lineWidth = width;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setColor(QColor color)
{
m_material->state()->color = color;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWarningMinColor(QColor color)
{
m_material->state()->warningMinColor = color;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWarningMaxColor(QColor color)
{
m_material->state()->warningMaxColor = color;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setMinValue(qreal v)
{
m_minValue = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setMaxValue(qreal v)
{
m_maxValue = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWarningMinValue(qreal v)
{
m_material->state()->warningMinValue = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWarningMaxValue(qreal v)
{
m_material->state()->warningMaxValue = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setFillDirection(qreal v)
{
m_material->state()->fillDirection = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setSpread(qreal v)
{
m_material->state()->aa = v;
markDirty(QSGNode::DirtyMaterial);
}
void LineNode::setWireframe(bool v)
{
m_wireframe = v;
}