This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
forked from KeckCAVES/SARndbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SandboxClient.cpp
1063 lines (924 loc) · 43.7 KB
/
SandboxClient.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
/***********************************************************************
SandboxClient - Vrui application connect to a remote AR Sandbox and
render its bathymetry and water level.
Copyright (c) 2019-2021 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Augmented Reality Sandbox is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "SandboxClient.h"
#include <string>
#include <stdexcept>
#include <iostream>
#include <Misc/SizedTypes.h>
#include <Misc/PrintInteger.h>
#include <Misc/FunctionCalls.h>
#include <Comm/TCPPipe.h>
#include <Math/Math.h>
#include <Geometry/LinearUnit.h>
#include <GL/gl.h>
#include <GL/GLMaterialTemplates.h>
#include <GL/GLLightTracker.h>
#include <GL/GLContextData.h>
#include <GL/Extensions/GLARBMultitexture.h>
#include <GL/Extensions/GLARBTextureRectangle.h>
#include <GL/Extensions/GLARBTextureFloat.h>
#include <GL/Extensions/GLARBTextureRg.h>
#include <GL/Extensions/GLARBVertexBufferObject.h>
#include <GL/Extensions/GLARBVertexShader.h>
#include <GL/Extensions/GLARBFragmentShader.h>
#include <GL/GLModels.h>
#include <GL/GLGeometryWrappers.h>
#include <Vrui/Viewer.h>
#include <Vrui/CoordinateManager.h>
#include <Vrui/Lightsource.h>
#include <Vrui/LightsourceManager.h>
#include <Vrui/ToolManager.h>
/****************************************************
Static eleemnts of class SandboxClient::TeleportTool:
****************************************************/
SandboxClient::TeleportToolFactory* SandboxClient::TeleportTool::factory = 0;
/********************************************
Methods of class SandboxClient::TeleportTool:
********************************************/
void SandboxClient::TeleportTool::applyNavState(void) const {
/* Compose and apply the navigation transformation: */
Vrui::NavTransform nav = physicalFrame;
nav *= Vrui::NavTransform::rotate(Vrui::Rotation::rotateZ(azimuth));
nav *= Geometry::invert(surfaceFrame);
Vrui::setNavigationTransformation(nav);
}
void SandboxClient::TeleportTool::initNavState(void) {
/* Calculate the main viewer's current head and foot positions: */
Point headPos = Vrui::getMainViewer()->getHeadPosition();
footPos = Vrui::calcFloorPoint(headPos);
headHeight = Geometry::dist(headPos, footPos);
/* Set up a physical navigation frame around the main viewer's current head position: */
calcPhysicalFrame(headPos);
/* Calculate the initial environment-aligned surface frame in navigation coordinates: */
surfaceFrame = Vrui::getInverseNavigationTransformation() * physicalFrame;
Vrui::NavTransform newSurfaceFrame = surfaceFrame;
/* Align the initial frame with the application's surface and calculate Euler angles: */
AlignmentData ad(surfaceFrame, newSurfaceFrame, Vrui::getMeterFactor()*Scalar(0.25),
Vrui::getMeterFactor());
Scalar elevation, roll;
align(ad, azimuth, elevation, roll);
/* Move the physical frame to the foot position, and adjust the surface frame accordingly: */
newSurfaceFrame *= Geometry::invert(physicalFrame) * Vrui::NavTransform::translate(
footPos - headPos) * physicalFrame;
physicalFrame.leftMultiply(Vrui::NavTransform::translate(footPos - headPos));
/* Apply the initial navigation state: */
surfaceFrame = newSurfaceFrame;
applyNavState();
}
void SandboxClient::TeleportTool::initClass(void) {
/* Create a factory object for the teleporting tool class: */
factory = new TeleportToolFactory("TeleportTool", "Teleport",
Vrui::getToolManager()->loadClass("SurfaceNavigationTool"), *Vrui::getToolManager());
/* Set the teleport tool class' input layout: */
factory->setNumButtons(2);
factory->setButtonFunction(0, "Toggle");
factory->setButtonFunction(1, "Teleport");
/* Register the teleport tool class with Vrui's tool manager: */
Vrui::getToolManager()->addClass(factory, Vrui::ToolManager::defaultToolFactoryDestructor);
}
SandboxClient::TeleportTool::TeleportTool(const Vrui::ToolFactory* factory,
const Vrui::ToolInputAssignment& inputAssignment)
: Vrui::SurfaceNavigationTool(factory, inputAssignment),
cast(false) {
sphereRenderer.setVariableRadius();
cylinderRenderer.setVariableRadius();
}
SandboxClient::TeleportTool::~TeleportTool(void) {
}
const Vrui::ToolFactory* SandboxClient::TeleportTool::getFactory(void) const {
return factory;
}
void SandboxClient::TeleportTool::buttonCallback(int buttonSlotIndex,
Vrui::InputDevice::ButtonCallbackData* cbData) {
switch (buttonSlotIndex) {
case 0:
if (cbData->newButtonState) { // Button has just been pressed
/* Act depending on this tool's current state: */
if (isActive()) {
if (!cast) {
/* Deactivate this tool: */
deactivate();
}
} else {
/* Try activating this tool: */
if (activate()) {
/* Initialize the navigation state: */
initNavState();
}
}
}
break;
case 1:
if (isActive()) {
if (cbData->newButtonState) {
cast = true;
} else {
/* Teleport to the end of the cast arc: */
surfaceFrame.leftMultiply(Vrui::NavTransform::translate(castArc.back() -
surfaceFrame.getOrigin()));
cast = false;
}
}
break;
}
}
void SandboxClient::TeleportTool::frame(void) {
if (isActive()) {
/* Calculate the new head and foot positions: */
Point newHead = Vrui::getMainViewer()->getHeadPosition();
Point newFootPos = Vrui::calcFloorPoint(newHead);
headHeight = Geometry::dist(newHead, newFootPos);
/* Create a physical navigation frame around the new foot position: */
calcPhysicalFrame(newFootPos);
/* Calculate the movement from walking: */
Vector move = newFootPos - footPos;
footPos = newFootPos;
/* Transform the movement vector from physical space to the physical navigation frame: */
move = physicalFrame.inverseTransform(move);
/* Rotate by the current azimuth angle: */
move = Vrui::Rotation::rotateZ(-azimuth).transform(move);
/* Move the surface frame: */
Vrui::NavTransform newSurfaceFrame = surfaceFrame;
newSurfaceFrame *= Vrui::NavTransform::translate(move);
/* Re-align the surface frame with the surface: */
AlignmentData ad(surfaceFrame, newSurfaceFrame, Vrui::getMeterFactor()*Scalar(0.25),
Vrui::getMeterFactor());
align(ad);
/* Apply the newly aligned surface frame: */
surfaceFrame = newSurfaceFrame;
applyNavState();
if (cast) {
/* Cast an arc from the current input device position: */
castArc.clear();
Point cp = Vrui::getInverseNavigationTransformation().transform(getButtonDevicePosition(1));
Vector cv = Vrui::getInverseNavigationTransformation().transform(getButtonDeviceRayDirection(1) *
(Vrui::getMeterFactor() * Scalar(15)));
Vector ca(0, 0, -Vrui::getInverseNavigationTransformation().getScaling()*Vrui::getMeterFactor()
*Scalar(9.81));
for (int i = 0; i < 100; ++i) {
castArc.push_back(cp);
Point cpn = cp + cv * Scalar(0.05);
cv += ca * Scalar(0.05);
Scalar lambda = application->intersectLine(cp, cpn);
if (lambda < Scalar(1)) {
castArc.push_back(Geometry::affineCombination(cp, cpn, lambda));
break;
}
cp = cpn;
}
}
}
}
void SandboxClient::TeleportTool::display(GLContextData& contextData) const {
if (isActive() && cast) {
/* Draw the cast arc: */
Vrui::goToNavigationalSpace(contextData);
Scalar radius = Vrui::getInchFactor() * Scalar(1) *
Vrui::getInverseNavigationTransformation().getScaling();
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(0.0f, 1.0f, 0.0f));
glMaterialSpecular(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(0.333f, 0.333f, 0.333f));
glMaterialShininess(GLMaterialEnums::FRONT, 32.0f);
glMaterialEmission(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(1.0f, 0.0f, 0.0f));
sphereRenderer.enable(Vrui::getNavigationTransformation().getScaling(), contextData);
glBegin(GL_POINTS);
for (std::vector<Point>::const_iterator caIt = castArc.begin(); caIt != castArc.end(); ++caIt) {
glVertex4f((*caIt)[0], (*caIt)[1], (*caIt)[2], radius);
}
glVertex4f(castArc.back()[0], castArc.back()[1], castArc.back()[2],
Vrui::getMeterFactor()*Scalar(0.125)*Vrui::getInverseNavigationTransformation().getScaling());
glEnd();
sphereRenderer.disable(contextData);
cylinderRenderer.enable(Vrui::getNavigationTransformation().getScaling(), contextData);
glBegin(GL_LINE_STRIP);
for (std::vector<Point>::const_iterator caIt = castArc.begin(); caIt != castArc.end(); ++caIt) {
glVertex4f((*caIt)[0], (*caIt)[1], (*caIt)[2], radius);
}
glEnd();
cylinderRenderer.disable(contextData);
glPopMatrix();
}
}
/****************************************
Methods of class SandboxClient::DataItem:
****************************************/
SandboxClient::DataItem::DataItem(void)
: bathymetryTexture(0), waterTexture(0), textureVersion(0),
bathymetryVertexBuffer(0), bathymetryIndexBuffer(0),
waterVertexBuffer(0), waterIndexBuffer(0),
bathymetryVertexShader(0), bathymetryFragmentShader(0), bathymetryShaderProgram(0),
waterVertexShader(0), waterFragmentShader(0), waterShaderProgram(0) {
/* Initialize required OpenGL extensions: */
GLARBMultitexture::initExtension();
GLARBTextureRectangle::initExtension();
GLARBTextureFloat::initExtension();
GLARBTextureRg::initExtension();
GLARBVertexBufferObject::initExtension();
GLARBShaderObjects::initExtension();
GLARBVertexShader::initExtension();
GLARBFragmentShader::initExtension();
/* Create texture objects: */
glGenTextures(1, &bathymetryTexture);
glGenTextures(1, &waterTexture);
/* Create buffer objects: */
glGenBuffersARB(1, &bathymetryVertexBuffer);
glGenBuffersARB(1, &bathymetryIndexBuffer);
glGenBuffersARB(1, &waterVertexBuffer);
glGenBuffersARB(1, &waterIndexBuffer);
/* Create shader objects: */
bathymetryVertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
bathymetryFragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
bathymetryShaderProgram = glCreateProgramObjectARB();
waterVertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
waterFragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
waterShaderProgram = glCreateProgramObjectARB();
/* Attach shader objects to the shader program: */
glAttachObjectARB(bathymetryShaderProgram, bathymetryVertexShader);
glAttachObjectARB(bathymetryShaderProgram, bathymetryFragmentShader);
glAttachObjectARB(waterShaderProgram, waterVertexShader);
glAttachObjectARB(waterShaderProgram, waterFragmentShader);
}
SandboxClient::DataItem::~DataItem(void) {
/* Destroy objects: */
glDeleteTextures(1, &bathymetryTexture);
glDeleteTextures(1, &waterTexture);
glDeleteBuffersARB(1, &bathymetryVertexBuffer);
glDeleteBuffersARB(1, &bathymetryIndexBuffer);
glDeleteBuffersARB(1, &waterVertexBuffer);
glDeleteBuffersARB(1, &waterIndexBuffer);
glDeleteObjectARB(bathymetryVertexShader);
glDeleteObjectARB(bathymetryFragmentShader);
glDeleteObjectARB(bathymetryShaderProgram);
glDeleteObjectARB(waterVertexShader);
glDeleteObjectARB(waterFragmentShader);
glDeleteObjectARB(waterShaderProgram);
}
/******************************
Methods of class SandboxClient:
******************************/
void SandboxClient::readGrids(void) {
/* Start a new set of grids: */
GridBuffers& gb = grids.startNewValue();
/* Calculate elevation quantization factors: */
GLfloat eScale = (elevationRange[1] - elevationRange[0]) / 65535.0f;
GLfloat eOffset = elevationRange[0];
/* Receive the bathymetry grid: */
GLfloat* bPtr = gb.bathymetry;
for (GLsizei y = 0; y < gridSize[1] - 1; ++y)
for (GLsizei x = 0; x < gridSize[0] - 1; ++x, ++bPtr) {
*bPtr = GLfloat(pipe->read<Misc::UInt16>()) * eScale + eOffset;
}
/* Receive the water level grid: */
GLfloat* wlPtr = gb.waterLevel;
for (GLsizei y = 0; y < gridSize[1]; ++y)
for (GLsizei x = 0; x < gridSize[0]; ++x, ++wlPtr) {
*wlPtr = GLfloat(pipe->read<Misc::UInt16>()) * eScale + eOffset;
}
/* Post the new set of grids: */
grids.postNewValue();
}
SandboxClient::Scalar SandboxClient::intersectLine(const SandboxClient::Point& p0,
const SandboxClient::Point& p1) const {
/* Convert the points to grid coordinates: */
Point gp0(p0[0] / Scalar(cellSize[0]) - Scalar(0.5), p0[1] / Scalar(cellSize[1]) - Scalar(0.5),
p0[2]);
Point gp1(p1[0] / Scalar(cellSize[0]) - Scalar(0.5), p1[1] / Scalar(cellSize[1]) - Scalar(0.5),
p1[2]);
Vector gd = gp1 - gp0;
/* Clip the line segment against the grid's boundaries: */
Scalar l0(0);
Scalar l1(1);
for (int i = 0; i < 2; ++i) {
/* Clip against the lower boundary: */
Scalar b(0);
if (gp0[i] < b) {
if (gp1[i] > b) {
l0 = Math::max(l0, (b - gp0[i]) / gd[i]);
} else {
return Scalar(1);
}
} else if (gp1[i] < b) {
if (gp0[i] > b) {
l1 = Math::min(l1, (b - gp0[i]) / gd[i]);
} else {
return Scalar(1);
}
}
/* Clip against the upper boundary: */
b = Scalar(gridSize[i] - 2);
if (gp0[i] > b) {
if (gp1[i] < b) {
l0 = Math::max(l0, (b - gp0[i]) / gd[i]);
} else {
return Scalar(1);
}
} else if (gp1[i] > b) {
if (gp0[i] < b) {
l1 = Math::min(l1, (b - gp0[i]) / gd[i]);
} else {
return Scalar(1);
}
}
}
if (l0 >= l1) {
return Scalar(1);
}
/* Find the grid cell containing the first point: */
Point gp = Geometry::affineCombination(gp0, gp1, l0);
GLsizei cp[2];
for (int i = 0; i < 2; ++i) {
cp[i] = Math::clamp(GLsizei(Math::floor(gp[i])), GLsizei(0), gridSize[i] - 3);
}
Scalar cl0 = l0;
while (cl0 < l1) {
/* Calculate the line parameter where the line segment leaves the current cell: */
Scalar cl1 = l1;
int exit = -1;
for (int i = 0; i < 2; ++i) {
Scalar el = cl1;
if (gp0[i] < gp1[i]) {
el = (Scalar(cp[i] + 1) - gp0[i]) / gd[i];
} else if (gp0[i] > gp1[i]) {
el = (Scalar(cp[i]) - gp0[i]) / gd[i];
}
if (cl1 > el) {
cl1 = el;
exit = i;
}
}
/* Intersect the line segment with the surface inside the current cell: */
const GLfloat* cell = grids.getLockedValue().bathymetry + (cp[1] * (gridSize[0] - 1) + cp[0]);
Scalar c0 = cell[0];
Scalar c1 = cell[1];
Scalar c2 = cell[gridSize[0] - 1];
Scalar c3 = cell[gridSize[0]];
Scalar cx0 = Scalar(cp[0]);
Scalar cx1 = Scalar(cp[0] + 1);
Scalar cy0 = Scalar(cp[1]);
Scalar cy1 = Scalar(cp[1] + 1);
Scalar fxy = c0 - c1 + c3 - c2;
Scalar fx = (c1 - c0) * cy1 - (c3 - c2) * cy0;
Scalar fy = (c2 - c0) * cx1 - (c3 - c1) * cx0;
Scalar f = (c0 * cx1 - c1 * cx0) * cy1 - (c2 * cx1 - c3 * cx0) * cy0;
Scalar a = fxy * gd[0] * gd[1];
Scalar bc0 = (fxy * gp0[1] + fx);
Scalar bc1 = (fxy * gp0[0] + fy);
Scalar b = bc0 * gd[0] + bc1 * gd[1] - gd[2];
Scalar c = bc0 * gp0[0] + bc1 * gp0[1] - gp0[2] - fxy * gp0[0] * gp0[1] + f;
Scalar il = cl1;
if (a != Scalar(0)) {
/* Solve the quadratic equation and use the smaller valid solution: */
Scalar det = b * b - Scalar(4) * a * c;
if (det >= Scalar(0)) {
det = Math::sqrt(det);
if (a > Scalar(0)) {
/* Test the smaller intersection first: */
il = b >= Scalar(0) ? (-b - det) / (Scalar(2) * a) : (Scalar(2) * c) / (-b + det);
if (il < cl0) {
il = b >= Scalar(0) ? (Scalar(2) * c) / (-b - det) : (-b + det) / (Scalar(2) * a);
}
} else {
/* Test the smaller intersection first: */
il = b >= Scalar(0) ? (Scalar(2) * c) / (-b - det) : (-b + det) / (Scalar(2) * a);
if (il < cl0) {
il = b >= Scalar(0) ? (-b - det) / (Scalar(2) * a) : (Scalar(2) * c) / (-b + det);
}
}
}
} else {
/* Solve the linear equation: */
il = -c / b;
}
/* Check if the intersection is valid: */
if (il >= cl0 && il < cl1) {
return il;
}
/* Go to the next cell: */
if (exit >= 0) {
if (gd[exit] < Scalar(0)) {
--cp[exit];
} else {
++cp[exit];
}
}
cl0 = cl1;
}
return Scalar(1);
}
bool SandboxClient::serverMessageCallback(Threads::EventDispatcher::ListenerKey eventKey,
int eventType, void* userData) {
SandboxClient* thisPtr = static_cast<SandboxClient*>(userData);
try {
/* Read a new set of grids: */
thisPtr->readGrids();
/* Wake up the main thread: */
Vrui::requestUpdate();
} catch (const std::runtime_error& err) {
}
return false;
}
void* SandboxClient::communicationThreadMethod(void) {
/* Wait for messages from the remote AR Sandbox until interrupted: */
while (dispatcher.dispatchNextEvent()) {
}
return 0;
}
void SandboxClient::alignSurfaceFrame(Vrui::SurfaceNavigationTool::AlignmentData& alignmentData) {
/* Get the frame's base point: */
Point base = alignmentData.surfaceFrame.getOrigin();
/* Snap the base point to the terrain: */
GLfloat* bathymetry = grids.getLockedValue().bathymetry;
Scalar dx = base[0] / Scalar(cellSize[0]) - Scalar(0.5);
GLsizei gx = Math::clamp(GLsizei(Math::floor(dx)), GLsizei(0), gridSize[0] - GLsizei(3));
dx -= gx;
Scalar dy = base[1] / Scalar(cellSize[1]) - Scalar(0.5);
GLsizei gy = Math::clamp(GLsizei(Math::floor(dy)), GLsizei(0), gridSize[1] - GLsizei(3));
dy -= gy;
GLfloat* cell = bathymetry + (gy * (gridSize[0] - 1) + gx);
Scalar b0 = cell[0] * (Scalar(1) - dx) + cell[1] * dx;
cell += gridSize[0] - 1;
Scalar b1 = cell[0] * (Scalar(1) - dx) + cell[1] * dx;
base[2] = b0 * (Scalar(1) - dy) + b1 * dy;
/* Align the frame with the bathymetry surface's x and y directions: */
alignmentData.surfaceFrame = Vrui::NavTransform(base - Point::origin, Vrui::Rotation::identity,
alignmentData.surfaceFrame.getScaling());
}
void SandboxClient::compileShaders(SandboxClient::DataItem* dataItem,
const GLLightTracker& lightTracker) const {
/* Create the bathymetry vertex shader source code: */
std::string bathymetryVertexShaderDefines = "\
#extension GL_ARB_texture_rectangle : enable\n";
std::string bathymetryVertexShaderFunctions;
std::string bathymetryVertexShaderUniforms = "\
uniform sampler2DRect bathymetrySampler; // Sampler for the bathymetry texture\n\
uniform vec2 bathymetryCellSize; // Cell size of the bathymetry grid\n";
std::string bathymetryVertexShaderVaryings = "\
varying float dist; // Eye-space distance to vertex for fogging\n";
std::string bathymetryVertexShaderMain = "\
void main()\n\
{\n\
/* Get the vertex's grid-space z coordinate from the bathymetry texture: */\n\
vec4 vertexGc=gl_Vertex;\n\
vertexGc.z=texture2DRect(bathymetrySampler,vertexGc.xy).r;\n\
\n\
/* Calculate the vertex's grid-space normal vector: */\n\
vec3 normalGc;\n\
normalGc.x=(texture2DRect(bathymetrySampler,vec2(vertexGc.x-1.0,vertexGc.y)).r-texture2DRect(bathymetrySampler,vec2(vertexGc.x+1.0,vertexGc.y)).r)*bathymetryCellSize.y;\n\
normalGc.y=(texture2DRect(bathymetrySampler,vec2(vertexGc.x,vertexGc.y-1.0)).r-texture2DRect(bathymetrySampler,vec2(vertexGc.x,vertexGc.y+1.0)).r)*bathymetryCellSize.x;\n\
normalGc.z=2.0*bathymetryCellSize.x*bathymetryCellSize.y;\n\
\n\
/* Transform the vertex and its normal vector from grid space to eye space for illumination: */\n\
vertexGc.x*=bathymetryCellSize.x;\n\
vertexGc.y*=bathymetryCellSize.y;\n\
vec4 vertexEc=gl_ModelViewMatrix*vertexGc;\n\
vec3 normalEc=normalize(gl_NormalMatrix*normalGc);\n\
\n\
/* Initialize the vertex color accumulators: */\n\
vec4 ambDiff=gl_LightModel.ambient*gl_FrontMaterial.ambient;\n\
vec4 spec=vec4(0.0,0.0,0.0,0.0);\n\
\n\
/* Accumulate all enabled light sources: */\n";
/* Create light application functions for all enabled light sources: */
for (int lightIndex = 0; lightIndex < lightTracker.getMaxNumLights(); ++lightIndex)
if (lightTracker.getLightState(lightIndex).isEnabled()) {
/* Create the light accumulation function: */
bathymetryVertexShaderFunctions += lightTracker.createAccumulateLightFunction(lightIndex);
/* Call the light application function from the bathymetry vertex shader's main function: */
bathymetryVertexShaderMain += "\
accumulateLight";
char liBuffer[12];
bathymetryVertexShaderMain.append(Misc::print(lightIndex, liBuffer + 11));
bathymetryVertexShaderMain +=
"(vertexEc,normalEc,gl_FrontMaterial.ambient,gl_FrontMaterial.diffuse,gl_FrontMaterial.specular,gl_FrontMaterial.shininess,ambDiff,spec);\n";
}
/* Finalize the bathymetry vertex shader's main function: */
bathymetryVertexShaderMain += "\
dist=length(vertexEc.xyz);\n\
gl_FrontColor=ambDiff+spec;\n\
gl_Position=gl_ModelViewProjectionMatrix*vertexGc;\n\
}\n";
/* Compile the bathymetry vertex shader: */
glCompileShaderFromStrings(dataItem->bathymetryVertexShader, 5,
bathymetryVertexShaderDefines.c_str(), bathymetryVertexShaderFunctions.c_str(),
bathymetryVertexShaderUniforms.c_str(), bathymetryVertexShaderVaryings.c_str(),
bathymetryVertexShaderMain.c_str());
/* Create the bathymetry fragment shader source code: */
std::string bathymetryFragmentShaderMain = "\
uniform vec4 waterColor; // Color of water surface for fogging\n\
uniform float waterOpacity; // Opacity of water for fogging\n\
\n\
varying float dist; // Eye-space distance to vertex for fogging\n\
\n\
void main()\n\
{\n\
gl_FragColor=mix(waterColor,gl_Color,exp(-dist*waterOpacity));\n\
}\n";
/* Compile the bathymetry fragment shader: */
glCompileShaderFromString(dataItem->bathymetryFragmentShader,
bathymetryFragmentShaderMain.c_str());
/* Link the bathymetry shader program: */
glLinkAndTestShader(dataItem->bathymetryShaderProgram);
/* Retrieve the bathymetry shader program's uniform variable locations: */
dataItem->bathymetryShaderUniforms[0] = glGetUniformLocationARB(dataItem->bathymetryShaderProgram,
"bathymetrySampler");
dataItem->bathymetryShaderUniforms[1] = glGetUniformLocationARB(dataItem->bathymetryShaderProgram,
"bathymetryCellSize");
dataItem->bathymetryShaderUniforms[2] = glGetUniformLocationARB(dataItem->bathymetryShaderProgram,
"waterColor");
dataItem->bathymetryShaderUniforms[3] = glGetUniformLocationARB(dataItem->bathymetryShaderProgram,
"waterOpacity");
/* Create the water surface vertex shader source code: */
std::string waterVertexShaderDefines = "\
#extension GL_ARB_texture_rectangle : enable\n";
std::string waterVertexShaderFunctions;
std::string waterVertexShaderUniforms = "\
uniform sampler2DRect bathymetrySampler; // Sampler for the bathymetry texture\n\
uniform sampler2DRect waterSampler; // Sampler for the water surface texture\n\
uniform vec2 waterCellSize; // Cell size of the water surface grid\n";
std::string waterVertexShaderMain = "\
void main()\n\
{\n\
/* Get the vertex's grid-space z coordinate from the water surface texture: */\n\
vec4 vertexGc=gl_Vertex;\n\
vertexGc.z=texture2DRect(waterSampler,vertexGc.xy).r;\n\
\n\
/* Get the bathymetry elevation at the same location: */\n\
float bathy=(texture2DRect(bathymetrySampler,vertexGc.xy-vec2(1.0,1.0)).r\n\
+texture2DRect(bathymetrySampler,vertexGc.xy-vec2(1.0,0.0)).r\n\
+texture2DRect(bathymetrySampler,vertexGc.xy-vec2(0.0,1.0)).r\n\
+texture2DRect(bathymetrySampler,vertexGc.xy-vec2(0.0,0.0)).r)*0.25;\n\
\n\
/* Calculate the vertex's grid-space normal vector: */\n\
vec3 normalGc;\n\
normalGc.x=(texture2DRect(waterSampler,vec2(vertexGc.x-1.0,vertexGc.y)).r-texture2DRect(waterSampler,vec2(vertexGc.x+1.0,vertexGc.y)).r)*waterCellSize.y;\n\
normalGc.y=(texture2DRect(waterSampler,vec2(vertexGc.x,vertexGc.y-1.0)).r-texture2DRect(waterSampler,vec2(vertexGc.x,vertexGc.y+1.0)).r)*waterCellSize.x;\n\
normalGc.z=1.0*waterCellSize.x*waterCellSize.y;\n\
\n\
/* Transform the vertex and its normal vector from grid space to eye space for illumination: */\n\
vertexGc.x=(vertexGc.x-0.5)*waterCellSize.x;\n\
vertexGc.y=(vertexGc.y-0.5)*waterCellSize.y;\n\
vec4 vertexEc=gl_ModelViewMatrix*vertexGc;\n\
vec3 normalEc=normalize(gl_NormalMatrix*normalGc);\n\
\n\
/* Initialize the vertex color accumulators: */\n\
vec4 ambDiff=gl_LightModel.ambient*gl_FrontMaterial.ambient;\n\
vec4 spec=vec4(0.0,0.0,0.0,0.0);\n\
\n\
/* Accumulate all enabled light sources: */\n";
/* Create light application functions for all enabled light sources: */
for (int lightIndex = 0; lightIndex < lightTracker.getMaxNumLights(); ++lightIndex)
if (lightTracker.getLightState(lightIndex).isEnabled()) {
/* Create the light accumulation function: */
waterVertexShaderFunctions += lightTracker.createAccumulateLightFunction(lightIndex);
/* Call the light application function from the bathymetry vertex shader's main function: */
waterVertexShaderMain += "\
accumulateLight";
char liBuffer[12];
waterVertexShaderMain.append(Misc::print(lightIndex, liBuffer + 11));
waterVertexShaderMain +=
"(vertexEc,normalEc,gl_FrontMaterial.ambient,gl_FrontMaterial.diffuse,gl_FrontMaterial.specular,gl_FrontMaterial.shininess,ambDiff,spec);\n";
}
/* Finalize the water vertex shader's main function: */
waterVertexShaderMain += "\
gl_FrontColor=vec4(ambDiff.xyz+spec.xyz,(vertexGc.z-bathy)*2.0);\n\
gl_BackColor=gl_FrontColor;\n\
gl_Position=gl_ModelViewProjectionMatrix*vertexGc;\n\
}\n";
/* Compile the water vertex shader: */
glCompileShaderFromStrings(dataItem->waterVertexShader, 4, waterVertexShaderDefines.c_str(),
waterVertexShaderFunctions.c_str(), waterVertexShaderUniforms.c_str(),
waterVertexShaderMain.c_str());
/* Create the water fragment shader source code: */
std::string waterFragmentShaderMain = "\
void main()\n\
{\n\
//if(gl_Color.a<0.005)\n\
// discard;\n\
gl_FragColor=gl_Color;\n\
}\n";
/* Compile the water fragment shader: */
glCompileShaderFromString(dataItem->waterFragmentShader, waterFragmentShaderMain.c_str());
/* Link the water shader program: */
glLinkAndTestShader(dataItem->waterShaderProgram);
/* Retrieve the water shader program's uniform variable locations: */
dataItem->waterShaderUniforms[0] = glGetUniformLocationARB(dataItem->waterShaderProgram,
"bathymetrySampler");
dataItem->waterShaderUniforms[1] = glGetUniformLocationARB(dataItem->waterShaderProgram,
"waterSampler");
dataItem->waterShaderUniforms[2] = glGetUniformLocationARB(dataItem->waterShaderProgram,
"waterCellSize");
/* Mark the bathymetry shader as up-to-date: */
dataItem->lightStateVersion = lightTracker.getVersion();
}
SandboxClient::SandboxClient(int& argc, char**& argv)
: Vrui::Application(argc, argv),
pipe(0),
gridVersion(0),
sun(0), underwater(false) {
/* Parse the command line: */
const char* serverName = 0;
int serverPortId = 26000;
for (int argi = 1; argi < argc; ++argi) {
if (argv[argi][0] == '-') {
std::cerr << "SandboxClient: Ignoring command line option " << argv[argi] << std::endl;
} else if (serverName == 0) {
serverName = argv[argi];
} else {
std::cerr << "SandboxClient: Ignoring command line argument " << argv[argi] << std::endl;
}
}
if (serverName == 0) {
throw std::runtime_error("SandboxClient: No server name provided");
}
/* Connect to the AR Sandbox server: */
pipe = new Comm::TCPPipe(serverName, serverPortId);
/* Send an endianness token to the server: */
pipe->write<Misc::UInt32>(0x12345678U);
pipe->flush();
/* Receive an endianness token from the server: */
Misc::UInt32 token = pipe->read<Misc::UInt32>();
if (token == 0x78563412U) {
pipe->setSwapOnRead(true);
} else if (token != 0x12345678U) {
delete pipe;
throw std::runtime_error("SandboxClient: Invalid response from remote AR Sandbox");
}
try {
/* Receive the remote AR Sandbox's water table grid size, cell size, and elevation range: */
for (int i = 0; i < 2; ++i) {
gridSize[i] = pipe->read<Misc::UInt32>();
cellSize[i] = pipe->read<Misc::Float32>();
}
for (int i = 0; i < 2; ++i) {
elevationRange[i] = pipe->read<Misc::Float32>();
}
/* Initialize the grid buffers: */
for (int i = 0; i < 3; ++i) {
grids.getBuffer(i).init(gridSize);
}
/* Read the initial set of grids: */
readGrids();
} catch (const std::runtime_error& err) {
/* Disconnect from the remote AR Sandbox: */
delete pipe;
/* Re-throw the exception: */
throw;
}
/* Start listening on the TCP pipe: */
dispatcher.addIOEventListener(pipe->getFd(), Threads::EventDispatcher::Read, serverMessageCallback,
this);
communicationThread.start(this, &SandboxClient::communicationThreadMethod);
/* Set the linear unit to scale the AR Sandbox 1:100: */
Vrui::getCoordinateManager()->setUnit(Geometry::LinearUnit(Geometry::LinearUnit::METER, 0.01));
/* Create a light source and disable all viewers' headlights: */
sun = Vrui::getLightsourceManager()->createLightsource(false);
sun->enable();
sun->getLight().position = GLLight::Position(-0.2, 0.3, 1.0, 0.0);
for (int i = 0; i < Vrui::getNumViewers(); ++i) {
Vrui::getViewer(i)->setHeadlightState(false);
}
/* Create tool classes: */
TeleportTool::initClass();
}
SandboxClient::~SandboxClient(void) {
/* Disconnect from the remote AR Sandbox: */
dispatcher.stop();
communicationThread.join();
delete pipe;
}
void SandboxClient::toolCreationCallback(Vrui::ToolManager::ToolCreationCallbackData* cbData) {
/* Check if the new tool is a surface navigation tool: */
Vrui::SurfaceNavigationTool* surfaceNavigationTool = dynamic_cast<Vrui::SurfaceNavigationTool*>
(cbData->tool);
if (surfaceNavigationTool != 0) {
/* Set the new tool's alignment function: */
surfaceNavigationTool->setAlignFunction(Misc::createFunctionCall(this,
&SandboxClient::alignSurfaceFrame));
}
/* Call the base class method: */
Vrui::Application::toolCreationCallback(cbData);
}
void SandboxClient::frame(void) {
/* Lock the most recent grid buffers: */
if (grids.lockNewValue()) {
++gridVersion;
}
/* Calculate the position of the main viewer's head in grid space: */
Point head = Vrui::getHeadPosition();
GLfloat* waterLevel = grids.getLockedValue().waterLevel;
Scalar dx = head[0] / Scalar(cellSize[0]);
GLsizei gx = GLsizei(Math::floor(dx));
dx -= gx;
Scalar dy = head[1] / Scalar(cellSize[1]);
GLsizei gy = GLsizei(Math::floor(dy));
dy -= gy;
if (gx >= 0 && gx < gridSize[0] - 1 && gy >= 0 && gy < gridSize[1] - 1) {
GLfloat* cell = waterLevel + (gy * gridSize[0] + gx);
Scalar b0 = cell[0] * (Scalar(1) - dx) + cell[1] * dx;
cell += gridSize[0];
Scalar b1 = cell[0] * (Scalar(1) - dx) + cell[1] * dx;
Scalar water = b0 * (Scalar(1) - dy) + b1 * dy;
underwater = head[2] <= water;
} else {
underwater = false;
}
/* Send the current head position to the remote AR Sandbox: */
Geometry::Point<Misc::Float32, 3> fhead(head);
pipe->write<Misc::UInt16>(0);
pipe->write(fhead.getComponents(), 3);
Geometry::Vector<Misc::Float32, 3> fview(Vrui::getViewDirection());
pipe->write(fview.getComponents(), 3);
pipe->flush();
}
void SandboxClient::display(GLContextData& contextData) const {
/* Retrieve the context data item: */
DataItem* dataItem = contextData.retrieveDataItem<DataItem>(this);
/* Set up OpenGL state: */
glPushAttrib(GL_ENABLE_BIT);
/* Update the shader programs if necessary: */
const GLLightTracker& lightTracker = *contextData.getLightTracker();
if (dataItem->lightStateVersion != lightTracker.getVersion()) {
compileShaders(dataItem, lightTracker);
}
/* Activate the bathymetry shader: */
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(0.6f, 0.4f, 0.1f));
glMaterialSpecular(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(1.0f, 1.0f, 1.0f));
glMaterialShininess(GLMaterialEnums::FRONT, 32.0f);
glUseProgramObjectARB(dataItem->bathymetryShaderProgram);
/* Render the locked bathymetry grid: */
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, dataItem->bathymetryTexture);
if (dataItem->textureVersion != gridVersion) {
/* Upload the new bathymetry grid: */
glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, gridSize[0] - 1, gridSize[1] - 1, GL_RED,
GL_FLOAT, grids.getLockedValue().bathymetry);
}
glUniform1iARB(dataItem->bathymetryShaderUniforms[0], 0);
/* Bind the vertex and index buffers: */
glBindBufferARB(GL_ARRAY_BUFFER_ARB, dataItem->bathymetryVertexBuffer);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, dataItem->bathymetryIndexBuffer);
glUniform2fARB(dataItem->bathymetryShaderUniforms[1], cellSize[0], cellSize[1]);
glUniform4fARB(dataItem->bathymetryShaderUniforms[2], 0.2f, 0.5f, 0.8f, 1.0f);
glUniform1fARB(dataItem->bathymetryShaderUniforms[3], underwater ? 0.1f : 0.0f);
/* Draw the bathymetry: */
{
GLVertexArrayParts::enable(Vertex::getPartsMask());
glVertexPointer(static_cast<const Vertex*>(0));
GLuint* indexPtr = 0;
for (GLsizei y = 1; y < gridSize[1] - 1; ++y, indexPtr += (gridSize[0] - 1) * 2) {
glDrawElements(GL_QUAD_STRIP, (gridSize[0] - 1) * 2, GL_UNSIGNED_INT, indexPtr);
}
GLVertexArrayParts::disable(Vertex::getPartsMask());
}
/* Activate the water surface shader: */
glMaterialAmbientAndDiffuse(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(0.2f, 0.5f, 0.8f));
glMaterialSpecular(GLMaterialEnums::FRONT, GLColor<GLfloat, 4>(1.0f, 1.0f, 1.0f));
glMaterialShininess(GLMaterialEnums::FRONT, 64.0f);
glUseProgramObjectARB(dataItem->waterShaderProgram);
/* Render the locked water surface grid: */
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, dataItem->waterTexture);
if (dataItem->textureVersion != gridVersion) {
/* Upload the new water surface grid: */
glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, gridSize[0], gridSize[1], GL_RED, GL_FLOAT,
grids.getLockedValue().waterLevel);
}
glUniform1iARB(dataItem->waterShaderUniforms[1], 1);
/* Bind the vertex and index buffers: */
glBindBufferARB(GL_ARRAY_BUFFER_ARB, dataItem->waterVertexBuffer);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, dataItem->waterIndexBuffer);
glUniform2fARB(dataItem->waterShaderUniforms[2], cellSize[0], cellSize[1]);
if (underwater) {
glCullFace(GL_FRONT);
} else {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
/* Draw the water surface: */
{
GLVertexArrayParts::enable(Vertex::getPartsMask());
glVertexPointer(static_cast<const Vertex*>(0));
GLuint* indexPtr = 0;
for (GLsizei y = 1; y < gridSize[1]; ++y, indexPtr += gridSize[0] * 2) {
glDrawElements(GL_QUAD_STRIP, gridSize[0] * 2, GL_UNSIGNED_INT, indexPtr);
}
GLVertexArrayParts::disable(Vertex::getPartsMask());
}
if (underwater) {
glCullFace(GL_BACK);
} else {
glDisable(GL_BLEND);
}
/* Protect the buffers and textures and deactivate the shaders: */
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
glUseProgramObjectARB(0);
/* Mark the textures as up-to-date: */
dataItem->textureVersion = gridVersion;
/* Restore OpenGL state: */
glPopAttrib();
}
void SandboxClient::resetNavigation(void) {
}
void SandboxClient::initContext(GLContextData& contextData) const {
/* Create context data item and store it in the GLContextData object: */
DataItem* dataItem = new DataItem;
contextData.addDataItem(this, dataItem);
/* Create the bathymetry elevation texture: */
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, dataItem->bathymetryTexture);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_R32F, gridSize[0] - 1, gridSize[1] - 1, 0, GL_RED,
GL_FLOAT, 0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
/* Create the water surface elevation texture: */
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, dataItem->waterTexture);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_R32F, gridSize[0], gridSize[1], 0, GL_RED, GL_FLOAT,
0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
/* Upload the grid of bathymetry template vertices into the vertex buffer: */
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB, dataItem->bathymetryVertexBuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, (gridSize[1] - 1) * (gridSize[0] - 1)*sizeof(Vertex), 0,
GL_STATIC_DRAW_ARB);
Vertex* vPtr = static_cast<Vertex*>(glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB));