-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
Copy pathshaderdrawable.cpp
212 lines (169 loc) · 5.09 KB
/
shaderdrawable.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
//#define sNan NAN;
#include "shaderdrawable.h"
#ifdef GLES
#include <GLES/gl.h>
#endif
ShaderDrawable::ShaderDrawable()
{
m_needsUpdateGeometry = true;
m_visible = true;
m_lineWidth = 1.0;
m_pointSize = 6.0;
}
ShaderDrawable::~ShaderDrawable()
{
if (!m_vao.isCreated()) m_vao.destroy();
if (!m_vbo.isCreated()) m_vbo.destroy();
}
void ShaderDrawable::init()
{
// Init openGL functions
initializeOpenGLFunctions();
// Create buffers
m_vao.create();
m_vbo.create();
}
void ShaderDrawable::update()
{
m_needsUpdateGeometry = true;
}
void ShaderDrawable::updateGeometry(QOpenGLShaderProgram *shaderProgram)
{
// Init in context
if (!m_vao.isCreated()) init();
#ifndef GLES
// Prepare vao
m_vao.bind();
#endif
// Prepare vbo
m_vbo.bind();
// Update vertex buffer
if (updateData()) {
QVector<VertexData> vertexData(m_lines);
vertexData += m_points;
m_vbo.allocate(vertexData.constData(), vertexData.count() * sizeof(VertexData));
}
#ifndef GLES
// Offset for position
quintptr offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = shaderProgram->attributeLocation("a_position");
shaderProgram->enableAttributeArray(vertexLocation);
shaderProgram->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
// Offset for color
offset = sizeof(QVector3D);
// Tell OpenGL programmable pipeline how to locate vertex color data
int color = shaderProgram->attributeLocation("a_color");
shaderProgram->enableAttributeArray(color);
shaderProgram->setAttributeBuffer(color, GL_FLOAT, offset, 3, sizeof(VertexData));
// Offset for line start point
offset += sizeof(QVector3D);
// Tell OpenGL programmable pipeline how to locate vertex line start point
int start = shaderProgram->attributeLocation("a_start");
shaderProgram->enableAttributeArray(start);
shaderProgram->setAttributeBuffer(start, GL_FLOAT, offset, 3, sizeof(VertexData));
m_vao.release();
#endif
m_vbo.release();
m_needsUpdateGeometry = false;
}
bool ShaderDrawable::updateData()
{
// Test data
m_lines = {
{QVector3D(0, 0, 0), QVector3D(1, 0, 0), QVector3D(sNan, 0, 0)},
{QVector3D(10, 0, 0), QVector3D(1, 0, 0), QVector3D(sNan, 0, 0)},
{QVector3D(0, 0, 0), QVector3D(0, 1, 0), QVector3D(sNan, 0, 0)},
{QVector3D(0, 10, 0), QVector3D(0, 1, 0), QVector3D(sNan, 0, 0)},
{QVector3D(0, 0, 0), QVector3D(0, 0, 1), QVector3D(sNan, 0, 0)},
{QVector3D(0, 0, 10), QVector3D(0, 0, 1), QVector3D(sNan, 0, 0)}
};
return true;
}
bool ShaderDrawable::needsUpdateGeometry() const
{
return m_needsUpdateGeometry;
}
void ShaderDrawable::draw(QOpenGLShaderProgram *shaderProgram)
{
if (!m_visible) return;
#ifndef GLES
// Prepare vao
m_vao.bind();
#else
// Prepare vbo
m_vbo.bind();
// Offset for position
quintptr offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = shaderProgram->attributeLocation("a_position");
shaderProgram->enableAttributeArray(vertexLocation);
shaderProgram->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
// Offset for color
offset = sizeof(QVector3D);
// Tell OpenGL programmable pipeline how to locate vertex color data
int color = shaderProgram->attributeLocation("a_color");
shaderProgram->enableAttributeArray(color);
shaderProgram->setAttributeBuffer(color, GL_FLOAT, offset, 3, sizeof(VertexData));
// Offset for line start point
offset += sizeof(QVector3D);
// Tell OpenGL programmable pipeline how to locate vertex line start point
int start = shaderProgram->attributeLocation("a_start");
shaderProgram->enableAttributeArray(start);
shaderProgram->setAttributeBuffer(start, GL_FLOAT, offset, 3, sizeof(VertexData));
#endif
glLineWidth(m_lineWidth);
glDrawArrays(GL_LINES, 0, m_lines.count());
#ifdef GLES
shaderProgram->setUniformValue("point_size", (GLfloat)m_pointSize);
#else
glPointSize(m_pointSize);
#endif
glDrawArrays(GL_POINTS, m_lines.count(), m_points.count());
#ifndef GLES
m_vao.release();
#else
shaderProgram->setUniformValue("point_size", (GLfloat)0.0);
m_vbo.release();
#endif
}
QVector3D ShaderDrawable::getSizes()
{
return QVector3D(0, 0, 0);
}
QVector3D ShaderDrawable::getMinimumExtremes()
{
return QVector3D(0, 0, 0);
}
QVector3D ShaderDrawable::getMaximumExtremes()
{
return QVector3D(0, 0, 0);
}
int ShaderDrawable::getVertexCount()
{
return m_lines.count() + m_points.count();
}
double ShaderDrawable::lineWidth() const
{
return m_lineWidth;
}
void ShaderDrawable::setLineWidth(double lineWidth)
{
m_lineWidth = lineWidth;
}
bool ShaderDrawable::visible() const
{
return m_visible;
}
void ShaderDrawable::setVisible(bool visible)
{
m_visible = visible;
}
double ShaderDrawable::pointSize() const
{
return m_pointSize;
}
void ShaderDrawable::setPointSize(double pointSize)
{
m_pointSize = pointSize;
}