-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.s3d
1822 lines (1564 loc) · 55.8 KB
/
Utils.s3d
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
// ******************************************************************************************************************************************
//
// Utils.s3d
//
// A collection of utility functions for use in XVR
//
// by Marcello Carrozzino, marcello 'AT' sssup.it
// contributing: Giovanni Avveduto, Cristian Lorenzini, Franco Tecchia
//
// Released 31/8/2006, Last Update 23/01/2015
//
// ---------------------------------------------------------------------------------------
// Includes:
// ---------------------------------------------------------------------------------------
//
// Interaction functions:
// MouseUp(button) - Checks whether a specified mouse button has been released
// KeyStatus(keycode) - Checks whether a specified key has been pressed for the first time
// JoyStatus(keycode) - Checks whether a specified joystick button has been pressed for the first time
//
// Graphics functions:
// Selection(vr_obj, mode, &compID) - Determines if a certain XVR object has been selected using the mouse
// MoveCameraTo(dest, inertia) - Moves smoothly the position of the current camera to a specified destination position
// MoveCameraTarget(dest, inertia) - Moves smoothly the target of the current camera to a specified destination position
// MoveCameraAndTarget(cdest, tdest, frames) - Moves smoothly the position and the target of the current camera to specified destination positions
// DrawForegroundTexturedQuad (texture, size, doclear) - Draws a textured quad in foreground
// ToolTip(s_text, reverse, bkg_color, fgd_color) - Displays a tooltip at the mouse location
//
// Vector functions:
// swap (vett, i, j) - Swaps the elements i and j of a vector
// qsort (vec, vec_indices, first, last) - Sorts the vector 'vec', starting from the initial position 'fpos' to the ending position 'lpos'. The reslast is stored
//
// Math functions:
// Distance(p0, p1) - Calculates the square of the distance between P0 and P1
// Max(x, y) - Calculates the maximum between x and y
// Min(x, y) - Calculates the minimum between x and y
// Sign(x) - Calculates the signum of a given number
//
// String functions:
// str_replace(src_string, old_subs, new_subs) - Replaces a substring with a new one inside a given string.
// str_contains(src, substr) - Verifies is a string contains a given substring
// get_path(url) - Return the path part of a given url
// get_filename(url) - Return the filename part of a given url
// get_extension(filename) - Return the extension of a given filename
//
//
// Classes:
//
// DebugBox - display text information (for instance, variable values) on screen, in a customizable box using Console series of functions
// ProgressBar - allows to display a customizable progress bar, optionally enriched with text and splash images
// SWPointer - isplay a custom billboard pointer, available also for stereo rendering
// ShadowEnabler - cast shadows on selected objects
//
//
//---------------------------------------------------------------------------------------
//
//
//
//
// Reference:
//---------------------------------------------------------------------------------------
// MouseUp(button)
//
// Checks whether a specified mouse button has been released
//
// [out] returns true if the specified button has changed status from pressed to unpressed
// [button - int] might be: MOUSELEFT, MOUSERIGHT, MOUSEMIDDLE
//----------------------------------------------------------------------
// KeyStatus(keycode)
//
// Checks whether a specified key has been pressed for the first time
//
// [out] returns true if the key specified in 'keycode' has changed status from unpressed to pressed
// [keycode - int|string] might be a virtualkey or an ascii *capital* symbol in quotation marks (e.g. "A", "1", etc.)
//----------------------------------------------------------------------
// JoyStatus(keycode)
//
// Checks whether a specified joystick button has been pressed for the first time
//
// [out] returns true if the joystick button specified in 'keycode' has changed status from unpressed to pressed
// [joycode -int] is the ID of a joystick button, might assume values ranging from 1 to 10
//----------------------------------------------------------------------
// Selection(vr_obj, mode, &compID)
//
// Determines if a certain XVR object has been selected using the mouse
//
// [out] returns true if the object vr_obj is selected by mouse, false otherwise
// [out] if mode is SEL_INT_P, returns an array {bool, vec3} where bool is selection true/false and vec3 is the intersection point
// [vr_obj - CVmObj|CVmCharacter] is the object to be tested against mouse selection
// [mode - int] specifies the selection mode. It might assume the following values:
// SEL_PICK - The object is selected if the mouse intersects the object AND the left mouse button is pressed
// SEL_PASS - The object is selected if the mouse intersects the object
// SEL_MOVE - As SEL_PICK, additionally the picked object is moved on the [X,Y] camera plane by dragging the mouse
// SEL_RPICK - As SEL_PICK but testing the right mouse button
// SEL_INT_P - As SEL_PICK, additionally returns also the intersection point
// [compid - int] if the object is selected, represents the ID of the selected subset (in case of CVmObj) or component (in case of CVmCharacter)
//----------------------------------------------------------------------
// MoveCameraTo(dest, inertia)
//
// Moves smoothly the position of the current camera to a specified destination position
//
// [out] returns true if the destination has been reached, false otherwise
// [dest - vec3] is the destination camera position
// [inertia - float] determines the speed of the movement. If inertia = 1, the camera does not move; if inertia = 0, the camera is set in the destination.
// typical values for inertia are in the range of 0.95 - 0.98
//----------------------------------------------------------------------
// MoveCameraTarget(dest, inertia)
//
// Moves smoothly the target of the current camera to a specified destination position
//
// [out] returns true if the destination has been reached, false otherwise
// [dest - vec3] is the destination camera target
// [inertia - float] determines the speed of the movement. If inertia = 1, the target does not move; if inertia = 0, the target is set in the destination.
// typical values for inertia are in the range of 0.95 - 0.98
//----------------------------------------------------------------------
// MoveCameraAndTarget(cdest, tdest, frames)
//
// Moves smoothly the position and the target of the current camera to specified destination positions
//
// [out] returns true if the destination has been reached, false otherwise
// [cdest - vec3] is the destination camera position
// [tdest - vec3] is the destination camera target
// [frames - int] is the number of animation frames set for the movement to be accomplished
//----------------------------------------------------------------------
// swap (vett, i, j)
//
// Swaps the elements i and j of a vector
//
// [out] returns the swapped vector
// [vett - vector] is the source vector
// [i - int] the first element index to be swapped
// [j - int] the second element index to be swapped
// Utility function used by qsort
//----------------------------------------------------------------------
// qsort (vec, vec_indices, first, last)
//
// Sorts the vector 'vec', starting from the initial position 'fpos' to the ending position 'lpos'. The reslast is stored
// in the vector 'ind' which contains the ordered indices (vec[ind[fpos]] is the first element of the ordered vector, etc.
//
// [out] the ordered vector
// [vett - vector] is the source vector
// [i - int] the first element index to be swapped
// [j - int] the secondt element index to be swapped
//----------------------------------------------------------------------
// DrawForegroundTexturedQuad (texture, size, doclear)
//
// Draws a textured quad in foreground
//
// [out] none
// [texture - CVmTexture] the texture to be mapped
// [size - float, vec2] quad size as a vector [x,y] or a single number (same size will be used for x and y) - default: [2,2], i.e. full screen
// [doclear - boolean] if true, clears the color buffer - default: false
//----------------------------------------------------------------------
// Distance(p0, p1)
//
// Calculates the square of the distance between P0 and P1
//
// [out] the calculated distance
// [p0 - vec2, vec3] the first point, might be a 2-dim or a 3-dim vector
// [p1 - vec2, vec3] the second point, might be a 2-dim or a 3-dim vector
//----------------------------------------------------------------------
// Max(x, y)
//
// Calculates the maximum between x and y
//
// [out] the maximum
// [x - float] the first number
// [Y - float] the second number
//----------------------------------------------------------------------
// Min(x, y)
//
// Calculates the minimum between x and y
//
// [out] the minimum
// [x - float] the first number
// [Y - float] the second number
//----------------------------------------------------------------------
// Sign(x)
//
// Calculates the signum of a given number
//
// [out] 1 if positive, -1 if negative
// [x - float] the given number
//----------------------------------------------------------------------//----------------------------------------------------------------------
// str_replace(src_string, old_subs, new_subs)
//
// Replaces a substring with a new one inside a given string.
//
// [out] the updated string
// [src_string - string] the given string
// [old_subs - string] the old substring
// [new_subs - string] the new substring
//----------------------------------------------------------------------
// str_contains(src, substr)
//
// Verifies is a string contains a given substring
//
// [out] tru if src contains substr
// [src - string] the given string
// [substr - string] the given substring
//----------------------------------------------------------------------
// get_path(url)
//
// Return the path part of a given url
//
// [out] the url path
// [url - string] the given url
//----------------------------------------------------------------------
// get_filename(url)
//
// Return the filename part of a given url
//
// [out] the url filename part
// [url - string] the given url
//----------------------------------------------------------------------
// get_extension(filename)
//
// Return the extension of a given filename
//
// [out] the filename extension
// [url - string] the given filename
//----------------------------------------------------------------------
// ToolTip(s_text, reverse, bkg_color, fgd_color)
//
// Displays a tooltip at the mouse location
//
// [out] none
// [s_text - string] the tooltip text
// [reverse - boolean] if true the tooltip ENDS at the mouse location, otherwise STARTS there - default: false
// [bkg_color - vec4] background color - default: [1,1,0,,0.5] (semitransparent yellow)
// [fgd_color - vec4] foreground color - default: [0,0,0,1] (black)
//
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//
// DebugBox class
//
// allows to display text information (for instance, variable values) on screen, in a customizable box using Console series of functions
//
// methods:
// Initialize() - must be called after the constructor to init the object
// SetItem(text ,value) - creates a new item for the DebugBox with the specified [text - string] label and [value - float] value
// SetPosition(x, y) - sets the position of the DebugBox in windows coordinates [x, y - int]
// Draw( (vx,vy) ) - draws the DebugBox. [vx, vy - float] (optional) is the size of the viewport; if missing, the viewport will be internally retrieved
// SetColor(col) - sets [col - vector 3|4] as the color of the DebugBox frame (default is black)
// SetFillColor(fill) - sets [fill - vector 3|4] the fill color of the DebugBox (default is semitransparent yellow)
// SetMargins(left, bot) - sets [left - float] and [bot - float] as internal margins for the DebugBox. Default values are 4, 4.
// SetFont(name, size) - sets [name - string] as the new font name and [size - int] size for the debug box (default is "Arial", 14)
//----------------------------------------------------------------------
//
// ProgressBar class
// allows to display a customizable progress bar, optionally enriched with text and splash images
//
// methods:
// InitTextures(texarray) - Sets [texarray - array of CVmTexture] the array of splash textures
// SetProgress(prf) - Sets the progress of the bar [prf - int] expressed as a percentage
// SetText(txt) - Sets [txt - string] as the default text label for the bar
// SetTextureIndex(ind) - Sets the current splash texture, identified with [ind - int] as index in the splash texture array
// Draw(pr,tx,ind,col,fnt)- Draws the progress bar. If [pr - int] is specified it will be set as current progress. If [tx - string] is specified it will
// be set as current text label. If [ind - int] is specified it will be set as the current splash texture index.
// If [col - vec3|4] is specified it will be set as the text label color. If [fnt - array[string,int] is specified, fnt[0] will be set as
// the font name of the text label, fnt[1] as the new font size of the text label.
// SetStereoOn() - Must be called in case of side-by-side stereo to enable drawing on both halves of the screen.
//----------------------------------------------------------------------
//
// SWPointer class
// allows to display a custom billboard pointer, available also for stereo rendering
//
// methods:
// init(image,sz,dist) - Inits the billboard pointer with the image file [image - string], with the size [sz - vec2]
// and at the distance [dist - float] in the range 0.0 .. 1.0
// setcolor(col) - Sets the blending color to [col - vec4] - default: [1, 0.5, 0.5, 1]
// draw(x,y,oculusOn) - Displays the pointer at mouse cordinates, or, if OculusOn = true, at coordinates [x,y - float]
// In the latter case, coordinates will be transformed with the Oculus Deformation
//----------------------------------------------------------------------
// ShadowEnabler class
// allows to cast shadows on selected objects
//
// public methods:
// addLight(pos) - Add a casting shadows point light in a given position [pos - vec3]. Currently only one light is supported. Returns the light id.
// SetLightPos(id,pos) - Sets the position of the light with id [id -int] to [pos -vec3].
// EnableEmbeddedLightmaps(enable) - If [enable - boolean] is true, mixes casted shadows with lightmaps embedded in the mesh (otherwise these are discarded)
// SetupShadows(draw_func) - To be called before the actual object draw. Receives [draw_func - string] as the name of the drawing function including the objects
// on which to cast shadows.
// Start() - Starts shadow casting
// Stop() - Stops shadow casting
//
// Example:
//
// function OnInit()
// {..
// global var shadows = shadowEnabler();
// shadows.addlight(light_pos);
// shadows.EnableEmbeddedLightmaps(true);
// }
//
//
// function drawScene()
// {
// shadows.Start();
//
// obj1.Draw();
// ..
// objN.Draw();
// shadows.Stop();
// }
//
// function OnFrame()
// {..
// shadows.SetupShadows("drawscene");
//
// SceneBegin();
// drawScene();
// SceneEnd();
// }
//
// ******************************************************************************************************************************************
#ifdef _INCLUDE_OCULUS_UTILS
#include"OVR_DK2.s3d"
#endif
#define SEL_PASS 0
#define SEL_PICK 1
#define SEL_MOVE 2
#define SEL_RPICK 3
#define SEL_L_UP 4
#define SEL_INT_P 5
#define CAMERA_SPEED 0.95
#define CAMERA_DIST 0.005
#define SPLASH_SIZE 0.3
#define MOUSELEFT 0
#define MOUSERIGHT 1
#define MOUSEMIDDLE 2
//----------------------------------------------------------------------
function MouseUp(button)
//----------------------------------------------------------------------
// MouseUp(button)
//
// Checks whether a specified mouse button has been released
//
// [out] returns true if the specified button has changed status from pressed to unpressed
// [button - int] might be: MOUSELEFT, MOUSERIGHT, MOUSEMIDDLE
//----------------------------------------------------------------------
{
static var mupressed = {false,false,false};
var m = false;
switch (button)
{
case MOUSELEFT:
m = mouse.ButtonL;
break;
case MOUSERIGHT:
m = mouse.ButtonR;
break;
case MOUSEMIDDLE:
m = mouse.ButtonM;
}
if (!mupressed[button] && m)
{
mupressed[button] = true;
}
else if (mupressed[button] && !m)
{
mupressed[button] = false;
return true;
}
return false;
}
//----------------------------------------------------------------------
function KeyStatus(keycode)
//----------------------------------------------------------------------
// KeyStatus(keycode)
//
// Checks whether a specified key has been pressed for the first time
//
// [out] returns true if the key specified in 'keycode' has changed status from unpressed to pressed
// [keycode - int|string] might be a virtualkey or an ascii *capital* symbol in quotation marks (e.g. "A", "1", etc.)
//----------------------------------------------------------------------
{
if (valtype(keycode) == "S")
{
keycode = asc(keycode);
}
static var keys_stati = vector(255);
if (KeyPressed(keycode))
{
if (keys_stati[keycode] == 0.0)
{
keys_stati[keycode] = 1.0;
return true;
}
}
else
{
keys_stati[keycode] = 0.0;
return false;
}
return false;
}
//----------------------------------------------------------------------
function JoyStatus(joycode)
//----------------------------------------------------------------------
// JoyStatus(keycode)
//
// Checks whether a specified joystick button has been pressed for the first time
//
// [out] returns true if the joystick button specified in 'keycode' has changed status from unpressed to pressed
// [joycode -int] is the ID of a joystick button, might assume values ranging from 1 to 10
//----------------------------------------------------------------------
{
static var joy_stati = vector(10);
var retjoy;
if (joycode == 1) retjoy = Joystick.Button1;
else if (joycode == 2) retjoy = Joystick.Button2;
else if (joycode == 3) retjoy = Joystick.Button3;
else if (joycode == 4) retjoy = Joystick.Button4;
else if (joycode == 5) retjoy = Joystick.Button5;
else if (joycode == 6) retjoy = Joystick.Button6;
else if (joycode == 7) retjoy = Joystick.Button7;
else if (joycode == 8) retjoy = Joystick.Button8;
else if (joycode == 9) retjoy = Joystick.Button9;
else if (joycode == 10) retjoy = Joystick.Button10;
if (retjoy)
{
if (joy_stati[joycode] == 0.0)
{
joy_stati[joycode] = 1.0;
return true;
}
}
else
{
joy_stati[joycode] = 0.0;
return false;
}
return false;
}
//----------------------------------------------------------------------
class DebugBox
//----------------------------------------------------------------------
// DebugBox class
//
// allows to display text information (for instance, variable values) on screen, in a customizable box using Console series of functions
//
// methods:
// Initialize() - must be called after the constructor to init the object
// SetItem(text ,value) - creates a new item for the DebugBox with the specified [text - string] label and [value - float] value
// SetPosition(x, y) - sets the position of the DebugBox in windows coordinates [x, y - int]
// Draw( (vx,vy) ) - draws the DebugBox. [vx, vy - float] (optional) is the size of the viewport; if missing, the viewport will be internally retrieved
// SetColor(col) - sets [col - vector 3|4] as the color of the DebugBox frame (default is black)
// SetFillColor(fill) - sets [fill - vector 3|4] the fill color of the DebugBox (default is semitransparent yellow)
// SetMargins(left, bot) - sets [left - float] and [bot - float] as internal margins for the DebugBox. Default values are 4, 4.
// SetFont(name, size) - sets [name - string] as the new font name and [size - int] size for the debug box (default is "Arial", 14)
{
var items_array;
var last_item;
var startx;
var starty;
var linespacing;
var font;
var fontsize;
var updirection;
var xsize;
var margin_left;
var margin_bottom;
var color;
var fill_color;
Initialize();
SetItem(t,v);
SetPosition(x,y);
Draw(vx,vy);
SetColor(col);
SetFillColor(fill);
SetMargins(mleft, mbottom);
SetFont(newfont,newdim);
};
function DebugBox::Initialize()
//----------------------------------------------------------------------
{
items_array = array(0);
last_item = 0;
xsize = 0;
updirection = false;
linespacing = 1;
font = "Arial";
fontsize = 14;
margin_left = 4;
margin_bottom = 4;
color = [0.0,0.0,0.0,1.0];
fill_color = [1.0, 1.0, 0.5, 0.5];
}
function DebugBox::SetFont(newfont,newdim)
//----------------------------------------------------------------------
{
font = newfont;
if (newdim != void)
fontsize = newdim;
}
function DebugBox::SetColor(col)
//----------------------------------------------------------------------
{
color = col;
}
function DebugBox::SetFillColor(col)
//----------------------------------------------------------------------
{
fill_color = col;
}
function DebugBox::SetMargins(mleft,mbottom)
//----------------------------------------------------------------------
{
margin_left = mleft;
margin_bottom = mbottom;
}
function DebugBox::SetPosition(x,y)
//----------------------------------------------------------------------
{
startx = x;
starty = y;
}
function DebugBox::SetItem(testo,value)
//----------------------------------------------------------------------
{
var testo_item;
if (value != void)
testo_item = sprintf("%s: %.3f", testo, value);
else
testo_item = sprintf("%s", testo);
Aadd(items_array, testo_item);
var lun = len(testo_item);
if (lun > xsize) xsize = lun;
last_item ++;
}
function DebugBox::Draw(viewportx, viewporty)
//----------------------------------------------------------------------
{
if (viewportx == VOID || viewporty == VOID)
{
var viewport = glget(GL_VIEWPORT);
viewportx = viewport[2];
viewporty = viewport[3];
}
ConsoleFont(font,fontsize);
var vspace;
if (viewporty == -1)
vspace = linespacing * 0.02;
else
{
vspace = linespacing * fontsize / viewporty;
}
var i,j;
var startbox_x = startx-margin_left/viewportx;
var startbox_y = vspace*last_item + starty;
var dim_x = 0.75*(xsize*fontsize)/viewportx;
var dim_y = startbox_y-starty+margin_bottom/viewporty ;
ConsoleColor(fill_color[0],fill_color[1],fill_color[2],fill_color[3]);
ConsoleFilledRect(startbox_x,startbox_y, dim_x , dim_y);
ConsoleColor(color[0],color[1],color[2],color[3]);
ConsoleRect(startbox_x,startbox_y, dim_x , dim_y);
for (i=0;i<last_item;i++)
{
if (!updirection)
j = last_item - i - 1;
else
j = i;
ConsoleText(startx,starty+vspace*i,items_array[j]);
}
items_array = array(0);
last_item = 0;
}
//-------------------------------------------
function Selection(vr_obj, mode, compID)
//----------------------------------------------------------------------
// Selection(vr_obj, mode, &compID)
//
// Determines if a certain XVR object has been selected using the mouse
//
// [out] returns true if the object vr_obj is selected by mouse, false otherwise
// [out] if mode is SEL_INT_P, returns an array {bool, vec3} where bool is selection true/false and vec3 is the intersection point
// [vr_obj - CVmObj|CVmCharacter] is the object to be tested against mouse selection
// [mode - int] specifies the selection mode. It might assume the following values:
// SEL_PICK - The object is selected if the mouse intersects the object AND the left mouse button is pressed
// SEL_PASS - The object is selected if the mouse intersects the object
// SEL_MOVE - As SEL_PICK, additionally the picked object is moved on the [X,Y] camera plane by dragging the mouse
// SEL_RPICK - As SEL_PICK but testing the right mouse button
// SEL_INT_P - As SEL_PICK, additionally returns also the intersection point
// [compid - int] if the object is selected, represents the ID of the selected subset (in case of CVmObj) or component (in case of CVmCharacter)
//----------------------------------------------------------------------
{
static var taken_vr_obj = false;
var retval;
var viewport = array(4);
var winx,winy;
static var p0 = [0.0,0.0,0.0];
static var p1 = [0.0,0.0,0.0];
var pInt = [0.0,0.0,0.0];
var nrml = [0.0,0.0,0.0];
var n_tr;
viewport = glget(GL_VIEWPORT);
winx = Mouse.X; //x*1.0f/width;
winy = viewport[3] - 1 - Mouse.Y; //1.0f - y*1.0f/height;
var vec0 = [0.0,0.0,0.0];
var vec1 = [0.0,0.0,0.0];
vec0[0] = winx;
vec0[1] = winy;
vec0[2] = 0.0;
vec1[0] = winx;
vec1[1] = winy;
vec1[2] = 1.0;
p0 = gluUnProject(vec0);
p1 = gluUnProject(vec1);
// collision detection
retval = vr_obj.IsColliding(p0, p1, &pInt, &nrml, &n_tr, &compID);
if (mode == SEL_PASS)
return retval;
if (retval && compID == -1 && vr_obj.GetMesh() != VOID)
compID = vr_obj.GetMesh().GetSubSetFromTriIndex(n_Tr);
// pick and move objects
if ((mode == SEL_PICK || mode == SEL_MOVE || mode == SEL_INT_P) && !Mouse.ButtonL)
{
taken_vr_obj = Mouse.ButtonL;
retval = false;
}
if (mode == SEL_RPICK && !Mouse.ButtonR)
retval = false;
// move
if ((retval || taken_vr_obj) && mode == SEL_MOVE)
{
taken_vr_obj = taken_vr_obj = Mouse.ButtonL; //(!taken_vr_obj && Mouse.DoubleL) ? true : (taken_vr_obj && Mouse.DoubleL) ? false : taken_vr_obj;
retval = taken_vr_obj;
p0 = gluProject(vr_obj.GetPosition()); // p0: punto di intersezione in screen coordinates
vec0[2] = p0[2]; // vec0: mouse pointer in screen coordinates con z = quella di p0
p0 = gluUnProject(vec0); // p0: vec0 in world coordinates
vr_obj.SetPosition(p0);
}
if (mode == SEL_INT_P)
return {retval, pInt};
return retVal; // return pInt;
}
//-------------------------------------------
function MoveCameraTo(dest, speed)
//----------------------------------------------------------------------
// MoveCameraTo(dest, inertia)
//
// Moves smoothly the position of the current camera to a specified destination position
//
// [out] returns true if the destination has been reached, false otherwise
// [dest - vec3] is the destination camera position
// [inertia - float] determines the speed of the movement. If inertia = 1, the camera does not move; if inertia = 0, the camera is set in the destination.
// typical values for inertia are in the range of 0.95 - 0.98
//----------------------------------------------------------------------
{
var temp;
var dist;
// var speed=CAMERA_SPEED;
temp = CameraGetPosition();
temp = speed * temp + (1.0-speed) * dest;
CameraSetPosition(temp);
dist = ((temp[0]-dest[0])^2)+((temp[1]-dest[1])^2)+((temp[2]-dest[2])^2);
if (dist < CAMERA_DIST)
{
return true;
}
else return false;
}
//-------------------------------------------
function MoveCameraTarget(dest, speed)
//----------------------------------------------------------------------
// MoveCameraTarget(dest, inertia)
//
// Moves smoothly the target of the current camera to a specified destination position
//
// [out] returns true if the destination has been reached, false otherwise
// [dest - vec3] is the destination camera target
// [inertia - float] determines the speed of the movement. If inertia = 1, the target does not move; if inertia = 0, the target is set in the destination.
// typical values for inertia are in the range of 0.95 - 0.98
//----------------------------------------------------------------------
{
var temp;
var dist;
// var speed=CAMERA_SPEED;
temp=CameraGetTarget();
temp=speed*temp+(1.0-speed)*dest;
var temp2=temp;
CameraSetTarget(temp);
dist = ((temp[0]-dest[0])^2)+((temp[1]-dest[1])^2)+((temp[2]-dest[2])^2);
if (dist < CAMERA_DIST)
{
return true;
}
else return false;
}
//-------------------------------------------
function MoveCameraAndTarget(cdest,tdest,frames)
//----------------------------------------------------------------------
// MoveCameraAndTarget(cdest, tdest, frames)
//
// Moves smoothly the position and the target of the current camera to specified destination positions
//
// [out] returns true if the destination has been reached, false otherwise
// [cdest - vec3] is the destination camera position
// [tdest - vec3] is the destination camera target
// [frames - int] is the number of animation frames set for the movement to be accomplished
//----------------------------------------------------------------------
{
var temp;
static var cdist,tdist;
static var start = true;
static var nframes = 1;
static var cvm,tvm;
static var cdir,tdir;
var _up = CameraGetCurrent().GetYAxis();
if (start == true)
{
nframes = 0;
var sqdi;
temp = CameraGetPosition();
sqdi = ((temp[0]-cdest[0])^2)+((temp[1]-cdest[1])^2)+((temp[2]-cdest[2])^2);
cdist = sqrt(sqdi);
cdir = norm(cdest-temp);
cvm = cdist/frames;
temp = CameraGetTarget();
sqdi = ((temp[0]-tdest[0])^2)+((temp[1]-tdest[1])^2)+((temp[2]-tdest[2])^2);
tdist = sqrt(sqdi);
tdir = norm(tdest-temp);
tvm = tdist/frames;
}
var cspeed, tspeed;
var coeff = (frames+1)/2;
if (nframes < frames/2)
{
cspeed = nframes*cvm/coeff;
tspeed = nframes*tvm/coeff;
}
else
{
cspeed = (frames-nframes)*cvm/coeff;
tspeed = (frames-nframes)*tvm/coeff;
}
var cv = (2*cspeed);
var tv = (2*tspeed);
temp=CameraGetPosition();
if (cdist > 0.001)
temp=temp + cv * cdir;
CameraSetPosition(temp);
temp=CameraGetTarget();
if (tdist > 0.001)
temp=temp + tv * tdir;
CameraSetTarget(temp);
nframes ++;
if (nframes == frames || cdist < 0.001)
{
start = true;
}
else
start = false;
CameraGetCurrent().SetUpVector(_up);
return start;
}
//----------------------------------------------------------------------
class ProgressBar
//----------------------------------------------------------------------
// ProgressBar class
// allows to display a customizable progress bar, optionally enriched with text and splash images
//
// methods:
// InitTextures(texarray) - Sets [texarray - array of CVmTexture] the array of splash textures
// SetProgress(prf) - Sets the progress of the bar [prf - int] expressed as a percentage
// SetText(txt) - Sets [txt - string] as the default text label for the bar
// SetTextureIndex(ind) - Sets the current splash texture, identified with [ind - int] as index in the splash texture array
// Draw(pr,tx,ind,col,fnt)- Draws the progress bar. If [pr - int] is specified it will be set as current progress. If [tx - string] is specified it will
// be set as current text label. If [ind - int] is specified it will be set as the current splash texture index.
// If [col - vec3|4] is specified it will be set as the text label color. If [fnt - array[string,int] is specified, fnt[0] will be set as
// the font name of the text label, fnt[1] as the new font size of the text label.
// SetStereoOn() - Must be called in case of side-by-side stereo to enable drawing on both halves of the screen.
{
var progress;
var s_text;
var a_splash_textures;
var n_textures;
var cur_index;
var bkg_color;
var fgd_color;
var m_bar_top;
var m_bar_bottom;
var m_bar_left;
var m_bar_right;
var m_pic_top;
var m_pic_left;
var m_pic_sizex;
var m_pic_sizey;
var m_stereo;
SetProgress(prf);
InitTextures(tex_array);
SetText(txt);
SetTextureIndex(ind);
Draw(pr,txt,ind,col,font);
SetStereoOn();
// PRIVATE
DrawTexture(delta);
DrawBar(prog, delta);
};
function ProgressBar::ProgressBar()
//-------------------------------------------
{
progress = 0;
s_text = "";
n_textures = 0;
a_splash_textures = array(0);
cur_index = -1;
bkg_color = [0.0, 0.0, 0.0];
fgd_color = [1.0, 1.0, 1.0];
m_bar_top = 0.01;
m_bar_bottom = -0.01;
m_bar_left = -0.5;
m_bar_right = 0.5;
m_pic_top = 0.3;
m_pic_left = -0.3;
m_pic_sizex = 0.6;
m_pic_sizey = 0.6;
m_stereo = false;
}
function ProgressBar::SetStereoOn()
//-------------------------------------------
{
m_stereo = true;
m_bar_left = (m_bar_left-1)/2;
m_bar_right = (m_bar_right-1)/2;
m_pic_left = (m_pic_left-1)/2;
m_pic_sizex = m_pic_sizex / 2;
}
function ProgressBar::InitTextures(tex_array)
//-------------------------------------------
{
a_splash_textures = tex_array;
n_textures = len(a_splash_textures);
}
function ProgressBar::SetProgress(pr)
//-------------------------------------------
{
progress = pr;
}
function ProgressBar::SetText(txt)
//-------------------------------------------
{
s_text = txt;
}
function ProgressBar::SetTextureIndex(ind)
//-------------------------------------------
{
cur_index = ind;
}
function ProgressBar::DrawTexture(d)
// PRIVATE -------------------------------------------
{
if (d == void)
d = 0;
glTexcoord(1,0);
glVertex( d+ m_pic_left+m_pic_sizex, m_pic_top, 0);
glTexcoord(0,0);
glVertex( d+ m_pic_left, m_pic_top, 0);
glTexcoord(0,1);
glVertex( d+ m_pic_left, m_pic_top-m_pic_sizey, 0);
glTexcoord(1,1);
glVertex( d+ m_pic_left+m_pic_sizex, m_pic_top-m_pic_sizey, 0);
}
function ProgressBar::DrawBar(p,d)
// PRIVATE -------------------------------------------
{
if (d == void)
d = 0;
glVertex(d+ m_bar_left, m_bar_bottom, 0.0);
glVertex(d+ m_bar_left + p, m_bar_bottom, 0.0);
glVertex(d+ m_bar_left + p, m_bar_top, 0.0);
glVertex(d+ m_bar_left, m_bar_top, 0.0);
}
function ProgressBar::Draw(pr, txt, ind, col, font)
//-------------------------------------------
{
if ( valtype(pr) !="" ) progress = pr;
if ( valtype(txt)!="" ) s_text = txt;
if ( valtype(ind)!="" ) cur_index = ind;
var p = progress/100.0;
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_LIGHTING);