forked from jojo61/vdr-plugin-softhdcuvid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenglosd.cpp
2184 lines (1836 loc) · 64.2 KB
/
openglosd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define __STL_CONFIG_H
#include "openglosd.h"
#include <algorithm>
/****************************************************************************************
* Helpers
****************************************************************************************/
void ConvertColor(const GLint &colARGB, glm::vec4 &col) {
col.a = ((colARGB & 0xFF000000) >> 24) / 255.0;
col.r = ((colARGB & 0x00FF0000) >> 16) / 255.0;
col.g = ((colARGB & 0x0000FF00) >> 8) / 255.0;
col.b = ((colARGB & 0x000000FF)) / 255.0;
}
/****************************************************************************************
* cShader
****************************************************************************************/
#ifdef CUVID
const char *glversion = "#version 330 core ";
#else
const char *glversion = "#version 300 es ";
#endif
const char *rectVertexShader = "%s\n \
\
layout (location = 0) in vec2 position; \
out vec4 rectCol; \
uniform vec4 inColor; \
uniform mat4 projection; \
\
void main() \
{ \
gl_Position = projection * vec4(position.x, position.y, 0.0, 1.0); \
rectCol = inColor; \
} \
";
const char *rectFragmentShader = "%s\n \
\
precision mediump float; \
in vec4 rectCol; \
out vec4 color; \
\
void main() \
{ \
color = rectCol; \
} \
";
const char *textureVertexShader = "%s\n \
\
layout (location = 0) in vec2 position; \
layout (location = 1) in vec2 texCoords; \
\
out vec2 TexCoords; \
out vec4 alphaValue;\
\
uniform mat4 projection; \
uniform vec4 alpha; \
\
void main() \
{ \
gl_Position = projection * vec4(position.x, position.y, 0.0, 1.0); \
TexCoords = texCoords; \
alphaValue = alpha; \
} \
";
const char *textureFragmentShader = "%s\n \
precision mediump float; \
in vec2 TexCoords; \
in vec4 alphaValue; \
out vec4 color; \
\
uniform sampler2D screenTexture; \
\
void main() \
{ \
color = texture(screenTexture, TexCoords) * alphaValue; \
} \
";
const char *textVertexShader = "%s\n \
\
layout (location = 0) in vec2 position; \
layout (location = 1) in vec2 texCoords; \
\
out vec2 TexCoords; \
out vec4 textColor; \
\
uniform mat4 projection; \
uniform vec4 inColor; \
\
void main() \
{ \
gl_Position = projection * vec4(position.x, position.y, 0.0, 1.0); \
TexCoords = texCoords; \
textColor = inColor; \
} \
";
const char *textFragmentShader = "%s\n \
precision mediump float; \
in vec2 TexCoords; \
in vec4 textColor; \
\
out vec4 color; \
\
uniform sampler2D glyphTexture; \
\
void main() \
{ \
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(glyphTexture, TexCoords).r); \
color = textColor * sampled; \
} \
";
///
/// GLX check error.
///
#define GlxCheck(void) \
{ \
GLenum err; \
\
if ((err = glGetError()) != GL_NO_ERROR) { \
esyslog("video/glx: error %s:%d %d '%s'\n", __FILE__, __LINE__, err, gluErrorString(err)); \
} \
}
static cShader *Shaders[stCount];
void cShader::Use(void) { glUseProgram(id); }
bool cShader::Load(eShaderType type) {
this->type = type;
const char *vertexCode = NULL;
const char *fragmentCode = NULL;
switch (type) {
case stRect:
vertexCode = rectVertexShader;
fragmentCode = rectFragmentShader;
break;
case stTexture:
vertexCode = textureVertexShader;
fragmentCode = textureFragmentShader;
break;
case stText:
vertexCode = textVertexShader;
fragmentCode = textFragmentShader;
break;
default:
esyslog("[softhddev]unknown shader type\n");
break;
}
if (vertexCode == NULL || fragmentCode == NULL) {
esyslog("[softhddev]ERROR reading shader\n");
return false;
}
if (!Compile(vertexCode, fragmentCode)) {
esyslog("[softhddev]ERROR compiling shader\n");
return false;
}
return true;
}
void cShader::SetFloat(const GLchar *name, GLfloat value) { glUniform1f(glGetUniformLocation(id, name), value); }
void cShader::SetInteger(const GLchar *name, GLint value) { glUniform1i(glGetUniformLocation(id, name), value); }
void cShader::SetVector2f(const GLchar *name, GLfloat x, GLfloat y) {
glUniform2f(glGetUniformLocation(id, name), x, y);
}
void cShader::SetVector3f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z) {
glUniform3f(glGetUniformLocation(id, name), x, y, z);
}
void cShader::SetVector4f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
glUniform4f(glGetUniformLocation(id, name), x, y, z, w);
}
void cShader::SetMatrix4(const GLchar *name, const glm::mat4 &matrix) {
glUniformMatrix4fv(glGetUniformLocation(id, name), 1, GL_FALSE, glm::value_ptr(matrix));
}
bool cShader::Compile(const char *vertexCode, const char *fragmentCode) {
GLuint sVertex, sFragment;
char *buffer = (char *)malloc(1000);
// Vertex Shader
sVertex = glCreateShader(GL_VERTEX_SHADER);
sprintf(buffer, vertexCode, glversion);
glShaderSource(sVertex, 1, (const GLchar **)&buffer, NULL);
glCompileShader(sVertex);
// esyslog("[softhddev]:SHADER:VERTEX %s\n",vertexCode);
if (!CheckCompileErrors(sVertex)) {
free(buffer);
return false;
}
// Fragment Shader
sFragment = glCreateShader(GL_FRAGMENT_SHADER);
sprintf(buffer, fragmentCode, glversion);
glShaderSource(sFragment, 1, (const GLchar **)&buffer, NULL);
glCompileShader(sFragment);
// esyslog("[softhddev]:SHADER:FRAGMENT %s\n",fragmentCode);
if (!CheckCompileErrors(sFragment)) {
free(buffer);
return false;
}
// link Program
id = glCreateProgram();
glAttachShader(id, sVertex);
glAttachShader(id, sFragment);
glLinkProgram(id);
if (!CheckCompileErrors(id, true))
return false;
// Delete the shaders as they're linked into our program now and no longer
// necessery
glDeleteShader(sVertex);
glDeleteShader(sFragment);
free(buffer);
return true;
}
bool cShader::CheckCompileErrors(GLuint object, bool program) {
GLint success;
GLchar infoLog[1024];
if (!program) {
glGetShaderiv(object, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(object, 1024, NULL, infoLog);
esyslog("\n[softhddev]:SHADER: Compile-time error: Type: %d - \n>%s<\n", type, infoLog);
return false;
}
} else {
glGetProgramiv(object, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(object, 1024, NULL, infoLog);
esyslog("[softhddev]:SHADER: Link-time error: Type: %d - \n>%s<\n", type, infoLog);
return false;
}
}
return true;
}
#define KERNING_UNKNOWN (-10000)
/****************************************************************************************
* cOglGlyph
****************************************************************************************/
cOglGlyph::cOglGlyph(FT_ULong charCode, FT_BitmapGlyph ftGlyph) {
this->charCode = charCode;
bearingLeft = ftGlyph->left;
bearingTop = ftGlyph->top;
width = ftGlyph->bitmap.width;
height = ftGlyph->bitmap.rows;
advanceX = ftGlyph->root.advance.x >> 16; // value in 1/2^16 pixel
LoadTexture(ftGlyph);
}
cOglGlyph::~cOglGlyph(void) {}
int cOglGlyph::GetKerningCache(FT_ULong prevSym) {
for (int i = kerningCache.Size(); --i > 0;) {
if (kerningCache[i].prevSym == prevSym)
return kerningCache[i].kerning;
}
return KERNING_UNKNOWN;
}
void cOglGlyph::SetKerningCache(FT_ULong prevSym, int kerning) { kerningCache.Append(tKerning(prevSym, kerning)); }
void cOglGlyph::BindTexture(void) { glBindTexture(GL_TEXTURE_2D, texture); }
void cOglGlyph::LoadTexture(FT_BitmapGlyph ftGlyph) {
// Disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, ftGlyph->bitmap.width, ftGlyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
ftGlyph->bitmap.buffer);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
extern "C" void GlxInitopengl();
extern "C" void GlxDrawopengl();
extern "C" void GlxDestroy();
extern "C" void makejpg(uint8_t *data, int width, int height);
/****************************************************************************************
* cOglFont
****************************************************************************************/
FT_Library cOglFont::ftLib = 0;
cList<cOglFont> *cOglFont::fonts = 0;
bool cOglFont::initiated = false;
cOglFont::cOglFont(const char *fontName, int charHeight) : name(fontName) {
size = charHeight;
height = 0;
bottom = 0;
int error = FT_New_Face(ftLib, fontName, 0, &face);
if (error)
esyslog("[softhddev]ERROR: failed to open %s!", *name);
FT_Set_Char_Size(face, 0, charHeight * 64, 0, 0);
height = (face->size->metrics.ascender - face->size->metrics.descender + 63) / 64;
bottom = abs((face->size->metrics.descender - 63) / 64);
}
cOglFont::~cOglFont(void) { FT_Done_Face(face); }
cOglFont *cOglFont::Get(const char *name, int charHeight) {
if (!fonts)
Init();
cOglFont *font;
for (font = fonts->First(); font; font = fonts->Next(font))
if (!strcmp(font->Name(), name) && charHeight == font->Size()) {
return font;
}
font = new cOglFont(name, charHeight);
fonts->Add(font);
return font;
}
void cOglFont::Init(void) {
if (FT_Init_FreeType(&ftLib)) {
esyslog("[softhddev]failed to initialize FreeType library!");
return;
}
fonts = new cList<cOglFont>;
initiated = true;
}
void cOglFont::Cleanup(void) {
if (!initiated)
return;
delete fonts;
fonts = 0;
if (ftLib) {
if (FT_Done_FreeType(ftLib))
esyslog("failed to deinitialize FreeType library!");
}
ftLib = 0;
}
cOglGlyph *cOglFont::Glyph(FT_ULong charCode) const {
// Non-breaking space:
if (charCode == 0xA0)
charCode = 0x20;
// Lookup in cache:
for (cOglGlyph *g = glyphCache.First(); g; g = glyphCache.Next(g)) {
if (g->CharCode() == charCode) {
return g;
}
}
FT_UInt glyph_index = FT_Get_Char_Index(face, charCode);
FT_Int32 loadFlags = FT_LOAD_NO_BITMAP;
// Load glyph image into the slot (erase previous one):
int error = FT_Load_Glyph(face, glyph_index, loadFlags);
if (error) {
esyslog("[softhddev]FT_Error (0x%02x) : %s\n", FT_Errors[error].code, FT_Errors[error].message);
return NULL;
}
FT_Glyph ftGlyph;
FT_Stroker stroker;
error = FT_Stroker_New(ftLib, &stroker);
if (error) {
esyslog("[softhddev]FT_Stroker_New FT_Error (0x%02x) : %s\n", FT_Errors[error].code, FT_Errors[error].message);
return NULL;
}
float outlineWidth = 0.25f;
FT_Stroker_Set(stroker, (int)(outlineWidth * 64), FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
error = FT_Get_Glyph(face->glyph, &ftGlyph);
if (error) {
esyslog("[softhddev]FT_Get_Glyph FT_Error (0x%02x) : %s\n", FT_Errors[error].code, FT_Errors[error].message);
return NULL;
}
error = FT_Glyph_StrokeBorder(&ftGlyph, stroker, 0, 1);
if (error) {
esyslog("[softhddev]FT_Glyph_StrokeBorder FT_Error (0x%02x) : %s\n", FT_Errors[error].code,
FT_Errors[error].message);
return NULL;
}
FT_Stroker_Done(stroker);
error = FT_Glyph_To_Bitmap(&ftGlyph, FT_RENDER_MODE_NORMAL, 0, 1);
if (error) {
esyslog("[softhddev]FT_Glyph_To_Bitmap FT_Error (0x%02x) : %s\n", FT_Errors[error].code,
FT_Errors[error].message);
return NULL;
}
cOglGlyph *Glyph = new cOglGlyph(charCode, (FT_BitmapGlyph)ftGlyph);
glyphCache.Add(Glyph);
FT_Done_Glyph(ftGlyph);
return Glyph;
}
int cOglFont::Kerning(cOglGlyph *glyph, FT_ULong prevSym) const {
int kerning = 0;
if (glyph && prevSym) {
kerning = glyph->GetKerningCache(prevSym);
if (kerning == KERNING_UNKNOWN) {
FT_Vector delta;
FT_UInt glyph_index = FT_Get_Char_Index(face, glyph->CharCode());
FT_UInt glyph_index_prev = FT_Get_Char_Index(face, prevSym);
FT_Get_Kerning(face, glyph_index_prev, glyph_index, FT_KERNING_DEFAULT, &delta);
kerning = delta.x / 64;
glyph->SetKerningCache(prevSym, kerning);
}
}
return kerning;
}
/****************************************************************************************
* cOglFb
****************************************************************************************/
cOglFb::cOglFb(GLint width, GLint height, GLint viewPortWidth, GLint viewPortHeight) {
initiated = false;
fb = 0;
texture = 0;
this->width = width;
this->height = height;
this->viewPortWidth = viewPortWidth;
this->viewPortHeight = viewPortHeight;
if (width != viewPortWidth || height != viewPortHeight)
scrollable = true;
else
scrollable = false;
}
cOglFb::~cOglFb(void) {
if (texture)
glDeleteTextures(1, &texture);
if (fb)
glDeleteFramebuffers(1, &fb);
}
bool cOglFb::Init(void) {
initiated = true;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
esyslog("[softhddev]ERROR: %d Framebuffer is not complete!\n", __LINE__);
return false;
}
return true;
}
void cOglFb::Bind(void) {
if (!initiated)
Init();
glViewport(0, 0, width, height);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
}
void cOglFb::BindRead(void) { glBindFramebuffer(GL_READ_FRAMEBUFFER, fb); }
void cOglFb::BindWrite(void) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb); }
void cOglFb::Unbind(void) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
bool cOglFb::BindTexture(void) {
if (!initiated)
return false;
glBindTexture(GL_TEXTURE_2D, texture);
return true;
}
void cOglFb::Blit(GLint destX1, GLint destY1, GLint destX2, GLint destY2) {
glBlitFramebuffer(0, 0, width, height, destX1, destY1, destX2, destY2, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glFlush();
}
/****************************************************************************************
* cOglOutputFb
****************************************************************************************/
cOglOutputFb::cOglOutputFb(GLint width, GLint height) : cOglFb(width, height, width, height) {
// surface = 0;
initiated = false;
fb = 0;
texture = 0;
}
cOglOutputFb::~cOglOutputFb(void) {
// glVDPAUUnregisterSurfaceNV(surface);
glDeleteTextures(1, &texture);
glDeleteFramebuffers(1, &fb);
}
bool cOglOutputFb::Init(void) {
initiated = true;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
esyslog("[softhddev]ERROR::cOglOutputFb: Framebuffer is not complete!");
return false;
}
return true;
}
void cOglOutputFb::BindWrite(void) {
if (!initiated)
Init();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
}
void cOglOutputFb::Unbind(void) { glBindFramebuffer(GL_FRAMEBUFFER, 0); }
/****************************************************************************************
* cOglVb
****************************************************************************************/
static cOglVb *VertexBuffers[vbCount];
cOglVb::cOglVb(int type) {
this->type = (eVertexBufferType)type;
vao = 0;
vbo = 0;
sizeVertex1 = 0;
sizeVertex2 = 0;
numVertices = 0;
drawMode = 0;
}
cOglVb::~cOglVb(void) {}
bool cOglVb::Init(void) {
if (type == vbTexture) {
// Texture VBO definition
sizeVertex1 = 2;
sizeVertex2 = 2;
numVertices = 6;
drawMode = GL_TRIANGLES;
shader = stTexture;
} else if (type == vbRect) {
// Rectangle VBO definition
sizeVertex1 = 2;
sizeVertex2 = 0;
numVertices = 4;
drawMode = GL_TRIANGLE_FAN;
shader = stRect;
} else if (type == vbEllipse) {
// Ellipse VBO definition
sizeVertex1 = 2;
sizeVertex2 = 0;
numVertices = 182;
drawMode = GL_TRIANGLE_FAN;
shader = stRect;
} else if (type == vbSlope) {
// Slope VBO definition
sizeVertex1 = 2;
sizeVertex2 = 0;
numVertices = 102;
drawMode = GL_TRIANGLE_FAN;
shader = stRect;
} else if (type == vbText) {
// Text VBO definition
sizeVertex1 = 2;
sizeVertex2 = 2;
numVertices = 6;
drawMode = GL_TRIANGLES;
shader = stText;
}
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * (sizeVertex1 + sizeVertex2) * numVertices, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, sizeVertex1, GL_FLOAT, GL_FALSE, (sizeVertex1 + sizeVertex2) * sizeof(GLfloat),
(GLvoid *)0);
if (sizeVertex2 > 0) {
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, sizeVertex2, GL_FLOAT, GL_FALSE, (sizeVertex1 + sizeVertex2) * sizeof(GLfloat),
(GLvoid *)(sizeVertex1 * sizeof(GLfloat)));
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return true;
}
void cOglVb::Bind(void) { glBindVertexArray(vao); }
void cOglVb::Unbind(void) { glBindVertexArray(0); }
void cOglVb::ActivateShader(void) { Shaders[shader]->Use(); }
void cOglVb::EnableBlending(void) {
glEnable(GL_BLEND);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
void cOglVb::DisableBlending(void) { glDisable(GL_BLEND); }
void cOglVb::SetShaderColor(GLint color) {
glm::vec4 col;
ConvertColor(color, col);
Shaders[shader]->SetVector4f("inColor", col.r, col.g, col.b, col.a);
}
void cOglVb::SetShaderAlpha(GLint alpha) {
Shaders[shader]->SetVector4f("alpha", 1.0f, 1.0f, 1.0f, (GLfloat)(alpha) / 255.0f);
}
void cOglVb::SetShaderProjectionMatrix(GLint width, GLint height) {
glm::mat4 projection = glm::ortho(0.0f, (GLfloat)width, (GLfloat)height, 0.0f, -1.0f, 1.0f);
Shaders[shader]->SetMatrix4("projection", projection);
}
void cOglVb::SetVertexData(GLfloat *vertices, int count) {
if (count == 0)
count = numVertices;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * (sizeVertex1 + sizeVertex2) * count, vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void cOglVb::DrawArrays(int count) {
if (count == 0)
count = numVertices;
glDrawArrays(drawMode, 0, count);
glFlush();
}
/****************************************************************************************
* cOpenGLCmd
****************************************************************************************/
//------------------ cOglCmdInitOutputFb --------------------
cOglCmdInitOutputFb::cOglCmdInitOutputFb(cOglOutputFb *oFb) : cOglCmd(NULL) { this->oFb = oFb; }
bool cOglCmdInitOutputFb::Execute(void) {
bool ok = oFb->Init();
oFb->Unbind();
return ok;
}
//------------------ cOglCmdInitFb --------------------
cOglCmdInitFb::cOglCmdInitFb(cOglFb *fb, cCondWait *wait) : cOglCmd(fb) { this->wait = wait; }
bool cOglCmdInitFb::Execute(void) {
bool ok = fb->Init();
fb->Unbind();
if (wait)
wait->Signal();
return ok;
}
//------------------ cOglCmdDeleteFb --------------------
cOglCmdDeleteFb::cOglCmdDeleteFb(cOglFb *fb) : cOglCmd(fb) {}
bool cOglCmdDeleteFb::Execute(void) {
if (fb)
delete fb;
return true;
}
//------------------ cOglCmdRenderFbToBufferFb --------------------
cOglCmdRenderFbToBufferFb::cOglCmdRenderFbToBufferFb(cOglFb *fb, cOglFb *buffer, GLint x, GLint y, GLint transparency,
GLint drawPortX, GLint drawPortY, bool alphablending)
: cOglCmd(fb) {
this->buffer = buffer;
this->x = (GLfloat)x;
this->y = (GLfloat)y;
this->drawPortX = (GLfloat)drawPortX;
this->drawPortY = (GLfloat)drawPortY;
this->transparency = (alphablending ? transparency : ALPHA_OPAQUE);
this->alphablending = alphablending;
}
bool cOglCmdRenderFbToBufferFb::Execute(void) {
GLfloat x2 = x + fb->ViewportWidth(); // right
GLfloat y2 = y + fb->ViewportHeight(); // bottom
GLfloat texX1 = drawPortX / (GLfloat)fb->Width();
GLfloat texY1 = drawPortY / (GLfloat)fb->Height();
GLfloat texX2 = texX1 + 1.0f;
GLfloat texY2 = texY1 + 1.0f;
if (fb->Scrollable()) {
GLfloat pageHeight = (GLfloat)fb->ViewportHeight() / (GLfloat)fb->Height();
texX1 = abs(drawPortX) / (GLfloat)fb->Width();
texY1 = 1.0f - pageHeight - abs(drawPortY) / (GLfloat)fb->Height();
texX2 = texX1 + (GLfloat)fb->ViewportWidth() / (GLfloat)fb->Width();
// x2 = x + fb->Width();
texY2 = texY1 + pageHeight;
}
GLfloat quadVertices[] = {
// Pos // TexCoords
x, y, texX1, texY2, // left top
x, y2, texX1, texY1, // left bottom
x2, y2, texX2, texY1, // right bottom
x, y, texX1, texY2, // left top
x2, y2, texX2, texY1, // right bottom
x2, y, texX2, texY2 // right top
};
VertexBuffers[vbTexture]->ActivateShader();
VertexBuffers[vbTexture]->SetShaderAlpha(transparency);
VertexBuffers[vbTexture]->SetShaderProjectionMatrix(buffer->Width(), buffer->Height());
buffer->Bind();
if (!fb->BindTexture())
return false;
if (!alphablending)
VertexBuffers[vbTexture]->DisableBlending();
VertexBuffers[vbTexture]->Bind();
VertexBuffers[vbTexture]->SetVertexData(quadVertices);
VertexBuffers[vbTexture]->DrawArrays();
VertexBuffers[vbTexture]->Unbind();
if (!alphablending)
VertexBuffers[vbTexture]->EnableBlending();
buffer->Unbind();
return true;
}
//------------------ cOglCmdCopyBufferToOutputFb --------------------
cOglCmdCopyBufferToOutputFb::cOglCmdCopyBufferToOutputFb(cOglFb *fb, cOglOutputFb *oFb, GLint x, GLint y)
: cOglCmd(fb) {
this->oFb = oFb;
this->x = x;
this->y = y;
}
extern unsigned char *posd;
bool cOglCmdCopyBufferToOutputFb::Execute(void) {
pthread_mutex_lock(&OSDMutex);
fb->BindRead();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
if (posd)
glReadPixels(0, 0, fb->Width(), fb->Height(), GL_RGBA, GL_UNSIGNED_BYTE, posd);
glFlush();
pthread_mutex_unlock(&OSDMutex);
ActivateOsd(oFb->texture, x, y, fb->Width(), fb->Height());
return true;
}
//------------------ cOglCmdFill --------------------
cOglCmdFill::cOglCmdFill(cOglFb *fb, GLint color) : cOglCmd(fb) { this->color = color; }
bool cOglCmdFill::Execute(void) {
glm::vec4 col;
ConvertColor(color, col);
fb->Bind();
glClearColor(col.r, col.g, col.b, col.a);
glClear(GL_COLOR_BUFFER_BIT);
fb->Unbind();
return true;
}
//------------------ cOglCmdDrawRectangle --------------------
cOglCmdDrawRectangle::cOglCmdDrawRectangle(cOglFb *fb, GLint x, GLint y, GLint width, GLint height, GLint color)
: cOglCmd(fb) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->color = color;
}
bool cOglCmdDrawRectangle::Execute(void) {
if (width <= 0 || height <= 0)
return false;
GLfloat x1 = x;
GLfloat y1 = y;
GLfloat x2 = x + width;
GLfloat y2 = y + height;
GLfloat vertices[] = {
x1, y1, // left top
x2, y1, // right top
x2, y2, // right bottom
x1, y2 // left bottom
};
VertexBuffers[vbRect]->ActivateShader();
VertexBuffers[vbRect]->SetShaderColor(color);
VertexBuffers[vbRect]->SetShaderProjectionMatrix(fb->Width(), fb->Height());
fb->Bind();
VertexBuffers[vbRect]->DisableBlending();
VertexBuffers[vbRect]->Bind();
VertexBuffers[vbRect]->SetVertexData(vertices);
VertexBuffers[vbRect]->DrawArrays();
VertexBuffers[vbRect]->Unbind();
VertexBuffers[vbRect]->EnableBlending();
fb->Unbind();
return true;
}
//------------------ cOglCmdDrawEllipse --------------------
/// quadrants:
///< 0 draws the entire ellipse
///< 1..4 draws only the first, second, third or fourth quadrant,
///< respectively 5..8 draws the right, top, left or bottom half,
///< respectively -1..-4 draws the inverted part of the given quadrant
cOglCmdDrawEllipse::cOglCmdDrawEllipse(cOglFb *fb, GLint x, GLint y, GLint width, GLint height, GLint color,
GLint quadrants)
: cOglCmd(fb) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->color = color;
this->quadrants = quadrants;
}
bool cOglCmdDrawEllipse::Execute(void) {
if (width <= 0 || height <= 0)
return false;
int numVertices = 0;
GLfloat *vertices = NULL;
switch (quadrants) {
case 0:
vertices = CreateVerticesFull(numVertices);
break;
case 1:
case 2:
case 3:
case 4:
case -1:
case -2:
case -3:
case -4:
vertices = CreateVerticesQuadrant(numVertices);
break;
case 5:
case 6:
case 7:
case 8:
vertices = CreateVerticesHalf(numVertices);
break;
default:
break;
}
VertexBuffers[vbEllipse]->ActivateShader();
VertexBuffers[vbEllipse]->SetShaderColor(color);
VertexBuffers[vbEllipse]->SetShaderProjectionMatrix(fb->Width(), fb->Height());
// not antialiased
fb->Bind();
VertexBuffers[vbEllipse]->DisableBlending();
VertexBuffers[vbEllipse]->Bind();
VertexBuffers[vbEllipse]->SetVertexData(vertices, numVertices);
VertexBuffers[vbEllipse]->DrawArrays(numVertices);
VertexBuffers[vbEllipse]->Unbind();
VertexBuffers[vbEllipse]->EnableBlending();
fb->Unbind();
delete[] vertices;
return true;
}
GLfloat *cOglCmdDrawEllipse::CreateVerticesFull(int &numVertices) {
int size = 364;
numVertices = size / 2;
GLfloat radiusX = (GLfloat)width / 2;
GLfloat radiusY = (GLfloat)height / 2;
GLfloat *vertices = new GLfloat[size];
vertices[0] = x + radiusX;
vertices[1] = y + radiusY;
for (int i = 0; i <= 180; i++) {
vertices[2 * i + 2] = x + radiusX + (GLfloat)cos(2 * i * M_PI / 180.0f) * radiusX;
vertices[2 * i + 3] = y + radiusY - (GLfloat)sin(2 * i * M_PI / 180.0f) * radiusY;
}
return vertices;
}
GLfloat *cOglCmdDrawEllipse::CreateVerticesQuadrant(int &numVertices) {
int size = 94;
numVertices = size / 2;
GLfloat radiusX = (GLfloat)width;
GLfloat radiusY = (GLfloat)height;
GLint transX = 0;
GLint transY = 0;
GLint startAngle = 0;
GLfloat *vertices = new GLfloat[size];
switch (quadrants) {
case 1:
vertices[0] = x;
vertices[1] = y + height;
transY = radiusY;
break;
case 2:
vertices[0] = x + width;
vertices[1] = y + height;
transX = radiusX;
transY = radiusY;
startAngle = 90;
break;
case 3:
vertices[0] = x + width;
vertices[1] = y;
transX = radiusX;
startAngle = 180;
break;
case 4:
vertices[0] = x;
vertices[1] = y;
startAngle = 270;
break;
case -1:
vertices[0] = x + width;
vertices[1] = y;
transY = radiusY;
break;
case -2:
vertices[0] = x;
vertices[1] = y;
transX = radiusX;
transY = radiusY;
startAngle = 90;
break;
case -3:
vertices[0] = x;