-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQuadShape.cpp
More file actions
113 lines (92 loc) · 2.4 KB
/
Copy pathQuadShape.cpp
File metadata and controls
113 lines (92 loc) · 2.4 KB
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
#include "QuadShape.h"
#include "Prop.h"
#include "LayersRenderer.h"
#include <QDebug>
std::uint32_t QuadShape::_vertexAttribute = 0;
std::uint32_t QuadShape::_textureAttribute = 1;
QuadShape::QuadShape(Prop& prop, const QString& name) :
Shape(prop, name),
_rectangle(),
_vertexData()
{
_vertexData.resize(20);
}
void QuadShape::initialize()
{
Shape::initialize();
auto& renderer = _prop.getRenderer();
renderer.bindOpenGLContext();
{
_vao.bind();
{
_vbo.bind();
{
_vbo.setUsagePattern(QOpenGLBuffer::DynamicDraw);
_vbo.allocate(_vertexData.constData(), _vertexData.count() * sizeof(GLfloat));
}
_vbo.release();
}
}
renderer.releaseOpenGLContext();
}
bool QuadShape::canRender() const
{
return !_rectangle.isNull();
}
void QuadShape::render()
{
if (!canRender())
return;
auto& renderer = _prop.getRenderer();
renderer.bindOpenGLContext();
{
_vao.bind();
{
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
_vao.release();
}
}
QRectF QuadShape::getRectangle() const
{
return _rectangle;
}
QSizeF QuadShape::getImageSize() const
{
return _rectangle.size();
}
void QuadShape::setRectangle(const QRectF& rectangle)
{
if (rectangle == _rectangle)
return;
_rectangle = rectangle;
createQuad();
}
void QuadShape::createQuad()
{
const auto left = static_cast<float>(_rectangle.left());
const auto right = static_cast<float>(_rectangle.right());
const auto bottom = static_cast<float>(_rectangle.bottom());
const auto top = static_cast<float>(_rectangle.top());
const float coordinates[4][3] = {
{ left, top, 0.0f },
{ right, top, 0.0f },
{ right, bottom, 0.0f },
{ left, bottom, 0.0f }
};
for (int j = 0; j < 4; ++j)
{
_vertexData[j * 5 + 0] = coordinates[j][0];
_vertexData[j * 5 + 1] = coordinates[j][1];
_vertexData[j * 5 + 2] = coordinates[j][2];
_vertexData[j * 5 + 3] = j == 0 || j == 3;
_vertexData[j * 5 + 4] = j == 2 || j == 3;
}
auto& renderer = _prop.getRenderer();
renderer.bindOpenGLContext();
{
_vbo.bind();
_vbo.allocate(_vertexData.constData(), _vertexData.count() * sizeof(GLfloat));
_vbo.release();
}
}