-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path_GL2.i
2284 lines (1849 loc) · 73.6 KB
/
_GL2.i
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
// _GL2.i: definition & declaration for GL2.x, GLES2.x.
// Link to libGL.so or libGLESv2.so.
//
// SD 2014MAY20
//
// Project: OpENCview
/*
This file is part of the OpENCview project, a viewer of ENC.
Copyright (C) 2000-2018 Sylvain Duclos [email protected]
OpENCview is free software: you can redistribute it and/or modify
it under the terms of the Lesser GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpENCview 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
Lesser GNU General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with OpENCview. If not, see <http://www.gnu.org/licenses/>.
*/
//
// Note: if this code is included it mean that S52_USE_GL2 is allready defined
// to get GLES2 specific code (ex GLSL ES) define S52_USE_GLES2 also.
// Note: GL2 matrix stuff work with GL_DOUBLE normaly while GLES2 work only with GL_FLOAT
#include <strings.h> // bzero()
// Note: GLES2 is a subset of GL2, so declaration in GLES2 header cover all GL2 decl use in the code
#ifdef S52_USE_GLSC2
#include <GLSC2/glsc2.h>
#include <GLSC2/glsc2ext.h> //
#else // S52_USE_GLSC2
#ifdef S52_USE_GL2
#include <GLES2/gl2.h>
#define GL_GLEXT_PROTOTYPES
#include <GLES2/gl2ext.h> // glReadnPixelsEXT / KHR, glTexStorage2DEXT
#endif // S52_USE_GL2
#endif // S52_USE_GLSC2
typedef double GLdouble;
#include "tesselator.h" // will pull also: typedef void GLvoid;
typedef GLUtesselator GLUtesselatorObj;
typedef GLUtesselator GLUtriangulatorObj;
// hold copy of FrameBuffer
// Note: this is of no use in GL2 as tex stay in GPU
// only used when FB need to be dump to disk
static guint _fb_pixels_id = 0; // texture ID
static unsigned char *_fb_pixels = NULL;
static guint _fb_pixels_sz = 0;
static int _fb_pixels_udp = TRUE; // TRUE flag that the FB changed
#define _RGB 3
#define _RGBA 4
#ifdef S52_USE_ADRENO
static int _fb_pixels_format = _RGB; // alpha blending done in shader
#else
static int _fb_pixels_format = _RGBA;
//static int _fb_pixels_format = _RGB ; // Note: on TEGRA2 RGB (3) very slow
#endif
////////////////////////////////////////////////////////
// forward decl
static double _getWorldGridRef(S52_obj *, double *, double *, double *, double *, double *, double *);
static int _fillArea(S57_geo *);
static void _glMatrixMode(guint);
static void _glLoadIdentity(int);
static void _glUniformMatrix4fv_uModelview(void);
static int _glMatrixSet(VP);
static int _glMatrixDel(VP);
static int _pushScaletoPixel(int);
static int _popScaletoPixel(void);
static GLubyte _setFragAttrib(const S52_Color *, gboolean);
static void _glLineWidth(GLfloat);
static void _glPointSize(GLfloat);
static inline void _checkError(const char *);
static GLvoid _DrawArrays_LINE_STRIP(guint, vertex_t *);
static GLvoid _DrawArrays_LINES(guint npt, vertex_t *ppt);
static inline void _glGenBuffers(GLuint *);
static int _VBOCreate(S57_prim *);
static int _glCallList(S52_DListData *, gboolean);
// debug - i965
//static int _VBOCreate2_glCallList(S52_DListData *);
////////////////////////////////////////////////////////
// GL2 GPU Extension
// GL_OES_texture_npot:
// The npot extension for GLES2 is only about support of mipmaps and repeat/mirror wrap modes.
// If you don't care about mipmaps and use only the clamp wrap mode, you can use npot textures.
// It's part of the GLES2 spec.
static int _GL_OES_texture_npot = FALSE;
static int _GL_EXT_debug_marker = FALSE;
static int _GL_OES_point_sprite = FALSE;
static int _GL_EXT_robustness = FALSE;
static int _GL_KHR_no_error = FALSE;
// used to convert float to double for tesselator
static GArray *_tessWorkBuf_d = NULL;
// used to convert geo double to VBO float
static GArray *_tessWorkBuf_f = NULL;
// glsl main
static GLuint _programObject = 0;
// glsl uniform
static GLint _uProjection = 0;
static GLint _uModelview = 0;
static GLint _uColor = 0;
static GLint _uPointSize = 0;
static GLint _uSampler2d = 0;
//static GLint _uSampler2d1 = 0;
static GLint _uBlitOn = 0;
static GLint _uTextOn = 0; // textured line and text from freetype-gl
static GLint _uGlowOn = 0;
static GLint _uStipOn = 0;
//static GLint _uScaleOn = 0;
// test - optimisation
//static GLint _uPalOn = 0;
//static GLint _uPalArray = 0;
static GLint _uPattOn = 0;
static GLint _uPattGridX = 0;
static GLint _uPattGridY = 0;
static GLint _uPattW = 0;
static GLint _uPattH = 0;
// glsl varying
static GLint _aPosition = -2;
static GLint _aUV = -2;
static GLint _aAlpha = -2;
// alpha is 0.0 - 1.0
#define TRNSP_FAC_GLES2 0.25
//---- PATTERN GL2 / GLES2 -----------------------------------------------------------
//
// Note: 4 mask are drawn to fill the square made of 2 triangles (fan)
// Note: MSB 0x01, LSB 0xE0 - so it left most pixels is at 0x01 0x00
// and the right most pixel in a byte is at 0xE0
// 1 bit in _nodata_mask is 4 bytes (RGBA) in _rgba_nodata_mask (s0 x 8 bits x 4 )
/* FIXME:
+static cairo_always_inline cairo_bool_t
+_cairo_is_little_endian (void)
+{
+ static const int i = 1;
+ return *((char *) &i) == 0x01;
+}
+
*/
// NODATA03
static GLuint _nodata_mask_texID = 0;
static GLubyte _nodata_mask_rgba[4*32*8*4]; // 32 * 32 * 4 // 8bits * 4col * 32row * 4rgba
static const GLubyte _nodata_mask_bits[4*32] = {
// Note: indianness
0xFE, 0x00, 0x00, 0x00, // 1
0xFE, 0x00, 0x00, 0x00, // 2
0x00, 0x00, 0x00, 0x00, // 3
0x00, 0x00, 0x00, 0x00, // 4
0x00, 0x00, 0x00, 0x00, // 5
0x00, 0x00, 0x00, 0x00, // 6
0x00, 0x00, 0x00, 0x00, // 7
0x00, 0x00, 0xFE, 0x00, // 8
0x00, 0x00, 0xFE, 0x00, // 1
0x00, 0x00, 0x00, 0x00, // 2
0x00, 0x00, 0x00, 0x00, // 3
0x00, 0x00, 0x00, 0x00, // 4
0x00, 0x00, 0x00, 0x00, // 5
0x00, 0x00, 0x00, 0x00, // 6
0x00, 0x00, 0x00, 0x00, // 7
0xFE, 0x00, 0x00, 0x00, // 8
0xFE, 0x00, 0x00, 0x00, // 1
0x00, 0x00, 0x00, 0x00, // 2
0x00, 0x00, 0x00, 0x00, // 3
0x00, 0x00, 0x00, 0x00, // 4
0x00, 0x00, 0x00, 0x00, // 5
0x00, 0x00, 0x00, 0x00, // 6
0x00, 0x00, 0x00, 0x00, // 7
0x00, 0x00, 0xFE, 0x00, // 8
0x00, 0x00, 0xFE, 0x00, // 1
0x00, 0x00, 0x00, 0x00, // 2
0x00, 0x00, 0x00, 0x00, // 3
0x00, 0x00, 0x00, 0x00, // 4
0x00, 0x00, 0x00, 0x00, // 5
0x00, 0x00, 0x00, 0x00, // 6
0x00, 0x00, 0x00, 0x00, // 7
0x00, 0x00, 0x00, 0x00 // 8
};
// Line Style DOTT PAttern
// dotpitch 0.3: dott = 2 * 0.3, space = 4 * 0.3 (6 px wide, 32 / 6 = 5.333)
// 1100 0000 1100 0000 ...
// dotpitch 0.15: dott = 4 * 0.15, space = 8 * 0.3 (12 px wide, 32 / 12 = 2.666)
// llll 0000 0000 1111 0000 0000 1111 0000
static GLuint _dottpa_mask_texID = 0;
static GLubyte _dottpa_mask_rgba[8*4*4]; // 32 * 4rgba = 8bits * 4col * 1row * 4rgba
static const GLubyte _dottpa_mask_bits[4] = { // 4 x 8 bits = 32 bits
// Note: indianness
//0x03, 0x03, 0x03, 0x03
0x30, 0x30, 0x30, 0x30
};
// Line Style DASH pattern
static GLuint _dashpa_mask_texID = 0;
static GLubyte _dashpa_mask_rgba[8*4*4]; // 32 * 4rgba = 8bits * 4col * 1row * 4rgba
static const GLubyte _dashpa_mask_bits[4] = { // 4 x 8 bits = 32 bits
// Note: indianness
0xFF, 0xF0, 0xFF, 0xF0
//0xFF, 0xFF, 0xFF, 0xEF // --> right most pixel is OFF
};
// OVERSC01
// vertical line at each 400 units (4 mm)
// 0.15mm dotpitch then 27 pixels == 4mm (26.666..)
// 0.30mm dotpitch then 13 pixels == 4mm (13.333..)
//static GLuint _oversc_mask_texID = 0;
//static GLubyte _oversc_mask_rgba[8*4*4]; // 32 * 4rgba = 8bits * 4col * 1row * 4rgba
//static const GLubyte _oversc_mask_bits[16] = { // 4 x 8 bits = 32 bits
//static const GLubyte _oversc_mask_bits[4] = { // 4 x 8 bits = 32 bits
// 0x01, 0x00, 0x00, 0x00
//};
// other pattern are created using FBO
static GLuint _fboID = 0;
//---- PATTERN GL2 / GLES2 -----------------------------------------------------------
//static int _debugMatrix = 1;
#define MATRIX_STACK_MAX 8
// symbole not in GLES2 - but defined here to mimic GL2
#define GL_MODELVIEW 0x1700
#define GL_PROJECTION 0x1701
// QuadricDrawStyle
#define GLU_POINT 100010
#define GLU_LINE 100011
#define GLU_FILL 100012
#define GLU_SILHOUETTE 100013
// Boolean
#define GLU_TRUE 1
#define GLU_FALSE 0
// FIXME: current matrix dependency in GL2 code path need encapsulation - GL2ctx.crntMat
// capture GL2 ctx state in struct
//static GLenum _mode = GL_MODELVIEW; // GL_MODELVIEW (initial) or GL_PROJECTION
//static GLenum _mode = GL_PROJECTION; // GL_MODELVIEW (initial) or GL_PROJECTION
static GLfloat _mvm[MATRIX_STACK_MAX][16]; // modelview matrix
static GLfloat _pjm[MATRIX_STACK_MAX][16]; // projection matrix
static GLfloat *_crntMat; // point to active matrix
static int _mvmTop = 0; // point to stack top
static int _pjmTop = 0; // point to stack top
//static int _mvmTop = -1; // point to stack top
//static int _pjmTop = -1; // point to stack top
#include <wchar.h>
#include "vector.h"
#include "texture-atlas.h"
#include "texture-font.h"
static texture_atlas_t *_freetype_gl_atlas = NULL;
static texture_font_t *_freetype_gl_font[S52_MAX_FONT] = {0};
static const char *_freetype_gl_fontfilename =
#ifdef S52_USE_ANDROID
//"/system/fonts/Roboto-Regular.ttf" // not official, could change place
"/system/fonts/DroidSans.ttf"
#else
"./Roboto-Regular.ttf"
//"./Waree.ttf" // Thai glyph
//"../../ttf/13947.ttf" // Russian glyph
#endif
; // editor efte get confuse if ';' at the end of the string
typedef struct {
GLfloat x, y, z; // position
GLfloat s, t; // texture
} _freetype_gl_vertex_t;
// legend has no S52_obj, so this a place holder
static GLuint _freetype_gl_textureID = 0;
static GArray *_freetype_gl_buffer = NULL;
#define LF '\r' // Line Feed
#define TB '\t' // Tabulation
#define NL '\n' // New Line
static int _f2d(GArray *tessWorkBuf_d, guint npt, vertex_t *ppt)
// convert array of float (vertex_t with GLES2) to array of double
// as the tesselator work on double for OGR witch has coord. in double
{
g_array_set_size(tessWorkBuf_d, 0);
for (guint i=0; i<npt; i++) {
double d[3] = {ppt[0], ppt[1], 0.0}; // flush S57_OVERLAP_GEO_Z
g_array_append_val(tessWorkBuf_d, d);
ppt += 3;
}
return TRUE;
}
static int _d2f(GArray *tessWorkBuf_f, unsigned int npt, double *ppt)
// convert array of double to array of float, geo to VBO
// used by LS() command word - LC() line overlap info lost (S57_OVERLAP_GEO_Z)
{
g_array_set_size(tessWorkBuf_f, 0);
for (guint i=0; i<npt; ++i) {
// debug
//if (0.0 != ppt[2]) {
// PRINTF("DEBUG: S57_OVERLAP_GEO_Z found (%f)\n", ppt[2]);
// g_assert(0);
//}
float f[3] = {ppt[0], ppt[1], 0.0}; // flush S57_OVERLAP_GEO_Z
g_array_append_val(tessWorkBuf_f, f);
ppt += 3;
}
return TRUE;
}
static int _init_freetype_gl(void)
{
const wchar_t *cache = L" !\"#$%&'()*+,-./0123456789:;<=>?"
L"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
L"`abcdefghijklmnopqrstuvwxyz{|}~"
L"èàé";
_checkError("_init_freetype_gl() -beg-");
if (NULL == _freetype_gl_atlas) {
#ifdef S52_USE_GLSC2
_freetype_gl_atlas = texture_atlas_new(512, 512, 3); // GL_RGB in GLSC2 - code change in GLSL
#else
_freetype_gl_atlas = texture_atlas_new(512, 512, 1); // alpha only - not in GLSC2
#endif
} else {
PRINTF("WARNING: _init_freetype_gl() allready initialize\n");
g_assert(0);
return FALSE;
}
_checkError("_init_freetype_gl() -0-");
// FIXME: overkill!
if (NULL != _freetype_gl_font[0]) {
texture_font_delete(_freetype_gl_font[0]);
texture_font_delete(_freetype_gl_font[1]);
texture_font_delete(_freetype_gl_font[2]);
texture_font_delete(_freetype_gl_font[3]);
_freetype_gl_font[0] = NULL;
_freetype_gl_font[1] = NULL;
_freetype_gl_font[2] = NULL;
_freetype_gl_font[3] = NULL;
}
valueBuf TTFPath = {'\0'};
if (FALSE == S52_utils_getConfig(CFG_TTF, TTFPath)) {
PRINTF("NOTE: loading hard-coded TTF filename: %s\n", _freetype_gl_fontfilename);
} else {
if (TRUE == g_file_test(TTFPath, G_FILE_TEST_EXISTS)) {
_freetype_gl_fontfilename = TTFPath;
PRINTF("NOTE: loading TTF found in s52.cfg (%s)\n", TTFPath);
} else {
//FIXME: test this path
PRINTF("WARNING: no TTF found (no text)\n", TTFPath);
return FALSE;
}
}
// 10 points default
int basePtSz = 10 * (PICA / S52_MP_get(S52_MAR_DOTPITCH_MM_Y));
//PRINTF("DEBUG: basePtSz = %i, dotp mm=%f\n", basePtSz, _dotpitch_mm_y);
_freetype_gl_font[0] = texture_font_new(_freetype_gl_atlas, _freetype_gl_fontfilename, basePtSz + 0);
_freetype_gl_font[1] = texture_font_new(_freetype_gl_atlas, _freetype_gl_fontfilename, basePtSz + 6);
_freetype_gl_font[2] = texture_font_new(_freetype_gl_atlas, _freetype_gl_fontfilename, basePtSz + 12);
_freetype_gl_font[3] = texture_font_new(_freetype_gl_atlas, _freetype_gl_fontfilename, basePtSz + 18);
if (NULL == _freetype_gl_font[0]) {
PRINTF("WARNING: texture_font_new() failed\n");
g_assert(0);
return FALSE;
}
texture_font_load_glyphs(_freetype_gl_font[0], cache);
_checkError("_init_freetype_gl() -1-");
texture_font_load_glyphs(_freetype_gl_font[1], cache);
texture_font_load_glyphs(_freetype_gl_font[2], cache);
texture_font_load_glyphs(_freetype_gl_font[3], cache);
_checkError("_init_freetype_gl() -2-");
_glGenBuffers(&_freetype_gl_textureID);
if (NULL == _freetype_gl_buffer) {
_freetype_gl_buffer = g_array_new(FALSE, FALSE, sizeof(_freetype_gl_vertex_t));
}
return TRUE;
}
//static GArray *_fill_freetype_gl_buffer(GArray *ftglBuf, const char *str, unsigned int weight, double *strWpx, double *strHpx)
static GArray *_fill_freetype_gl_buffer(GArray *ftglBuf, const char *str, unsigned int bsize, double *strWpx, double *strHpx)
// fill buffer with triangles strip, W/H can be NULL
// experimental: smaller text size if second line
{
int pen_x = 0;
int pen_y = 0;
int nl = FALSE;
glong len = g_utf8_strlen(str, -1);
if (NULL!=strWpx && NULL!=strHpx) {
*strWpx = 0.0;
*strHpx = 0.0;
}
g_array_set_size(ftglBuf, 0);
for (glong i=0; i<len; ++i) {
gchar *utfc = g_utf8_offset_to_pointer(str, i);
gunichar unic = g_utf8_get_char(utfc);
//texture_glyph_t *glyph = texture_font_get_glyph(_freetype_gl_font[weight], unic);
texture_glyph_t *glyph = texture_font_get_glyph(_freetype_gl_font[bsize], unic);
if (NULL == glyph) {
PRINTF("DEBUG: NULL glyph %lc: \n", unic);
continue;
}
// experimental: smaller text size if second line
if (NL == unic) {
//weight = (0<weight) ? weight-1 : weight;
//texture_glyph_t *glyph = texture_font_get_glyph(_freetype_gl_font[weight], 'A');
bsize = (0<bsize) ? bsize-1 : bsize;
texture_glyph_t *glyph = texture_font_get_glyph(_freetype_gl_font[bsize], 'A');
pen_x = 0;
pen_y = (NULL!=glyph) ? -(glyph->height+5) : 10 ;
nl = TRUE;
continue;
}
// experimental: augmente kerning if second line
if (TRUE == nl) {
pen_x += texture_glyph_get_kerning(glyph, unic);
pen_x++;
}
GLfloat x0 = pen_x + glyph->offset_x;
GLfloat y0 = pen_y + glyph->offset_y;
GLfloat x1 = x0 + glyph->width;
GLfloat y1 = y0 - glyph->height; // Y is down, so flip glyph
//GLfloat y1 = y0 - (glyph->height+1); // Y is down, so flip glyph
// +1 check this, some device clip the top row
GLfloat s0 = glyph->s0;
GLfloat t0 = glyph->t0;
GLfloat s1 = glyph->s1;
GLfloat t1 = glyph->t1;
// debug
//PRINTF("CHAR: x0,y0,x1,y1: %lc: %f %f %f %f\n", str[i],x0,y0,x1,y1);
//PRINTF("CHAR: s0,t0,s1,t1: %lc: %f %f %f %f\n", str[i],s0,t0,s1,t1);
GLfloat z0 = 0.0;
_freetype_gl_vertex_t vertices[6] = {
{x0,y0,z0, s0,t0},
{x0,y1,z0, s0,t1},
{x1,y1,z0, s1,t1},
{x0,y0,z0, s0,t0},
{x1,y1,z0, s1,t1},
{x1,y0,z0, s1,t0}
};
ftglBuf = g_array_append_vals(ftglBuf, &vertices[0], 3);
ftglBuf = g_array_append_vals(ftglBuf, &vertices[3], 3);
pen_x += glyph->advance_x;
pen_y += glyph->advance_y;
// tally whole string size (what with NL)
if (NULL!=strWpx && NULL!=strHpx) {
*strWpx += glyph->width;
*strHpx = (*strHpx>glyph->height)? *strHpx : glyph->height;
}
}
//PRINTF("DEBUG: h/w px: %f %f\n", h_px, w_px);
return ftglBuf;
}
static int _justifyTXTPos(double strWpx, double strHpx, char hjust, char vjust, double *x, double *y)
// apply H/V justification to position X/Y
// Note: see doc at struct _Text in S52PL.c:150 for interpretation
{
double strWw = strWpx * _scalex;
double strHw = strHpx * _scaley;
double wx, wy; // width
double hx, hy; // height
// horizontal
switch(hjust) {
case '1': // CENTRE
wx = (cos(_view.north*DEG_TO_RAD) * strWw) / 2.0;
wy = (sin(_view.north*DEG_TO_RAD) * strWw) / 2.0;
break;
case '2': // RIGHT
wx = cos(_view.north*DEG_TO_RAD) * strWw;
wy = sin(_view.north*DEG_TO_RAD) * strWw;
break;
case '3': // LEFT
wx = 0.0;
wy = 0.0;
break;
default:
PRINTF("DEBUG: invalid hjust!\n");
wx = 0.0;
wy = 0.0;
g_assert(0);
}
// vertical
switch(vjust) {
case '1': // BOTTOM
hx = 0.0;
hy = 0.0;
break;
case '2': // CENTRE
hx = (cos(_view.north*DEG_TO_RAD) * strHw) / 2.0;
hy = (sin(_view.north*DEG_TO_RAD) * strHw) / 2.0;
break;
case '3': // TOP
hx = cos(_view.north*DEG_TO_RAD) * strHw;
hy = sin(_view.north*DEG_TO_RAD) * strHw;
break;
default:
PRINTF("DEBUG: invalid vjust!\n");
hx = 0.0;
hy = 0.0;
g_assert(0);
}
//PRINTF("DEBUG: px str W/H: %f/%f scl X/Y: %f/%f\n", strWpx, strHpx, _scalex, _scaley);
//PRINTF("DEBUG: w str W/H: %f/%f pos X/Y: %f/%f\n", strWw, strHw, *x, *y);
// FIXME: do the math
*x += +hx - wx;
*y += -hy + wy;
//*y += hy - wy;
return TRUE;
}
//-----------------------------------------
//
// gles2 float Matrix stuff (by hand)
//
static void __make_z_rot_matrix(GLfloat angle, GLfloat *m)
// called only by _glRotated()
{
float c = cos(angle * M_PI / 180.0);
float s = sin(angle * M_PI / 180.0);
//m[10] = m[15] = 1.0f;
m[0] = c;
m[1] = s;
m[4] = -s;
m[5] = c;
m[10] = 1.0f;
m[15] = 1.0f;
}
static void __make_scale_matrix(GLfloat xs, GLfloat ys, GLfloat zs, GLfloat *m)
// called only by _glScaled()
{
// caller allready done that
//memset(m, 0, sizeof(GLfloat) * 16);
m[0] = xs;
m[5] = ys;
m[10] = zs;
m[15] = 1.0f;
}
static void __gluMultMatrixVecf(const GLfloat matrix[16], const GLfloat in[4], GLfloat out[4])
// called by _gluProject() / _gluUnProject()
{
for (guint i=0; i<4; i++) {
out[i] = in[0] * matrix[0*4+i] +
in[1] * matrix[1*4+i] +
in[2] * matrix[2*4+i] +
in[3] * matrix[3*4+i];
}
}
static GLint _gluProject(GLfloat objx, GLfloat objy, GLfloat objz,
const GLfloat modelMatrix[16],
const GLfloat projMatrix[16],
const GLint viewport[4],
GLfloat* winx, GLfloat* winy, GLfloat* winz)
{
GLfloat in [4] = {objx, objy, objz, 1.0f};
GLfloat out[4] = {0.0f};
__gluMultMatrixVecf(modelMatrix, in, out);
__gluMultMatrixVecf(projMatrix, out, in);
if (0.0f == in[3])
return GL_FALSE;
in[0] /= in[3];
in[1] /= in[3];
in[2] /= in[3];
// Map x, y and z to range 0-1
in[0] = in[0] * 0.5f + 0.5f;
in[1] = in[1] * 0.5f + 0.5f;
in[2] = in[2] * 0.5f + 0.5f;
// Map x,y to viewport
in[0] = in[0] * viewport[2] + viewport[0];
in[1] = in[1] * viewport[3] + viewport[1];
*winx = in[0];
*winy = in[1];
*winz = in[2];
return GL_TRUE;
}
static int __gluInvertMatrixf(const GLfloat m[16], GLfloat invOut[16])
// called only by _gluUnProject()
{
GLfloat inv[16] = {0.0f};
GLfloat det;
inv[0] = m[5]*m[10]*m[15] - m[5] *m[11]*m[14] - m[9] *m[6]*m[15]
+ m[9]*m[7] *m[14] + m[13]*m[6] *m[11] - m[13]*m[7]*m[10];
inv[4] = - m[4]*m[10]*m[15] + m[4] *m[11]*m[14] + m[8] *m[6]*m[15]
- m[8]*m[7] *m[14] - m[12]*m[6] *m[11] + m[12]*m[7]*m[10];
inv[8] = m[4]*m[9] *m[15] - m[4] *m[11]*m[13] - m[8] *m[5]*m[15]
+ m[8]*m[7] *m[13] + m[12]*m[5] *m[11] - m[12]*m[7]*m[9];
inv[12]= - m[4]*m[9] *m[14] + m[4] *m[10]*m[13] + m[8] *m[5]*m[14]
- m[8]*m[6] *m[13] - m[12]*m[5] *m[10] + m[12]*m[6]*m[9];
inv[1] = - m[1]*m[10]*m[15] + m[1] *m[11]*m[14] + m[9] *m[2]*m[15]
- m[9]*m[3] *m[14] - m[13]*m[2] *m[11] + m[13]*m[3]*m[10];
inv[5] = m[0]*m[10]*m[15] - m[0] *m[11]*m[14] - m[8] *m[2]*m[15]
+ m[8]*m[3] *m[14] + m[12]*m[2] *m[11] - m[12]*m[3]*m[10];
inv[9] = - m[0]*m[9] *m[15] + m[0] *m[11]*m[13] + m[8] *m[1]*m[15]
- m[8]*m[3] *m[13] - m[12]*m[1] *m[11] + m[12]*m[3]*m[9];
inv[13]= m[0]*m[9] *m[14] - m[0] *m[10]*m[13] - m[8] *m[1]*m[14]
+ m[8]*m[2] *m[13] + m[12]*m[1] *m[10] - m[12]*m[2]*m[9];
inv[2] = m[1]*m[6] *m[15] - m[1] *m[7] *m[14] - m[5] *m[2]*m[15]
+ m[5]*m[3] *m[14] + m[13]*m[2] *m[7] - m[13]*m[3]*m[6];
inv[6] = - m[0]*m[6] *m[15] + m[0] *m[7] *m[14] + m[4] *m[2]*m[15]
- m[4]*m[3] *m[14] - m[12]*m[2] *m[7] + m[12]*m[3]*m[6];
inv[10]= m[0]*m[5] *m[15] - m[0] *m[7] *m[13] - m[4] *m[1]*m[15]
+ m[4]*m[3] *m[13] + m[12]*m[1] *m[7] - m[12]*m[3]*m[5];
inv[14]= - m[0]*m[5] *m[14] + m[0] *m[6] *m[13] + m[4] *m[1]*m[14]
- m[4]*m[2] *m[13] - m[12]*m[1] *m[6] + m[12]*m[2]*m[5];
inv[3] = - m[1]*m[6] *m[11] + m[1] *m[7] *m[10] + m[5] *m[2]*m[11]
- m[5]*m[3] *m[10] - m[9] *m[2] *m[7] + m[9] *m[3]*m[6];
inv[7] = m[0]*m[6] *m[11] - m[0] *m[7] *m[10] - m[4] *m[2]*m[11]
+ m[4]*m[3] *m[10] + m[8] *m[2] *m[7] - m[8] *m[3]*m[6];
inv[11]= - m[0]*m[5] *m[11] + m[0] *m[7] *m[9] + m[4] *m[1]*m[11]
- m[4]*m[3] *m[9] - m[8] *m[1] *m[7] + m[8] *m[3]*m[5];
inv[15]= m[0]*m[5] *m[10] - m[0] *m[6] *m[9] - m[4] *m[1]*m[10]
+ m[4]*m[2] *m[9] + m[8] *m[1] *m[6] - m[8] *m[2]*m[5];
det = m[0]*inv[0] + m[1]*inv[4] + m[2]*inv[8] + m[3]*inv[12];
if (0.0f == det) {
PRINTF("WARNING: det = 0, fail\n");
return GL_FALSE;
}
det=1.0f/det;
for (int i = 0; i < 16; i++)
invOut[i] = inv[i] * det;
return GL_TRUE;
}
static void __gluMultMatricesf(const GLfloat a[16], const GLfloat b[16], GLfloat r[16])
// called only by _gluUnProject()
{
for (guint i=0; i<4; ++i) {
for (guint j=0; j<4; ++j) {
r[i*4+j] = a[i*4+0] * b[0*4+j] +
a[i*4+1] * b[1*4+j] +
a[i*4+2] * b[2*4+j] +
a[i*4+3] * b[3*4+j];
}
}
}
static GLint _gluUnProject(GLfloat winx, GLfloat winy, GLfloat winz,
const GLfloat modelMatrix[16],
const GLfloat projMatrix[16],
const GLint viewport[4],
GLfloat* objx, GLfloat* objy, GLfloat* objz)
{
GLfloat finalMatrix[16] = {0.0f};
GLfloat in [4];
GLfloat out[4];
__gluMultMatricesf(modelMatrix, projMatrix, finalMatrix);
if (GL_FALSE == __gluInvertMatrixf(finalMatrix, finalMatrix)) {
PRINTF("WARNING: __gluInvertMatrixf() fail\n");
g_assert(0);
return GL_FALSE;
}
in[0] = winx;
in[1] = winy;
in[2] = winz;
in[3] = 1.0f;
// Map x and y from window coordinates
in[0] = (in[0] - viewport[0]) / viewport[2];
in[1] = (in[1] - viewport[1]) / viewport[3];
// Map to range -1 to 1
in[0] = in[0] * 2 - 1;
in[1] = in[1] * 2 - 1;
in[2] = in[2] * 2 - 1;
__gluMultMatrixVecf(finalMatrix, in, out);
if (0.0f == out[3]) {
PRINTF("WARNING: __gluMultMatrixVecf() fail [out[3]=0]\n");
g_assert(0);
return GL_FALSE;
}
out[0] /= out[3];
out[1] /= out[3];
out[2] /= out[3];
*objx = out[0];
*objy = out[1];
*objz = out[2];
return GL_TRUE;
}
static void _multiply(GLfloat *m, GLfloat *n)
{
GLfloat tmp[16] = {0.0f};
for (guint i = 0; i < 16; i++) {
div_t d = div(i, 4);
GLfloat *row = n + d.quot * 4;
GLfloat *column = m + d.rem;
for (int j = 0; j < 4; j++)
tmp[i] += row[j] * column[j * 4];
}
memcpy(m, &tmp, sizeof tmp);
}
// gles2 float Matrix stuff (by hand)
//-----------------------------------------
static void _glTranslated(double x, double y, double z)
{
GLfloat t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1 };
//GLfloat t[16] = {1, (GLfloat) x, (GLfloat) y, (GLfloat) z, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
_multiply(_crntMat, t);
// optimisation - reset flag
//if (GL_MODELVIEW == _mode)
// _identity_MODELVIEW = FALSE;
return;
}
static void _glScaled(double x, double y, double z)
{
GLfloat m[16] = {0.0f};
__make_scale_matrix((GLfloat) x, (GLfloat) y, (GLfloat) z, m);
_multiply(_crntMat, m);
// optimisation - reset flag
//if (GL_MODELVIEW == _mode)
// _identity_MODELVIEW = FALSE;
return;
}
static void _glRotated(double angle, double x, double y, double z)
// rotate on Z
{
GLfloat m[16] = {0.0f};
// FIXME: handle only 0.0==x 0.0==y 1.0==z
// silence warning for now
(void)x;
(void)y;
(void)z;
__make_z_rot_matrix((GLfloat) angle, m);
_multiply(_crntMat, m);
// optimisation - reset flag
//if (GL_MODELVIEW == _mode)
// _identity_MODELVIEW = FALSE;
return;
}
static int _renderTXTAA_gl2(double x, double y, GLfloat *data, guint len)
// render VBO static text (ie no data) or dynamic text
{
// FIXME: abort if no ttf (id check _freetype_gl_atlas->id)
// Note: static text could also come from MIO's (ie S52_GL_LAST cycle)
if ((NULL == data) &&
((S52_GL_DRAW==_crnt_GL_cycle) || (S52_GL_LAST==_crnt_GL_cycle))) {
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
glEnableVertexAttribArray(_aPosition);
glVertexAttribPointer (_aPosition, 3, GL_FLOAT, GL_FALSE, sizeof(_freetype_gl_vertex_t), BUFFER_OFFSET(0));
glEnableVertexAttribArray(_aUV);
glVertexAttribPointer (_aUV, 2, GL_FLOAT, GL_FALSE, sizeof(_freetype_gl_vertex_t), BUFFER_OFFSET(sizeof(float)*3));
} else {
// connect ftgl buffer coord data to GPU
glEnableVertexAttribArray(_aPosition);
glVertexAttribPointer (_aPosition, 3, GL_FLOAT, GL_FALSE, sizeof(_freetype_gl_vertex_t), data+0);
glEnableVertexAttribArray(_aUV);
glVertexAttribPointer (_aUV, 2, GL_FLOAT, GL_FALSE, sizeof(_freetype_gl_vertex_t), data+3);
}
// turn ON 'sampler2d'
//glUniform1f(_uTextOn, 1.0);
glUniform1i(_uTextOn, 1);
glBindTexture(GL_TEXTURE_2D, _freetype_gl_atlas->id);
_glLoadIdentity(GL_MODELVIEW);
_glTranslated(x, y, 0.0);
// FIXME: rot then scale
_pushScaletoPixel(FALSE);
// horizontal text
_glRotated(-_view.north, 0.0, 0.0, 1.0);
glUniformMatrix4fv(_uModelview, 1, GL_FALSE, _mvm[_mvmTop]);
glDrawArrays(GL_TRIANGLES, 0, len);
_popScaletoPixel();
glBindTexture(GL_TEXTURE_2D, 0);
//glUniform1f(_uTextOn, 0.0);
glUniform1i(_uTextOn, 0);
// disconnect buffer
glDisableVertexAttribArray(_aUV);
glDisableVertexAttribArray(_aPosition);
_checkError("_renderTXTAA_gl2() freetype-gl");
return TRUE;
}
typedef unsigned char u8;
#if !defined(S52_USE_ANDROID)
#ifdef S52_USE_GLSC2
typedef void (GL_APIENTRYP PFNGLREADNPIXELSKHRPROC) (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei bufSize, void *data);
static PFNGLREADNPIXELSKHRPROC _glReadnPixels = NULL;
//typedef void (GL_APIENTRYP PFNGLTEXTURESTORAGE2DEXTPROC) (GLuint texture, GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height);
static PFNGLTEXSTORAGE2DEXTPROC _glTexStorage2DEXT = NULL;
#else // S52_USE_GLSC2
// Note: on Intel GL_PROGRAM_BINARY_LENGTH_OES is 0 - bailout
// https://bugs.freedesktop.org/show_bug.cgi?id=87516
// FIXME: trigger reset to test this call
//static PFNGLGETGRAPHICSRESETSTATUSEXTPROC _glGetGraphicsResetStatus = NULL;
//static PFNGLREADNPIXELSEXTPROC _glReadnPixels = NULL; // EXT fail with mesa-git/master(2016AUG28)
static PFNGLPROGRAMPARAMETERIEXTPROC _glProgramParameteriEXT = NULL;
static PFNGLGETPROGRAMBINARYOESPROC _glGetProgramBinaryOES = NULL;
static PFNGLPROGRAMBINARYOESPROC _glProgramBinaryOES = NULL;
static PFNGLTEXSTORAGE2DEXTPROC _glTexStorage2DEXT = NULL;
#endif // S52_USE_GLSC2
#ifdef S52_USE_EGL
static int _loadProcEXT()
{
typedef void (*proc)(void);
extern proc eglGetProcAddress(const char *procname);
#ifdef S52_USE_GLSC2
_glGetGraphicsResetStatus = (PFNGLGETGRAPHICSRESETSTATUSEXTPROC) eglGetProcAddress("glGetGraphicsResetStatusEXT");
PRINTF("DEBUG: eglGetProcAddress(glGetGraphicsResetStatusEXT) %s\n", (NULL==_glGetGraphicsResetStatus)?"FAILED":"OK");
if (NULL == _glGetGraphicsResetStatus) {
_glGetGraphicsResetStatus = (PFNGLGETGRAPHICSRESETSTATUSKHRPROC) eglGetProcAddress("glGetGraphicsResetStatusKHR");
PRINTF("DEBUG: eglGetProcAddress(glGetGraphicsResetStatusKHR) %s\n", (NULL==_glGetGraphicsResetStatus)?"FAILED":"OK");
}
_glReadnPixels = (PFNGLREADNPIXELSKHRPROC) eglGetProcAddress("glReadnPixelsKHR");
PRINTF("DEBUG: eglGetProcAddress(glReadnPixelsKHR) %s\n", (NULL==_glReadnPixelsKHR)?"FAILED":"OK");
_glTexStorage2DEXT = (PFNGLTEXSTORAGE2DEXTPROC) eglGetProcAddress("glTexStorage2DEXT");
PRINTF("DEBUG: eglGetProcAddress(glTexStorage2DEXT) %s\n", (NULL==_glTexStorage2DEXT)?"FAILED":"OK");
#else
/* need gles2.h 20180316
_glGetGraphicsResetStatus = (PFNGLGETGRAPHICSRESETSTATUSEXTPROC) eglGetProcAddress("glGetGraphicsResetStatusEXT");
PRINTF("DEBUG: eglGetProcAddress(glGetGraphicsResetStatusEXT) %s\n", (NULL==_glGetGraphicsResetStatus)?"FAILED":"OK");
if (NULL == _glGetGraphicsResetStatus) {
_glGetGraphicsResetStatus = (PFNGLGETGRAPHICSRESETSTATUSKHRPROC) eglGetProcAddress("glGetGraphicsResetStatusKHR");
PRINTF("DEBUG: eglGetProcAddress(glGetGraphicsResetStatusKHR) %s\n", (NULL==_glGetGraphicsResetStatus)?"FAILED":"OK");
}
_glReadnPixels = (PFNGLREADNPIXELSEXTPROC) eglGetProcAddress("glReadnPixels");
PRINTF("DEBUG: eglGetProcAddress(glReadnPixels) %s\n", (NULL==_glReadnPixels)?"FAILED":"OK");
if (NULL == _glReadnPixels) {
_glReadnPixels = (PFNGLREADNPIXELSEXTPROC) eglGetProcAddress("glReadnPixelsEXT");
PRINTF("DEBUG: eglGetProcAddress(glReadnPixelsEXT) %s\n", (NULL==_glReadnPixels)?"FAILED":"OK");
}
if (NULL == _glReadnPixels) {
_glReadnPixels = (PFNGLREADNPIXELSKHRPROC) eglGetProcAddress("glReadnPixelsKHR");
PRINTF("DEBUG: eglGetProcAddress(glReadnPixelsKHR) %s\n", (NULL==_glReadnPixels)?"FAILED":"OK");
}
*/
_glProgramParameteriEXT = (PFNGLPROGRAMPARAMETERIEXTPROC)eglGetProcAddress("glProgramParameteriEXT");
PRINTF("DEBUG: eglGetProcAddress(glProgramParameteriEXT) %s\n", (NULL==_glProgramParameteriEXT)?"FAILED":"OK");
_glGetProgramBinaryOES = (PFNGLGETPROGRAMBINARYOESPROC) eglGetProcAddress("glGetProgramBinaryOES");
PRINTF("DEBUG: eglGetProcAddress(glGetProgramBinaryOES) %s\n", (NULL==_glGetProgramBinaryOES)?"FAILED":"OK");
_glProgramBinaryOES = (PFNGLPROGRAMBINARYOESPROC) eglGetProcAddress("glProgramBinaryOES");
PRINTF("DEBUG: eglGetProcAddress(glProgramBinaryOES) %s\n", (NULL==_glProgramBinaryOES)?"FAILED":"OK");
_glTexStorage2DEXT = (PFNGLTEXSTORAGE2DEXTPROC) eglGetProcAddress("glTexStorage2DEXT");
PRINTF("DEBUG: eglGetProcAddress(glTexStorage2DEXT) %s\n", (NULL==_glTexStorage2DEXT)?"FAILED":"OK");
#endif
return TRUE;
}
#endif // S52_USE_EGL
#if !defined(S52_USE_GLSC2)
static int _saveShaderBin(GLuint programObject)
// Save a GLSL shader bin into a file
{
//FIXME: no OES
// get the blob
GLint nFormats = 0;
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES, &nFormats);
if (0 != nFormats) {
//GLint formats[nFormats];
GLenum formats[nFormats];
glGetIntegerv(GL_PROGRAM_BINARY_FORMATS_OES, (GLint*)formats);
} else {
PRINTF("DEBUG: GL_NUM_PROGRAM_BINARY_FORMATS_OES failed nFormats=%i\n", nFormats);
}
GLsizei bufsize = 0;
glGetProgramiv(programObject, GL_PROGRAM_BINARY_LENGTH_OES, &bufsize);
_checkError("_saveShaderBin() -1-");
if (0 == bufsize) {
PRINTF("DEBUG: GL_PROGRAM_BINARY_LENGTH_OES failed [bufsize=%i]\n", bufsize);
//g_assert(0);
return FALSE;
}
GLsizei lenOut = 0;
u8 binary[bufsize];
if (NULL != _glGetProgramBinaryOES) {