/
main.cpp
1008 lines (753 loc) · 30 KB
/
main.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* DH2660 Haptic Programming Spring 2017
* HapMap - Group 5 (Thea, Linnéa, Kirsten)
* Code based on Chai3D Examples 14, 21
*
* Distribution license: BSD (e.g. free to use for
* most purposes, see end of file)
*
*/
//------------------------------------------------------------------------------
#include "chai3d.h"
//------------------------------------------------------------------------------
#include <GLFW/glfw3.h>
//------------------------------------------------------------------------------
using namespace chai3d;
using namespace std;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// GENERAL SETTINGS
//------------------------------------------------------------------------------
// stereo Mode
/*
C_STEREO_DISABLED: Stereo is disabled
C_STEREO_ACTIVE: Active stereo for OpenGL NVDIA QUADRO cards
C_STEREO_PASSIVE_LEFT_RIGHT: Passive stereo where L/R images are rendered next to each other
C_STEREO_PASSIVE_TOP_BOTTOM: Passive stereo where L/R images are rendered above each other
*/
cStereoMode stereoMode = C_STEREO_DISABLED;
// fullscreen mode
bool fullscreen = false;
// mirrored display
bool mirroredDisplay = false;
//------------------------------------------------------------------------------
// DECLARED VARIABLES
//------------------------------------------------------------------------------
// a world that contains all objects of the virtual environment
cWorld* world;
// a camera to render the world in the window display
cCamera* camera;
// a light source to illuminate the objects in the world
cDirectionalLight *light;
// a haptic device handler
cHapticDeviceHandler* handler;
// a pointer to the current haptic device
cGenericHapticDevicePtr hapticDevice;
// a virtual tool representing the haptic device in the scene
cToolCursor* tool;
// a few mesh objects
cMultiMesh* object;
cMultiMesh* object1;
cMesh* object2;
cMultiMesh* object3;
// a colored background
cBackground* background;
// a font for rendering text
cFontPtr font;
// a label to display the rate [Hz] at which the simulation is running
cLabel* labelRates;
// add labels for buildings
cLabel* buildingLabel;
cLabel* buildingLabel2;
cLabel* buildingLabel3;
cLabel* buildingLabel4;
cLabel* buildingLabel5;
cLabel* buildingLabel6;
// a flag that indicates if the haptic simulation is currently running
bool simulationRunning = false;
// a flag that indicates if the haptic simulation has terminated
bool simulationFinished = true;
// display options
bool showEdges = true;
bool showTriangles = true;
bool showNormals = false;
// display level for collision tree
int collisionTreeDisplayLevel = 0;
// a frequency counter to measure the simulation graphic rate
cFrequencyCounter freqCounterGraphics;
// a frequency counter to measure the simulation haptic rate
cFrequencyCounter freqCounterHaptics;
// haptic thread
cThread* hapticsThread;
// a handle to window display context
GLFWwindow* window = NULL;
// current width of window
int width = 0;
// current height of window
int height = 0;
// swap interval for the display context (vertical synchronization)
int swapInterval = 1;
// root resource path
string resourceRoot;
//------------------------------------------------------------------------------
// DECLARED MACROS
//------------------------------------------------------------------------------
// convert to resource path
#define RESOURCE_PATH(p) (char*)((resourceRoot+string(p)).c_str())
//------------------------------------------------------------------------------
// DECLARED FUNCTIONS
//------------------------------------------------------------------------------
// callback when the window display is resized
void windowSizeCallback(GLFWwindow* a_window, int a_width, int a_height);
// callback when an error GLFW occurs
void errorCallback(int error, const char* a_description);
// callback when a key is pressed
void keyCallback(GLFWwindow* a_window, int a_key, int a_scancode, int a_action, int a_mods);
// this function renders the scene
void updateGraphics(void);
// this function contains the main haptics simulation loop
void updateHaptics(void);
// this function closes the application
void close(void);
//==============================================================================
/*
DEMO: 14-textures.cpp
This example illustrates the use of haptic textures projected onto mesh
surfaces.
*/
//==============================================================================
int main(int argc, char* argv[])
{
//--------------------------------------------------------------------------
// INITIALIZATION
//--------------------------------------------------------------------------
cout << endl;
cout << "-----------------------------------" << endl;
cout << "CHAI3D" << endl;
cout << "Demo: 14-textures" << endl;
cout << "Copyright 2003-2016" << endl;
cout << "-----------------------------------" << endl << endl << endl;
cout << "Keyboard Options:" << endl << endl;
cout << "[f] - Enable/Disable full screen mode" << endl;
cout << "[m] - Enable/Disable vertical mirroring" << endl;
cout << "[q] - Exit application" << endl;
cout << endl << endl;
// parse first arg to try and locate resources
resourceRoot = string(argv[0]).substr(0,string(argv[0]).find_last_of("/\\")+1);
//--------------------------------------------------------------------------
// OPEN GL - WINDOW DISPLAY
//--------------------------------------------------------------------------
// initialize GLFW library
if (!glfwInit())
{
cout << "failed initialization" << endl;
cSleepMs(1000);
return 1;
}
// set error callback
glfwSetErrorCallback(errorCallback);
// compute desired size of window
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
int w = 0.8 * mode->height;
int h = 0.5 * mode->height;
int x = 0.5 * (mode->width - w);
int y = 0.5 * (mode->height - h);
// set OpenGL version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
// set active stereo mode
if (stereoMode == C_STEREO_ACTIVE)
{
glfwWindowHint(GLFW_STEREO, GL_TRUE);
}
else
{
glfwWindowHint(GLFW_STEREO, GL_FALSE);
}
// create display context
window = glfwCreateWindow(w, h, "HapMap", NULL, NULL);
if (!window)
{
cout << "failed to create window" << endl;
cSleepMs(1000);
glfwTerminate();
return 1;
}
// get width and height of window
glfwGetWindowSize(window, &width, &height);
// set position of window
glfwSetWindowPos(window, x, y);
// set key callback
glfwSetKeyCallback(window, keyCallback);
// set resize callback
glfwSetWindowSizeCallback(window, windowSizeCallback);
// set current display context
glfwMakeContextCurrent(window);
// sets the swap interval for the current display context
glfwSwapInterval(swapInterval);
// initialize GLEW library
#ifdef GLEW_VERSION
if (glewInit() != GLEW_OK)
{
cout << "failed to initialize GLEW library" << endl;
glfwTerminate();
return 1;
}
#endif
//--------------------------------------------------------------------------
// WORLD - CAMERA - LIGHTING
//--------------------------------------------------------------------------
// create a new world.
world = new cWorld();
// set the background color of the environment
world->m_backgroundColor.setBlack();
// create a camera and insert it into the virtual world
camera = new cCamera(world);
world->addChild(camera);
// position and orient the camera
camera->set(cVector3d(0.7, 0.0, 0.2), // SET Z TO 0.2!!!! camera position (eye)
cVector3d(0.0, 0.0, 0.0), // lookat position (target)
cVector3d(0.0, 0.0, 1.0)); // direction of the (up) vector
// set the near and far clipping planes of the camera
// anything in front or behind these clipping planes will not be rendered
camera->setClippingPlanes(0.01, 100);
// set stereo mode
camera->setStereoMode(stereoMode);
// set stereo eye separation and focal length (applies only if stereo is enabled)
camera->setStereoEyeSeparation(0.03);
camera->setStereoFocalLength(1.5);
// set vertical mirrored display mode
camera->setMirrorVertical(mirroredDisplay);
// enable multi-pass rendering to handle transparent objects
camera->setUseMultipassTransparency(true);
// create a light source
light = new cDirectionalLight(world);
// enable light source
light->setEnabled(true);
// attach light to camera
world->addChild(light);
camera->addChild(light);
// define the direction of the light beam
light->setDir(-3.0,-0.5, 0.0);
// set lighting conditions
light->m_ambient.set(1.0f, 1.0f, 1.0f);
light->m_diffuse.set(0.8f, 0.8f, 0.8f);
light->m_specular.set(1.0f, 1.0f, 1.0f);
//--------------------------------------------------------------------------
// HAPTIC DEVICES / TOOLS
//--------------------------------------------------------------------------
// create a haptic device handler
handler = new cHapticDeviceHandler();
// get access to the first available haptic device
handler->getDevice(hapticDevice, 0);
// retrieve information about the current haptic device
cHapticDeviceInfo hapticDeviceInfo = hapticDevice->getSpecifications();
// create a 3D tool and add it to the world
tool = new cToolCursor(world);
world->addChild(tool);
// connect the haptic device to the tool
tool->setHapticDevice(hapticDevice);
// if the haptic device has a gripper, enable it as a user switch
hapticDevice->setEnableGripperUserSwitch(true);
// set radius of tool
double toolRadius = 0.005;
// define a radius for the tool
tool->setRadius(toolRadius);
// hide the device sphere. only show proxy.
tool->setShowContactPoints(true, false);
// create a white cursor
tool->m_hapticPoint->m_sphereProxy->m_material->setBlueCadet();
// map the physical workspace of the haptic device to a larger virtual workspace.
tool->setWorkspaceRadius(0.25);
// oriente tool with camera
tool->setLocalRot(camera->getLocalRot());
// haptic forces are enabled only if small forces are first sent to the device;
// this mode avoids the force spike that occurs when the application starts when
// the tool is located inside an object for instance.
tool->setWaitForSmallForce(true);
// start the haptic tool
tool->start();
//--------------------------------------------------------------------------
// CREATE OBJECTS
//--------------------------------------------------------------------------
// read the scale factor between the physical workspace of the haptic
// device and the virtual workspace defined for the tool
//double workspaceScaleFactor = tool->getWorkspaceScaleFactor();
// properties
double maxStiffness = hapticDeviceInfo.m_maxLinearStiffness;// / workspaceScaleFactor;
/////////////////////////////////////////////////////////////////////////
// OBJECT 0: KTH Map - Thea, Linnéa, Kirsten
////////////////////////////////////////////////////////////////////////
// create a multimesh
object = new cMultiMesh();
// add object to world
world->addChild(object);
// set graphic properties
bool fileload;
fileload = object->loadFromFile("image_objects/kth_campus.obj");
if (!fileload)
{
cout << "Error - image failed to load correctly." << endl;
close();
return (-1);
}
// set material of object
cMaterial m;
m.setWhite();
object->setMaterial(m);
// object->setTransparencyLevel(0.8);
// disable culling so that faces are rendered on both sides
object->setUseCulling(false);
// compute a boundary box
object->computeBoundaryBox(true);
// show/hide boundary box
object->setShowBoundaryBox(false);
// create collision detector
object->createAABBCollisionDetector(toolRadius);
// center object in scene
object->setLocalPos(-1.0 * object->getBoundaryCenter());
std::cout <<"Position: "<< object->getLocalPos() << std::endl;
object->setLocalPos(0.05, 0, 0.05);
// compute all edges of object for which adjacent triangles have more than 40 degree angle
object->computeAllEdges(0);
// set line width of edges and color
cColorf colorEdges;
colorEdges.setBlack();
object->setEdgeProperties(1, colorEdges);
// set normal properties for display
cColorf colorNormals;
colorNormals.setOrangeTomato();
object->setNormalsProperties(0.01, colorNormals);
// set haptic properties
object->setStiffness(0.3*maxStiffness);
// display options
object->setShowTriangles(showTriangles);
object->setShowEdges(showEdges);
object->setShowNormals(showNormals);
/////////////////////////////////////////////////////////////////////////
// OBJECT 1: Plane - Thea, Linnéa, Kirsten
////////////////////////////////////////////////////////////////////////
// create a multimesh
object1 = new cMultiMesh();
// add object to world
world->addChild(object1);
// set the position of the object
object1->setLocalPos(0, 0, 0.05);
// set graphic properties
fileload = object1->loadFromFile("image_objects/kth_campus_plane.obj");
if (!fileload)
{
cout << "Error - image failed to load correctly." << endl;
close();
return (-1);
}
// create collision detector
object1->createAABBCollisionDetector(toolRadius);
// set material of object
cMaterial p;
p.setGray();
object1->setMaterial(p);
// disable culling so that faces are rendered on both sides
object1->setUseCulling(false);
// compute a boundary box
object1->computeBoundaryBox(true);
// show/hide boundary box
object1->setShowBoundaryBox(false);
// center object in scene
//object1->setLocalPos(-1.0 * object->getBoundaryCenter());
// compute all edges of object for which adjacent triangles have more than 40 degree angle
object1->computeAllEdges(0);
// set haptic properties
object1->setStiffness(0.3 * maxStiffness);
object1->setFriction(0.5, 0.1);
// display options
object1->setShowTriangles(showTriangles);
object1->setShowEdges(false);
object1->setShowNormals(false);
/////////////////////////////////////////////////////////////////////////
// OBJECT 2: Grass Texture - Thea, Linnéa, Kirsten
////////////////////////////////////////////////////////////////////////
// create a mesh
object2 = new cMesh();
// create plane
cCreatePlane(object2, 0.19, 0.14);
// create collision detector
object2->createAABBCollisionDetector(toolRadius);
// add object to world
world->addChild(object2);
// set the position of the object
object2->setLocalPos(-0.02, 0.15, 0.0501);
object2->rotateAboutLocalAxisDeg (0,0,1,-20);
// set graphic properties
object2->m_texture = cTexture2d::create();
fileload = object2->m_texture->loadFromFile("image_objects/grass.jpg");
if (!fileload)
{
cout << "Error - Texture image failed to load correctly." << endl;
close();
return (-1);
}
// enable texture mapping
object2->setUseTexture(true);
object2->m_material->setWhite();
// create normal map from texture data
cNormalMapPtr normalMap2 = cNormalMap::create();
normalMap2->createMap(object2->m_texture);
object2->m_normalMap = normalMap2;
// set haptic properties
object2->m_material->setStiffness(0.2 * maxStiffness);
object2->m_material->setStaticFriction(0.2);
object2->m_material->setDynamicFriction(0.2);
object2->m_material->setTextureLevel(0.075);
object2->m_material->setHapticTriangleSides(true, false);
/////////////////////////////////////////////////////////////////////////
// OBJECT 3: Beacon - Thea, Linnéa, Kirsten
////////////////////////////////////////////////////////////////////////
// create a multimesh
object3 = new cMultiMesh();
// add object to world
world->addChild(object3);
// set the position of the object
object3->setLocalPos(0.16, -.16, 0.055);
// set graphic properties
fileload = object3->loadFromFile("image_objects/beacon.obj");
if (!fileload)
{
cout << "Error - image failed to load correctly." << endl;
close();
return (-1);
}
// create collision detector
object3->createAABBCollisionDetector(toolRadius);
// disable culling so that faces are rendered on both sides
object3->setUseCulling(false);
// compute a boundary box
object3->computeBoundaryBox(true);
// show/hide boundary box
object3->setShowBoundaryBox(false);
// compute all edges of object for which adjacent triangles have more than 40 degree angle
object3->computeAllEdges(0);
// set haptic properties
object3->setStiffness(0.005 * maxStiffness);
// display options
object3->setShowTriangles(showTriangles);
object3->setShowEdges(false);
object3->setShowNormals(false);
//--------------------------------------------------------------------------
// WIDGETS
//--------------------------------------------------------------------------
// create a font
font = NEW_CFONTCALIBRI20();
// create a label to display the haptic and graphic rate of the simulation
labelRates = new cLabel(font);
labelRates->m_fontColor.setBlack();
// camera->m_frontLayer->addChild(labelRates);
buildingLabel = new cLabel(font);
buildingLabel->m_fontColor.setGrayLight();
camera->m_frontLayer->addChild(buildingLabel);
buildingLabel2 = new cLabel(font);
buildingLabel2->m_fontColor.setWhite();
// camera->m_frontLayer->addChild(buildingLabel2);
buildingLabel3 = new cLabel(font);
buildingLabel3->m_fontColor.setWhite();
// camera->m_frontLayer->addChild(buildingLabel3);
buildingLabel4 = new cLabel(font);
buildingLabel4->m_fontColor.setWhite();
// camera->m_frontLayer->addChild(buildingLabel4);
buildingLabel5 = new cLabel(font);
buildingLabel5->m_fontColor.setWhite();
// camera->m_frontLayer->addChild(buildingLabel5);
buildingLabel6 = new cLabel(font);
buildingLabel6->m_fontColor.setWhite();
// camera->m_frontLayer->addChild(buildingLabel6);
// create a background
background = new cBackground();
camera->m_backLayer->addChild(background);
// set background properties
background->setCornerColors(cColorf(0.3, 0.3, 0.3),
cColorf(0.2, 0.2, 0.2),
cColorf(0.1, 0.1, 0.1),
cColorf(0.0, 0.0, 0.0));
//--------------------------------------------------------------------------
// START SIMULATION
//--------------------------------------------------------------------------
// create a thread which starts the main haptics rendering loop
hapticsThread = new cThread();
hapticsThread->start(updateHaptics, CTHREAD_PRIORITY_HAPTICS);
// setup callback when application exits
atexit(close);
//--------------------------------------------------------------------------
// MAIN GRAPHIC LOOP
//--------------------------------------------------------------------------
// call window size callback at initialization
windowSizeCallback(window, width, height);
// main graphic loop
while (!glfwWindowShouldClose(window))
{
// get width and height of window
glfwGetWindowSize(window, &width, &height);
// render graphics
updateGraphics();
// swap buffers
glfwSwapBuffers(window);
// process events
glfwPollEvents();
// signal frequency counter
freqCounterGraphics.signal(1);
}
// close window
glfwDestroyWindow(window);
// terminate GLFW library
glfwTerminate();
// exit
return 0;
}
//------------------------------------------------------------------------------
void windowSizeCallback(GLFWwindow* a_window, int a_width, int a_height)
{
// update window size
width = a_width;
height = a_height;
}
//------------------------------------------------------------------------------
void errorCallback(int a_error, const char* a_description)
{
cout << "Error: " << a_description << endl;
}
//------------------------------------------------------------------------------
void keyCallback(GLFWwindow* a_window, int a_key, int a_scancode, int a_action, int a_mods)
{
// filter calls that only include a key press
if ((a_action != GLFW_PRESS) && (a_action != GLFW_REPEAT))
{
return;
}
// option - exit
else if ((a_key == GLFW_KEY_ESCAPE) || (a_key == GLFW_KEY_Q))
{
glfwSetWindowShouldClose(a_window, GLFW_TRUE);
}
// option - toggle fullscreen
else if (a_key == GLFW_KEY_F)
{
// toggle state variable
fullscreen = !fullscreen;
// get handle to monitor
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
// get information about monitor
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
// set fullscreen or window mode
if (fullscreen)
{
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
glfwSwapInterval(swapInterval);
}
else
{
int w = 0.8 * mode->height;
int h = 0.5 * mode->height;
int x = 0.5 * (mode->width - w);
int y = 0.5 * (mode->height - h);
glfwSetWindowMonitor(window, NULL, x, y, w, h, mode->refreshRate);
glfwSwapInterval(swapInterval);
}
}
// option - toggle vertical mirroring
else if (a_key == GLFW_KEY_M)
{
mirroredDisplay = !mirroredDisplay;
camera->setMirrorVertical(mirroredDisplay);
}
}
//------------------------------------------------------------------------------
void close(void)
{
// stop the simulation
simulationRunning = false;
// wait for graphics and haptics loops to terminate
while (!simulationFinished) { cSleepMs(100); }
// close haptic device
tool->stop();
// delete resources
delete hapticsThread;
delete world;
delete handler;
}
//------------------------------------------------------------------------------
void updateGraphics(void)
{
/////////////////////////////////////////////////////////////////////
// UPDATE WIDGETS
/////////////////////////////////////////////////////////////////////
// update haptic and graphic rate data
labelRates->setText(cStr(freqCounterGraphics.getFrequency(), 0) + " Hz / " +
cStr(freqCounterHaptics.getFrequency(), 0) + " Hz");
// update position of label
labelRates->setLocalPos((int)(0.5 * (width - labelRates->getWidth())), 15);
buildingLabel->setText("Nymble");
buildingLabel->setLocalPos((int)(.27 * width), (int)(.48 * height));
buildingLabel2->setText("Entre");
buildingLabel2->setLocalPos(400,175,0);
buildingLabel3->setText("D");
buildingLabel3->setLocalPos(725,100,0);
buildingLabel4->setText("E");
buildingLabel4->setLocalPos(555,135,0);
buildingLabel5->setText("Biblioteket");
buildingLabel5->setLocalPos(400,410,0);
buildingLabel6->setText("Arktektur");
buildingLabel6->setLocalPos(425,300,0);
/////////////////////////////////////////////////////////////////////
// RENDER SCENE
/////////////////////////////////////////////////////////////////////
// update shadow maps (if any)
world->updateShadowMaps(false, mirroredDisplay);
// render world
camera->renderView(width, height);
// wait until all GL commands are completed
glFinish();
// check for any OpenGL errors
GLenum err = glGetError();
if (err != GL_NO_ERROR) cout << "Error: " << gluErrorString(err) << endl;
}
//------------------------------------------------------------------------------
enum cMode
{
IDLE,
SELECTION
};
void updateHaptics(void)
{
cMode state = IDLE;
cGenericObject* selectedObject = NULL;
cTransform tool_T_object;
// simulation in now running
simulationRunning = true;
simulationFinished = false;
// main haptic simulation loop
while(simulationRunning)
{
/////////////////////////////////////////////////////////////////////////
// HAPTIC RENDERING
/////////////////////////////////////////////////////////////////////////
// signal frequency counter
freqCounterHaptics.signal(1);
// compute global reference frames for each object
world->computeGlobalPositions(true);
// update position and orientation of tool
tool->updateFromDevice();
// compute interaction forces
tool->computeInteractionForces();
/*
/////////////////////////////////////////////////////////////////////////
// MANIPULATION
/////////////////////////////////////////////////////////////////////////
// compute transformation from world to tool (haptic device)
cTransform world_T_tool = tool->getDeviceGlobalTransform();
// get status of user switch
bool button = tool->getUserSwitch(0);
//
// STATE 1:
// Idle mode - user presses the user switch
//
if ((state == IDLE) && (button == true))
{
// check if at least one contact has occurred
if (tool->m_hapticPoint->getNumCollisionEvents() > 0)
{
// get contact event
cCollisionEvent* collisionEvent = tool->m_hapticPoint->getCollisionEvent(0);
// get object from contact event
selectedObject = collisionEvent->m_object;
}
else
{
selectedObject = object;
}
// get transformation from object
cTransform world_T_object = selectedObject->getGlobalTransform();
// compute inverse transformation from contact point to object
cTransform tool_T_world = world_T_tool;
tool_T_world.invert();
// store current transformation tool
tool_T_object = tool_T_world * world_T_object;
// update state
state = SELECTION;
}
//
// STATE 2:
// Selection mode - operator maintains user switch enabled and moves object
//
else if ((state == SELECTION) && (button == true))
{
// compute new transformation of object in global coordinates
cTransform world_T_object = world_T_tool * tool_T_object;
// compute new transformation of object in local coordinates
cTransform parent_T_world = selectedObject->getParent()->getLocalTransform();
parent_T_world.invert();
cTransform parent_T_object = parent_T_world * world_T_object;
// assign new local transformation to object
selectedObject->setLocalTransform(parent_T_object);
// set zero forces when manipulating objects
tool->setDeviceGlobalForce(0.0, 0.0, 0.0);
tool->initialize();
}
//
// STATE 3:
// Finalize Selection mode - operator releases user switch.
//
else
{
state = IDLE;
}
*/
/////////////////////////////////////////////////////////////////////////
// FINALIZE
/////////////////////////////////////////////////////////////////////////
// send forces to haptic device
//tool->applyToDevice();
cVector3d computedForce = tool->getDeviceGlobalForce();
computedForce += cVector3d(0,0,-.5);
hapticDevice->setForce(computedForce);
}
// exit haptics thread
simulationFinished = true;
}
//------------------------------------------------------------------------------
//==============================================================================
/*
*
* Example code borrowed from Chai3D library:
Software License Agreement (BSD License)
Copyright (c) 2003-2016, CHAI3D.
(www.chai3d.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of CHAI3D nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE