forked from andeplane/SimpleVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoints2d.cpp
More file actions
122 lines (91 loc) · 3.4 KB
/
Copy pathpoints2d.cpp
File metadata and controls
122 lines (91 loc) · 3.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
114
115
116
117
118
119
120
121
122
#include "points2d.h"
using std::vector;
namespace CompPhys {
Points2D::Points2D(std::function<void(Points2D *renderableObject)> copyDataFunction)
{
// copyData = copyDataFunction;
myCopyData = copyDataFunction;
setNumberOfVBOs(1);
}
Points2D::~Points2D()
{
}
void Points2D::render(QMatrix4x4 &modelViewMatrix, QMatrix4x4 &projectionMatrix)
{
if(m_data.size() == 0) return;
createShaderProgram();
m_program->bind();
// Tell OpenGL which VBOs to use
m_funcs->glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
// Offset for position
quintptr offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = m_program->attributeLocation("a_position");
m_program->enableAttributeArray(vertexLocation);
m_funcs->glVertexAttribPointer(vertexLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Point2DData), (const void *)offset);
// Offset for texture coordinate
offset += sizeof(QVector2D);
// Tell OpenGL programmable pipeline how to locate vertex color data
int colorLocation = m_program->attributeLocation("a_color");
m_program->enableAttributeArray(colorLocation);
m_funcs->glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Point2DData), (const void *)offset);
// Draw cube geometry using indices from VBO 1
glPointSize(m_pointSize);
m_funcs->glDrawArrays(GL_POINTS, 0, m_data.size());
m_program->disableAttributeArray(vertexLocation);
m_program->release();
}
void Points2D::uploadVBOs()
{
// Transfer vertex data to VBO 0
m_funcs->glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
m_funcs->glBufferData(GL_ARRAY_BUFFER, m_data.size() * sizeof(Point2DData), &m_data.front(), GL_STATIC_DRAW);
}
void Points2D::initialize()
{
}
void Points2D::copyDataFunction()
{
myCopyData(this);
}
void Points2D::setPointSize(float pointSize)
{
m_pointSize = pointSize;
}
void Points2D::setData(std::vector<QVector2D> &positions, std::vector<QVector3D> &colors)
{
m_data.resize(positions.size());
for(unsigned int i=0; i<positions.size(); i++) {
m_data[i].position = positions[i];
m_data[i].color = colors[i];
}
}
void Points2D::setData(vector<QVector2D> &positions, QVector3D color)
{
m_data.resize(positions.size());
for(unsigned int i=0; i<positions.size(); i++) {
m_data[i].position = positions[i];
m_data[i].color = color;
}
}
void Points2D::createShaderProgram()
{
if (!m_program) {
m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute highp vec4 a_position;\n"
"attribute highp vec3 a_color;\n"
"varying highp vec3 color;\n"
"void main() {\n"
" gl_Position = a_position;\n"
" color = a_color;"
"}");
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
"varying highp vec3 color;\n"
"void main() {\n"
" gl_FragColor = vec4(color, 1.0);\n"
"}");
m_program->link();
}
}
}