-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_util.cpp
More file actions
11621 lines (10290 loc) · 423 KB
/
app_util.cpp
File metadata and controls
11621 lines (10290 loc) · 423 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "app_util.h"
// Globals
nvDraw g_2D;
nvGui g_Gui;
// Utility functions
void init2D ( const char* fontName ) { g_2D.Initialize( fontName ); }
void start2D () { g_2D.start2D(); }
void start2D (bool bStatic) { g_2D.start2D(bStatic); }
void updatestatic2D ( int n ) { g_2D.updateStatic2D (n); }
void static2D () { g_2D.start2D(true); }
void end2D () { g_2D.end2D(); }
void draw2D () { g_2D.Draw(); }
void setview2D (float w, float h) { g_2D.setView2D(w,h); }
void setview2D ( float* model, float* view, float* proj ) { g_2D.setView2D( model, view, proj ); }
void setorder2D ( bool zt, float zfactor ) { g_2D.setOrder2D( zt, zfactor ); }
void setText ( float scale, float kern ) { g_2D.setText(scale,kern); }
void drawLine ( float x1, float y1, float x2, float y2, float r, float g, float b, float a ) { g_2D.drawLine(x1,y1,x2,y2,r,g,b,a); }
void drawRect ( float x1, float y1, float x2, float y2, float r, float g, float b, float a ) { g_2D.drawRect(x1,y1,x2,y2,r,g,b,a); }
void drawFill ( float x1, float y1, float x2, float y2, float r, float g, float b, float a ) { g_2D.drawFill(x1,y1,x2,y2,r,g,b,a); }
void drawTri ( float x1, float y1, float x2, float y2, float x3, float y3, float r, float g, float b, float a ) { g_2D.drawTri(x1,y1,x2,y2,x3,y3,r,g,b,a); }
void drawCircle ( float x1, float y1, float radius, float r, float g, float b, float a ) { g_2D.drawCircle(x1,y1,radius,r,g,b,a); }
void drawCircleDash ( float x1, float y1, float radius, float r, float g, float b, float a ) { g_2D.drawCircleDash(x1,y1,radius,r,g,b,a); }
void drawCircleFill ( float x1, float y1, float radius, float r, float g, float b, float a ) { g_2D.drawCircleFill(x1,y1,radius,r,g,b,a); }
void drawText ( float x1, float y1, char* msg, float r, float g, float b, float a ) { g_2D.drawText(x1,y1,msg,r,g,b,a); }
float getTextX ( char* msg ) { return g_2D.getTextX(msg); }
float getTextY ( char* msg ) { return g_2D.getTextY(msg); }
void drawGui () { g_Gui.Draw(); }
int addGui ( float x, float y, float w, float h, char* name, int gtype, int dtype, void* data, float vmin, float vmax ) { return g_Gui.AddGui ( x, y, w, h, name, gtype, dtype, data, vmin, vmax ); }
bool guiChanged ( int n ) { return g_Gui.guiChanged(n); }
bool guiMouseDown ( float x, float y ) { return g_Gui.MouseDown(x,y); }
bool guiMouseDrag ( float x, float y ) { return g_Gui.MouseDrag(x,y); }
#include <sstream>
//------------------------------------------------------ UTILITIES
int strToI (std::string s) {
//return ::atoi ( s.c_str() );
std::istringstream str_stream ( s );
int x;
if (str_stream >> x) return x; // this is the correct way to convert std::string to int, do not use atoi
return 0;
};
float strToF (std::string s) {
//return ::atof ( s.c_str() );
std::istringstream str_stream ( s );
float x;
if (str_stream >> x) return x; // this is the correct way to convert std::string to float, do not use atof
return 0;
};
std::string strParse ( std::string& str, std::string lsep, std::string rsep )
{
std::string result;
size_t lfound, rfound;
lfound = str.find_first_of ( lsep );
if ( lfound != std::string::npos) {
rfound = str.find_first_of ( rsep, lfound+1 );
if ( rfound != std::string::npos ) {
result = str.substr ( lfound+1, rfound-lfound-1 ); // return string strickly between lsep and rsep
str = str.substr ( 0, lfound ) + str.substr ( rfound+1 );
return result;
}
}
return "";
}
bool strGet ( std::string str, std::string& result, std::string lsep, std::string rsep )
{
size_t lfound, rfound;
lfound = str.find_first_of ( lsep );
if ( lfound != std::string::npos) {
rfound = str.find_first_of ( rsep, lfound+1 );
if ( rfound != std::string::npos ) {
result = str.substr ( lfound+1, rfound-lfound-1 ); // return string strickly between lsep and rsep
return true;
}
}
return false;
}
std::string strSplit ( std::string& str, std::string sep )
{
std::string result;
size_t found;
found = str.find_first_of ( sep );
if ( found != std::string::npos) {
result = str.substr ( 0, found );
str = str.substr ( found+1 );
} else {
result = str;
str = "";
}
return result;
}
std::string strReplace ( std::string str, std::string delim, std::string ins )
{
size_t found = str.find_first_of ( delim );
while ( found != std::string::npos ) {
str = str.substr ( 0, found ) + ins + str.substr ( found+1 );
found = str.find_first_of ( delim );
}
return str;
}
bool strSub ( std::string str, int first, int cnt, std::string cmp )
{
if ( str.substr ( first, cnt ).compare ( cmp ) == 0 ) return true;
return false;
}
// trim from start
#include <algorithm>
#include <cctype>
std::string strLTrim(std::string str) {
str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return str;
}
// trim from end
std::string strRTrim(std::string str) {
str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end());
return str;
}
// trim from both ends
std::string strTrim(std::string str) {
return strLTrim(strRTrim(str));
}
std::string strLeft ( std::string str, int n )
{
return str.substr ( 0, n );
}
int strExtract ( std::string& str, std::vector<std::string>& list )
{
size_t found ;
for (int n=0; n < list.size(); n++) {
found = str.find ( list[n] );
if ( found != std::string::npos ) {
str = str.substr ( 0, found ) + str.substr ( found + list[n].length() );
return n;
}
}
return -1;
}
unsigned long getFileSize ( char* fname )
{
FILE* fp = fopen ( fname, "rb" );
fseek ( fp, 0, SEEK_END );
return ftell ( fp );
}
unsigned long getFilePos ( FILE* fp )
{
return ftell ( fp );
}
nvDraw::nvDraw ()
{
mCurrZ = 0;
localPos = 0;
localClr = 1;
localUV = 2;
mDynNum = 0;
mTextScale = 1;
mTextKern = 0;
mWidth = 0;
mHeight = 0;
}
void nvDraw::Initialize ( const char* fontName )
{
MakeShaders2D ();
LoadFont ( fontName );
mWhiteImg.Create ( 1, 1, IMG_RGBA );
mWhiteImg.Fill ( 1,1,1,1 );
glGenVertexArrays ( 1, &mVAO );
}
void nvDraw::setView2D ( float w, float h )
{
mWidth = w; mHeight = h;
}
void nvDraw::setView2D ( float* model, float* view, float* proj )
{
mWidth = -1; mHeight = -1;
memcpy ( mModelMtx, model, 16 * sizeof(float) );
memcpy ( mViewMtx, view, 16 * sizeof(float) );
memcpy ( mProjMtx, proj, 16 * sizeof(float) );
}
void nvDraw::updateStatic2D ( int n )
{
if ( n < 0 || n >= mStatic.size()) return;
if ( mWidth==-1 ) {
SetMatrixView ( mStatic[n], mModelMtx, mViewMtx, mProjMtx, mZFactor );
} else {
SetDefaultView ( mStatic[n], mWidth, mHeight, mZFactor );
}
}
int nvDraw::start2D ( bool bStatic )
{
Set2D new_set;
Set2D* s;
if ( bStatic ) {
mStatic.push_back ( new_set );
s = &mStatic[ mStatic.size()-1 ];
mCurrSet = s;
} else {
int curr = mDynNum;
if ( mDynNum >= mDynamic.size() ) {
mDynamic.push_back ( new_set );
mDynNum = (int) mDynamic.size();
} else {
mDynNum++;
}
s = &mDynamic[curr];
mCurrSet = s;
}
for (int n=0; n < GRP_NUM; n++ ) {
s->mNum[n] = 0; s->mMax[n] = 0; s->mGeom[n] = 0;
s->mNumI[n] = 0; s->mMaxI[n] = 0; s->mIdx[n] = 0;
}
if ( mWidth==-1 ) {
SetMatrixView ( *s, mModelMtx, mViewMtx, mProjMtx, mZFactor );
} else {
SetDefaultView ( *s, mWidth, mHeight, mZFactor );
}
return mCurr;
}
void nvDraw::setOrder2D ( bool zt, float zfactor )
{
if ( zt == false ) zfactor = 1;
mZFactor = zfactor;
}
void nvDraw::SetDefaultView ( Set2D& s, float w, float h, float zf )
{
s.zfactor = zf;
Matrix4F proj, view, model;
view.Scale ( 2.0/w, -2.0/h, s.zfactor );
model.Translate ( -w/2.0, -h/2.0, 0 );
view *= model;
model.Identity ();
proj.Identity ();
memcpy ( s.model, model.GetDataF(), 16 * sizeof(float) );
memcpy ( s.view, view.GetDataF(), 16 * sizeof(float) );
memcpy ( s.proj, proj.GetDataF(), 16 * sizeof(float) );
}
void nvDraw::SetMatrixView ( Set2D& s, float* model, float* view, float* proj, float zf )
{
s.zfactor = zf;
memcpy ( s.model, model, 16 * sizeof(float) );
memcpy ( s.view, view, 16 * sizeof(float) );
memcpy ( s.proj, proj, 16 * sizeof(float) );
}
void nvDraw::end2D ()
{
mCurrSet = 0x0;
}
Vert2D* nvDraw::allocGeom ( int cnt, int grp, Set2D* s, int& ndx )
{
if ( s->mNum[grp] + cnt >= s->mMax[grp] ) {
unsigned long new_max = s->mMax[grp] * 8 + cnt;
// app_printf ( "allocGeom: expand, %lu\n", new_max );
Vert2D* new_data = (Vert2D*) malloc ( new_max*sizeof(Vert2D) );
if ( s->mGeom[grp] != 0x0 ) {
memcpy ( new_data, s->mGeom[grp], s->mNum[grp]*sizeof(Vert2D) );
delete s->mGeom[grp];
}
s->mGeom[grp] = new_data;
s->mMax[grp] = new_max;
}
Vert2D* start = s->mGeom[grp] + s->mNum[grp];
if ( s->zfactor != 1.0 ) {
for (int j=0; j < cnt; j++ ) (start+j)->z = (float) mCurrZ;
mCurrZ++;
} else {
for (int j=0; j < cnt; j++ ) (start+j)->z = 0.0;
}
ndx = s->mNum[grp];
s->mNum[grp] += cnt;
return start;
}
uint* nvDraw::allocIdx ( int cnt, int grp, Set2D* s )
{
if ( s->mNumI[grp] + cnt >= s->mMaxI[grp] ) {
unsigned long new_max = s->mMaxI[grp] * 8 + cnt;
// app_printf ( "allocIdx: expand, %lu\n", new_max );
uint* new_data = (uint*) malloc ( new_max*sizeof(uint) );
if ( s->mIdx[grp] != 0x0 ) {
memcpy ( new_data, s->mIdx[grp], s->mNumI[grp]*sizeof(uint) );
delete s->mIdx[grp];
}
s->mIdx[grp] = new_data;
s->mMaxI[grp] = new_max;
}
uint* start = s->mIdx[grp] + s->mNumI[grp];
s->mNumI[grp] += cnt;
return start;
}
void nvDraw::remove2D ( int id )
{
}
void nvDraw::drawLine ( float x1, float y1, float x2, float y2, float r, float g, float b, float a )
{
int ndx;
Vert2D* v = allocGeom ( 2, GRP_LINES, mCurrSet, ndx );
v->x = x1; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x2; v->y = y2; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0;
}
void nvDraw::drawRect ( float x1, float y1, float x2, float y2, float r, float g, float b, float a )
{
int ndx;
Vert2D* v = allocGeom ( 8, GRP_LINES, mCurrSet, ndx );
v->x = x1; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x2; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x2; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x2; v->y = y2; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x2; v->y = y2; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x1; v->y = y2; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x1; v->y = y2; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x1; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0;
}
void nvDraw::drawFill ( float x1, float y1, float x2, float y2, float r, float g, float b, float a )
{
int ndx;
Vert2D* v = allocGeom ( 4, GRP_TRI, mCurrSet, ndx );
uint* i = allocIdx ( 5, GRP_TRI, mCurrSet );
v->x = x1; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x2; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x1; v->y = y2; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x2; v->y = y2; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0;
*i++ = ndx++; *i++ = ndx++; *i++ = ndx++; *i++ = ndx++; *i++ = IDX_NULL;
}
void nvDraw::drawTri ( float x1, float y1, float x2, float y2, float x3, float y3, float r, float g, float b, float a )
{
int ndx;
Vert2D* v = allocGeom ( 3, GRP_TRI, mCurrSet, ndx );
uint* i = allocIdx ( 4, GRP_TRI, mCurrSet );
v->x = x1; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x2; v->y = y2; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x3; v->y = y3; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0;
*i++ = ndx++; *i++ = ndx++; *i++ = ndx++; *i++ = IDX_NULL;
}
void nvDraw::drawCircle ( float x1, float y1, float radius, float r, float g, float b, float a )
{
int ndx;
Vert2D* v = allocGeom ( 62, GRP_LINES, mCurrSet, ndx );
float dx, dy, dxl, dyl;
dxl = (float) cos( (0/31.0)*3.141592*2.0 )*radius;
dyl = (float) sin( (0/31.0)*3.141592*2.0 )*radius;
for (int n=1; n < 32; n++ ) {
dx = (float) cos( (n/31.0)*3.141592*2.0 )*radius;
dy = (float) sin( (n/31.0)*3.141592*2.0 )*radius;
v->x = x1+dxl; v->y = y1+dyl; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
v->x = x1+dx; v->y = y1+dy; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
dxl = dx; dyl = dy;
}
}
void nvDraw::drawCircleDash ( float x1, float y1, float radius, float r, float g, float b, float a )
{
int ndx;
Vert2D* v = allocGeom ( 32, GRP_LINES, mCurrSet, ndx );
float dx, dy;
for (int n=0; n < 32; n++ ) {
dx = (float) cos( (n/31.0)*3.141592*2.0 )*radius;
dy = (float) sin( (n/31.0)*3.141592*2.0 )*radius;
v->x = x1+dx; v->y = y1+dy; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
}
}
void nvDraw::drawCircleFill ( float x1, float y1, float radius, float r, float g, float b, float a )
{
int ndx;
Vert2D* v = allocGeom ( 64, GRP_TRI, mCurrSet, ndx );
uint* i = allocIdx ( 65, GRP_TRI, mCurrSet );
float dx, dy;
for (int n=0; n < 32; n++ ) {
dx = (float) cos( (n/31.0)*3.141592*2.0 )*radius;
dy = (float) sin( (n/31.0)*3.141592*2.0 )*radius;
v->x = x1; v->y = y1; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
*i++ = ndx++;
v->x = x1+dx; v->y = y1+dy; v->r = r; v->g = g; v->b = b; v->a = a; v->tx = 0; v->ty = 0; v++;
*i++ = ndx++;
}
*i++ = IDX_NULL;
}
// from Tristan Lorach, OpenGLText
void nvDraw::drawText ( float x1, float y1, char* msg, float r, float g, float b, float a )
{
int len = (int) strlen ( msg );
int ndx;
Vert2D* v = allocGeom ( len*4, GRP_TRITEX, mCurrSet, ndx );
uint* i = allocIdx ( len*5, GRP_TRITEX, mCurrSet );
int h = mGlyphInfos.pix.ascent + mGlyphInfos.pix.descent + mGlyphInfos.pix.linegap;
float lPosX = x1+1;
float lPosY = y1;
float lLinePosX = lPosX;
float lLinePosY = lPosY;
const char* c = msg;
while (*c != '\0' ) {
if ( *c == '\n' ) {
lPosX = lLinePosX;
lLinePosY += h;
lPosY = lLinePosY;
} else if ( *c >=0 && *c <= 128 ) {
GlyphInfo& gly = mGlyphInfos.glyphs[*c];
float pX = lPosX + gly.pix.offX;
float pY = lPosY + gly.pix.height + gly.pix.offY;
v->x = pX; v->y = pY; v->r = r; v->g = g; v->b = b; v->a = a;
v->tx = gly.norm.u; v->ty = gly.norm.v; v++;
v->x = pX; v->y = pY - gly.pix.height*mTextScale; v->r = r; v->g = g; v->b = b; v->a = a;
v->tx = gly.norm.u; v->ty = gly.norm.v + gly.norm.height; v++;
v->x = pX + gly.pix.width*mTextScale; v->y = pY; v->r = r; v->g = g; v->b = b; v->a = a;
v->tx = gly.norm.u + gly.norm.width; v->ty = gly.norm.v; v++;
v->x = pX + gly.pix.width*mTextScale; v->y = pY - gly.pix.height*mTextScale; v->r = r; v->g = g; v->b = b; v->a = a;
v->tx = gly.norm.u + gly.norm.width; v->ty = gly.norm.v + gly.norm.height; v++;
*i++ = ndx++; *i++ = ndx++; *i++ = ndx++; *i++ = ndx++; *i++ = IDX_NULL;
lPosX += gly.pix.advance* mTextScale + mTextKern;
lPosY += 0;
}
c++;
}
}
float nvDraw::getTextX ( char* msg )
{
int len = (int) strlen ( msg );
int h = mGlyphInfos.pix.ascent + mGlyphInfos.pix.descent + mGlyphInfos.pix.linegap;
float lPosX = 0;
float lPosY = 0;
float lLinePosX = lPosX;
float lLinePosY = lPosY;
const char* c = msg;
while (*c != '\0' ) {
if ( *c == '\n' ) {
lPosX = lLinePosX;
lLinePosY += h;
lPosY = lLinePosY;
} else if ( *c >=0 && *c <= 128 ) {
GlyphInfo& gly = mGlyphInfos.glyphs[*c];
float pX = lPosX + gly.pix.offX;
float pY = lPosY + gly.pix.height + gly.pix.offY;
lPosX += gly.pix.advance* mTextScale + mTextKern;
lPosY += 0;
}
c++;
}
return lPosX;
}
float nvDraw::getTextY ( char* msg )
{
int len = (int) strlen ( msg );
int h = mGlyphInfos.pix.ascent + mGlyphInfos.pix.descent + mGlyphInfos.pix.linegap;
float lPosX = 0;
float lPosY = 0;
float lLinePosX = lPosX;
float lLinePosY = lPosY;
const char* c = msg;
while (*c != '\0' ) {
if ( *c == '\n' ) {
lPosX = lLinePosX;
lLinePosY += h;
lPosY = lLinePosY;
} else if ( *c >=0 && *c <= 128 ) {
GlyphInfo& gly = mGlyphInfos.glyphs[*c];
float pX = lPosX + gly.pix.offX;
float pY = lPosY + gly.pix.height + gly.pix.offY;
lPosX += gly.pix.advance* mTextScale + mTextKern;
lPosY += 0;
}
c++;
}
return lPosY;
}
void nvDraw::UpdateVBOs ( Set2D& s )
{
glBindVertexArray ( mVAO );
for (int n=0; n < GRP_NUM; n++ ) {
if ( s.mNum[n] == 0 ) continue;
if ( s.mVBO[n] == -1 ) glGenBuffers ( 1, &s.mVBO[n] );
glBindBuffer ( GL_ARRAY_BUFFER, s.mVBO[n] );
glBufferData ( GL_ARRAY_BUFFER, s.mNum[n] * sizeof(Vert2D), s.mGeom[n], GL_DYNAMIC_DRAW_ARB);
//for (int j=0; j < s.mNum[n]; j++ ) app_printf ( "%d %f,%f,%f\n", j, s.mGeom[n][j].x, s.mGeom[n][j].y, s.mGeom[n][j].z );
}
}
void nvDraw::Draw ( Set2D& s )
{
if ( s.zfactor == 1.0 )
glDisable ( GL_DEPTH_TEST ); // don't preserve order
else
glEnable ( GL_DEPTH_TEST ); // preserve order
glEnable ( GL_TEXTURE_2D );
glActiveTexture ( GL_TEXTURE1 );
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
glBindTexture ( GL_TEXTURE_2D, mWhiteImg.getTex() );
glUniformMatrix4fv ( mProj, 1, GL_FALSE, s.proj );
glUniformMatrix4fv ( mModel, 1, GL_FALSE, s.model );
glUniformMatrix4fv ( mView, 1, GL_FALSE, s.view );
glBindBuffer ( GL_ARRAY_BUFFER, s.mVBO[ GRP_LINES ] );
glVertexAttribPointer( localPos, 3, GL_FLOAT, GL_FALSE, sizeof(Vert2D), 0 );
glVertexAttribPointer( localClr, 4, GL_FLOAT, GL_FALSE, sizeof(Vert2D), (void*) 12 );
glVertexAttribPointer( localUV, 2, GL_FLOAT, GL_FALSE, sizeof(Vert2D), (void*) 28 );
glDrawArrays ( GL_LINES, 0, s.mNum[GRP_LINES] );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
glPrimitiveRestartIndex ( IDX_NULL );
glEnable ( GL_PRIMITIVE_RESTART );
glBindBuffer ( GL_ARRAY_BUFFER, s.mVBO[ GRP_TRI ] );
glVertexAttribPointer( localPos, 3, GL_FLOAT, GL_FALSE, sizeof(Vert2D), 0 );
glVertexAttribPointer( localClr, 4, GL_FLOAT, GL_FALSE, sizeof(Vert2D), (void*) 12 );
glVertexAttribPointer( localUV, 2, GL_FLOAT, GL_FALSE, sizeof(Vert2D), (void*) 28 );
glDrawElements ( GL_TRIANGLE_STRIP, s.mNumI[GRP_TRI], GL_UNSIGNED_INT, s.mIdx[GRP_TRI] );
glDisable ( GL_PRIMITIVE_RESTART );
glBindTexture ( GL_TEXTURE_2D, mFontImg.getTex() );
glUniform1i ( mFont, 1 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
glPrimitiveRestartIndex ( IDX_NULL );
glEnable ( GL_PRIMITIVE_RESTART );
glBindBuffer ( GL_ARRAY_BUFFER, s.mVBO[ GRP_TRITEX ] );
glVertexAttribPointer( localPos, 3, GL_FLOAT, GL_FALSE, sizeof(Vert2D), 0 );
glVertexAttribPointer( localClr, 4, GL_FLOAT, GL_FALSE, sizeof(Vert2D), (void*) 12 );
glVertexAttribPointer( localUV, 2, GL_FLOAT, GL_FALSE, sizeof(Vert2D), (void*) 28 );
glDrawElements ( GL_TRIANGLE_STRIP, s.mNumI[GRP_TRITEX], GL_UNSIGNED_INT, s.mIdx[GRP_TRITEX] );
glDisable ( GL_PRIMITIVE_RESTART );
glDisable ( GL_TEXTURE_2D );
}
void nvDraw::Draw ()
{
glEnable ( GL_BLEND );
glUseProgram ( mSH2D );
glBindVertexArray ( mVAO );
glEnableVertexAttribArray( localPos );
glEnableVertexAttribArray( localClr );
glEnableVertexAttribArray( localUV );
std::vector<Set2D>::iterator it;
for ( it = mStatic.begin(); it != mStatic.end(); it++ ) {
UpdateVBOs ( (*it) );
Draw ( (*it) );
}
for ( it = mDynamic.begin(); it != mDynamic.end(); it++ ) {
UpdateVBOs ( (*it) );
Draw ( (*it) );
}
// delete dynamic buffers
Set2D* s;
for (int n=0; n < mDynamic.size(); n++ ) {
s = &mDynamic[n];
for (int grp=0; grp < GRP_NUM; grp++) {
if ( s->mGeom[grp] != 0x0 ) delete s->mGeom[grp];
if ( s->mIdx[grp] != 0x0 ) delete s->mIdx[grp];
s->mNum[grp] = 0; s->mMax[grp] = 0; s->mGeom[grp] = 0;
s->mNumI[grp] = 0; s->mMaxI[grp] = 0; s->mIdx[grp] = 0;
}
}
mDynNum = 0; // reset first dynamic buffer (reuses VBOs)
mCurrZ = 0;
glUseProgram ( 0 );
glBindVertexArray ( 0 );
glEnable ( GL_DEPTH_TEST );
}
void nvDraw::MakeShaders2D ()
{
// OpenGL - Create shaders
char buf[16384];
int len = 0;
checkGL( "Start shaders" );
// OpenGL 4.2 Core
// -- Cannot use hardware lighting pipeline (e.g. glLightfv, glMaterialfv)
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
GLchar const * vss =
"#version 420\n"
"\n"
"layout(location = 0) in vec4 inPosition;\n"
"layout(location = 1) in vec4 inColor;\n"
"layout(location = 2) in vec2 inTexCoord;\n"
"out vec4 position;\n"
"out vec4 color;\n"
"out vec2 texcoord;\n"
"uniform mat4 modelMatrix;\n"
"uniform mat4 viewMatrix;\n"
"uniform mat4 projMatrix;\n"
"out gl_PerVertex {\n"
" vec4 gl_Position;\n"
"};\n"
"\n"
"void main()\n"
"{\n"
" position = modelMatrix * inPosition;\n"
" color = inColor;\n"
" texcoord = inTexCoord;\n"
" gl_Position = projMatrix * viewMatrix * modelMatrix * inPosition;\n"
"}\n"
;
glShaderSource(vs, 1, &vss, 0);
glCompileShader(vs);
glGetShaderInfoLog ( vs, 16384, (GLsizei*) &len, buf );
app_printf ( "%s\n", buf );
checkGL( "Compile vertex shader" );
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
GLchar const * fss =
"#version 420\n"
"\n"
"uniform sampler2D fontTex;\n"
"in vec4 position;\n"
"in vec4 color;\n"
"in vec2 texcoord;\n"
"layout(location = 0) out vec4 outColor;\n"
"\n"
"void main()\n"
"{\n"
" outColor = color * vec4(1,1,1,texture(fontTex, texcoord).x); \n"
"}\n"
;
glShaderSource(fs, 1, &fss, 0);
glCompileShader(fs);
glGetShaderInfoLog ( fs, 16384, (GLsizei*) &len, buf );
app_printf ( "%s\n", buf );
checkGL( "Compile fragment shader" );
mSH2D = glCreateProgram();
glAttachShader( mSH2D, vs);
glAttachShader( mSH2D, fs);
checkGL( "Attach program" );
glLinkProgram( mSH2D );
checkGL( "Link program" );
glUseProgram( mSH2D );
checkGL( "Use program" );
mProj = glGetUniformLocation( mSH2D, "projMatrix");
mModel = glGetUniformLocation( mSH2D, "modelMatrix");
mView = glGetUniformLocation( mSH2D, "viewMatrix");
mFont = glGetUniformLocation( mSH2D, "fontTex");
checkGL( "Get Shader Matrices" );
}
/*
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
*/
bool nvDraw::LoadFont ( const char * fontName )
{
if (!fontName) return false;
char fname[200];
sprintf (fname, "%s.tga", fontName);
if ( !mFontImg.LoadTga ( fname ) ) return false;
sprintf(fname, "%s.bin", fontName);
FILE *fd = fopen(fname, "rb");
if ( !fd ) return false;
int r = (int)fread(&mGlyphInfos, 1, sizeof(FileHeader), fd);
fclose(fd);
return true;
}
//--------------------------------------- 2D GUIs
nvGui::nvGui ()
{
mActiveGui = -1;
}
int nvGui::AddGui ( float x, float y, float w, float h, char* name, int gtype, int dtype, void* data, float vmin, float vmax )
{
Gui g;
g.name = name;
g.x = x; g.y = y;
g.w = w; g.h = h;
g.gtype = gtype;
g.dtype = dtype;
g.data = data;
g.vmin = vmin;
g.vmax = vmax;
mGui.push_back ( g );
return mGui.size()-1;
}
bool nvGui::guiChanged ( int n )
{
if ( n < 0 || n >= mGui.size() ) return false;
if ( mGui[n].changed ) {
mGui[n].changed = false;
return true;
}
return false;
}
void nvGui::Draw ()
{
char buf[1024];
float x1, y1, x2, y2, frac, dx;
bool bval;
start2D ();
for (int n=0; n < mGui.size(); n++ ) {
x1 = mGui[n].x; y1 = mGui[n].y;
x2 = x1 + mGui[n].w; y2 = y1 + mGui[n].h;
switch ( mGui[n].gtype ) {
case GUI_PRINT: {
switch ( mGui[n].dtype ) {
case GUI_INT: sprintf ( buf, "%d", *(int*) mGui[n].data ); break;
case GUI_FLOAT: sprintf ( buf, "%.5f", *(float*) mGui[n].data ); break;
case GUI_BOOL: if (*(bool*) mGui[n].data) sprintf (buf, "on" ); else sprintf(buf,"off"); break;
};
dx = getTextX (buf);
drawText ( x2 - dx, y1+10, buf, .9f, .9f, .9f, 1.0f );
sprintf ( buf, "%s", mGui[n].name.c_str() );
drawText ( x1, y1+10, buf, 1,1,1,1 );
} break;
case GUI_SLIDER: {
drawFill ( x1, y1+12, x2, y2, .3f, .3f, .3f, 1.0f );
switch ( mGui[n].dtype ) {
case GUI_INT: frac = (float(*(int *) mGui[n].data) - mGui[n].vmin) / (mGui[n].vmax-mGui[n].vmin); sprintf ( buf, "%d", *(int*) mGui[n].data ); break;
case GUI_FLOAT: frac = ( (*(float*) mGui[n].data) - mGui[n].vmin) / (mGui[n].vmax-mGui[n].vmin); sprintf ( buf, "%.3f", *(float*) mGui[n].data ); break;
};
drawFill ( x1, y1+12, x1+frac*(x2-x1), y2, .6f, 1.0f, .8f, 1.0f );
dx = getTextX (buf);
drawText ( x2 - dx, y1+10, buf, 1.0f, 1.0f, 1.0f, 1.0f );
sprintf ( buf, "%s", mGui[n].name.c_str() );
drawText ( x1, y1+10, buf, 1,1,1,1 );
} break;
case GUI_CHECK: {
drawRect ( x2-10, y1, x2, y1+10, .8f, .8f, .8f, 1.0f);
switch ( mGui[n].dtype ) {
case GUI_INT: bval = (*(int*) mGui[n].data) == 0 ? false : true; break;
case GUI_FLOAT: bval = (*(float*) mGui[n].data) == 0.0 ? false : true; break;
case GUI_BOOL: bval = *(bool*) mGui[n].data; break;
};
if ( bval ) {
drawText ( x2-40, y1+10, "on", .6f, 1.0f, .8f, 1.0f );
drawLine ( x2-10, y1, x2, y1+10, .6f, 1.0f, .8f, 1.0f );
drawLine ( x2-10, y1+10, x2, y1, .6f, 1.0f, .8f, 1.0f );
} else {
drawText ( x2-40, y1+10, "off", .8f, .8f, .8f, 1.0f );
}
sprintf ( buf, "%s", mGui[n].name.c_str() );
drawText ( x1, y1+10, buf, 1.0f, 1.0f, 1.0f, 1.0f );
} break;
}
}
end2D ();
}
bool nvGui::MouseDown ( float x, float y )
{
// GUI down - Check if GUI is hit
float x1, y1, x2, y2;
for (int n=0; n < mGui.size(); n++ ) {
x1 = mGui[n].x; y1 = mGui[n].y;
x2 = x1 + mGui[n].w; y2 = y1 + mGui[n].h;
switch ( mGui[n].gtype ) {
case GUI_SLIDER:
if ( x > x1 && x < x2 && y > y1 && y < y2) {
mActiveGui = n; // set active gui
return true;
}
break;
case GUI_CHECK:
if ( x > x2-10 && x < x2 && y > y1 && y < y1+10 ) {
mActiveGui = -1;
mGui[ n ].changed = true;
switch ( mGui[ n ].dtype ) {
case GUI_INT: *(int*) mGui[n].data = ( (*(int*) mGui[n].data) == 0 ) ? 1 : 0; break;
case GUI_FLOAT: *(float*) mGui[n].data = ( (*(int*) mGui[n].data) == 0.0 ) ? 1.0 : 0.0; break;
case GUI_BOOL: *(bool*) mGui[n].data = ! (*(bool*) mGui[n].data); break;
};
return true;
}
break;
default: break;
};
}
mActiveGui = -1;
return false;
}
bool nvGui::MouseDrag ( float x, float y )
{
// GUI drag - Adjust value of hit gui
float x1, y1, x2, y2;
if ( mActiveGui != -1 ) {
x1 = mGui[ mActiveGui].x; y1 = mGui[mActiveGui ].y;
x2 = x1 + mGui[ mActiveGui ].w; y2 = y1 + mGui[mActiveGui ].h;
if ( x <= x1 ) {
mGui[ mActiveGui ].changed = true;
switch ( mGui[ mActiveGui ].dtype ) {
case GUI_INT: *(int*) mGui[ mActiveGui ].data = (int) mGui[ mActiveGui ].vmin; break;
case GUI_FLOAT: *(float*) mGui[ mActiveGui ].data = (float) mGui[ mActiveGui ].vmin; break;
};
return true;
}
if ( x >= x2 ) {
mGui[ mActiveGui ].changed = true;
switch ( mGui[ mActiveGui ].dtype ) {
case GUI_INT: *(int*) mGui[ mActiveGui ].data = (int) mGui[ mActiveGui ].vmax; break;
case GUI_FLOAT: *(float*) mGui[ mActiveGui ].data = (float) mGui[ mActiveGui ].vmax; break;
};
return true;
}
if ( x > x1 && x < x2 ) {
mGui[ mActiveGui ].changed = true;
switch ( mGui[ mActiveGui ].dtype ) {
case GUI_INT: *(int*) mGui[ mActiveGui ].data = (int) (mGui[ mActiveGui ].vmin + (x-x1)*mGui[ mActiveGui ].vmax / (x2-x1)); break;
case GUI_FLOAT: *(float*) mGui[ mActiveGui ].data = (float) (mGui[ mActiveGui ].vmin + (x-x1)*mGui[ mActiveGui ].vmax / (x2-x1)); break;
};
return true;
}
}
return false;
}
bool readword ( char *line, char *word, char delim )
{
int max_size = 200;
char *buf_pos;
char *start_pos;
// read past spaces/tabs, or until end of line/string
for (buf_pos=line; (*buf_pos==' ' || *buf_pos=='\t') && *buf_pos!='\n' && *buf_pos!='\0';)
buf_pos++;
// if end of line/string found, then no words found, return null
if (*buf_pos=='\n' || *buf_pos=='\0') {*word = '\0'; return false;}
// mark beginning of word, read until end of word
for (start_pos = buf_pos; *buf_pos != delim && *buf_pos!='\t' && *buf_pos!='\n' && *buf_pos!='\0';)
buf_pos++;
if (*buf_pos=='\n' || *buf_pos=='\0') { // buf_pos now points to the end of buffer
//strcpy_s (word, max_size, start_pos); // copy word to output string
strncpy (word, start_pos, max_size);
if ( *buf_pos=='\n') *(word + strlen(word)-1) = '\0';
*line = '\0'; // clear input buffer
} else {
// buf_pos now points to the delimiter after word
*buf_pos++ = '\0'; // replace delimiter with end-of-word marker
//strcpy_s (word, max_size, start_pos);
strncpy (word, start_pos, buf_pos-line ); // copy word(s) string to output string
// move start_pos to beginning of entire buffer
strcpy ( start_pos, buf_pos ); // copy remainder of buffer to beginning of buffer
}
return true; // return word(s) copied
}
/*void save_png ( char* fname, unsigned char* img, int w, int h )
{
unsigned error = lodepng::encode ( "test.png", img, w, h );
if (error) printf ( "png write error: %s\n", lodepng_error_text(error) );
}*/
void nvImg::Create ( int x, int y, int fmt )
{
mXres = x;
mYres = y;
mSize = mXres * mYres;
mFmt = fmt;
switch ( mFmt ) {
case IMG_RGB: mSize *= 3; break;
case IMG_RGBA: mSize *= 4; break;
case IMG_LUM: break;
}
if ( mData != 0x0 ) free ( mData );
mData = (unsigned char*) malloc ( mSize );
memset ( mData, 0, mSize );
UpdateTex();
}
void nvImg::Fill ( float r, float g, float b, float a )
{
unsigned char* pix = mData;
for (int n=0; n < mXres*mYres; n++ ) {
*pix++ = r*255.0f; *pix++ = g*255.0f; *pix++ = b*255.0f; *pix++ = a*255.0f;
}
UpdateTex ();
}
bool nvImg::LoadPng ( char* fname )
{
std::vector<unsigned char> out;
unsigned int w, h;
app_printf ( "Reading PNG: %s\n", fname );
unsigned error = lodepng::decode ( out, w, h, fname );
if (error) {
app_printf ( "png read error: %s\n", lodepng_error_text(error) );
return false;
}
mXres = w;
mYres = h;
mSize = w*h*4;
mFmt = IMG_RGBA;
if ( mData != 0x0 ) free ( mData );
mData = (unsigned char*) malloc ( mSize );
memcpy ( mData, &out[0], mSize );
//memset ( mData, 128, mSize );
UpdateTex ();
return true;
}
bool nvImg::LoadTga ( char* fname )
{
app_printf ( "Reading TGA: %s\n", fname );
TGA* fontTGA = new TGA;
TGA::TGAError err = fontTGA->load(fname);
if (err != TGA::TGA_NO_ERROR) {
delete fontTGA;
return false;
}
mXres = fontTGA->m_nImageWidth;
mYres = fontTGA->m_nImageHeight;
mSize = mXres * mYres;
switch ( fontTGA->m_texFormat ) {
case TGA::RGB: mFmt = IMG_RGB; mSize *= 3; break;
case TGA::RGBA: mFmt = IMG_RGBA; mSize *= 4; break;
case TGA::ALPHA: mFmt = IMG_LUM; break;
}