-
Notifications
You must be signed in to change notification settings - Fork 1
/
myglwidget.cpp
240 lines (202 loc) · 5.95 KB
/
myglwidget.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
229
230
231
232
233
234
235
236
237
238
239
240
#include "myglwidget.h"
#include <GL/glu.h>
#include <QCoreApplication>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QOpenGLShaderProgram>
#include <iostream>
#include <math.h>
#include <string>
MyGLWidget::MyGLWidget(QWidget *parent) : QOpenGLWidget(parent) {
fullscreen = false;
}
MyGLWidget::~MyGLWidget() {
delete pointcloud;
makeCurrent();
delete m_program;
m_vbo.destroy();
m_vao.destroy();
doneCurrent();
}
static void qNormalizeAngle(int &angle) {
while (angle < 0)
angle += 360 * 16;
while (angle > 360 * 16)
angle -= 360 * 16;
}
void MyGLWidget::initializeGL() //此处开始对OpenGL进行所以设置
{
displayPercent = 1;
initializeOpenGLFunctions();
m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceFile(
QOpenGLShader::Vertex,
"/home/richard/Qt Project/PointCloudSimplify/shader.vert");
m_program->addShaderFromSourceFile(
QOpenGLShader::Fragment,
"/home/richard/Qt Project/PointCloudSimplify/shader.frag");
m_program->link();
m_program->bind();
m_projMatrixLoc = m_program->uniformLocation("projMatrix");
m_modelMatrixLoc = m_program->uniformLocation("modelMatrix");
m_viewMatrixLoc = m_program->uniformLocation("viewMatrix");
pointcloud = new PointCloud();
updatedata();
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
//位置参数
f->glEnableVertexAttribArray(0);
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0);
//颜色参数
f->glEnableVertexAttribArray(1);
f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
(void *)(3 * sizeof(float)));
glPointSize(1);
m_view.setToIdentity();
m_proj.setToIdentity();
m_view.translate(0, 0, -1);
m_model.setToIdentity();
m_program->release();
}
void MyGLWidget::resizeGL(int w, int h) //重置OpenGL窗口的大小
{
m_proj.setToIdentity();
// m_proj.perspective(45.0f, GLfloat(w) / h, 0.01f, 10.0f);
float radious = GLfloat(w) / h;
m_proj.ortho(-1 * radious, 1 * radious, -1, 1, -10.0f, 10.0f);
}
void MyGLWidget::paintGL() //从这里开始进行所以的绘制
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClearColor(1.0, 1.0, 1.0, 1.0);
m_model.setToIdentity();
m_model.rotate(m_yRot / 16, 1, 0, 0);
m_model.rotate(m_xRot / 16, 0, 1, 0);
m_model.rotate(m_zRot / 16.0f, 0, 0, 1);
m_model.scale(m_scale);
m_program->bind();
m_program->setUniformValue(m_modelMatrixLoc, m_model);
m_program->setUniformValue(m_viewMatrixLoc, m_view);
m_program->setUniformValue(m_projMatrixLoc, m_proj);
glDrawArrays(GL_POINTS, 0, data.size() / 6 * displayPercent);
m_program->release();
}
void MyGLWidget::keyPressEvent(QKeyEvent *event) {}
void MyGLWidget::mousePressEvent(QMouseEvent *event) {
m_lastPos = event->pos();
}
void MyGLWidget::mouseMoveEvent(QMouseEvent *event) {
int dx = event->x() - m_lastPos.x();
int dy = event->y() - m_lastPos.y();
if (event->buttons() & Qt::LeftButton) {
setXRotation(m_xRot + dx);
setYRotation(m_yRot + dy);
} else if (event->buttons() & Qt::RightButton) {
setXRotation(m_xRot + dy);
setZRotation(m_zRot + dx);
}
}
void MyGLWidget::wheelEvent(QWheelEvent *event) {
if (event->angleDelta().y() > 0) {
m_scale *= 2;
update();
} else if (event->angleDelta().y() < 0) {
m_scale *= 0.5;
update();
}
// std::cout << m_scale << std::endl;
}
void MyGLWidget::setXRotation(int angle) {
qNormalizeAngle(angle);
if (angle != m_xRot) {
m_xRot = angle;
emit xRotationChanged(angle);
update();
}
}
void MyGLWidget::setYRotation(int angle) {
qNormalizeAngle(angle);
if (angle != m_yRot) {
m_yRot = angle;
emit yRotationChanged(angle);
update();
}
}
void MyGLWidget::setZRotation(int angle) {
qNormalizeAngle(angle);
if (angle != m_zRot) {
m_zRot = angle;
emit zRotationChanged(angle);
update();
}
}
void MyGLWidget::setScale(float s) {
if (s != m_scale) {
m_scale = s;
}
}
void MyGLWidget::setPointCloud(std::string path) {
if (path != "") {
if (pointcloud != nullptr) {
delete pointcloud;
}
pointcloud = new PointCloud(std::string(path));
}
std::vector<float> temp = pointcloud->data;
data.clear();
for (int i = 1; i <= temp.size(); i++) {
data.push_back(temp[i - 1]);
if (i % 3 == 0) {
data.push_back(0);
data.push_back(0);
data.push_back(0);
}
}
std::string str("当前的点云模型包含" + std::to_string(data.size() / 6) +
"个点");
QString currentState = QString::fromStdString(str);
emit showCurrentStatus(currentState);
updatedata();
}
void MyGLWidget::updatedata() {
if (!m_vao.isCreated())
m_vao.create();
if (m_vao.isCreated())
m_vao.bind();
m_vbo.create();
m_vbo.bind();
m_vbo.allocate(&data[0], data.size() * sizeof(float));
update();
}
void MyGLWidget::setDisplayPercent(int percent) {
displayPercent = percent / 100.0f;
std::cout << displayPercent << std::endl;
update();
}
void MyGLWidget::averageSimplify(double cellLength) {
displayPercent = 1;
data = pointcloud->averageSimplify((float)cellLength);
// emit celllengthChange((float)cellLength);
updatedata();
updateStateAfterSimplify();
}
void MyGLWidget::averageSimplifyDBSCAN(double s) {
displayPercent = 1;
data = pointcloud->averageSimplifyDBSCAN((float)s);
// emit celllengthChange((float)s);
updatedata();
updateStateAfterSimplify();
}
void MyGLWidget::curvSimplify(int percent) {
data = pointcloud->curvatureSimplify();
this->displayPercent = percent / 100.0f;
updatedata();
updateStateAfterSimplify();
}
void MyGLWidget::updateStateAfterSimplify() {
std::string str("点云模型:" + std::to_string(pointcloud->data.size() / 3) +
"点," + "简化后:" + std::to_string(data.size() / 6) + "点");
QString currentState = QString::fromStdString(str);
emit showCurrentStatus(currentState);
}