-
Notifications
You must be signed in to change notification settings - Fork 0
/
openglscene.cpp
294 lines (246 loc) · 8.29 KB
/
openglscene.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
#include "openglscene.h"
#include "ui_openglscene.h"
#include <GL/freeglut.h>
#include "Image_Processing.h"
#include "qtopencv.h"
#include "GRtool.h"
OpenGLScene::OpenGLScene(QWidget *parent) :
QGLWidget(parent),
ui(new Ui::OpenGLScene)
{
ui->setupUi(this);
setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
rotationX=0.0;
rotationY=0.0;
rotationZ=0.0;
view_distance = -100;
timer = new QTimer(this);
timer->setInterval(50);
connect(timer,SIGNAL(timeout()),this,SLOT(updateGL()));
timer->start();
}
OpenGLScene::~OpenGLScene()
{
delete ui;
}
void OpenGLScene::initializeGL()
{
qglClearColor(Qt::gray);
glEnable(GL_DEPTH_TEST);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
GLfloat diffuseMaterial[4]={0.5, 0.5, 0.5, 1.0};
GLfloat mat_specular[]={1.0, 1.0, 1.0, 1.0};
GLfloat light_position[]={-1.0, 3.0, 3.0, 0.0};
GLfloat light_ambient[]={1, 1, 1, 0.1};
GLfloat white_light[] = {0.5f, 0.5f, 0.5f, 1.0f};
//Enable lighting
glEnable(GL_LIGHTING);
//Setup and enable light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white_light);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
resizeGL(845, 580);
}
void OpenGLScene::resizeGL(int w, int h)
{
GLfloat fAspect;
//Prevent a devide by zero
if (h==0) h=1;
//Set viewport to window dimensions
glViewport(0,0,w,h);
fAspect = (GLfloat)w/(GLfloat)h;
//Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Produce the perspective projection
gluPerspective(60.0f, fAspect, 1.0, 400.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
#ifdef __ORTHO__
#define
GLfloat nRange = 100.0f;
//Prevent a divide by Zero
if (0==h)
h = 1;
//Set Viewport to window dimensions
glViewport(0, 0, w, h);
//Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho(-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho(-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
//Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
#endif
}
void OpenGLScene::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
flushView(); //change camera view accroding to mouse Events;
glColor3f(0.1, 0.1, 0.1);
glutWireCube(80.0f);
grDrawGround(40, 40, -40);
GLfloat x, y, z;
x = (AUTO_CAL_X-320)/6.4; //AUTO_CAL_Y=0;
y = (AUTO_CAL_Y-240)/4.8;
z = -80;
drawSphere(10, x, y, z);
struct TransfMat t;
t.rotx = 2; t.roty=0; t.rotz=-30; t.x=-10; t.y=0; t.z=0;
// for (t.rotz = 0; t.rotz < 360; t.rotz+=30)
// for (t.roty = 0; t.roty < 360; t.roty+=30)
// this->drawManipulator(1.0, &t);
this->drawManipulator(1.0f, &t);
drawGripper(1.0f, 45.0, &t);
t.rotx = 0; t.roty=180; t.rotz=-30; t.x=0; t.y=0; t.z=0;
this->drawManipulator(1.0f, &t);
drawGripper(1.0f, 10.0, &t);
}
void OpenGLScene::flushView()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, view_distance);
glRotatef(rotationX*3.14, 0.0f, 1.0f, 0.0f);
glRotatef(rotationY*3.14, 1.0f, 0.0f, 0.0f);
}
void OpenGLScene::drawSphere(GLdouble size, GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glTranslatef(x, y, z);
glColor3f(0.8, 0.4, 0.2);
glutSolidSphere(size, 20, 16);
glPopMatrix();
}
void OpenGLScene::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
}
void OpenGLScene::mouseMoveEvent(QMouseEvent *event)
{
GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
if(event->buttons() & Qt::LeftButton) {
rotationX += 180 * dx;
rotationY += 180 * dy;
updateGL();
}
else if(event->buttons() & Qt::RightButton) {
rotationX += 180 * dy;
rotationZ += 180 * dx;
}
cout << "rotateX, Y = " << rotationX <<", "<< rotationY<<endl;
lastPos = event->pos();
cout << "dx, dy = " << dx <<", "<< dy <<endl;
}
void OpenGLScene::wheelEvent(QWheelEvent *event)
{
view_distance += 4*event->delta()/8.0/15.0;
cout << "mouseWheet = " << view_distance << endl;
}
void OpenGLScene::drawManipulator(GLdouble size_factor, struct TransfMat *t)
{
//define the geometric shape of manipulator in (cm)
GLfloat shaft_len = 28.5;
GLfloat shaft_dia = 0.6/2;
GLfloat motor_group_len = 3+2.95+4.45+0.2;
GLfloat motot_group_dia = 6/2;
GLfloat single_motor_len = 4.45;
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glRotatef(t->rotx, 1, 0, 0);
glRotatef(t->roty, 0, 1, 0);
glRotatef(t->rotz, 0, 0, 1);
glTranslatef(t->x, t->y, t->z);
glTranslatef(-shaft_len/2, 0, 0);
//Draw a big cylinder as motor group
glPushMatrix();
glColor3f(0.5, 0.5, 0.2);
glTranslatef(-(motor_group_len/2+shaft_len/2), 0, 0);
glRotatef(90, 0, 1, 0);
grDrawSolidCylinder(motot_group_dia*size_factor, motot_group_dia*size_factor, motor_group_len*size_factor);
glPopMatrix();
//Draw a single motor
glPushMatrix();
glColor3f(0.01, 0.01, 0.01);
glTranslatef(-(single_motor_len/2+motor_group_len+shaft_len/2), 0, 1.7);
glRotatef(90, 0, 1, 0);
grDrawSolidCylinder(1*size_factor, 1*size_factor, single_motor_len*size_factor);
glPopMatrix();
//Draw a long thin cylinder as shaft
glPushMatrix();
glColor3f(0.5, 0.5, 0.1);
glTranslatef(0, 0, 0);
glRotatef(90, 0, 1, 0);
grDrawSolidCylinder(shaft_dia*size_factor, shaft_dia*size_factor, shaft_len*size_factor);
glPopMatrix();
glPopMatrix();
}
void OpenGLScene::drawGripper(GLdouble size_factor, GLfloat grip_angle, struct TransfMat *t)
{
//define the geometric shape of gripper and wrist in (cm)
GLfloat gripper_len = 1.45;
GLfloat driving_len = 1.1;
GLfloat wrist_len = 1.28;
GLfloat driving_dia = 0.6/2;
GLfloat wrist_dia = 0.6/2;
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glRotatef(t->rotx, 1, 0, 0);
glRotatef(t->roty, 0, 1, 0);
glRotatef(t->rotz, 0, 0, 1);
glTranslatef(t->x, t->y, t->z);
glTranslatef(driving_len+wrist_len, 0 ,0);
//Draw two cube as gripper
glPushMatrix();
glRotatef(-grip_angle/2, 0, 1, 0);
glTranslatef(gripper_len/2, 0, 0.1f*gripper_len/2);
glColor3f(0.3, 0.03, 0.0);
glScalef(gripper_len, 0.2f*gripper_len, 0.1f*gripper_len);
glutSolidCube(1.0f);
glPopMatrix();
glPushMatrix();
glRotatef(grip_angle/2, 0, 1, 0);
glTranslatef(gripper_len/2, 0, -0.1f*gripper_len/2);
glScalef(gripper_len, 0.2f*gripper_len, 0.1f*gripper_len);
glutSolidCube(1.0f);
glPopMatrix();
//Draw gripper driving
glPushMatrix();
glColor3f(0.5, 0.5, 0.2);
glTranslatef(-(driving_len/2), 0, 0);
glRotatef(90, 0, 1, 0);
grDrawSolidCylinder(driving_dia*size_factor, driving_dia*size_factor, driving_len*size_factor);
glPopMatrix();
//Draw wrist
glPushMatrix();
glColor3f(0.5, 0.5, 0.2);
glTranslatef(-((driving_len+wrist_len)/2), 0, 0);
//glRotatef(90, 0, 1, 0);
//grDrawSolidCylinder(wrist_dia*size_factor, wrist_dia*size_factor, wrist_len*size_factor);
glTranslatef(-wrist_len/7, 0, 0);
glutSolidSphere(wrist_dia, 10, 10);
glTranslatef(-wrist_len/2.5, 0, 0);
glutSolidSphere(wrist_dia, 10, 10);
glTranslatef(-wrist_len/2.5, 0, 0);
glutSolidSphere(wrist_dia, 10, 10);
glPopMatrix();
glPopMatrix();
}