-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscene3d.cpp
586 lines (466 loc) · 13.5 KB
/
scene3d.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include <QtGui>
#include <math.h>
#include "scene3d.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <cmath>
#include<sys/types.h>
#include<dirent.h>
#include<unistd.h>
#include "tetgen.h"
#include "grav_calc.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGLWidget>
//QGLWidget my_QGLWidget;
struct CFace{
int v1,v2,v3;
};
/*!
\brief Vertex of triangles
Vertex of triangles for asteroid's triangulation
*/
struct CVertex3{
GLfloat x,y,z;
};
/*!
\brief Color of triangles
Color of triangles for asteroid's triangulation
*/
struct CColor3{
GLfloat r,g,b;
};
static unsigned long IndexSize; //!< Size of index
static CFace * StrMyIndexArray; //!< Array of all indexes (?)
static CVertex3 * StrMyVertexArray; //!< Array of all vertices
static CColor3 * StrMyColorArray;//!< Array of all colors
Scene3D::Scene3D(QWidget* parent) : QOpenGLWidget(parent)
{
zTra=0; xRot=-90; yRot=0; zRot=0; nSca=1; xTra=0;
}
//!\brief Initialization of GL
void Scene3D::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(1.0,1.0,1.0,1.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glEnable(GL_CULL_FACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
}
/*!
\brief Resize of window and GLWidget
GLWidhet is resizing here
*/
void Scene3D::resizeGL(int nWidth, int nHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double ratio= static_cast<double>(nHeight)/static_cast<double>(nWidth);
if (nWidth>=nHeight)
glOrtho(-1.0/ratio, 1.0/ratio, -1.0, 1.0, -10.0, 1.0);
else
glOrtho(-1.0, 1.0, -1.0*ratio, 1.0*ratio, -10.0, 1.0);
glViewport(0, 0, static_cast<GLint>(nWidth), static_cast<GLint>(nHeight));
}
/*!
\brief Reaction when some key is pressed
Switch of reactions for some key pressed.
Functions controling by keyboard are described later.
\param[in] pe PressEvent - when one of keys is pressed
*/
void Scene3D::keyPressEvent(QKeyEvent* pe)
{
switch (pe->key())
{
case Qt::Key_Plus:
scale_plus();
break;
case Qt::Key_Equal:
scale_plus();
break;
case Qt::Key_Minus:
scale_minus();
break;
case Qt::Key_Up:
rotate_up();
break;
case Qt::Key_Down:
rotate_down();
break;
case Qt::Key_Left:
rotate_left();
break;
case Qt::Key_Right:
rotate_right();
break;
case Qt::Key_S:
translate_down();
break;
case Qt::Key_W:
translate_up();
break;
case Qt::Key_A:
translate_left();
break;
case Qt::Key_D:
translate_right();
break;
case Qt::Key_Space:
defaultScene();
break;
case Qt::Key_Escape:
this->close();
break;
}
update();
}
//!\brief Zoom in
void Scene3D::scale_plus()
{
nSca = nSca*1.1f;
}
//!\brief Zoom out
void Scene3D::scale_minus()
{
nSca = nSca/1.1f;
}
//!\brief Rotation around the horizontal axis
void Scene3D::rotate_up()
{
xRot += 1.0f;
}
//!\brief Rotation around the horizontal axis
void Scene3D::rotate_down()
{
xRot -= 1.0f;
}
//!\brief Rotation around the vertical axis
void Scene3D::rotate_left()
{
zRot += 1.0f;
}
//!\brief Rotation around the vertical axis
void Scene3D::rotate_right()
{
zRot -= 1.0f;
}
//!\brief Translation down
void Scene3D::translate_down()
{
zTra -= 0.05f;
}
//!\brief Translation up
void Scene3D::translate_up()
{
zTra += 0.05f;
}
//!\brief Translation right
void Scene3D::translate_right()
{
xTra += 0.05f;
}
//!\brief Translation left
void Scene3D::translate_left()
{
xTra -= 0.05f;
}
/*!
\brief Full drawing
*/
void Scene3D::paintGL()
{
//my_QGLWidget.renderText(1, 1, 1, "test");
if (this->flag_pnt) {
CalcPoint();
flag_pnt = 0;
flag_ans = 1;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(nSca, nSca, nSca);
glTranslatef(xTra, zTra, 0.0f);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glRotatef(zRot, 0.0f, 0.0f, 1.0f);
drawAxis();
drawAxisSegment();
if (this->flag_arr) drawArrow();
drawFigure();
}
/*!
\brief Drawing of axis
*/
void Scene3D::drawAxisSegment()
{
glLineWidth(3.0f);
GLfloat a = 0.02f;
glBegin(GL_LINES);
glColor4f(1.00f, 0.00f, 0.00f, 1.0f);
for(int i = -10; i < 11; i++){
if(i!=0){
glVertex3f( i*0.1f, 0.0f, a);
glVertex3f( i*0.1f, 0.0f, -a);
}
}
glColor4f(0.00f, 1.00f, 0.00f, 1.0f);
for(int i = -10; i < 11; i++){
if(i!=0){
glVertex3f( a, i*0.1f, 0.0f);
glVertex3f( -a, i*0.1f, 0.0f);
}
}
glColor4f(0.00f, 0.00f, 1.00f, 1.0f);
for(int i = -10; i < 11; i++){
if(i!=0){
glVertex3f( a, 0.0f, i*0.1f);
glVertex3f( -a, 0.0f, i*0.1f);
}
}
glEnd();
QColor halfGreen(0, 128, 0, 255);
initializeOpenGLFunctions();
glColor4f(0.00f, 1.00f, 0.00f, 1.0f);
glBegin(GL_LINES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 0.0f, -1.0f, 0.0f);
glColor4f(0.00f, 0.00f, 1.00f, 1.0f);
glVertex3f( 0.0f, 0.0f, 1.0f);
glVertex3f( 0.0f, 0.0f, -1.0f);
glEnd();
}
void Scene3D::drawAxis()
{
glLineWidth(3.0f);
glColor4f(1.00f, 0.00f, 0.00f, 1.0f);
glBegin(GL_LINES);
glVertex3f( 1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glEnd();
QColor halfGreen(0, 128, 0, 255);
initializeOpenGLFunctions();
glColor4f(0.00f, 1.00f, 0.00f, 1.0f);
glBegin(GL_LINES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 0.0f, -1.0f, 0.0f);
glColor4f(0.00f, 0.00f, 1.00f, 1.0f);
glVertex3f( 0.0f, 0.0f, 1.0f);
glVertex3f( 0.0f, 0.0f, -1.0f);
glEnd();
//my_QGLWidget.renderText(1, 1, 1, "test");
}
/*!
\brief Drawing of arrow
After calculation of point and the garvity force in this point this function will draw a beautiful red arrow.
Its direction will coincide with the direction of the gravity force's vector.
*/
void Scene3D::drawArrow()
{
float l = sqrtf(grav_Arrow.x2*grav_Arrow.x2 +
grav_Arrow.y2*grav_Arrow.y2 +
grav_Arrow.z2*grav_Arrow.z2)*5;
//std::cout << "l = " << l << std::endl;
glLineWidth(100.0f);
glColor4f(1.00f, 0.00f, 0.00f, 1.0f);
glBegin(GL_LINES);
glVertex3f(grav_Arrow.x1, grav_Arrow.y1, grav_Arrow.z1);
glVertex3f(grav_Arrow.x1 + grav_Arrow.x2/l, grav_Arrow.y1 + grav_Arrow.y2/l, grav_Arrow.z1 + grav_Arrow.z2/l);
glVertex3f(grav_Arrow.x1 + grav_Arrow.x2/l, grav_Arrow.y1 + grav_Arrow.y2/l, grav_Arrow.z1 + grav_Arrow.z2/l);
glVertex3f(grav_Arrow.x1 + grav_Arrow.x2/l - (grav_Arrow.x2 - grav_Arrow.z2)/(3*l),
grav_Arrow.y1 + grav_Arrow.y2/l - (grav_Arrow.y2)/(3*l),
grav_Arrow.z1 + grav_Arrow.z2/l - (grav_Arrow.x2 + grav_Arrow.z2)/(3*l));
glVertex3f(grav_Arrow.x1 + grav_Arrow.x2/l, grav_Arrow.y1 + grav_Arrow.y2/l, grav_Arrow.z1 + grav_Arrow.z2/l);
glVertex3f(grav_Arrow.x1 + grav_Arrow.x2/l - (grav_Arrow.x2 + grav_Arrow.z2)/(3*l),
grav_Arrow.y1 + grav_Arrow.y2/l - (grav_Arrow.y2)/(3*l),
grav_Arrow.z1 + grav_Arrow.z2/l - (-grav_Arrow.x2 + grav_Arrow.z2)/(3*l));
glEnd();
}
/*!
\brief Processing of information of mouse's wheel
Processing of the mouse's wheel events. Zoom in and zoom out.
*/
void Scene3D::wheelEvent(QWheelEvent* pe)
{
if ((pe->delta())>0) scale_plus(); else if ((pe->delta())<0) scale_minus();
update();
}
/*!
\brief Default settings for scene
Give you an opportunity to return to the start scene, if you need to do it.
*/
void Scene3D::defaultScene()
{
xRot=-90; yRot=0; zRot=0; xTra=0; zTra=0; nSca=1;
}
/*!
\brief Creation of arrow with verteices and colors for triangles
Always calling after improting of file. Create an arrow with the location of all vertices. Furthermore, define colors for triangles, depending on their location.
*/
void Scene3D::my_getArrays(std::string path)
{
std::ifstream file (path, std::ifstream::in);
if (!file)
std::perror("ifstream");
R=static_cast<double>(1.0);
if (!file)
{
std::cout << "file does not open" << std::endl;
}
fin.open(path);
tetrahedralization(&in, &out, &behavior, &fin);
fin.close();
std::string s;
int a, b, n_, i;
GLfloat n, max = 0, min = 1000, max_ = 0;
file >> s;
a = atoi(s.c_str());
file >> s;
b = atoi(s.c_str());
StrMyIndexArray = new CFace[static_cast<unsigned long long>(b)];
StrMyVertexArray = new CVertex3[static_cast<unsigned long long>(a)];
StrMyColorArray = new CColor3[static_cast<unsigned long long>(a)];
IndexSize =static_cast<unsigned long long>(b);
if (StrMyIndexArray == nullptr){
std::cout << "Could not allocate memory for StrMyIndexArray" << std::endl;
}
if (StrMyVertexArray == nullptr){
std::cout << "Could not allocate memory for StrMyVertexArray" << std::endl;
}
if (StrMyColorArray == nullptr){
std::cout << "Could not allocate memory for StrMyColorArray" << std::endl;
}
std::cout << "sizes: " << a << ' ' << b << std::endl;
for(i = 0; i < a; i++){
file >> s;
if (file.eof()){
std::cout << "wrong number of vertexes";
}
n = static_cast <GLfloat> (atof(s.c_str()));
if(max < abs(n)) {
max = abs(n);
}
StrMyVertexArray[i].x = n * R;
file >> s;
if (file.eof()){
std::cout << "wrong number of vertexes";
}
n = static_cast <GLfloat>(atof(s.c_str()));
if(max < abs(n)) {
max = abs(n);
}
StrMyVertexArray[i].y = n * R;
file >> s;
if (file.eof()){
std::cout << "wrong number of vertexes";
}
n = static_cast <GLfloat> (atof(s.c_str()));
if(max < abs(n)) {
max = abs(n);
}
StrMyVertexArray[i].z = n * R;
n = sqrt(StrMyVertexArray[i].x *StrMyVertexArray[i].x + StrMyVertexArray[i].y *StrMyVertexArray[i].y + StrMyVertexArray[i].z *StrMyVertexArray[i].z);
if(max_ < n) {
max_ = n;
}
if (min > n){
min = n;
}
}
for(i = 0; i < b; i++) {
file >> s;
if (file.eof()){
std::cout << "wrong number of vertexes";
}
n_ = atoi(s.c_str());
StrMyIndexArray[i].v1= (n_ - 1);
file >> s;
if (file.eof()){
std::cout << "wrong number of vertexes";
}
n_ = atoi(s.c_str());
StrMyIndexArray[i].v2 = (n_ - 1);
file >> s;
if (file.eof()){
std::cout << "wrong number of vertexes";
}
n_ = atoi(s.c_str());
StrMyIndexArray[i].v3 =(n_ - 1);
}
R = 0.75f/max;
std::cout << "max = " << max << ", R = " << R << std::endl;
std::cout << max << ' ' << min << std::endl;
for(i = 0; i < a; i++)
{
n = sqrt(StrMyVertexArray[i].x *StrMyVertexArray[i].x + StrMyVertexArray[i].y *StrMyVertexArray[i].y + StrMyVertexArray[i].z *StrMyVertexArray[i].z);
n = (0.7f*(n - min) - 0.4f*(n-max_))/(max_ - min);
StrMyColorArray[i].r=n;
StrMyColorArray[i].g=n;
StrMyColorArray[i].b=n;
if ((n < 0.4f) || (n > 0.7f)){
std::cout << n << std::endl; //shader texture vertex освещение - цвета
}
StrMyVertexArray[i].x = StrMyVertexArray[i].x * R;
StrMyVertexArray[i].y = StrMyVertexArray[i].y * R;
StrMyVertexArray[i].z = StrMyVertexArray[i].z * R;
}
file.close();
}
/*!
\brief Calculation of the point
Processing of the data that was given by user. Result is given in the field: "Answer"
*/
void Scene3D::CalcPoint() {
grav_in_point(&out, p, v);
grav_Arrow.x1 = static_cast <GLfloat> (p[0])*(R);
grav_Arrow.y1 = static_cast <GLfloat>(p[1])*R;
grav_Arrow.z1 = static_cast <GLfloat>(p[2])*R;
grav_Arrow.x2 = static_cast <GLfloat>(v[0])*R;
grav_Arrow.y2 = static_cast <GLfloat>(v[1])*R;
grav_Arrow.z2 = static_cast <GLfloat> (v[2])*R;
}
/*!
\brief Drawing the triangles
This function draw an asteroid using GL function for drawing triangles.
*/
void Scene3D::drawFigure()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glVertexPointer(3, GL_FLOAT, sizeof(CVertex3), StrMyVertexArray);
glColorPointer(3, GL_FLOAT, sizeof(CVertex3), StrMyColorArray);
glDrawElements(GL_TRIANGLES, static_cast <GLsizei>(3 * IndexSize), GL_UNSIGNED_INT, StrMyIndexArray);
glDisableClientState(GL_VERTEX_ARRAY);
}
/*!
\brief Processing of mouse events
Processing of the mouse events when it is pressed
*/
void Scene3D::mousePressEvent(QMouseEvent* pe)
{
ptrMousePosition = pe->pos();
}
/*!
\brief Processing of mouse moves
*/
void Scene3D::mouseMoveEvent(QMouseEvent* pe)
{
xRot += 180/nSca*static_cast<GLfloat>(pe->y()-ptrMousePosition.y())/height();
zRot += 180/nSca*static_cast<GLfloat>(pe->x()-ptrMousePosition.x())/width();
ptrMousePosition = pe->pos();
update();
}
/*!
\brief Avoiding memory leaks
Ths function will free tetgenio arrows.
*/
void Scene3D::free_scene3D() {
delete[] StrMyIndexArray;
delete[] StrMyVertexArray;
delete[] StrMyColorArray;
}