-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuickRectangle.cpp
228 lines (194 loc) · 5.88 KB
/
QuickRectangle.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "QuickRectangle.h"
#include <QDebug>
#include <QQuickWindow>
#include <QOpenGLFramebufferObject>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QSignalBlocker>
using namespace Eigen;
template<typename T, typename C>
int offset(T C::* p)
{
return static_cast<int>(reinterpret_cast<long long>(
&(static_cast<const C*>(nullptr)->*p)));
}
template<typename T, int N>
int size(const T (&)[N])
{
return N;
}
class RectangleRenderer : public QQuickFramebufferObject::Renderer,
public QOpenGLFunctions
{
QOpenGLVertexArrayObject vertexArray;
QOpenGLBuffer vertexBuffer;
QOpenGLShaderProgram program;
int vertexAttr;
int colorAttr;
int matrixUniform;
QMatrix4x4 transform;
public:
RectangleRenderer()
: vertexBuffer(QOpenGLBuffer::VertexBuffer)
{
initializeOpenGLFunctions();
const char* vsrc = R"(
uniform mediump mat4 matrix;
attribute vec4 vertex;
attribute vec4 color;
varying vec4 fcolor;
void main()
{
fcolor = color;
gl_Position = matrix * vertex;
}
)";
const char *fsrc = R"(
varying vec4 fcolor;
void main()
{
gl_FragColor = fcolor;
}
)";
program.addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vsrc);
program.addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fsrc);
program.link();
matrixUniform = program.uniformLocation("matrix");
vertexAttr = program.attributeLocation("vertex");
colorAttr = program.attributeLocation("color");
struct VertexData
{
GLfloat color[3];
GLfloat position[3];
};
VertexData data[] = {
{{1.0f, 0.0f, 0.0f}, {-1, -1, 0.0}},
{{0.0f, 1.0f, 0.0f}, {1, 0.0, 0.0}},
{{0.0f, 0.0f, 1.0f}, {0.0, 1, 0.0}},
};
vertexBuffer.create();
vertexBuffer.bind();
vertexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
vertexBuffer.allocate(data, sizeof(data));
vertexArray.create();
QOpenGLVertexArrayObject::Binder _(&vertexArray);
program.setAttributeBuffer(colorAttr, GL_FLOAT,
offset(&VertexData::color),
size(data[0].color),
sizeof(VertexData));
program.setAttributeBuffer(vertexAttr, GL_FLOAT,
offset(&VertexData::position),
size(data[0].position),
sizeof(VertexData));
program.enableAttributeArray(colorAttr);
program.enableAttributeArray(vertexAttr);
}
void render() override
{
glClearColor(0.5f, 0.5f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
program.bind();
vertexArray.bind();
program.setUniformValue(matrixUniform, transform);
glDrawArrays(GL_TRIANGLES, 0, 3);
vertexArray.release();
program.release();
glDisable(GL_DEPTH_TEST);
}
QOpenGLFramebufferObject *createFramebufferObject(const QSize &size) override
{
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
format.setSamples(4);
return new QOpenGLFramebufferObject(size, format);
}
void synchronize(QQuickFramebufferObject* f) override
{
if (auto r = qobject_cast<QuickRectangle*>(f))
{
transform = r->transform();
}
}
};
QuickRectangle::QuickRectangle() : orth(true)
{
setAcceptedMouseButtons(Qt::AllButtons);
if (orth) projection.ortho(-1, 1, -1, 1, 1, 3 /* -1 */);
else projection.frustum(-1.0, 1.0, -1.0, 1.0, 1, 100);
reset();
}
QQuickFramebufferObject::Renderer* QuickRectangle::createRenderer() const
{
return new RectangleRenderer;
}
QMatrix4x4 QuickRectangle::transform() const
{
QMatrix4x4 m(modelview.cast<float>().data());
// TODO
// return projection * m;
return m;
}
void QuickRectangle::reset()
{
zoomValue = 0;
rotationValue = 0;
if(QQuickWindow* const w = window())
{
// auto _ = QSignalBlocker(w);
w->setProperty("zoomValue", zoomValue);
w->setProperty("rotationValue", rotationValue);
}
modelview = Translation3f(0, 0, -2.f);
update();
}
void QuickRectangle::zoom(double value)
{
double dv = value - zoomValue;
if (this->orth) modelview.linear() *= std::exp(dv * 0.04f);
else modelview.translation() += Vector3f(0, 0, dv * 0.1f);
zoomValue = value;
update();
}
void QuickRectangle::rotate(double value)
{
modelview.linear() = AngleAxisf((rotationValue - value) * 0.1f, Vector3f::UnitZ()) * modelview.linear();
rotationValue = value;
update();
}
#if QT_VERSION_MAJOR < 6
#define position localPos
#endif
void QuickRectangle::mousePressEvent(QMouseEvent* e)
{
pos = e->position();
}
void QuickRectangle::mouseReleaseEvent(QMouseEvent* e)
{
if (e->button() == Qt::MiddleButton)
{
reset();
}
}
void QuickRectangle::mouseMoveEvent(QMouseEvent* e)
{
QPointF dp = e->position() - this->pos;
if (e->buttons() == Qt::LeftButton)
{
Vector3f axis(dp.y(), dp.x(), 0);
modelview.linear() = AngleAxisf(axis.norm() * 0.01f, axis.normalized()) * modelview.linear();
}
else if(e->buttons() == Qt::RightButton)
{
Vector3f dir(dp.x(), -dp.y(), 0);
modelview.translation() += dir * 0.01f;
}
this->pos = e->position();
update();
}
#undef position
void QuickRectangle::wheelEvent(QWheelEvent* e)
{
}