-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenGLWidget.cpp
361 lines (289 loc) · 10.6 KB
/
OpenGLWidget.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QMouseEvent>
#include <QDateTime>
#include <QTimer>
#include "MainWindow.h"
#include "OpenGLWidget.h"
#include "EasyGL/EasyCamera.h"
#include "EasyGL/EasyCameraTransition.h"
#include "EasyGL/EasyGeometry.h"
#include "EasyGL/EasyLight.h"
#include "EasyGL/EasyMaterial.h"
#include "EasyGL/EasyMesh.h"
#include "EasyGL/EasyScene.h"
#include "BasicMaterial.h"
#include "SquareGeometry.h"
#define SERIALPORT_NAME "cu.MonsterBT-DevB"
#define SERIALPORT_DATA_PARAMS 5
BasicMaterial *mOrange;
EasyMesh *armBase;
EasyMesh *armLink1;
EasyMesh *armLink2;
EasyMesh *armLink3;
EasyMesh *armLink4;
EasyCameraTransition *camera;
EasyCamera *cameraStatic;
EasyCamera *cameraArm;
EasyCamera *cameraUp;
EasyMesh *root;
EasyMaterial *mFloor;
SquareGeometry *gFloor;
EasyMesh *floorMesh;
EasyMaterial *mSky;
SquareGeometry *gBackground;
EasyMesh *background;
// Serial Port
QSerialPort serialPort; // Novo objecto QSerialPort
QString serialPortName; // Variavel que ira receber o caminho da porta serial
int serialPortBaudRate; // Variavel que ira receber o baudrate da conexão
// Keeps values
float serialData[SERIALPORT_DATA_PARAMS];
//Construtor da classe
OpenGLWidget::OpenGLWidget(QWidget *parent) : QOpenGLWidget(parent) {
scene = NULL;
square = NULL;
squareMesh = NULL;
timer = new QTimer(this->parent());
connect(timer, SIGNAL(timeout()), this, SLOT(updateScene()));
timer->start(1000 / 30.0);
// Setup Serial Port
openSerialPort();
}
//Destrutor da classe
OpenGLWidget::~OpenGLWidget()
{
if(serialPort.isOpen())
serialPort.close();
delete scene;
// delete materials;
delete square;
delete squareMesh;
}
// Abre porta serial
void OpenGLWidget::openSerialPort(){
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos) {
qDebug() << QObject::tr("Port: ") << info.portName() << endl
<< QObject::tr("Location: ") << info.systemLocation() << endl
<< QObject::tr("Description: ") << info.description() << endl
<< QObject::tr("Manufacturer: ") << info.manufacturer() << endl
<< QObject::tr("Serial number: ") << info.serialNumber() << endl
<< QObject::tr("Vendor Identifier: ") <<
(info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) << endl
<< QObject::tr("Product Identifier: ") <<
(info.hasProductIdentifier() ? QString::number(info.productIdentifier(), 16) : QString()) << endl
<< QObject::tr("Busy: ") << (info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) << endl;
}
serialPort.setPortName(SERIALPORT_NAME);
serialPort.setBaudRate(QSerialPort::Baud115200, QSerialPort::AllDirections);
serialPort.setDataBits(QSerialPort::Data8);
serialPort.setFlowControl(QSerialPort::NoFlowControl);
serialPort.setParity(QSerialPort::NoParity);
serialPort.setStopBits(QSerialPort::OneStop);
if (!serialPort.open(QIODevice::ReadWrite)) {
qWarning() << QObject::tr("Falha ao abrir porta %1, erro: %2").arg(SERIALPORT_NAME).arg(serialPort.errorString()) << endl;
// exit(1);
}
}
// Atualiza dados vindos da porta serial
QByteArray inData;
void OpenGLWidget::readSerialData(){
if(!serialPort.isOpen())
return;
// Reads data until no line available
while(serialPort.canReadLine()){
// Read and append data
parseData(inData = serialPort.readLine());
}
}
void OpenGLWidget::parseData(QByteArray & rawData){
QString str = QString::fromLatin1(rawData);
int spliters = std::count(str.begin(), str.end(), ':');
if(spliters != SERIALPORT_DATA_PARAMS - 1){
qWarning() << "Malformed data:" << str;
return;
}
str.replace("\n", "");
str.replace("\r", "");
QStringList pieces = str.split(":");
for(int i = 0; i < pieces.size(); i++){
serialData[i] = pieces.at(i).toFloat();
}
qDebug() << "Parsed data. ID:" << serialData[0];
}
//Inicializa funções OpenGL
void OpenGLWidget::initializeGL()
{
makeCurrent();
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
//Exibe informacoes sobre a versao do OpenGL e GLSL
std :: cerr << " Version " << glGetString ( GL_VERSION ) <<" \n ";
std :: cerr << " GLSL " << glGetString ( GL_SHADING_LANGUAGE_VERSION ) << endl << endl;
// Create root mesh
root = new EasyMesh();
root->rotation.setX(-90);
// Create light
EasyLight *light = new EasyLight();
light->position = QVector4D(0, 0, 4, 0);
// Create basic cameras
cameraArm = new EasyCamera();
cameraArm->setPosition(QVector3D(0, 1, 0));
cameraArm->lookAt(QVector3D(0, 1, 0));
cameraUp = new EasyCamera();
//cameraUp->up = QVector3D(1, 0, 0);
cameraUp->setPosition(QVector3D(-0.5, 2, 0));
cameraUp->lookAt(QVector3D(0, 0, 0));
cameraStatic = new EasyCamera();
cameraStatic->setPosition(QVector3D(0, 1, 2));
cameraStatic->lookAt(QVector3D(0, 1, 0));
// Create transition camera
camera = new EasyCameraTransition(cameraUp,cameraStatic, 2);
// Initialize scene
scene = new EasyScene(this);
scene->light = light;
scene->camera = camera;
// Create material
BasicMaterial *mOrange = new BasicMaterial();
mOrange->diffuse = QVector4D(228 / 255.0, 127 / 255.0, 40 / 255.0, 1.0);
BasicMaterial *mDark = new BasicMaterial();
mDark->diffuse = QVector4D(0.3, 0.3, 0.3, 1.0);
mDark->shininess = 50;
BasicMaterial *mRed = new BasicMaterial();
mRed->diffuse = QVector4D(0.9, 0.1, 0.1, 1.0);
// Create background mesh
mSky = new EasyMaterial();
mSky->setFragmentShader(":/shaders/background_fshader.glsl", true);
mSky->setVertexShader(":/shaders/background_vshader.glsl", true);
mSky->link();
mSky->setTexture(new QImage(":/textures/background_mountains.jpg"));
gBackground = new SquareGeometry();
background = new EasyMesh(gBackground, mSky);
// Create floor mesh
mFloor = new BasicMaterial();
mFloor->setTexture(new QImage(":/textures/floor.jpg"));
mFloor->shininess = 5;
mFloor->diffuse = QVector4D(0,0,0,0);
mFloor->specular = QVector4D(0.3,0.3,0.3,0);
mFloor->ambient = QVector4D(0,0,0,0);
gFloor = new SquareGeometry();
floorMesh = new EasyMesh(gFloor, mFloor);
root->addChild(floorMesh);
//EasyGeometry *camel = new EasyGeometry();
// camel->loadFromOFF(":/models/camel.off");
//camel->loadFromSTL(":/models/car.stl");
//squareMesh = new EasyMesh(camel, camelMaterial);
EasyGeometry *gArmBase = new EasyGeometry();
gArmBase->loadFromSTL(":/models/IRB140_BASE.STL", 0.001);
armBase = new EasyMesh(gArmBase, mOrange);
armBase->position = QVector3D(0, 0, 0.02);
root->addChild(armBase);
EasyGeometry *gArmLink1 = new EasyGeometry();
gArmLink1->loadFromSTL(":/models/IRB140_LINK1.STL", 0.001);
armLink1 = new EasyMesh(gArmLink1, mOrange);
armLink1->position = QVector3D(0, 0, 0.02);
armBase->addChild(armLink1);
EasyGeometry *gArmLink1Motors = new EasyGeometry();
gArmLink1Motors->loadFromSTL(":/models/IRB140_LINK1_MOTORS.STL", 0.001);
EasyMesh *armLink1Motors = new EasyMesh(gArmLink1Motors, mDark);
armLink1Motors->position = QVector3D(0, 0, 0);
armLink1->addChild(armLink1Motors);
EasyGeometry *gArmLink2 = new EasyGeometry();
gArmLink2->loadFromSTL(":/models/IRB140_LINK2.STL", 0.001);
gArmLink2->offset = QVector3D(-70, 0, -352);
armLink2 = new EasyMesh(gArmLink2, mOrange);
armLink2->position = QVector3D(0.070, 0, 0.352);
armLink1->addChild(armLink2);
EasyGeometry *gArmLink3 = new EasyGeometry();
gArmLink3->loadFromSTL(":/models/IRB140_LINK3.STL", 0.001);
gArmLink3->offset = QVector3D(-70, -1, -712);
armLink3 = new EasyMesh(gArmLink3, mOrange);
armLink3->position = QVector3D(0.0, 0, 0.352);
armLink2->addChild(armLink3);
armLink3->visible = true;
EasyGeometry *gArmLink4 = new EasyGeometry();
gArmLink4->loadFromSTL(":/models/IRB140_LINK4.STL", 0.001);
gArmLink4->offset = QVector3D(-288, 0, -712);
armLink4 = new EasyMesh(gArmLink4, mRed);
armLink4->position = QVector3D(0.218, 0, 0);
armLink3->addChild(armLink4);
}
//Resize
void OpenGLWidget::resizeGL(int w, int h)
{
qDebug() << " ! resizeGL" << w << h;
cameraStatic->projectionMatrix.setToIdentity ();
cameraStatic->projectionMatrix.perspective (
60.0,
static_cast <qreal>(w) / static_cast <qreal>(h),
0.1,
20.0);
cameraArm->projectionMatrix.setToIdentity ();
cameraArm->projectionMatrix.perspective (
60.0,
static_cast <qreal>(w) / static_cast <qreal>(h),
0.1,
20.0);
cameraUp->projectionMatrix.setToIdentity ();
cameraUp->projectionMatrix.perspective (
60.0,
static_cast <qreal>(w) / static_cast <qreal>(h),
0.1,
20.0);
}
//PaintGL
void OpenGLWidget::paintGL()
{
makeCurrent();
long startTime = QDateTime::currentMSecsSinceEpoch();
// Read serial data
readSerialData();
// Reset screen
glClear(GL_COLOR_BUFFER_BIT);
// Draw Background and clear Depth buffer
background->draw(scene, NULL);
glClear(GL_DEPTH_BUFFER_BIT);
// Render
scene->render(root);
int took = (QDateTime::currentMSecsSinceEpoch() - startTime);
if(took > 3)
qDebug() << "took : " << (QDateTime::currentMSecsSinceEpoch() - startTime) << "ms ";
}
// Update and render
void OpenGLWidget::updateScene() {
static float s;
static float t;
s += 2;
t += 0.03;
armLink1->rotation.setZ(-serialData[1]);
armLink2->rotation.setY(-serialData[2] + 90);
armLink3->rotation.setY(-serialData[3] - 90);
armLink4->rotation.setX(-serialData[4]);
// Update models
root->computeModels(NULL);
// Keep on arm
QVector3D camPos = armLink4->model * QVector4D(-0.3, 0.5, 0, 1).toVector3D();
cameraArm->position = camPos;
QVector3D camPosUp = (armLink4->model * QVector4D(-0.3, 0.5, 1, 1).toVector3D() - camPos);
camPosUp.normalize();
cameraArm->up = camPosUp;
// Follow tip of arm
QVector3D armPos = armLink4->model * QVector4D(0, 0.1, 0, 1).toVector3D();
cameraArm->center = armPos;
cameraArm->update();
camera->animate(0.03);
if(!camera->completed || !MainWindow::wantsChangeCamera){
// Does nothing if didn't completed camera change or not wants camera change
}else if(camera->completed && camera->to == cameraArm){
camera->gotoCamera(cameraUp, 3);
MainWindow::wantsChangeCamera = false;
}else if(camera->completed && camera->to == cameraStatic){
camera->gotoCamera(cameraArm, 3);
MainWindow::wantsChangeCamera = false;
}else if(camera->completed && camera->to == cameraUp){
camera->gotoCamera(cameraStatic, 3);
MainWindow::wantsChangeCamera = false;
}
update();
}