-
Notifications
You must be signed in to change notification settings - Fork 15
/
DrawLib.cpp
652 lines (516 loc) · 13.4 KB
/
DrawLib.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//
// Copyright (c) 2009-2015 Glen Berseth, Mubbasir Kapadia, Shawn Singh, Petros Faloutsos, Glenn Reinman
// See license.txt for complete license.
//
/// @file DrawLib.cpp
/// @brief Implements Util::DrawLib functionality.
///
#ifdef ENABLE_GUI
#include "util/DrawLib.h"
#include "util/Geometry.h"
#include <iostream>
using namespace Util;
using namespace std;
std::vector<GLuint> DrawLib::_displayLists;
int DrawLib::_currentDisplayListBeingWritten = -1;
int DrawLib::_agentDisplayList = -1;
int DrawLib::_agentDotDisplayList = -1;
int DrawLib::_flagDisplayList = -1;
int DrawLib::_cubeDisplayList = -1;
int DrawLib::_sphereDisplayList = -1;
int DrawLib::_cylinderDisplayList = -1;
GLUquadricObj * DrawLib::_quadric = NULL;
bool DrawLib::_initialized = false;
void DrawLib::init() {
if (_initialized) return;
_initialized = true;
_displayLists.clear();
// setup quadric
_quadric = gluNewQuadric();
gluQuadricNormals(_quadric, GLU_SMOOTH);
gluQuadricTexture(_quadric, GL_TRUE);
// create display lists
_agentDisplayList = _buildAgentDisplayList();
_agentDotDisplayList = _buildAgentDotDisplayList();
_flagDisplayList = _buildFlagDisplayList();
_cubeDisplayList = _buildCubeDisplayList();
_sphereDisplayList = _buildSphereDisplayList();
_cylinderDisplayList = _buildCylinderDisplayList();
}
//
// setBackgroundColor()
//
void DrawLib::setBackgroundColor(const Color& color)
{
glClearColor(color.r, color.g, color.b, 1.0f);
}
//
// enableLights()
//
void DrawLib::enableLights()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
//
// disableLights()
//
void DrawLib::disableLights()
{
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
}
//
// positionLights()
//
void DrawLib::positionLights()
{
GLfloat position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
glLightfv(GL_LIGHT0, GL_POSITION, position);
}
//
// drawLine()
//
void DrawLib::drawLine(const Point & startPoint, const Point & endPoint)
{
// draw line
glBegin(GL_LINES);
{
glNormal(Vector(0.0f, 1.0f, 0.0f));
glVertex(startPoint);
glVertex(endPoint);
}
glEnd();
}
void DrawLib::drawLine(const Point & startPoint, const Point & endPoint, const Color &color)
{
glColor(color);
// draw line
glBegin(GL_LINES);
{
glNormal(Vector(0.0f, 1.0f, 0.0f));
glVertex(startPoint);
glVertex(endPoint);
}
glEnd();
}
void DrawLib::drawLineAlpha(const Point & startPoint, const Point & endPoint, const Color &color, float alpha)
{
glColor4f (color.r, color.g, color.b, alpha);
// draw line
glBegin(GL_LINES);
{
glNormal(Vector(0.0f, 1.0f, 0.0f));
glVertex(startPoint);
glVertex(endPoint);
}
glEnd();
}
void DrawLib::drawLine(const Point & startPoint, const Point & endPoint, const Color &color, float thickness)
{
glLineWidth(thickness);
drawLine(startPoint, endPoint, color);
glLineWidth(1.0f);
}
void DrawLib::drawRay(const Ray & r, const Color & color)
{
drawLine(r.pos, r.pos + (r.dir*r.maxt), color);
drawLine(r.pos, r.pos + (r.dir*r.mint), color);
}
//
// drawQuad()
//
void DrawLib::drawQuad(const Point & a, const Point & b, const Point & c, const Point & d)
{
// Assuming the quad is convex and planar. that way, we only have to compute the normal once.
Vector norm = normalize(cross((b-a),(d-a)));
glBegin(GL_QUADS);
{
glNormal(norm);
glVertex(a);
glNormal(norm);
glVertex(b);
glNormal(norm);
glVertex(c);
glNormal(norm);
glVertex(d);
}
glEnd();
}
//
// drawStar()
//
void DrawLib::drawStar(const Point & pos, const Vector & dir, float radius, const Color& color)
{
Vector forward = radius * normalize(dir);
Vector side;
side = cross(forward, Vector(0.0f, 1.0f, 0.0f));
float theta = M_PI/4;
DrawLib::glColor(color);
// draw star
drawQuad(pos-side, pos-forward, pos+side, pos+forward);
drawQuad(pos - rotateInXZPlane(side,theta), pos - rotateInXZPlane(forward,theta), pos + rotateInXZPlane(side,theta), pos + rotateInXZPlane(forward,theta));
}
//
// drawFlag() - use only after the flag display lists are built.
//
/*
void DrawLib::drawFlag(const Point & loc, const Color& color)
{
glPushMatrix();
{
glTranslatef(loc.x, loc.y, loc.z);
glColor3f(color.r, color.g, color.b);
_drawDisplayList(_flagDisplayList);
}
glPopMatrix();
}
*/
void DrawLib::drawFlag(const Point & loc, const Color& color, float scale)
{
glPushMatrix();
{
glTranslatef(loc.x, loc.y, loc.z);
glColor3f(color.r, color.g, color.b);
glScalef(scale, scale, scale);
_drawDisplayList(_flagDisplayList);
}
glPopMatrix();
}
void DrawLib::drawCircle(const Point & loc, const Color& color, float scale, int points)
{
float angleIncrement = (2*M_PI)/(float)points;
// angleIncrement = 90.0f;
Util::Vector turningVector = Vector(1,0,0) * scale;
Util::Vector nextTurningVector = rotateInXZPlane(turningVector, angleIncrement);
for (int i = 0; i < points; i++)
{
DrawLib::drawLine( loc + turningVector, loc + nextTurningVector, color);
turningVector = nextTurningVector;
nextTurningVector = rotateInXZPlane(turningVector, angleIncrement);
}
}
//
// drawAgentDisc() - use only after the agent display lists are built.
//
void DrawLib::drawAgentDisc(const Point & pos, const Vector & dir, float radius, const Color& color)
{
glPushMatrix();
{
float rad = atan2(dir.z, dir.x)*(-M_180_OVER_PI);
glColor(color);
glTranslate(pos);
glRotatef(rad,0.0f,1.0f,0.0f);
glScalef(radius, radius*4.0f, radius);
_drawDisplayList(_agentDisplayList);
}
glPopMatrix();
}
void DrawLib::drawAgentDisc(const Point & pos, const Vector & dir, const Vector & up, float radius, const Color& color)
{
glPushMatrix();
{
float yaxisrad = atan2(dir.z, dir.x)*(-M_180_OVER_PI);
float xaxisrad = atan2(dir.y, dir.z)*(-M_180_OVER_PI);
float zaxisrad = atan2(dir.y, dir.x)*(-M_180_OVER_PI);
glColor(color);
glTranslate(pos);
glRotatef(yaxisrad,0.0f,1.0f,0.0f);
glRotatef(xaxisrad,1.0f,0.0f,0.0f);
glRotatef(zaxisrad,0.0f,0.0f,1.0f);
glScalef(radius, radius*4.0f, radius);
_drawDisplayList(_agentDisplayList);
}
glPopMatrix();
}
//
// drawAgentDisc() - use only after the agent display lists are built.
//
void DrawLib::drawAgentDisc(const Point & pos, float radius, const Color& color)
{
glPushMatrix();
{
glColor(color);
glTranslate(pos);
glScalef(radius, radius*4.0f, radius);
_drawDisplayList(_agentDotDisplayList);
}
glPopMatrix();
}
//
// drawBox() - draws a box with wireframe
//
void DrawLib::drawBox(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax)
{
Point topLeft(xmin, ymin-0.01f, zmin);
Point topRight(xmax, ymin-0.01f, zmin);
Point botLeft(xmin, ymin-0.01f, zmax);
Point botRight(xmax, ymin-0.01f, zmax);
Point topLefth(xmin, ymax, zmin);
Point topRighth(xmax, ymax, zmin);
Point botLefth(xmin, ymax, zmax);
Point botRighth(xmax, ymax, zmax);
// upper/lower plane
DrawLib::drawQuad(botLefth, botRighth, topRighth, topLefth);
DrawLib::drawQuad(topLeft, topRight, botRight, botLeft);
// top/bot sides
DrawLib::drawQuad(topLeft, topLefth, topRighth, topRight);
DrawLib::drawQuad(botRight, botRighth, botLefth, botLeft);
// left/right sides
DrawLib::drawQuad(botLeft, botLefth, topLefth, topLeft);
DrawLib::drawQuad(topRight, topRighth, botRighth, botRight);
}
//
// drawBoxWireFrame() - draws a wireframe of a box
//
void DrawLib::drawBoxWireFrame(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax)
{
Point topLeft(xmin, ymin-0.01f, zmin);
Point topRight(xmax, ymin-0.01f, zmin);
Point botLeft(xmin, ymin-0.01f, zmax);
Point botRight(xmax, ymin-0.01f, zmax);
Point topLefth(xmin, ymax, zmin);
Point topRighth(xmax, ymax, zmin);
Point botLefth(xmin, ymax, zmax);
Point botRighth(xmax, ymax, zmax);
DrawLib::drawLine(topRight,topRighth);
DrawLib::drawLine(topLeft,topLefth);
DrawLib::drawLine(botLeft,botLefth);
DrawLib::drawLine(botRight,botRighth);
DrawLib::drawLine(topRighth, topLefth);
DrawLib::drawLine(topLefth, botLefth);
DrawLib::drawLine(botLefth, botRighth);
DrawLib::drawLine(botRighth, topRighth);
DrawLib::drawLine(topRight, topLeft);
DrawLib::drawLine(topLeft, botLeft);
DrawLib::drawLine(botLeft, botRight);
DrawLib::drawLine(botRight, topRight);
}
//
// drawCylinder() - draws a cylinder
//
void DrawLib::drawCylinder(const Point & pos, float radius, float ymin, float ymax )
{
glPushMatrix();
{
glTranslate(pos);
glScalef(radius, fabsf(ymax-ymin), radius);
_drawDisplayList(_cylinderDisplayList);
}
glPopMatrix();
}
void DrawLib::drawCylinder(const Point & pos, float radius, float ymin, float ymax, Color color)
{
glColor(color);
drawCylinder(pos ,radius ,ymin ,ymax );
}
//
// drawHighlight() - draws a highlight that can be used to identify various objects for annotation
//
void DrawLib::drawHighlight(const Point & pos, const Vector & dir, float radius, const Color& color)
{
drawStar(pos+Vector(0.f,0.249f,0.f), dir, radius*1.75f, color);
}
//
// _startDefiningDisplayList()
//
int DrawLib::_startDefiningDisplayList()
{
// we better not be already creating another list
if (_currentDisplayListBeingWritten >= 0)
return -1;
// create a new list
GLuint newDisplayList = glGenLists(1);
// see if a new one was actually created
if(newDisplayList == 0)
return -1;
// save the new list
_displayLists.push_back(newDisplayList);
// start the list
glNewList(newDisplayList, GL_COMPILE);
_currentDisplayListBeingWritten = (int)_displayLists.size() - 1;
// return the id for the display list
return _currentDisplayListBeingWritten;
}
//
// _endDefiningDisplayList()
//
int DrawLib::_endDefiningDisplayList(int displayList)
{
// check to make sure we're actually ending the current list
if(_currentDisplayListBeingWritten != displayList)
return -1;
// end the display list
glEndList();
// end the list
_currentDisplayListBeingWritten = -1;
return displayList;
}
//
// drawDisplayList()
//
int DrawLib::_drawDisplayList(int displayList)
{
if(displayList < 0 || displayList >= (int)_displayLists.size())
return -1;
glCallList(_displayLists[displayList]);
return displayList;
}
//
// buildAgentDisplayList
//
int DrawLib::_buildAgentDisplayList()
{
static const float h = .5;
int dl = _startDefiningDisplayList();
glPushMatrix();
{
glPushAttrib(GL_ENABLE_BIT);
{
glDisable(GL_CULL_FACE);
// align cylinder to y axis
glRotatef(-90, 1, 0, 0);
// draw cylinder aligned w/ x axis
gluCylinder(_quadric, 1, 1, h, 32, 16);
glTranslatef(0, 0, h);
gluDisk(_quadric, 0, 1, 32, 16);
}
glPopAttrib();
}
glPopMatrix();
// draw arrow
//glColor3f(0.2f, 0.0f, 1.0f);
glColor3f(0.0f, 0.0f, 0.0f);
drawQuad(Point(1.0f, h+.01f, 0.0f),
Point(-0.6f, h+0.01f, -0.5f),
Point(-0.62f, h+0.01f, 0.0f),
Point(-0.6f, h+0.01f, 0.5f));
_endDefiningDisplayList(dl);
return dl;
}
//
// buildAgentDisplayList
//
int DrawLib::_buildAgentDotDisplayList()
{
static const float h = .5;
int dl = _startDefiningDisplayList();
glPushMatrix();
{
glPushAttrib(GL_ENABLE_BIT);
{
glDisable(GL_CULL_FACE);
// align cylinder to y axis
glRotatef(-90, 1, 0, 0);
// draw cylinder aligned w/ x axis
gluCylinder(_quadric, 1, 1, h, 32, 16);
glTranslatef(0, 0, h);
gluDisk(_quadric, 0, 1, 32, 16);
}
glPopAttrib();
}
glPopMatrix();
_endDefiningDisplayList(dl);
return dl;
}
int DrawLib::_buildFlagDisplayList()
{
int dl = _startDefiningDisplayList();
glPushMatrix();
{
// raise the flag
glRotatef(-90,1,0,0);
// flag
drawQuad(Point(0.0f, 0.0f, 1.05f),
Point(0.5, 0.0f, 0.9f),
Point(0.0f, 0.0f, 0.75f),
Point(-0.02f, 0.0f, 0.9f));
drawQuad(Point(-0.02f, 0.0f, 0.9f),
Point(0.0f, 0.0f, 0.75f),
Point(0.5f, 0.0f, 0.9f),
Point(0.0f, 0.0f, 1.05f));
// flag pole
glColor(Color(0.6f, 0.6f, 0.6f));
gluCylinder(_quadric, 0.05, 0.05, 1.1, 32, 1);
glTranslatef(0.0f, 0.0f, 1.1f);
gluSphere(_quadric, .07, 16, 16);
}
glPopMatrix();
_endDefiningDisplayList(dl);
return dl;
}
int DrawLib::_buildCubeDisplayList()
{
int dl = _startDefiningDisplayList();
// front
drawQuad(Point(-0.5f, 0.5f, -0.5f),
Point(0.5f, 0.5f, -0.5f),
Point(0.5f, -0.5f, -0.5f),
Point(-0.5f, -0.5f, -0.5f));
// back
drawQuad(Point(0.5f, 0.5f, 0.5f),
Point(-0.5f, 0.5f, 0.5f),
Point(-0.5f, -0.5f, 0.5f),
Point(0.5f, -0.5f, 0.5f));
// left
drawQuad(Point(-0.5f, 0.5f, 0.5f),
Point(-0.5f, 0.5f, -0.5f),
Point(-0.5f, -0.5f, -0.5f),
Point(-0.5f, -0.5f, 0.5f));
// right
drawQuad(Point(0.5f, 0.5f, -0.5f),
Point(0.5f, 0.5f, 0.5f),
Point(0.5f, -0.5f, 0.5f),
Point(0.5f, -0.5f, -0.5f));
// top
drawQuad(Point(0.5f, 0.5f, 0.5f),
Point(0.5f, 0.5f, -0.5f),
Point(-0.5f, 0.5f, -0.5f),
Point(-0.5f, 0.5f, 0.5f));
// bottom
drawQuad(Point(0.5f, -0.5f, -0.5f),
Point(0.5f, -0.5f, 0.5f),
Point(-0.5f, -0.5f, 0.5f),
Point(-0.5f, -0.5f, -0.5f));
_endDefiningDisplayList(dl);
return dl;
}
int DrawLib::_buildSphereDisplayList()
{
int dl = _startDefiningDisplayList();
gluSphere(_quadric, 1, 16, 16);
_endDefiningDisplayList(dl);
return dl;
}
int DrawLib::_buildCylinderDisplayList()
{
int dl = _startDefiningDisplayList();
glPushMatrix();
{
// draw cylinder
glPushAttrib(GL_ENABLE_BIT);
{
glDisable(GL_CULL_FACE);
glEnable(GL_NORMALIZE);
// align cylinder to y axis
glRotatef(-90, 1, 0, 0);
// draw cylinder aligned w/ x axis
gluCylinder(_quadric, 1.0f, 1.0f, 1.0f, 16, 1);
glTranslatef(0, 0, 1);
gluDisk(_quadric, 0, 1.0f, 16, 1);
}
glPopAttrib();
}
glPopMatrix();
_endDefiningDisplayList(dl);
return dl;
}
void DrawLib::drawCube()
{
_drawDisplayList(_cubeDisplayList);
}
void DrawLib::drawSphere()
{
_drawDisplayList(_sphereDisplayList);
}
#endif // ifdef ENABLE_GUI