forked from PeterS242/YARRH
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglwidget.cpp
410 lines (342 loc) · 12.1 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
410
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
#include "glwidget.h"
#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D
#endif
//constructor
GlWidget::GlWidget(QWidget *parent)
: QGLWidget(parent)
{
theta=90;
phi=90;
r=20;
eyeX=0;
eyeY=0;
eyeZ=r;
upX=0;
upY=1;
upZ=0;
sizeX=20;
sizeY=20;
zoom = 2.5;
xMove = 0;
yMove = 0;
currentLayer=0;
layers = 5000;
show_travel=false;
qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
this->setFormat(QGLFormat(QGL::SampleBuffers));
}
//destructor
GlWidget::~GlWidget()
{
}
void GlWidget::setCurrentLayer(int layer){
this->currentLayer=layer;
this->repaint();
}
//size hints
QSize GlWidget::minimumSizeHint() const
{
return QSize(50, 50);
}
QSize GlWidget::sizeHint() const
{
return QSize(800, 800);
}
//inizalization of gl
void GlWidget::initializeGL()
{
qglClearColor(qtPurple.dark());
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_FLAT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_MULTISAMPLE);
glEnable(GL_COLOR_MATERIAL);
static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
}
//resize event
void GlWidget::resizeGL(int width, int height)
{
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifdef QT_OPENGL_ES_1
glOrthof(-0.5 + zoom , +0.5 - zoom, -0.5 + zoom, +0.5 - zoom, 4.0, 100.0);
#else
glOrtho(-((float)this->width()/1000*zoom),+((float)this->width()/1000*zoom), -((float)this->height()/1000*zoom), +((float)this->height()/1000*zoom), 4.0, 100.0);
#endif
glMatrixMode(GL_MODELVIEW);
}
void GlWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(eyeX, eyeY, eyeZ, -xMove, -yMove, 0.0, upX, upY, upZ);
for(int i=0; i<this->objects.size(); i++){
this->objects.at(i)->draw(0.01,this->layers, show_travel, this->currentLayer);
}
drawAxis();
drawGrid();
}
//mouse press event
void GlWidget::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
}
//mouse move event
void GlWidget::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->x() - lastPos.x();
int dy = event->y() - lastPos.y();
if(event->x()>0 && event->y()>0 && event->x()<this->width() && event->y()< this->height()){
QPointF clicked=screenToWorld(lastPos.x(),lastPos.y());
QPointF currentClicked=screenToWorld(event->x(),event->y());
if (event->buttons() & Qt::MiddleButton) {
} else if (event->buttons() & Qt::LeftButton) {
// Mouse point to angle conversion
theta -= dy*1.0;
phi -= dx*1.0;
// Spherical to Cartesian conversion.
// Degrees to radians conversion factor 0.0174532
eyeX = r * cos(theta*0.0174532) * cos(phi*0.0174532);
eyeY = r * cos(theta*0.0174532) * sin(phi*0.0174532);
eyeZ = r * sin(theta*0.0174532);
// Reduce theta slightly to obtain another point on the same longitude line on the sphere.
GLfloat dt=0.01;
GLfloat eyeXtemp = r * cos(theta*0.0174532-dt) * cos(phi*0.0174532);
GLfloat eyeYtemp = r * cos(theta*0.0174532-dt) * sin(phi*0.0174532);
GLfloat eyeZtemp = r * sin(theta*0.0174532-dt);
// Connect these two points to obtain the camera's up vector.
upX=eyeXtemp-eyeX;
upY=eyeYtemp-eyeY;
upZ=eyeZtemp-eyeZ;
updateGL();
} else if (event->buttons() & Qt::RightButton){
xMove -= clicked.x()-currentClicked.x();
yMove -= clicked.y()-currentClicked.y();
updateGL();
}
lastPos = event->pos();
}
}
//wheel event
void GlWidget::wheelEvent(QWheelEvent * event){
if((zoom-(float)event->delta()/1920)>0){
zoom -= (float)event->delta()/1920;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifdef QT_OPENGL_ES_1
glOrthof(-0.5 + zoom , +0.5 - zoom, -0.5 + zoom, +0.5 - zoom, 4.0, 100.0);
#else
glOrtho(-((float)this->width()/1000*zoom),+((float)this->width()/1000*zoom), -((float)this->height()/1000*zoom), +((float)this->height()/1000*zoom), 4.0, 100.0);
#endif
glMatrixMode(GL_MODELVIEW);
updateGL();
}
}
//drawing 20x20cm grind in 3dview
void GlWidget::drawGrid(){
GLint iSlices=32;
GLint iStacks=32;
GLdouble lineRadius=0.001;
bool bSolid=true;
bool bBlend=true;
GLfloat fCurrentColor[4];
// Get the current color
glGetFloatv(GL_CURRENT_COLOR, fCurrentColor);
// Save the current transformation matrix..
glPushMatrix();
// Create a quadratic object used to draw our axis
// cylinders and cones
GLUquadricObj* pQuadric = gluNewQuadric();
if(!pQuadric)
return;
// Set the quadric state
gluQuadricNormals(pQuadric, GLU_SMOOTH);
gluQuadricTexture(pQuadric, GL_FALSE);
if(bSolid)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
gluQuadricDrawStyle(pQuadric, GLU_FILL);
}
else
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
gluQuadricDrawStyle(pQuadric, GLU_LINE);
}
// Enable alpha blending?
if(bBlend)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
//draw grid
//rotate for x
glRotated(90, 0.0, 1.0, 0.0);
for(int i=0; i<sizeY; i++){
gluCylinder(pQuadric, lineRadius, lineRadius, (double)sizeX/10, iSlices, iStacks);
glTranslated(0.0f, 0.1f, 0.0f);
}
gluCylinder(pQuadric, lineRadius, lineRadius, (double)sizeX/10, iSlices, iStacks);
glRotated(90, 1.0, 0.0, 0.0);
for(int i=0; i<sizeX+1; i++){
gluCylinder(pQuadric, lineRadius, lineRadius, (double)sizeY/10, iSlices, iStacks);
glTranslated(0.0f, 0.1f, 0.0f);
}
// Delete the quadric
gluDeleteQuadric(pQuadric);
// Restore the current transformation matrix..
glPopMatrix();
// Restore the current color
glColor4fv(fCurrentColor);
// Disable blend function
glDisable(GL_BLEND);
}
//draw axis
void GlWidget::drawAxis()
{
GLdouble dAxisLength=1.0;
GLdouble dAxisRadius=0.05;
GLdouble dArrowLength=0.1;
GLdouble dArrowRadius=0.1;
float fScale=0.1;
GLint iSlices=32;
GLint iStacks=32;
GLfloat fColorX[4]={1.0,0.0,0.0,1.0};
GLfloat fColorY[4]={0.0,1.0,0.0,1.0};
GLfloat fColorZ[4]={0.0,0.0,1.0,1.0};
bool bSolid=true;
bool bBlend=true;
GLdouble dArrowPosn = dAxisLength;// - (dArrowLength/2);
GLfloat fCurrentColor[4];
// Get the current color
glGetFloatv(GL_CURRENT_COLOR, fCurrentColor);
// Save the current transformation matrix..
glPushMatrix();
// Create a quadratic object used to draw our axis
// cylinders and cones
GLUquadricObj* pQuadric = gluNewQuadric();
if(!pQuadric)
return;
// Set the quadric state
gluQuadricNormals(pQuadric, GLU_SMOOTH);
gluQuadricTexture(pQuadric, GL_FALSE);
if(bSolid)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
gluQuadricDrawStyle(pQuadric, GLU_FILL);
}
else
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
gluQuadricDrawStyle(pQuadric, GLU_LINE);
}
// Enable alpha blending?
if(bBlend)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
// Display a Sphere at the axis origin
glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
gluSphere(pQuadric, dAxisRadius*fScale*2.5f, iSlices, iStacks);
// Display the Z-Axis and arrow
// Set the color
glColor4fv(fColorZ);
gluCylinder(pQuadric, dAxisRadius*fScale, dAxisRadius*fScale, dAxisLength*fScale, iSlices, iStacks);
glTranslated(0.0f, 0.0f, dArrowPosn*fScale); // Move to arrow position
gluCylinder(pQuadric, dArrowRadius*fScale, 0.0f, dArrowLength*fScale, iSlices, iStacks);
gluQuadricOrientation(pQuadric, GLU_INSIDE);
gluDisk(pQuadric, 0.0f, dArrowRadius*fScale, iSlices, 1);
gluQuadricOrientation(pQuadric, GLU_OUTSIDE);
glTranslated(0.0f, 0.0f, -dArrowPosn*fScale); // Move to 0, 0, 0
// Display the X-Axis and arrow
// Set the color
glColor4fv(fColorX);
glRotated(90, 0.0, 1.0, 0.0); // Rotate for X
gluCylinder(pQuadric, dAxisRadius*fScale, dAxisRadius*fScale, dAxisLength*fScale, iSlices, iStacks);
glTranslated(0.0f, 0.0f, dArrowPosn*fScale); // Move to arrow position
gluCylinder(pQuadric, dArrowRadius*fScale, 0.0f, dArrowLength*fScale, iSlices, iStacks);
gluQuadricOrientation(pQuadric, GLU_INSIDE);
gluDisk(pQuadric, 0.0f, dArrowRadius*fScale, iSlices, 1);
gluQuadricOrientation(pQuadric, GLU_OUTSIDE);
glTranslated(0.0f, 0.0f, -dArrowPosn*fScale); // Move to 0, 0, 0
// Display the Y-Axis and arrow
// Set the color
glColor4fv(fColorY);
glRotated(-90, 1.0, 0.0, 0.0); // Rotate for Y
gluCylinder(pQuadric, dAxisRadius*fScale, dAxisRadius*fScale, dAxisLength*fScale, iSlices, iStacks);
glTranslated(0.0f, 0.0f, dArrowPosn*fScale); // Move to arrow position
gluCylinder(pQuadric, dArrowRadius*fScale, 0.0f, dArrowLength*fScale, iSlices, iStacks);
gluQuadricOrientation(pQuadric, GLU_INSIDE);
gluDisk(pQuadric, 0.0f, dArrowRadius*fScale, iSlices, 1);
gluQuadricOrientation(pQuadric, GLU_OUTSIDE);
glTranslated(0.0f, 0.0f, -dArrowPosn*fScale); // Move to 0, 0, 0
// Delete the quadric
gluDeleteQuadric(pQuadric);
// Restore the current transformation matrix..
glPopMatrix();
// Restore the current color
glColor4fv(fCurrentColor);
// Disable blend function
glDisable(GL_BLEND);
}
void GlWidget::addObject(GCodeObject *object){
this->objects.append(object);
}
//clearing object list... not sure if deletes them also, will check later
void GlWidget::clearObjects(){
for(int i=0; i<this->objects.size(); i++){
this->objects.at(i)->freeLists();
delete this->objects.at(i);
}
this->objects.clear();
}
void GlWidget::setLayers(int layers){
this->layers=layers;
updateGL();
}
void GlWidget::showTravel(bool show){
this->show_travel=show;
updateGL();
}
QPointF GlWidget::screenToWorld(int x, int y){
// get line of sight through mouse cursor
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
glClear(GL_DEPTH_BUFFER_BIT);
GLfloat fCurrentColor[4];
glGetFloatv(GL_CURRENT_COLOR, fCurrentColor);
glBegin(GL_QUADS);
glVertex3f(-20.0f,-20.0f, 0.00f);
glVertex3f( 20.0f,-20.0f, 0.00f);
glVertex3f( 20.0f, 20.0f, 0.00f);
glVertex3f(-20.0f, 20.0f, 0.00f);
glEnd();
glColor4fv(fCurrentColor);
winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
return QPointF(posX,posY);
}
void GlWidget::setTableSize(int x, int y){
this->sizeX=x;
this->sizeY=y;
this->xMove=(double)-x/(double)20;
this->yMove=(double)-y/(double)20;
updateGL();
}