/
glwidget.cpp
409 lines (344 loc) · 16 KB
/
glwidget.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
#include "glwidget.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <GL/gl.h>
#include <GL/glu.h>
//#include <GL/freeglut.h>
#include <objectrotation.h>
#include <math.h>
#include <iostream>
// User Defined Variables
GLUquadricObj *quadratic; // Used for Quadric
const float PI2 = 2.0*3.1415926535f; // PI Squared
Matrix4fT Transform = { 1.0f, 0.0f, 0.0f, 0.0f, // NEW: Final Transform
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
Matrix3fT LastRot = { 1.0f, 0.0f, 0.0f, // NEW: Last Rotation
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
Matrix3fT ThisRot = { 1.0f, 0.0f, 0.0f, // NEW: This Rotation
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
ObjectRotation_t ObjectRotation(640.0f, 480.0f); // NEW: ObjectRotation Instance
Point2fT MousePt; // NEW: Current Mouse Point
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
mouseIsPressed = false;
mouseIsDragging = false;
typeOfView = 0;
typeOfColoring = 0;
zoom = -6.0f;
rvd = new ReadVTKData();
pd = new PrepareData(rvd);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(myTimerSlot()));
}
void GLWidget::initializeGL()
{
glClearColor (0.35f, 0.35f, 0.4f, 0.5f); // Background color
glColor4f(0.35f, 0.35f, 0.4f,1.0f);
glClearDepth (1.0f); // Depth Buffer Setup
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
glEnable(GL_DEPTH_TEST); // Enable Depth Testing
glShadeModel(GL_FLAT); // Select Flat Shading (Nice Definition Of Objects)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate
quadratic = gluNewQuadric(); // Create A Pointer To The Quadric Object
gluQuadricNormals(quadratic, GLU_SMOOTH); // Create Smooth Normals
gluQuadricTexture(quadratic, GL_TRUE); // Create Texture Coords
glMatrixMode(GL_TEXTURE);
// glEnable(GL_LIGHTING); // Enable Lighting
// glEnable(GL_COLOR_MATERIAL); // Enable Color Material
// glEnable(GL_LIGHT0); // Enable Default Light
// glEnable(GL_LIGHT1);
// float light_diffuse[] = {0.4, 0.4, 0.8, 1.0};
// float light_position[] = {0.0,-1000.0,0.0,0.0};
// float light_position1[] = {0.0,0.0,100.0,0.0};
// glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
// glLightfv(GL_LIGHT0, GL_POSITION, light_position);
// glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
// float material[] = { 0.5, 0.5, 0.5, 1.0};
// glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,material);
// glEnable(GL_NORMALIZE);
}
void GLWidget::resizeGL(int width, int height) // Reshape The Window When It's Moved Or Resized
{
glViewport(0, 0, width, height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
if( width < height && width > 0 )
glFrustum( -0.1, 0.1, -0.1 * height / width, 0.1 * height / width, 0.1, 100.0 );
else if( width >= height && height > 0 )
glFrustum( -0.1 * width / height, 0.1 * width / height, -0.1, 0.1, 0.1, 100.0 );
// gluPerspective(90.0f,(GLfloat)width/(GLfloat)height,1.0f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
ObjectRotation.setBounds((GLfloat)width, (GLfloat)height); //*NEW* Update mouse bounds for objectrotation
}
void GLWidget::Update(/*DWORD milliseconds*/) // Perform Motion Updates Here
{
/* if (isRClicked) // If Right Mouse Clicked, Reset All Rotations
{
Matrix3fSetIdentity(&LastRot); // Reset Rotation
Matrix3fSetIdentity(&ThisRot); // Reset Rotation
Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Reset Rotation
}
*/
if (!mouseIsDragging) // Not Dragging
{
if (mouseIsPressed) // First Click
{
this->mouseIsDragging = true; // Prepare For Dragging
LastRot = ThisRot; // Set Last Static Rotation To Last Dynamic One
ObjectRotation.click(&MousePt); // Update Start Vector And Prepare For Dragging
}
}
else
{
if (mouseIsPressed) // Still Clicked, So Still Dragging
{
Quat4fT ThisQuat;
ObjectRotation.drag(&MousePt, &ThisQuat); // Update End Vector And Get Rotation As Quaternion
Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat); // Convert Quaternion Into Matrix3fT
Matrix3fMulMatrix3f(&ThisRot, &LastRot); // Accumulate Last Rotation Into This One
Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Set Our Final Transform's Rotation From This One
}
else // No Longer Dragging
mouseIsDragging = false;
}
}
void GLWidget::drawWireframeMonoColor()
{
unsigned int i;
glLineWidth(1.5f);
for(i = 0; i < rvd->cellsNumber; i++)
{
glBegin(GL_LINE_LOOP);
glColor3d(0.0,0.0,0.5);
glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
/* glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glVertex3d(rvd->points[rvd->cells[i].p3].x, rvd->points[rvd->cells[i].p3].y, rvd->points[rvd->cells[i].p3].z);
glVertex3d(rvd->points[rvd->cells[i].p4].x, rvd->points[rvd->cells[i].p4].y, rvd->points[rvd->cells[i].p4].z);
glVertex3d(rvd->points[rvd->cells[i].p5].x, rvd->points[rvd->cells[i].p5].y, rvd->points[rvd->cells[i].p5].z);
glVertex3d(rvd->points[rvd->cells[i].p3].x, rvd->points[rvd->cells[i].p3].y, rvd->points[rvd->cells[i].p3].z);
glVertex3d(rvd->points[rvd->cells[i].p5].x, rvd->points[rvd->cells[i].p5].y, rvd->points[rvd->cells[i].p5].z);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glVertex3d(rvd->points[rvd->cells[i].p4].x, rvd->points[rvd->cells[i].p4].y, rvd->points[rvd->cells[i].p4].z);
*/ glEnd();
}
}
void GLWidget::draw()
{
glPointSize(2);
glLineWidth(1.0f);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
for(unsigned i = 0; i < rvd->cellsNumber; i++)
{
switch(typeOfColoring)
{
case 0:
glBindTexture(GL_TEXTURE_2D, pd->scalarTexID[i]);
break;
case 1:
glBindTexture(GL_TEXTURE_2D, pd->magnitudeTexID[i]);
break;
default:
break;
}
switch(typeOfView)
{
case 0:
glBegin(GL_POINTS); //Drawing points
break;
case 1:
glBegin(GL_TRIANGLES); //Drawing surface
break;
case 2:
glBegin(GL_TRIANGLES); //Drawing surface with monocolor wireframe
break;
case 3:
glBegin(GL_LINE_LOOP); //Drawing wireframe
break;
}
switch (pd->orientation[i])
{
case 0:
glTexCoord2f(0.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(1.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glTexCoord2f(1.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
break;
case 1:
glTexCoord2f(0.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
glTexCoord2f(1.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(1.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
break;
case 2:
glTexCoord2f(0.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(1.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glTexCoord2f(1.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
break;
case 3:
glTexCoord2f(1.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(1.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
break;
case 4:
glTexCoord2f(1.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
glTexCoord2f(1.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
break;
case 5:
glTexCoord2f(1.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glTexCoord2f(1.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
break;
case 6:
glTexCoord2f(0.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(1.0, 0.0);
glVertex3f(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
break;
case 7:
glTexCoord2f(0.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
glTexCoord2f(1.0, 0.0);
glVertex3f(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
break;
case 8:
glTexCoord2f(0.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glTexCoord2f(1.0, 0.0);
glVertex3f(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
break;
case 9:
glTexCoord2f(0.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(1.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
break;
case 10:
glTexCoord2f(0.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
glTexCoord2f(1.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
break;
case 11:
glTexCoord2f(0.0, 1.0);
glVertex3f(rvd->points[rvd->cells[i].p1].x, rvd->points[rvd->cells[i].p1].y, rvd->points[rvd->cells[i].p1].z);
glTexCoord2f(1.0, 1.0);
glVertex3d(rvd->points[rvd->cells[i].p2].x, rvd->points[rvd->cells[i].p2].y, rvd->points[rvd->cells[i].p2].z);
glTexCoord2f(0.0, 0.0);
glVertex3d(rvd->points[rvd->cells[i].p0].x, rvd->points[rvd->cells[i].p0].y, rvd->points[rvd->cells[i].p0].z);
break;
default:
break;
}
glEnd();
}
glDisable(GL_TEXTURE_2D);
if(typeOfView == 2)
drawWireframeMonoColor();
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f,0.0f,this->zoom); // Zoom
glPushMatrix(); // NEW: Prepare Dynamic Transform
glMultMatrixf(Transform.M); // NEW: Apply Dynamic Transform
if(rvd->dataWasReaded)
{
draw();
}
glPopMatrix(); // NEW: Unapply Dynamic Transform
glFlush (); // Flush The GL Rendering Pipeline
}
void GLWidget::myTimerSlot()
{
Update();
paintGL();
makeCurrent();
swapBuffers();
}
void GLWidget::getPathToFile(std::string strPath)
{
if(strPath != "")
{
timer->stop();
rvd->readVTKFile(strPath);
if(rvd->dataWasReaded)
{
zoom = rvd->correctZoom;
emit setCombobox_2(rvd->scalar,rvd->vector);
pd->prepareToShow();
timer->start();
}
}
}
void GLWidget::getTypeOfView(int type)
{
typeOfView = type;
}
void GLWidget::getTypeOfColoring(int type)
{
typeOfColoring = type;
}
void GLWidget::mouseMoveEvent(QMouseEvent *ev)
{
this->x = ev->x();
this->y = ev->y();
MousePt.s.X = ev->x();
MousePt.s.Y = ev->y();
}
void GLWidget::mousePressEvent(QMouseEvent *)
{
this->mouseIsPressed = true;
}
void GLWidget::mouseReleaseEvent(QMouseEvent *)
{
this->mouseIsPressed = false;
}
void GLWidget::wheelEvent(QWheelEvent *ev)
{
if(ev->angleDelta().y() > 0)
{
zoom += 0.2;
}
else
{
zoom -= 0.2;
}
}