-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarcelloUtils.s3d
676 lines (563 loc) · 13.1 KB
/
MarcelloUtils.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
#ifndef _MARCELLOUTILS
#define _MARCELLOUTILS
// Collection of utilities for XVR
// Written by SSSA
// (C) 2013-2014
// Contact: m.carrozzino@sssup.it, g.avveduto@sssup.it, f.tecchia@sssup.it
#define SEL_PASS 0
#define SEL_PICK 1
#define SEL_MOVE 2
#define CAMERA_SPEED 0.95
#define CAMERA_DIST 0.005
#define SPLASH_SIZE 0.3
//----------------------------------------------------------------------
function KeyStatus(keycode)
//----------------------------------------------------------------------
{
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;
}
//----------------------------------------------------------------------
class DebugBox
//----------------------------------------------------------------------
{
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);
};
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::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 = sprintf("%s: %.3f", testo, value);
Aadd(items_array, testo_item);
var lun = len(testo_item);
if (lun > xsize) xsize = lun;
last_item ++;
}
function DebugBox::Draw(viewportx, viewporty)
{
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, OculusOn)
//-------------------------------------------
{
var retval= false;
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];
if (OculusOn)
{
var inv = Oculus_InvertCoords([Mouse.X,Mouse.Y]);
var fbores = Oculus_GetCorrectFBORes();
vec0[0] = inv.x*fbores.x;
vec0[1] = inv.y*fbores.y;
vec0[2] = 0.0;
vec1[0] = inv.x*fbores.x;
vec1[1] = inv.y*fbores.y;
vec1[2] = 1.0;
}
else
{
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);
/*
if (OculusOn)
{
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glPointSize(15.0);
glColor(1,0,0,1);
glBegin(GL_POINTS);
glvertex(p0);
glEnd();
glENable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
}
/
if ( vr_obj.IsColliding(p0, p1,pInt,nrml,n_tr,&compID) )
retval = true;
else
retval = false;
if ((mode == SEL_PICK || mode == SEL_MOVE) && !Mouse.ButtonL)
retval = false;
if (retval && mode == SEL_MOVE)
{
p0 = gluProject(pInt); // 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
trace(p0);
vr_obj.SetPosition(p0);
}
return retval;
}
function SelectionOculus(vr_obj, mode, compID)
{
return Selection(vr_obj, mode, compID, true);
}
*/
//-------------------------------------------
function MoveCameraTo(dest)
//-------------------------------------------
{
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)
//-------------------------------------------
{
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)
//-------------------------------------------
{
var temp;
static var cdist,tdist;
static var start = true;
static var nframes = 1;
static var cvm,tvm;
static var cdir,tdir;
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();
temp=temp + cv * cdir;
CameraSetPosition(temp);
temp=CameraGetTarget();
temp=temp + tv * tdir;
CameraSetTarget(temp);
nframes ++;
if (nframes == frames || cdist == 0)
{
start = true;
}
else
start = false;
return start;
}
//----------------------------------------------------------------------
class ProgressBar
//----------------------------------------------------------------------
{
// progress: expressed in %
// s_text: string to display
// t_index: index of the texture array (if -1 no texture is present)
// tex_splash: array of splash textures
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;
SetProgress(prf);
InitTextures(tex_array);
SetText(txt);
SetTextureIndex(ind);
Draw(pr,txt,ind);
};
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;
}
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::Draw(pr, txt, ind)
//-------------------------------------------
{
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);
if(cur_index != -1 && n_textures > 0)
{
SetActiveTexture(a_splash_textures[cur_index]);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor(1.0,1.0,1.0);// ++ -+ -- +-
glTexcoord(1,0);
glVertex( m_pic_left+m_pic_sizex, m_pic_top, 0);
glTexcoord(0,0);
glVertex( m_pic_left, m_pic_top, 0);
glTexcoord(0,1);
glVertex( m_pic_left, m_pic_top-m_pic_sizey, 0);
glTexcoord(1,1);
glVertex( m_pic_left+m_pic_sizex, m_pic_top-m_pic_sizey, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
}
glTranslate(0,-0.53,0);
if (progress>=0)
{
//Barra
if (progress>=100.0)
{
glBegin(GL_QUADS); //Barra verde finita
glColor(0.6, 1.0, 0.0); glVertex(m_bar_left, m_bar_bottom, 0.0);
glColor(0.6, 1.0, 0.0); glVertex(m_bar_right, m_bar_bottom, 0.0);
glColor(0.6, 1.0, 0.0); glVertex(m_bar_right, m_bar_top, 0.0);
glColor(0.6, 1.0, 0.0); glVertex(m_bar_left, m_bar_top, 0.0);
glEnd();
}
else
{
glColor(1.0-p*0.4,p,0.0,1.0);
glBegin(GL_QUADS); //Barra cangiante
glVertex(m_bar_left, m_bar_bottom, 0.0);
glVertex(m_bar_left+p, m_bar_bottom, 0.0);
glVertex(m_bar_left+p, m_bar_top, 0.0);
glVertex(m_bar_left, m_bar_top, 0.0);
glEnd();
}
}
glEnable(GL_LIGHTING);
glPopMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
ConsoleText(0.249, 0.2, s_text);
SwapBuffers();
}
//-------------------------------------------
function ToolTip(s_text, reverse, bkg_color, fgd_color)
//-------------------------------------------
{
var mx = 0.0;
var my = 0.0;
var viewport = glget(GL_VIEWPORT);
if ( valtype(reverse)=="" )
reverse = false;
if ( valtype(bkg_color)=="" )
bkg_color = [1.0,1.0,0.0,0.5];
if ( valtype(fgd_color)=="" )
fgd_color = [0.0,0.0,0.0,1.0];
glColor(1.0,1.0,1.0,0.1);
var dim = 0.01+0.006*len(s_text);
mx = (Mouse.x/Viewport[2]);
if (reverse)
mx = mx-dim;
my = (Mouse.y/Viewport[3]);
var fontsize = 12;
var ydim = (fontsize+4)/Viewport[3];
ConsoleFont("Arial",fontsize);
ConsoleColor( bkg_color[0], bkg_color[1], bkg_color[2], bkg_color[3]);
ConsoleFilledRect(mx,1.05-my-ydim, dim, ydim );
var margin = 8/Viewport[3];
ConsoleColor( fgd_color[0], fgd_color[1], fgd_color[2], fgd_color[3]);
ConsoleText(mx+0.01,1.015-my+margin,s_text);
}
//-------------------------------------------
function swap (vett, i, j)
//-------------------------------------------
{
var v = vett;
var temp = v[i];
v[i] = v[j];
v[j] = temp;
return v;
}
//-------------------------------------------
function qsort (vettore, prim, ult, indices)
//-------------------------------------------
{
var last,v,i;
var vind = indices;
if (prim >= ult)
return vettore;
var ind = (prim+ult)/2;
v = swap (vettore, prim, ind);
vind = swap (vind, prim, ind);
last = prim;
for (i=prim+1;i<=ult;i++)
if (v[i] < v[prim])
{
last++;
v = swap (v, last, i);
vind = swap (vind, last,i);
}
v = swap (v,prim,last);
indices = swap (vind, prim, last);
v = qsort (v,prim,last-1,indices);
v = qsort (v,last+1,ult,indices);
return v;
}
//-------------------------------------------
class SWPointer
//-------------------------------------------
{
var b_pointer;
var size;
var distance;
var color;
init(image,sz,dist);
setcolor(col);
draw(x,y,on);
};
function SWPointer::init(image,sz,dist)
//-------------------------------------------
{
if (sz == VOID)
{
size = vector(2);
size.x = 3;
size.y = 4;
}
else
size = sz;
if (dist == VOID)
{
distance = 0.994;
}
else
distance = dist;
b_pointer = CVmBillBoard(image,size.x,size.y);
color = [1, 0.5, 0.5];
}
function SWPointer::SetColor(col)
//-------------------------------------------
{
color = col;
}
function SWPointer::Draw(x,y,oculusOn)
//-------------------------------------------
{
ShowCursor(false);
var viewport = Glget(GL_VIEWPORT);
var pwin = vector(3);
/*
if (OculusOn)
{
var inv = Oculus_InvertCoords([x,y]);
var fbores = Oculus_GetCorrectFBORes();
pwin[0] = inv.x*fbores.x;
pwin[1] = inv.y*fbores.y;
pwin[2] = distance;
b_pointer.SetSize(0.05*size.x,0.05*size.y);
}
else
*/
{
pwin = [Mouse.x,viewport[3]-Mouse.y,distance];
b_pointer.SetSize(size.x,size.y);
}
var point=gluUnproject(pwin);
glEnable(GL_BLEND);
glColor([color.r, color.g, color.b,1]);
glDisable(GL_DEPTH_TEST);
b_pointer.SetPosition(point);
b_pointer.Draw(VR_CLAMP);
glEnable(GL_DEPTH_TEST);
}
#endif