-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.wheel.cpp
487 lines (377 loc) · 10.1 KB
/
3.wheel.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
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<windows.h>
#include<GL/glut.h>
#define pi (2*acos(0.0))
double cameraHeight;
double cameraAngle;
int drawGrid;
int drawAxes;
float angle;
struct Point
{
float x,y,z;
};
Point pos,u,r,l;
/* FUNCTIONS */
void drawAxes_();
void drawGrid_();
void specialKeyListener(int key, int x, int y);
void keyboardListener(unsigned char key, int x, int y);
void mouseListener(int button, int state, int x, int y);
void init();
void display();
Point wheelpos;
int wheelRadius;
int wheelAngle;
float wheelDist;
int wheelSegments;
/*
The wheel is made with only rectangles
and translating and rotating the
rectangular slabs
*/
void drawWheel(int radius, int segments, float angle, float dist, Point pos )
{
int i;
float perimeter = 2*pi*radius;
double rotateBy = 360*dist/perimeter;
glPushMatrix();
{
glTranslatef(0,0,radius);
glRotated(90,0,1,0);
glPushMatrix();
{
glTranslatef(pos.x,pos.y,pos.z);
glRotatef(angle,1,0,0);
glRotatef(rotateBy,0,0,1);
float width = perimeter / segments;
//width /= 2;
//draw segments using generated points
for(i=0; i<segments; i++)
{
glPushMatrix();
{
glColor3f((double)i/(double)segments+0.5, (double)i/(double)segments, (double)i/(double)segments);
glRotatef((((double)i/(double)segments)*360.0),0,0,1);
glTranslatef(radius,0,0);
//glRotatef(180,0,0,1);
//glRotated(270,1,0,0);
glBegin(GL_QUADS);
glVertex3f(0,width, width);
glVertex3f(0,width,-width);
glVertex3f(0,-width, -width);
glVertex3f(0,-width, width);
glEnd();
}
glPopMatrix();
}
// the two spikes
glBegin(GL_QUADS);
glVertex3f(0,radius, width);
glVertex3f(0,radius,-width);
glVertex3f(0,-radius, -width);
glVertex3f(0,-radius, width);
glEnd();
glRotatef(90,0,0,1);
glBegin(GL_QUADS);
glVertex3f(0,radius, width);
glVertex3f(0,radius,-width);
glVertex3f(0,-radius, -width);
glVertex3f(0,-radius, width);
glEnd();
}
glPopMatrix();
}
glPopMatrix();
}
/*
The wheel is made with only two circles
and rectangles in the middle
*/
void drawWheel_1(int radius, int segments, int dist,int angle, Point pos)
{
int i;
struct Point points[100];
float perimeter = 2*pi*radius;
//float moveBy = perimeter * angle/360;
float rotateBy = 360*dist/perimeter;
int width = perimeter/segments;
glPushMatrix();
{
glTranslatef(0,0,20);
glRotatef(90,0,1,0);
glPushMatrix();
{
glTranslatef(pos.x,pos.y, pos.z);
glRotatef(angle,1,0,0);
glRotatef(-rotateBy,0,0,1);
//generate points
for(i=0; i<=segments; i++)
{
points[i].x=radius*cos(((double)i/(double)segments)*2*pi);
points[i].y=radius*sin(((double)i/(double)segments)*2*pi);
}
//draw segments using generated points
for(i=0; i<segments; i++)
{
//first circle
glBegin(GL_LINES);
{
glVertex3f(points[i].x,points[i].y,0);
glVertex3f(points[i+1].x,points[i+1].y,0);
}
glEnd();
//second circle
glBegin(GL_LINES);
{
glVertex3f(points[i].x,points[i].y,10);
glVertex3f(points[i+1].x,points[i+1].y,10);
}
glEnd();
}
//drawing the rectangles
for(i=0; i<segments; i++)
{
glColor3f((double)i/(double)segments+0.5, (double)i/(double)segments, (double)i/(double)segments);
glBegin(GL_QUADS);
glVertex3f(points[i].x,points[i].y,0);
glVertex3f(points[i+1].x,points[i+1].y,0);
glVertex3f(points[i+1].x,points[i+1].y,10);
glVertex3f(points[i].x,points[i].y,10);
glEnd();
}
//drawing the spikes
glPushMatrix();
glColor3f(1,0.4,1);
glRotatef(90,1,0,0);
glTranslatef(0,5,0);
glBegin(GL_QUADS);
glVertex3f(radius,width/2,0);
glVertex3f(radius,-width/2,0);
glVertex3f(-radius,-width/2,0);
glVertex3f(-radius,width/2,0);
glEnd();
glPopMatrix();
glPushMatrix();
glColor3f(1,0.4,1);
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glTranslatef(0,5,0);
glBegin(GL_QUADS);
glVertex3f(radius,width/2,0);
glVertex3f(radius,-width/2,0);
glVertex3f(-radius,-width/2,0);
glVertex3f(-radius,width/2,0);
glEnd();
glPopMatrix();
}
glPopMatrix();
}
glPopMatrix();
}
/*
tried to draw the wheel using trianglular fans
*/
//void drawWheel()
//{
// float x,y;
//
// int radius = 20;
// int segs = 40;
// int step = (360 / segs) * 3.1416/pi;
// float prev_x, prev_y;
// glColor3f(0,0,1);
//glPushMatrix();
//glRotatef(-90,0,1,0);
//drawWheelBody(radius, segs);
// drawCircle(radius,segs);
// drawCircle(radius+10,segs);
//glTranslatef(0,0,20);
//drawCircle(radius,segs);
//drawCircle(radius+10,segs);
// glPopMatrix();
// glPushMatrix();
// glRotatef(-90,0,1,0);
// for(i=0; i<segs; ++i)
// {
// prev_x = x;
// prev_y = y;
//
// x = sin(step * i) * radius;
// y = cos(step * i) * radius;
//
// glColor3f((double)i/(double)segs+0.5, (double)i/(double)segs, (double)i/(double)segs);
// glBegin(GL_TRIANGLES);
// glVertex3f(10, 0, 0);
//
// glVertex3f(x+10, y, 0);
// glVertex3f(prev_x+10, prev_y,0);
// glEnd();
// }
// //glVertex3f(radius, 10, 10);
// glPopMatrix();
//}
void drawAxes_()
{
if(drawAxes == 1)
{
glBegin(GL_LINES);
///red x
glColor3f(1.0,0,0);
glVertex3f(100,0,0);
glVertex3f(-100,0,0);
///green y
glColor3f(0,1.0,0);
glVertex3f(0,100,0);
glVertex3f(0,-100,0);
///blue z
glColor3f(0,0,1.0);
glVertex3f(0,0,100);
glVertex3f(0,0,-100);
glEnd();
}
}
void drawGrid_()
{
int i;
if(drawGrid == 1)
{
glColor3f(1,0.6,0.6);
glBegin(GL_LINES);
for(i = -8; i <=8; i++)
{
if(i ==0 )
continue;
///lines parallel to y axis
glVertex3f(i*10,-90,0);
glVertex3f(i*10,90,0);
///lines parallel to x axis
glVertex3f(-90,i*10,0);
glVertex3f(90,i*10,0);
}
glEnd();
}
}
void specialKeyListener(int key, int x,int y)
{
switch(key)
{
case GLUT_KEY_DOWN: //down arrow key
cameraHeight -= 3.0;
break;
case GLUT_KEY_UP: // up arrow key
cameraHeight += 3.0;
break;
case GLUT_KEY_RIGHT:
cameraAngle += 0.03;
break;
case GLUT_KEY_LEFT:
cameraAngle -= 0.03;
break;
default:
break;
}
}
void keyboardListener(unsigned char key, int x, int y)
{
printf("%c\n",key);
Point l_, u_, r_;
float angInRadian = wheelAngle * pi/180; //converting degree to radian
switch(key)
{
case 's':
wheelpos.y += cos(angInRadian) ;
wheelpos.z += sin(angInRadian);
wheelDist += 2.0;
break;
case 'd':
wheelAngle +=2 ;
break;
case 'w':
wheelpos.y -= cos(angInRadian) ;
wheelpos.z -= sin(angInRadian);
wheelDist -= 2.0;
break;
case 'a':
wheelAngle -=2 ;
break;
case 'q':
exit(1);
break;
default:
break;
}
}
void mouseListener(int button, int state, int x, int y)
{
printf("%d %d\n",button, state);
}
void init()
{
///initialization
drawGrid = 1;
drawAxes = 1;
cameraHeight = 40.0;
cameraAngle = 1.0;
//angle = 0.05f;
pos.x = 110;
pos.y = 110;
pos.z = 40;
wheelpos.x = 0;
wheelpos.y = 0;
wheelpos.z = 0;
wheelAngle = 0;
wheelDist = 0;
wheelRadius = 20;
wheelSegments = 20;
///clear screen
glClearColor(0.1,0.5,0.1,0);
///set up projection here
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80,1,1,1000.0);
}
void display()
{
///clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///set up camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(100*cos(cameraAngle), 100*sin(cameraAngle), cameraHeight, 0,0,0, 0,0,1);
glMatrixMode(GL_MODELVIEW);
///add objects from here
drawAxes_();
drawGrid_();
drawWheel_1(wheelRadius, wheelSegments, wheelDist, wheelAngle, wheelpos);
//drawWheel(wheelRadius,wheelSegments, wheelAngle,wheelDist, wheelpos);
//drawWheel(wheelpos, wheelAngle, wheelDist, wheelRadius);
///flush
glutSwapBuffers();
}
void animate()
{
//angle+=0.05;
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(600,600);
glutInitWindowPosition(300,100);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("DEMO_WHEEL");
init();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutIdleFunc(animate);
glutKeyboardFunc(keyboardListener);
glutSpecialFunc(specialKeyListener);
glutMouseFunc(mouseListener);
glutMainLoop();
return 0;
}