-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-shaders.cpp
1339 lines (1024 loc) · 41.7 KB
/
08-shaders.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
//==============================================================================
/*
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
POSSIBILITY OF SUCH DAMAGE.
\author <http://www.chai3d.org>
\author Francois Conti
\version 3.2.0 $Rev: 1929 $
*/
//==============================================================================
//------------------------------------------------------------------------------
#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;
const double SPHERE_RADIUS = 0.02;
//------------------------------------------------------------------------------
// 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;
// four cameras to render the world from different perspectives
cCamera* cameraView1;
cCamera* cameraView2;
// Four colored background
cBackground* background;
cBackground* background2;
// Four view panels
cViewPanel* viewPanel1;
cViewPanel* viewPanel2;
// Four framebuffer
cFrameBufferPtr frameBuffer1;
cFrameBufferPtr frameBuffer2;
//------------------------------------------------------------------------------
// STATES
//------------------------------------------------------------------------------
enum MouseState
{
MOUSE_IDLE,
MOUSE_SELECTION
};
// a light source
cSpotLight *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 line representing the velocity vector of the haptic device
cShapeLine* velocity;
// a virtual mesh like object
cMesh* object;
// sphere objects
cMesh* spheres;
// a font for rendering text
cFontPtr font;
// a font for rendering text of position tool
cFontPtr fontPos;
// a label to display the rate [Hz] at which the simulation is running
cLabel* labelRates;
cLabel* labelRates2;
// a label to display the rate [Hz] at which the simulation is running
cLabel* labelRatesPos;
// 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;
// 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;
// mouse position
double mouseX, mouseY;
// mouse state
MouseState mouseState = MOUSE_IDLE;
//------------------------------------------------------------------------------
// RELIEF MAPPING
//------------------------------------------------------------------------------
//Scale Relief
float heightScale;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// 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);
// callback to handle mouse click
void mouseButtonCallback(GLFWwindow* a_window, int a_button, int a_action, int a_mods);
// callback to handle mouse motion
void mouseMotionCallback(GLFWwindow* a_window, double a_posX, double a_posY);
// 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);
bool moveW = false;
//==============================================================================
/*
DEMO: 08-shaders.cpp
This example illustrates how to build a small mesh cube.
The applications also presents the use of texture properties by defining
a texture image and associated texture coordinates for each of the vertices.
A bump map is also used in combination with a shader to create a more
realistic rendering of the surface.
*/
//==============================================================================
int main(int argc, char* argv[])
{
//--------------------------------------------------------------------------
// INITIALIZATION
//--------------------------------------------------------------------------
cout << endl;
cout << "-----------------------------------" << endl;
cout << "Final Project" << 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
string 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, "Final Project", 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 mouse position callback
glfwSetCursorPosCallback(window, mouseMotionCallback);
// set mouse button callback
glfwSetMouseButtonCallback(window, mouseButtonCallback);
// set current display context
glfwMakeContextCurrent(window);
// sets the swap interval for the current display context
glfwSwapInterval(swapInterval);
#ifdef GLEW_VERSION
// initialize GLEW library
if (glewInit() != GLEW_OK)
{
cout << "failed to initialize GLEW library" << endl;
glfwTerminate();
return 1;
}
#endif
//--------------------------------------------------------------------------
// WORLD
//--------------------------------------------------------------------------
// create a new world.
world = new cWorld();
// set the background color of the environment
// the color is defined by its (R,G,B) components.
world->m_backgroundColor.setWhite();
//--------------------------------------------------------------------------
// CAMERA
//--------------------------------------------------------------------------
// create a camera and insert it into the virtual world
//camera = new cCamera(world);
camera = new cCamera(NULL);
cameraView1 = new cCamera(world);
world->addChild(cameraView1);
// position and orient the camera
cameraView1->set(cVector3d(1.43, 0.4, 0.860), // camera position (eye)
cVector3d(-0.732, -0.252, -0.63), // lookat position (target)
cVector3d(-0.602, -0.180, 0.773)); // 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
cameraView1->setClippingPlanes(0.01, 10.0);
// set stereo mode
cameraView1->setStereoMode(stereoMode);
// set stereo eye separation and focal length (applies only if stereo is enabled)
cameraView1->setStereoEyeSeparation(0.03);
cameraView1->setStereoFocalLength(3.0);
// set vertical mirrored display mode
cameraView1->setMirrorVertical(mirroredDisplay);
// set camera field of view
cameraView1->setFieldViewAngleDeg(45);
//create camera 2
cameraView2 = new cCamera(world);
world->addChild(cameraView2);
// position and orient the camera
cameraView2->set(cVector3d(1.5, 0.0, -0.27), // 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
cameraView2->setClippingPlanes(0.01, 10.0);
// set stereo mode
cameraView2->setStereoMode(stereoMode);
// set stereo eye separation and focal length (applies only if stereo is enabled)
cameraView2->setStereoEyeSeparation(0.03);
cameraView2->setStereoFocalLength(3.0);
// set vertical mirrored display mode
cameraView2->setMirrorVertical(mirroredDisplay);
// set camera field of view
cameraView2->setFieldViewAngleDeg(45);
//--------------------------------------------------------------------------
// LIGHT SOURCES
//--------------------------------------------------------------------------
// create a light source
light = new cSpotLight(world);
// add light to world
world->addChild(light);
// enable light source
light->setEnabled(true);
// position the light source
light->setLocalPos(3.5, 2.0, 8.0);
// define the direction of the light beam
light->setDir(-3.5, -2.0, -8.0);
// set light cone half angle
light->setCutOffAngleDeg(20);
//--------------------------------------------------------------------------
// 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();
// if the device has a gripper, enable the gripper to simulate a user switch
hapticDevice->setEnableGripperUserSwitch(true);
// 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);
// define the radius of the tool (sphere)
double toolRadius = SPHERE_RADIUS;
// define a radius for the tool
tool->setRadius(toolRadius);
// set color of proxy sphere
tool->m_hapticPoint->m_sphereProxy->m_material->setGreenLimeGreen();
// show proxy and device position of finger-proxy algorithm
tool->setShowContactPoints(true, true, cColorf(0.0, 0.0, 0.0));
// enable if objects in the scene are going to rotate of translate
// or possibly collide against the tool. If the environment
// is entirely static, you can set this parameter to "false"
tool->enableDynamicObjects(false);
// map the physical workspace of the haptic device to a larger virtual workspace.
tool->setWorkspaceRadius(0.9);
// start the haptic tool
tool->start();
// create small line to illustrate the velocity of the haptic device
velocity = new cShapeLine(cVector3d(0, 0, 0),
cVector3d(0, 0, 0));
// insert line inside world
world->addChild(velocity);
//--------------------------------------------------------------------------
// CREATE OBJECT
//--------------------------------------------------------------------------
// read the scale factor between the physical workspace of the haptic
// device and the virtual workspace defined for the tool
double workspaceScaleFactor = tool->getWorkspaceScaleFactor();
// stiffness properties
double maxStiffness = hapticDeviceInfo.m_maxLinearStiffness / workspaceScaleFactor;
// create a virtual mesh
object = new cMesh();
// add object to world
world->addChild(object);
// set the position of the object at the center of the world
//object->setLocalPos(0.0, 0.0, -0.75);
object->setLocalPos(0.0, 0.0, -0.3);
// create cube
//cCreateBox(object, 0.8, 0.8, 0.8);
// create plane
cCreatePlane(object, 0.9, 0.9);
// create a texture
cTexture2dPtr texture = cTexture2d::create();
bool fileload = texture->loadFromFile(RESOURCE_PATH("../resources/images/wood.png"));
if (!fileload)
{
#if defined(_MSVC)
fileload = texture->loadFromFile("../../../bin/resources/images/wood.png");
#endif
}
if (!fileload)
{
cout << "Error - Texture image failed to load correctly." << endl;
// close();
// return (-1);
}
cTexture2dPtr texture2 = cTexture2d::create();
texture2->setTextureUnit(GL_TEXTURE4);
fileload = texture2->loadFromFile(RESOURCE_PATH("../../../resources/images/toy_box_disp.png"));
if (!fileload)
{
#if defined(_MSVC)
fileload = texture2->loadFromFile("../../../bin/resources/images/toy_box_disp.png");
#endif
}
if (!fileload)
{
cout << "Error - Texture2 image failed to load correctly." << endl;
// close();
// return (-1);
}
// apply texture to object
object->setTexture(texture);
object->setTexture2(texture2);
// enable texture rendering
object->setUseTexture(true);
// Since we don't need to see our polygons from both sides, we enable culling.
object->setUseCulling(true);
// set material properties to light gray
object->m_material->setWhite();
// set material shininess
object->m_material->setShininess(80);
// compute collision detection algorithm
object->createAABBCollisionDetector(toolRadius);
// define a default stiffness for the object
//object->m_material->setStiffness(0.5 * maxStiffness);
object->m_material->setStiffness(0.5 * maxStiffness);
// define some static friction
object->m_material->setStaticFriction(0.0); //0.2
// define some dynamic friction
object->m_material->setDynamicFriction(0.0); //0.2
// define some texture rendering
object->m_material->setTextureLevel(1.0);
// render triangles haptically on front side only
object->m_material->setHapticTriangleSides(true, false);
// create a normal texture
cNormalMapPtr normalMap = cNormalMap::create();
// load normal map from file
fileload = normalMap->loadFromFile(RESOURCE_PATH("../resources/images/toy_box_normal.png"));
if (!fileload)
{
#if defined(_MSVC)
fileload = normalMap->loadFromFile("../../../bin/resources/images/toy_box_normal.pngq");
#endif
}
if (!fileload)
{
cout << "Error - Texture image failed to load correctly." << endl;
close();
return (-1);
}
// assign normal map to object
object->m_normalMap = normalMap;
// compute tangent vectors
object->computeBTN();
//--------------------------------------------------------------------------
// CREATE SHADERS
//--------------------------------------------------------------------------
// create vertex shader
cShaderPtr vertexShader = cShader::create(C_VERTEX_SHADER);
string modeMappingV, modeMappingF;
int modeM = 1;
switch (modeM)
{
case 0:
modeMappingV = RESOURCE_PATH("../resources/shaders/bump.vert");
modeMappingF = RESOURCE_PATH("../resources/shaders/bump.frag");
#if defined(_MSVC)
modeMappingV = "../../../bin/resources/shaders/bump.vert";
modeMappingF = "../../../bin/resources/shaders/bump.frag";
#endif
break;
case 1:
modeMappingV = RESOURCE_PATH("../resources/shaders/parallaxmapping.vert");
modeMappingF = RESOURCE_PATH("../resources/shaders/parallaxmapping.frag");
#if defined(_MSVC)
modeMappingV = "../../../bin/resources/shaders/parallaxmapping.vert";
modeMappingF = "../../../bin/resources/shaders/parallaxmapping.frag";
#endif
break;
case 2:
modeMappingV = RESOURCE_PATH("../resources/shaders/ReliefMapping.vert");
modeMappingF = RESOURCE_PATH("../resources/shaders/ReliefMapping.frag");
#if defined(_MSVC)
modeMappingV = "../../../bin/resources/shaders/ReliefMapping.vert";
modeMappingF = "../../../bin/resources/shaders/ReliefMapping.frag";
#endif
break;
default:
break;
}
// load vertex shader from file
fileload = vertexShader->loadSourceFile(modeMappingV);
/*if (!fileload)
{
#if defined(_MSVC)
fileload = vertexShader->loadSourceFile("../../../bin/resources/shaders/parallaxmapping.vert");
#endif
}*/
// create fragment shader
cShaderPtr fragmentShader = cShader::create(C_FRAGMENT_SHADER);
// load fragment shader from file
fileload = fragmentShader->loadSourceFile(modeMappingF);
/*if (!fileload)
{
#if defined(_MSVC)
fileload = fragmentShader->loadSourceFile("../../../bin/resources/shaders/parallaxmapping.frag");
#endif
}*/
// create program shader
cShaderProgramPtr programShader = cShaderProgram::create();
// assign vertex shader to program shader
programShader->attachShader(vertexShader);
// assign fragment shader to program shader
programShader->attachShader(fragmentShader);
// assign program shader to object
object->setShaderProgram(programShader);
// link program shader
programShader->linkProgram();
// set uniforms
programShader->setUniformi("uColorMap", 0);
//programShader->setUniformi("uColorMap2", 4);
programShader->setUniformi("uDepthMap", 4);
//programShader->setUniformi("uShadowMap", 0);
programShader->setUniformi("uNormalMap", 2);
programShader->setUniformf("uInvRadius", 0.0f);
//--------------------------------------------------------------------------
// CREATE SPHERES
//--------------------------------------------------------------------------
// create a virtual mesh
spheres = new cMesh();
// add object to world
world->addChild(spheres);
spheres->setLocalPos(tool->getDeviceGlobalPos());
cCreateSphere(spheres, toolRadius*1.01);
//cCreateBox(spheres, toolRadius*2, toolRadius*2, toolRadius*2);
// create texture
cTexture2dPtr texture3 = cTexture2d::create();
// load texture file
fileload = texture3->loadFromFile(RESOURCE_PATH("../resources/images/spheremap-3.jpg"));
if (!fileload)
{
#if defined(_MSVC)
fileload = texture3->loadFromFile("../../../bin/resources/images/spheremap-3.jpg");
#endif
}
if (!fileload)
{
cout << "Error - Texture image failed to load correctly." << endl;
close();
return (-1);
}
// apply texture to object
spheres->setTexture(texture3);
// enable texture rendering
spheres->setUseTexture(true);
// Since we don't need to see our polygons from both sides, we enable culling.
spheres->setUseCulling(true);
// set material properties to light gray
spheres->m_material->setWhite();
// set material shininess
spheres->m_material->setShininess(80);
// compute collision detection algorithm
//spheres->createAABBCollisionDetector(toolRadius);
// define a default stiffness for the object
//object->m_material->setStiffness(0.5 * maxStiffness);
spheres->m_material->setStiffness(0.8 * maxStiffness);
// define some static friction
spheres->m_material->setStaticFriction(0.0); //0.2
// define some dynamic friction
spheres->m_material->setDynamicFriction(0.0); //0.2
// define some texture rendering
spheres->m_material->setTextureLevel(1.0);
// compute tangent vectors
spheres->computeBTN();
// create fragment shader
cShaderPtr fragmentShader2 = cShader::create(C_FRAGMENT_SHADER);
cShaderPtr vertexShader2 = cShader::create(C_VERTEX_SHADER);
fileload = vertexShader2->loadSourceFile(RESOURCE_PATH("../resources/shaders/phong.vert"));
if (!fileload)
{
#if defined(_MSVC)
fileload = vertexShader2->loadSourceFile("../../../bin/resources/shaders/phong.vert");
#endif
}
fileload = fragmentShader2->loadSourceFile(RESOURCE_PATH("../resources/shaders/phong.frag"));
if (!fileload){
#if defined(_MSVC)
fileload = fragmentShader2->loadSourceFile("../../../bin/resources/shaders/phong.frag");
#endif
}
// create program shader
cShaderProgramPtr programShader2 = cShaderProgram::create();
// assign vertex shader to program shader
programShader2->attachShader(vertexShader2);
// assign fragment shader to program shader
programShader2->attachShader(fragmentShader2);
spheres->setShaderProgram(programShader2);
// link program shader
programShader2->linkProgram();
// set uniforms
//programShader2->setUniformi("uColorMap", 0);
//programShader->setUniformi("uColorMap2", 4);
//programShader2->setUniformi("uDepthMap", 4);
programShader->setUniformi("uShadowMap", 0);
//programShader2->setUniformi("uNormalMap", 2);
//programShader2->setUniformf("uInvRadius", 0.0f);
//tool->setShaderProgram(programShader2);
// link program shader
//--------------------------------------------------------------------------
// FRAMEBUFFERS
//--------------------------------------------------------------------------
// create framebuffer for view 1
frameBuffer1 = cFrameBuffer::create();
frameBuffer1->setup(cameraView1);
// create framebuffer for view 2
frameBuffer2 = cFrameBuffer::create();
frameBuffer2->setup(cameraView2);
//--------------------------------------------------------------------------
// VIEW PANELS
//--------------------------------------------------------------------------
// create and setup view panel 1
viewPanel1 = new cViewPanel(frameBuffer1);
camera->m_frontLayer->addChild(viewPanel1);
// create and setup view panel 2
viewPanel2 = new cViewPanel(frameBuffer2);
camera->m_frontLayer->addChild(viewPanel2);
//--------------------------------------------------------------------------
// WIDGETS
//--------------------------------------------------------------------------
// create a font
font = NEW_CFONTCALIBRI20();
// create a label to display the haptic and graphic rate of the simulation
labelRates = new cLabel(font);
cameraView1->m_frontLayer->addChild(labelRates);
// set font color
labelRates->m_fontColor.setWhite();
// create a background
background = new cBackground();
cameraView1->m_backLayer->addChild(background);
// set background properties
background->setCornerColors(cColorf(0.3, 0.3, 0.3),
cColorf(0.3, 0.3, 0.3),
cColorf(0.1, 0.1, 0.1),
cColorf(0.1, 0.1, 0.1));
//-----------------------------------------
// create a label to display the haptic and graphic rate of the simulation
labelRates2 = new cLabel(font);
cameraView2->m_frontLayer->addChild(labelRates2);
// set font color
labelRates2->m_fontColor.setWhite();
// create a background
background2 = new cBackground();
cameraView2->m_backLayer->addChild(background2);
// set background properties
background2->setCornerColors(cColorf(0.3, 0.3, 0.3),
cColorf(0.3, 0.3, 0.3),
cColorf(0.1, 0.1, 0.1),
cColorf(0.1, 0.1, 0.1));
//--------------------------------------------------------------------------
// 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);
//object->setWireMode(true);
//object->setShowNormals(true);
//object->setShowTangents(true);
//object->setNormalsProperties(0.5, cColorf(0.1, 0.8, 0.2));
heightScale = 0.0;
//object->heighC = 0.3125 * heightScale + 0.01;
object->heighC = 0.45977 * heightScale + 0.01;
//--------------------------------------------------------------------------
// 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();
programShader->setUniformf("heightScale", heightScale);
//programShader2->setUniformf("heightScale", heightScale);
//print scale relieve
//cout << heightScale <<", "<< object->heighC << endl;
// 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;
int halfW = width / 2;
int halfH = height;
int offset = 1;
// update display panel sizes and positions
viewPanel1->setLocalPos(0.0, 0.0);
viewPanel1->setSize(halfW, halfH);
viewPanel2->setLocalPos(halfW, 0.0);
viewPanel2->setSize(halfW, halfH);
// update frame buffer sizes
frameBuffer1->setSize(halfW, halfH);
frameBuffer2->setSize(halfW, halfH);
}
//------------------------------------------------------------------------------
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;