-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendert.cu
1773 lines (1481 loc) · 44.9 KB
/
rendert.cu
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "windows.h"
#include <time.h>
#include "GL/glee.h"
#include "consts.h"
#include "nanorod.h"
#include "global.h"
#include "film.h"
#include "tracer.h"
#include "obj_object.h"
#include "texture.h"
#include "IL/ilut.h"
#include "GL/glut.h"
#include "GL/glui.h"
#include "gpu_util.h"
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include "gpu_util.cu"
#include "tracer.cu"
#define _MSVC
#include "cWorldVol.h"
#include "cCameraVol.h"
#include "chai3d/src/chai3d.h"
///////////////////////////
float *deviceData = NULL;
int *idData = NULL;
///////////////////////////
//
//***Haptic globals***
float *hostData = NULL;
int *hostIdData = NULL;
void initHaptic();
// function called before exiting the application
void closeHaptic(void);
// main graphics callback
void updateHapticGraphics(void);
// main haptics loop
void updateHaptics(void);
const int MAX_DEVICES = 1;
// a world that contains all objects of the virtual environment
cWorldVol* world;
// a camera that renders the world in a window display
cCameraVol* camera;
// a light source to illuminate the objects in the virtual scene
cLight *light;
// a little "chai3d" bitmap logo at the bottom of the screen
cBitmap* logo;
// width and height of the current window display
int displayW;
int displayH;
// a haptic device handler
cHapticDeviceHandler* handler;
// a table containing pointers to all haptic devices detected on this computer
cGenericHapticDevice* hapticDevices[MAX_DEVICES];
// a table containing pointers to label which display the position of
// each haptic device
cLabel* labels[MAX_DEVICES];
cGenericObject* rootLabels;
// number of haptic devices detected
int numHapticDevices;
// table containing a list of 3D cursors for each haptic device
cShapeSphere* cursors[MAX_DEVICES];
// table containing a list of lines to display velocity
cShapeLine* velocityVectors[MAX_DEVICES];
// material properties used to render the color of the cursors
cMaterial matCursorButtonON;
cMaterial matCursorButtonOFF;
// status of the main simulation haptics loop
bool simulationRunning;
// root resource path
string resourceRoot;
// damping mode ON/OFF
bool useDamping;
// force field mode ON/OFF
bool useForceField;
// has exited haptics simulation thread
bool simulationFinished;
//Camera tool vector
vect3d vCameraToolVect;
//Toool position
cVector3d posTool;
// a virtual tool representing the haptic device in the scene
cGeneric3dofPointer* tool;
// a spherical object representing the volume so it can have material properties
// and we can change them when volume values change
cShapeSphere* object0;
double stiffnessMax;
double forceMax;
double dampingMax;
int typeOfForce = 0;
int shouldDrawAxes = 1;
ObjObject *pCap0;
//***Haptic Globals end***
//***Haptic Functions***
void initHaptic()
{
displayW = 0;
displayH = 0;
numHapticDevices = 0;
simulationRunning = false;
useDamping = false;
useForceField = true;
simulationFinished = false;
// create a new world.
world = new cWorldVol();
// set the background color of the environment
// the color is defined by its (R,G,B) components.
world->setBackgroundColor(0.0, 0.0, 0.0);
// create a camera and insert it into the virtual world
camera = new cCameraVol(world);
world->addChild(camera);
// position and oriente the camera
camera->set( cVector3d (0.5, 0.0, 0.0), // 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/behind these clipping planes will not be rendered
camera->setClippingPlanes(0.01, 10.0);
// create a light source and attach it to the camera
light = new cLight(world);
camera->addChild(light); // attach light to camera
light->setEnabled(true); // enable light source
light->setPos(cVector3d( 2.0, 0.5, 1.0)); // position the light source
light->setDir(cVector3d(-2.0, 0.5, 1.0)); // define the direction of the light beam
//-----------------------------------------------------------------------
// HAPTIC DEVICES / TOOLS
//-----------------------------------------------------------------------
// create a haptic device handler
handler = new cHapticDeviceHandler();
// read the number of haptic devices currently connected to the computer
numHapticDevices = handler->getNumDevices();
// limit the number of devices to MAX_DEVICES
numHapticDevices = cMin(numHapticDevices, MAX_DEVICES);
// create a node on which we will attach small labels that display the
// position of each haptic device
rootLabels = new cGenericObject();
camera->m_front_2Dscene.addChild(rootLabels);
// create a small label as title
cLabel* titleLabel = new cLabel();
rootLabels->addChild(titleLabel);
// define its position, color and string message
titleLabel->setPos(0, 30, 0);
titleLabel->m_fontColor.set(1.0, 1.0, 1.0);
titleLabel->m_string = "Haptic Device Pos [mm]:";
// for each available haptic device, create a 3D cursor
// and a small line to show velocity
int i = 0;
while (i < numHapticDevices)
{
// get a handle to the next haptic device
cGenericHapticDevice* newHapticDevice;
handler->getDevice(newHapticDevice, i);
// open connection to haptic device
newHapticDevice->open();
// initialize haptic device
newHapticDevice->initialize();
// store the handle in the haptic device table
hapticDevices[i] = newHapticDevice;
// retrieve information about the current haptic device
cHapticDeviceInfo info = newHapticDevice->getSpecifications();
// create a 3D tool and add it to the world
tool = new cGeneric3dofPointer(world);
world->addChild(tool);
// connect the haptic device to the tool
tool->setHapticDevice(hapticDevices[i]);
// initialize tool by connecting to haptic device
tool->start();
// map the physical workspace of the haptic device to a larger virtual workspace.
tool->setWorkspaceRadius(1.0);
// define a radius for the tool
tool->setRadius(0.03);
// read the scale factor between the physical workspace of the haptic
// device and the virtual workspace defined for the tool
double workspaceScaleFactor = tool->getWorkspaceScaleFactor();
// define a maximum stiffness that can be handled by the current
// haptic device. The value is scaled to take into account the
// workspace scale factor
stiffnessMax = info.m_maxForceStiffness / workspaceScaleFactor;
forceMax = info.m_maxForce;
// define the maximum damping factor that can be handled by the
// current haptic device. The The value is scaled to take into account the
// workspace scale factor
dampingMax = info.m_maxLinearDamping / workspaceScaleFactor;
/////////////////////////////////////////////////////////////////////////
// OBJECT 0: "VIBRATIONS"
////////////////////////////////////////////////////////////////////////
// temp variable
cGenericEffect* newEffect;
// create a sphere and define its radius
object0 = new cShapeSphere(2.0);
// add object to world
world->addChild(object0);
// set the position of the object at the center of the world
object0->setPos(0.0, 0.0, 0.0);
object0->setUseTexture(false);
// create a haptic viscous effect
newEffect = new cEffectVibration(object0);
object0->addEffect(newEffect);
newEffect = new cEffectSurface(object0);
object0->addEffect(newEffect);
//newEffect = new cEffectViscosity(object0);
//object0->addEffect(newEffect);
//newEffect = new cEffectMagnet(object0);
//object0->addEffect(newEffect);
// create a cursor by setting its radius
cShapeSphere* newCursor = new cShapeSphere(0.01);
// add cursor to the world
world->addChild(newCursor);
// add cursor to the cursor table
cursors[i] = newCursor;
// create a small line to illustrate velocity
cShapeLine* newLine = new cShapeLine(cVector3d(0,0,0), cVector3d(0,0,0));
velocityVectors[i] = newLine;
// add line to the world
world->addChild(newLine);
// create a string that concatenates the device number and model name.
string strID;
cStr(strID, i);
string strDevice = "#" + strID + " - " +info.m_modelName;
// attach a small label next to the cursor to indicate device information
cLabel* newLabel = new cLabel();
newCursor->addChild(newLabel);
newLabel->m_string = strDevice;
newLabel->setPos(0.00, 0.02, 0.00);
newLabel->m_fontColor.set(1.0, 1.0, 1.0);
// if the device provided orientation sensing (stylus), a reference
// frame is displayed
if (info.m_sensedRotation == true)
{
// display a reference frame
newCursor->setShowFrame(true);
// set the size of the reference frame
newCursor->setFrameSize(0.05, 0.05);
}
// crate a small label to indicate the position of the device
cLabel* newPosLabel = new cLabel();
rootLabels->addChild(newPosLabel);
newPosLabel->setPos(0, -20 * i, 0);
newPosLabel->m_fontColor.set(0.6, 0.6, 0.6);
labels[i] = newPosLabel;
// increment counter
i++;
}
// simulation in now running
simulationRunning = true;
// create a thread which starts the main haptics rendering loop
cThread* hapticsThread = new cThread();
hapticsThread->set(updateHaptics, CHAI_THREAD_PRIORITY_HAPTICS);
}
void closeHaptic(void)
{
// stop the simulation
simulationRunning = false;
// wait for graphics and haptics loops to terminate
while (!simulationFinished) { cSleepMs(100); }
// close all haptic devices
int i=0;
while (i < numHapticDevices)
{
hapticDevices[i]->close();
i++;
}
}
float getElecCellValue(int x, int y, int z, float *elecData, int *idData)
{
if( x < 0 || x >= VOL_X ||
y < 0 || y >= VOL_Y ||
z < 0 || z >= VOL_Z ) // Hard-code it for now
{
return 0;
}
unsigned offset = x + y * VOL_X + z * VOL_X * VOL_Y;
return *(elecData + offset);
}
cVector3d toolCoord2VolCoord(cVector3d toolCoord)
{
//TODO: now it's rotation dependent, it shouldn't
cVector3d result;
float hapticWorkSpaceRadius = 1.f;
if(numHapticDevices > 0)
hapticWorkSpaceRadius = hapticDevices[0]->getSpecifications().m_workspaceRadius;
result.x = toolCoord.y / (hapticWorkSpaceRadius * 2.f) * VOL_X;
result.y = toolCoord.x / (hapticWorkSpaceRadius * 2.f) * VOL_Y;
result.z = toolCoord.z / (hapticWorkSpaceRadius * 2.f) * VOL_Z;
/* %%%Before
cVector3d result;
result.x = (toolCoord.y * VOL_X) / 0.4f;
result.y = (-toolCoord.x * VOL_Y) / 0.4f;
result.z = (toolCoord.z * VOL_Z) / 0.4f;
*/
return result;
}
void setCameraToolVector()
{
if(numHapticDevices > 0)
{
//Save previous camera before doing anything
scene.setPreviousCameraCenter(scene.getCamera());
//Get vector: camera center -> tool
hapticDevices[0]->getPosition(posTool);
posTool = toolCoord2VolCoord(posTool);
vect3d convertedPosTool(posTool.x, posTool.y, posTool.z);
//Vector: camera center -> tool
vCameraToolVect = convertedPosTool - *scene.getPreviousCameraCenter();
}
}
//Use the camera tool vector to set the new position of the tool object in GPU so it does not move
//when transforms are made to the scene
void setToolPositionGPU()
{
if(numHapticDevices > 0)
{
//TODO: for now, just rotation
//Transform cameraToolVect with the same transformations as the ones for the camera
vect3d posNewCameraTool;
vect3d vtmp;
mat_rot(vCameraToolVect, vtmp);
//Add this vector to the camera center point
vect3d *posCamera = scene.computeCameraCenter(scene.getCamera());
posNewCameraTool = *posCamera + vtmp;
//Generate vector: cameraToolVect -> posNewCameraTool
vect3d vOldToolNewTool = posNewCameraTool - (vCameraToolVect + *scene.getPreviousCameraCenter());
//Translate the object in the GPU
setObjectCenterGPU(vOldToolNewTool, 1);
}
}
//Just detect the tool's position and pass it to the GPU
void moveToolPositionGPU()
{
if(numHapticDevices > 0)
{
//Transform cameraToolVect with the same transformations as the ones for the camera
static cVector3d previousPosTool(0,0,0);
cVector3d translation = posTool - previousPosTool;
vect3d convertedTranslation(translation.x, translation.y, translation.z);
//Translate the object in the GPU
translateObjectGPU(convertedTranslation, 1);
previousPosTool = posTool;
}
}
//Just detect the tool's position and translate coordinates to volume coordinates
void moveToolPositionCPU()
{
if(numHapticDevices > 0)
{
//Transform cameraToolVect with the same transformations as the ones for the camera
cVector3d newPosTool;
//Get vector: camera center -> tool
hapticDevices[0]->getPosition(newPosTool);
//Account for the tool's actual imprecisions
//%%%newPosTool.mul(5);
newPosTool = toolCoord2VolCoord(newPosTool);
posTool = newPosTool;
}
}
void updateHapticGraphics(void)
{
// update content of position label
// read position of device an convert into millimeters
if(numHapticDevices > 0)
{
//This is for drawing from the volume's camera
moveToolPositionGPU();
//This is for drawing from the haptic's camera
cVector3d pos;
hapticDevices[0]->getPosition(pos);
pos.mul(5);
// create a string that concatenates the device number and its position.
string strID;
cStr(strID, 0);
string strLabel = "#" + strID + " x: ";
cStr(strLabel, pos.x, 2);
strLabel = strLabel + " y: ";
cStr(strLabel, pos.y, 2);
strLabel = strLabel + " z: ";
cStr(strLabel, pos.z, 2);
labels[0]->m_string = strLabel;
}
//TODO: need to draw it correctly
// camera->renderView(displayW, displayH);
// check for any OpenGL errors
GLenum err;
err = glGetError();
if (err != GL_NO_ERROR) printf("Error: %s\n", gluErrorString(err));
}
void ResetForces()
{
//Vibration
object0->m_material.setVibrationFrequency(0);
object0->m_material.setVibrationAmplitude(0);
//Friction
object0->m_material.setStiffness(0);
object0->m_material.setStaticFriction(0);
object0->m_material.setViscosity(0);
}
void updateHaptics(void)
{
// main haptic simulation loop
while(simulationRunning)
{
if(numHapticDevices > 0)
{
// read position of haptic device
cVector3d newPosition;
hapticDevices[0]->getPosition(newPosition);
// read orientation of haptic device
cMatrix3d newRotation;
hapticDevices[0]->getRotation(newRotation);
// update position and orientation of cursor
cursors[0]->setPos(newPosition);
cursors[0]->setRot(newRotation);
// read linear velocity from device
cVector3d linearVelocity;
hapticDevices[0]->getLinearVelocity(linearVelocity);
// update arrow
velocityVectors[0]->m_pointA = newPosition;
velocityVectors[0]->m_pointB = cAdd(newPosition, linearVelocity);
// read user button status
bool buttonStatus;
hapticDevices[0]->getUserSwitch(0, buttonStatus);
// adjustthe color of the cursor according to the status of
// the user switch (ON = TRUE / OFF = FALSE)
if (buttonStatus)
{
cursors[0]->m_material = matCursorButtonON;
}
else
{
cursors[0]->m_material = matCursorButtonOFF;
}
//get value from data at the position of the tool (the converted position)
moveToolPositionCPU();
float dataRange = fEnd-fStart;
cVector3d newForce (0,0,0);
float val = getElecCellValue(posTool.x + VOL_X/2.f, posTool.y + VOL_Y/2.f, posTool.z+VOL_Z/2.f, hostData, hostIdData) / dataRange;
//printf("%f\n",val);
// set haptic properties according to the voxel inside the volume
// NOTE that there are two ways this is being done, first, object0
// has some properties, then some forces will be applied through the
// tool variable and also some forces are applied directly to the
// haptic device through hapticDevices[0]->setForce()
if(typeOfForce == 1)
{
ResetForces();
//Vibration
object0->m_material.setVibrationFrequency(50.f);
object0->m_material.setVibrationAmplitude(1.0 * forceMax * val);
}
//Magnetic Force
//object0->m_material.setStiffness(0.1 * stiffnessMax * val);
//object0->m_material.setMagnetMaxForce(0.1 * 2000.0 * val);
//object0->m_material.setMagnetMaxDistance(0.05);
//object0->m_material.setViscosity(1.0 * dampingMax);
else if(typeOfForce == 2)
{
ResetForces();
//Friction
object0->m_material.setStiffness(0.1 * stiffnessMax * val);
object0->m_material.setDynamicFriction(1.0 * 2000.0 * val);
object0->m_material.setViscosity(1.0 * dampingMax);
}
else if (typeOfForce == 3)
{
ResetForces();
//Vibration
object0->m_material.setVibrationFrequency(50.f);
object0->m_material.setVibrationAmplitude(1.0 * forceMax * val);
//Friction
object0->m_material.setStiffness(0.1 * stiffnessMax * val);
object0->m_material.setStaticFriction(1.0 * 2000.0 * val);
object0->m_material.setViscosity(1.0 * dampingMax);
}
/*Question: which one reveals more the high value areas? Which one reveals more the structure of the rod?*/
// apply force field
if (typeOfForce == 0)
{
//Compute force
double Kp = 2000.0 * val; // [N/m]
cVector3d force = cMul(-Kp, newPosition);
newForce.add(force);
//Damp
cHapticDeviceInfo info = hapticDevices[0]->getSpecifications();
double Kv = info.m_maxLinearDamping*val;
cVector3d force2 = cMul(-Kv, linearVelocity);
newForce.add(force2);
}
// compute global reference frames for each object
world->computeGlobalPositions(true);
// 4 position and orientation of tool
tool->updatePose();
// compute interaction forces
tool->computeInteractionForces();
if(typeOfForce == 0)
{
// send computed force to haptic device (direct forces)
hapticDevices[0]->setForce(newForce);
}
else
{
// send forces to device (like vibration)
tool->applyForces();
}
}
}
// exit haptics thread
simulationFinished = true;
}
//***Haptic Functions End***
//Shader
//shader variables
GLuint fragShader;
GLuint vertShader;
GLuint program;
GLint fragCompiled;
GLint vertCompiled;
const char *vertProgram;
const char *fragProgram;
void setupShaders()
{
if (!GL_ARB_vertex_program)
{
printf("No shaders!");
return;
}
FILE *file;
file = fopen("./blend.frag","r");
if(file==NULL)
{
MessageBox(NULL,"Couldn't open frag file.","ERROR",MB_OK|MB_ICONEXCLAMATION);
exit(0);
}
char *fragProg;
int size=0;
fseek(file, 0, SEEK_END);
size = ftell(file)+1;
fragProg = new char[size];
fseek(file, 0, SEEK_SET);
size = fread(fragProg,1,size,file);
fragProg[size]='\0';
fclose(file);
fragProgram = fragProg;
file = fopen("blend.vert","r");
if(file==NULL)
{
MessageBox(NULL,"Couldn't open vert file.","ERROR",MB_OK|MB_ICONEXCLAMATION);
exit(0);
}
char *vertProg;
size=0;
fseek(file, 0, SEEK_END);
size = ftell(file)+1;
vertProg = new char[size];
fseek(file, 0, SEEK_SET);
size = fread(vertProg,1,size,file);
vertProg[size]='\0';
fclose(file);
vertProgram = vertProg;
vertShader = glCreateShader(GL_VERTEX_SHADER);
fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, &fragProgram, NULL);
glShaderSource(vertShader, 1, &vertProgram, NULL);
glCompileShader(vertShader);
// getErrors();
// printShaderInfoLog(vertShader);
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &vertCompiled);
glCompileShader(fragShader);
// getErrors();
// printShaderInfoLog(fragShader);
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &fragCompiled);
program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
glUseProgram(program);
}
void drawAxes()
{
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(-100.f, 100.f, -100.f, 100.f, -10000.f, 10000.f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glLineWidth(2);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
glRotatef(2.f, 1, 1, 0);
glBegin(GL_LINES);
{
glColor4f(1.f, 0, 0, 0.2f);
glVertex3f(-100.f, 0,0);
glVertex3f(100.f,0,0);
glColor4f(0, 1.f, 0, 0.2f);
glVertex3f(0, -100.f,0);
glVertex3f(0,100.f,0);
glColor4f(0, 0, 1.f, 0);
glVertex3f(0, 0,-1000.f);
glColor4f(0, 0, 1.f, 1.f);
glVertex3f(0,0,1000.f);
}
glEnd();
glPopMatrix();
glLineWidth(1);
glDisable(GL_BLEND);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_TEXTURE_2D);
// check error
GLenum err = glGetError();
if(err != GL_NO_ERROR)
{
printf("[GL ERROR] %s - %d : 0x%x\n", __FILE__, __LINE__, err);
}
}
void drawCap()
{
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(-1000.f, 1000.f, -1000.f, 1000.f, -10000.f, 10000.f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glLineWidth(2);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
glBegin(GL_TRIANGLES);
{
for(int i =0; i < pCap0->getTriCount(); i++)
{
glVertex3f(
pCap0->getTriangle(i)->_vertices->data[0],
pCap0->getTriangle(i)->_vertices->data[1],
pCap0->getTriangle(i)->_vertices->data[2]);
}
}
glEnd();
glPopMatrix();
glLineWidth(1);
glDisable(GL_BLEND);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_TEXTURE_2D);
// check error
GLenum err = glGetError();
if(err != GL_NO_ERROR)
{
printf("[GL ERROR] %s - %d : 0x%x\n", __FILE__, __LINE__, err);
}
}
//
static
void resize(int w, int h)
{
//%%%
displayW = w;
displayH = h;
//%%%
glViewport(0, 0, w, h);
//%%%
// update position of labels
rootLabels->setPos(10, displayH-70, 0);
//%%%
}
static
void destroy()
{
if(deviceData)
{
cudaFree(deviceData);
deviceData = NULL;
}
if(idData)
{
cudaFree(idData);
idData = NULL;
}
if(deviceTexData)
{
cudaFree(deviceTexData);
}
nanoGeoDestroy();
nanoPlaneDestroy();
internalCap0Destroy();
internalCap1Destroy();
SliceDestroy();
global_destroy();
exit(EXIT_SUCCESS);
}
int iWinId;
void idle()
{
glutSetWindow(iWinId);
glutPostRedisplay();
}
clock_t nTick = 0;
GLUI_RadioGroup *pCMGroup = NULL;
GLUI_EditText *pImgPath = NULL;
// Zooming
static int InitViewZ = 6100;
const int MaxViewZ = 12000;
const int MinViewZ = 100;
static int nCurrViewZ = InitViewZ;
static int nZoomStep = -10;
static int nRotStep = 1;
void zoom_cam(Camera *pCam, float deltaStep);
void rotate_cam(Camera *pCam, float deltaAngle, vect3d &axis);
void capture();
static int volCount = 0;
static
void display()
{
nTick = clock();
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 1, 0, 1);
//%%%
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
0, 0, 1,
0, 0, 0,
0, 1, 0);
glDisable(GL_DEPTH_TEST);
//%%%
scene.setTFMode(iTfMode);
scene.compute();
scene.render( pCMGroup->get_int_val() == 0 ? NULL : pImgPath->get_text());
//%%%
//Render Haptic graphcis
updateHapticGraphics();
//%%%
if(shouldDrawAxes) drawAxes();
glutSwapBuffers();
clock_t nCount = clock() - nTick;
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b FPS: %.4f", 1000.f / (nCount * 1.0));
cudaError_t err = cudaGetLastError();
if(err != cudaSuccess)
{
printf("DUDE: %s \n", cudaGetErrorString(err));
}
//
//if(volCount <= 360)
//{
// capture();
// //nCurrViewZ += nZoomStep;
// //zoom_cam(scene.getCamera(), -nZoomStep);
// rotate_cam(scene.getCamera(), -nRotStep, vect3d(0, 0.3, 1));
//}
}
/// Rotation
static void rotate_cam(Camera *pCam, float deltaAngle, vect3d &axis)
{
PerpCamera *pPCam = dynamic_cast<PerpCamera *>(pCam);
// Rotate
deltaAngle *= - PIon180;
set_matrix(sinf(deltaAngle), cosf(deltaAngle), axis);
vect3d tmp;
mat_rot(pPCam->_eyePos, tmp); vecCopy(pPCam->_eyePos, tmp);
mat_rot(pPCam->_ctrPos, tmp); vecCopy(pPCam->_ctrPos, tmp);
mat_rot(pPCam->_upVec, tmp); vecCopy(pPCam->_upVec, tmp);
mat_rot(pPCam->_rightVec, tmp); vecCopy(pPCam->_rightVec, tmp);
mat_rot(pPCam->_dir, tmp); vecCopy(pPCam->_dir, tmp);
}
static void zoom_cam(Camera *pCam, float deltaStep)
{
PerpCamera *pPCam = dynamic_cast<PerpCamera *>(pCam);
vect3d deltaVec;
vect3d eye(pPCam->_eyePos[0], pPCam->_eyePos[1], pPCam->_eyePos[2]);
vect3d ctr(pPCam->_ctrPos[0], pPCam->_ctrPos[1], pPCam->_ctrPos[2]);
vect3d viewVec;
points2vec(eye, ctr, viewVec);
vecCopy(deltaVec, viewVec); normalize(deltaVec); vecScale(deltaVec, deltaStep, deltaVec);
//printf("->%.3f,%.3f,%.3f \n", viewVec.data[0], viewVec.data[0], viewVec.data[0]);
point2point(pPCam->_eyePos, deltaVec, pPCam->_eyePos);
}
static
void specialKey(int key, int x, int y)
{
switch(key)
{
///
/// Zooming
///
case GLUT_KEY_UP:
//if( (nCurrViewZ - nZoomStep) >= MinViewZ)
{
nCurrViewZ -= nZoomStep;
zoom_cam(scene.getCamera(), nZoomStep);
printf("Zoom-in to %d\n", nCurrViewZ);
}
break;
case GLUT_KEY_DOWN:
//if( (nCurrViewZ + nZoomStep) <= MaxViewZ)
{
nCurrViewZ += nZoomStep;
zoom_cam(scene.getCamera(), -nZoomStep);
printf("Zoom-out to %d\n", nCurrViewZ);
}
break;
///
/// Rotation
///
case GLUT_KEY_RIGHT:
printf(" Right Rot: %d\n", nRotStep);
rotate_cam(scene.getCamera(), -nRotStep, vect3d(0, 0.3, 1));
break;
case GLUT_KEY_LEFT:
printf(" Left Rot: %d\n", -nRotStep);
rotate_cam(scene.getCamera(), nRotStep, vect3d(0, 0.3, 1));
break;
}
}
void capture()