OSDN Git Service

[Qt][Draw][OpenGL] More fix against buffer overflow.
[csp-qt/common_source_project-fm7.git] / source / src / qt / gui / gl3 / qt_glutil_gl3_0.cpp
1 /*
2  * qt_glutil_gl3_0.cpp
3  * (c) 2016 K.Ohta <whatisthis.sowhat@gmail.com>
4  * License: GPLv2.
5  * Renderer with OpenGL v3.0 (extend from renderer with OpenGL v2.0).
6  * History:
7  * Jan 22, 2016 : Initial.
8  */
9
10 #include "qt_gldraw.h"
11 #include "qt_glpack.h"
12 #include "qt_glutil_gl3_0.h"
13 #include "csp_logger.h"
14 #include "menu_flags.h"
15 #include <QOpenGLTexture>
16 #include <QOpenGLFunctions_3_0>
17
18 //extern USING_FLAGS *using_flags;
19 #if 0
20         // OLD_THREE_PHASE
21 const float luma_filter[24 + 1] = {
22                 -0.000071070,
23                 -0.000032816,
24                 0.000128784,
25                 0.000134711,
26                 -0.000226705,
27                 -0.000777988,
28                 -0.000997809,
29                 -0.000522802,
30                 0.000344691,
31                 0.000768930,
32                 0.000275591,
33                 -0.000373434,
34                 0.000522796,
35                 0.003813817,
36                 0.007502825,
37                 0.006786001,
38                 -0.002636726,
39                 -0.019461182,
40                 -0.033792479,
41                 -0.029921972,
42                 0.005032552,
43                 0.071226466,
44                 0.151755921,
45                 0.218166470,
46                 0.243902439
47         };
48 const float chroma_filter[24 + 1] = {
49                 0.001845562,
50                 0.002381606,
51                 0.003040177,
52                 0.003838976,
53                 0.004795341,
54                 0.005925312,
55                 0.007242534,
56                 0.008757043,
57                 0.010473987,
58                 0.012392365,
59                 0.014503872,
60                 0.016791957,
61                 0.019231195,
62                 0.021787070,
63                 0.024416251,
64                 0.027067414,
65                 0.029682613,
66                 0.032199202,
67                 0.034552198,
68                 0.036677005,
69                 0.038512317,
70                 0.040003044,
71                 0.041103048,
72                 0.041777517,
73                 0.042004791
74         };
75 #else
76 #if 1
77         // THREE_PHASE
78 const float luma_filter[24 + 1] = {
79                 -0.000012020,
80                 -0.000022146,
81                 -0.000013155,
82                 -0.000012020,
83                 -0.000049979,
84                 -0.000113940,
85                 -0.000122150,
86                 -0.000005612,
87                 0.000170516,
88                 0.000237199,
89                 0.000169640,
90                 0.000285688,
91                 0.000984574,
92                 0.002018683,
93                 0.002002275,
94                 -0.000909882,
95                 -0.007049081,
96                 -0.013222860,
97                 -0.012606931,
98                 0.002460860,
99                 0.035868225,
100                 0.084016453,
101                 0.135563500,
102                 0.175261268,
103                 0.190176552
104         };
105 const float chroma_filter[24 + 1] = {
106                 -0.000118847,
107                 -0.000271306,
108                 -0.000502642,
109                 -0.000930833,
110                 -0.001451013,
111                 -0.002064744,
112                 -0.002700432,
113                 -0.003241276,
114                 -0.003524948,
115                 -0.003350284,
116                 -0.002491729,
117                 -0.000721149,
118                 0.002164659,
119                 0.006313635,
120                 0.011789103,
121                 0.018545660,
122                 0.026414396,
123                 0.035100710,
124                 0.044196567,
125                 0.053207202,
126                 0.061590275,
127                 0.068803602,
128                 0.074356193,
129                 0.077856564,
130                 0.079052396
131         };
132 // END "ntsc-decode-filter-3phase.inc" //
133 #else
134                                 // TWO_PHASE
135 const float luma_filter[24 + 1] = {
136                 -0.000012020,
137                 -0.000022146,
138                 -0.000013155,
139                 -0.000012020,
140                 -0.000049979,
141                 -0.000113940,
142                 -0.000122150,
143                 -0.000005612,
144                 0.000170516,
145                 0.000237199,
146                 0.000169640,
147                 0.000285688,
148                 0.000984574,
149                 0.002018683,
150                 0.002002275,
151                 -0.000909882,
152                 -0.007049081,
153                 -0.013222860,
154                 -0.012606931,
155                 0.002460860,
156                 0.035868225,
157                 0.084016453,
158                 0.135563500,
159                 0.175261268,
160                 0.190176552
161         };
162         
163 const float chroma_filter[24 + 1] = {
164                 -0.000118847,
165                 -0.000271306,
166                 -0.000502642,
167                 -0.000930833,
168                 -0.001451013,
169                 -0.002064744,
170                 -0.002700432,
171                 -0.003241276,
172                 -0.003524948,
173                 -0.003350284,
174                 -0.002491729,
175                 -0.000721149,
176                 0.002164659,
177                 0.006313635,
178                 0.011789103,
179                 0.018545660,
180                 0.026414396,
181                 0.035100710,
182                 0.044196567,
183                 0.053207202,
184                 0.061590275,
185                 0.068803602,
186                 0.074356193,
187                 0.077856564,
188                 0.079052396
189         };
190 // END "ntsc-decode-filter-3phase.inc" //
191 #endif
192 #endif
193
194 GLDraw_3_0::GLDraw_3_0(GLDrawClass *parent, USING_FLAGS *p, CSP_Logger *logger, EMU *emu) : GLDraw_2_0(parent, p, logger, emu)
195 {
196         uTmpTextureID = 0;
197         
198         grids_shader = NULL;
199         
200         main_pass = NULL;
201         std_pass = NULL;
202         ntsc_pass1 = NULL;
203         ntsc_pass2 = NULL;
204         led_pass = NULL;
205         for(int i = 0; i < 32; i++) {
206                 led_pass_vao[i] = NULL;
207                 led_pass_vbuffer[i] = NULL;
208                 osd_pass_vao[i] = NULL;
209                 osd_pass_vbuffer[i] = NULL;
210         }
211         grids_horizonal_buffer = NULL;
212         grids_horizonal_vertex = NULL;
213         
214         grids_vertical_buffer = NULL;
215         grids_vertical_vertex = NULL;
216         ringing_phase = 0.0f;
217
218 }
219
220 GLDraw_3_0::~GLDraw_3_0()
221 {
222
223         if(main_pass  != NULL) delete main_pass;
224         if(std_pass   != NULL) delete std_pass;
225         if(ntsc_pass1 != NULL) delete ntsc_pass1;
226         if(ntsc_pass2 != NULL) delete ntsc_pass2;
227         if(led_pass   != NULL) delete led_pass;
228         for(int i = 0; i < 32; i++) {
229                 if(led_pass_vao[i] != NULL) delete led_pass_vao[i];     
230                 if(led_pass_vbuffer[i] != NULL) delete led_pass_vbuffer[i];
231                 if(osd_pass_vao[i] != NULL) delete osd_pass_vao[i];     
232                 if(osd_pass_vbuffer[i] != NULL) delete osd_pass_vbuffer[i];
233         }
234         
235         if(grids_horizonal_buffer != NULL) {
236                 if(grids_horizonal_buffer->isCreated()) grids_horizonal_buffer->destroy();
237         }
238         if(grids_horizonal_vertex != NULL) {
239                 if(grids_horizonal_vertex->isCreated()) grids_horizonal_vertex->destroy();
240         }
241         if(grids_vertical_buffer != NULL) {
242                 if(grids_vertical_buffer->isCreated()) grids_vertical_buffer->destroy();
243         }
244         if(grids_horizonal_vertex != NULL) {
245                 if(grids_vertical_vertex->isCreated()) grids_vertical_vertex->destroy();
246         }
247
248 }
249
250 void GLDraw_3_0::initFBO(void)
251 {
252         glHorizGrids = (GLfloat *)malloc(sizeof(float) * (using_flags->get_real_screen_height() + 2) * 6);
253         if(glHorizGrids != NULL) {
254                 doSetGridsHorizonal(using_flags->get_real_screen_height(), true);
255         }
256         glVertGrids  = (GLfloat *)malloc(sizeof(float) * (using_flags->get_real_screen_width() + 2) * 6);
257         if(glVertGrids != NULL) {
258                 doSetGridsVertical(using_flags->get_real_screen_width(), true);
259         }
260         if(using_flags->get_max_button() > 0) {
261                 initButtons();
262         }
263         // Init view
264         extfunc->glClearColor(0.0, 0.0, 0.0, 1.0);
265 }
266
267 void GLDraw_3_0::setNormalVAO(QOpenGLShaderProgram *prg,
268                                                            QOpenGLVertexArrayObject *vp,
269                                                            QOpenGLBuffer *bp,
270                                                            VertexTexCoord_t *tp,
271                                                            int size)
272 {
273         int vertex_loc = prg->attributeLocation("vertex");
274         int texcoord_loc = prg->attributeLocation("texcoord");
275
276         vp->bind();
277         bp->bind();
278
279         if(tp == NULL) {
280         } else {
281                 bp->write(0, tp, sizeof(VertexTexCoord_t) * size);
282         }
283         prg->setAttributeBuffer(vertex_loc, GL_FLOAT, 0, 3, sizeof(VertexTexCoord_t));
284         prg->setAttributeBuffer(texcoord_loc, GL_FLOAT, 3 * sizeof(GLfloat), 2, sizeof(VertexTexCoord_t));
285         prg->setUniformValue("a_texture", 0);
286                            
287         extfunc->glVertexAttribPointer(vertex_loc, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTexCoord_t), 0); 
288         extfunc->glVertexAttribPointer(texcoord_loc, 2, GL_FLOAT, GL_FALSE, sizeof(VertexTexCoord_t), 
289                                                                (char *)NULL + 3 * sizeof(GLfloat)); 
290         bp->release();
291         vp->release();
292         prg->enableAttributeArray(vertex_loc);
293         prg->enableAttributeArray(texcoord_loc);
294 }
295
296 void GLDraw_3_0::initGLObjects()
297 {
298         extfunc = new QOpenGLFunctions_3_0;
299         extfunc->initializeOpenGLFunctions();
300         extfunc->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texture_max_size);
301 }       
302
303 void GLDraw_3_0::initPackedGLObject(GLScreenPack **p,
304                                                                         int _width, int _height,
305                                                                         const QString vertex_shader, const QString fragment_shader,
306                                                                         const QString _name)
307 {
308         QString s;
309         GLScreenPack *pp;
310         if(p != NULL) {
311                 pp = new GLScreenPack(_width, _height, _name, p_wid);
312                 *p = pp;
313                 if(pp != NULL) {
314                         pp->initialize(_width, _height, vertex_shader, fragment_shader);
315                         s = pp->getShaderLog();
316                         if(s.size() > 0) {
317                                 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GL_SHADER, "In shader of %s ", _name.toLocal8Bit().constData());
318                                 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GL_SHADER, "Vertex: %s ",  vertex_shader.toLocal8Bit().constData());
319                                 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GL_SHADER, "Fragment: %s ", fragment_shader.toLocal8Bit().constData());
320                                 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GL_SHADER, "%s", s.toLocal8Bit().constData());
321                         }
322                         s = pp->getGLLog();
323                         if(s.size() > 0) {
324                                 csp_logger->debug_log(CSP_LOG_INFO, CSP_LOG_TYPE_GL_SHADER, "%s", s.toLocal8Bit().constData());
325                                 pp->clearGLLog();
326                         }
327                 }
328         }
329 }
330                                         
331
332
333 bool GLDraw_3_0::initGridShaders(const QString vertex_fixed, const QString vertex_rotate, const QString fragment)
334 {
335         bool f = false;
336         grids_shader = new QOpenGLShaderProgram(p_wid);
337         if(grids_shader != NULL) {
338                 if(using_flags->is_use_screen_rotate()) {
339                         f = grids_shader->addShaderFromSourceFile(QOpenGLShader::Vertex, vertex_rotate);
340                 } else {
341                         f = grids_shader->addShaderFromSourceFile(QOpenGLShader::Vertex, vertex_fixed);
342                 }
343                 f &= grids_shader->addShaderFromSourceFile(QOpenGLShader::Fragment, fragment);
344                 f &= grids_shader->link();
345         }
346         return f;
347 }
348
349 bool GLDraw_3_0::initGridVertexObject(QOpenGLBuffer **vbo, QOpenGLVertexArrayObject **vao, int alloc_size)
350 {
351         QOpenGLBuffer *bp = NULL;
352         QOpenGLVertexArrayObject *ap = NULL;
353         *vao = NULL;
354         *vbo = NULL;
355         *vbo  = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
356         bp = *vbo;
357         if(bp != NULL) {
358                 if(bp->create()) {
359                         bp->bind();
360                         bp->allocate(alloc_size * sizeof(GLfloat) * 3 * 2);
361                         bp->release();
362                 } else {
363                         delete *vbo;
364                         return false;
365                 }
366         } else {
367                 return false;
368         }
369         
370         *vao = new QOpenGLVertexArrayObject;
371         ap = *vao;
372         if(ap == NULL) {
373                 bp->destroy();
374                 delete *vbo;
375                 return false;
376         }
377         if(!ap->create()) {
378                 delete *vao;
379                 bp->destroy();
380                 delete *vbo;
381                 return false;
382         }
383         return true;
384 }
385
386
387 void GLDraw_3_0::initLocalGLObjects(void)
388 {
389
390         int _width = using_flags->get_screen_width();
391         int _height = using_flags->get_screen_height();
392         
393         if((_width * 4) <= texture_max_size) {
394                 _width = _width * 4;
395                 low_resolution_screen = true;
396         } else {
397                 _width = _width * 2;
398         }
399         p_wid->makeCurrent();
400         
401         vertexFormat[0].x = -1.0f;
402         vertexFormat[0].y = -1.0f;
403         vertexFormat[0].z = -0.9f;
404         vertexFormat[0].s = 0.0f;
405         vertexFormat[0].t = 1.0f;
406         
407         vertexFormat[1].x = +1.0f;
408         vertexFormat[1].y = -1.0f;
409         vertexFormat[1].z = -0.9f;
410         vertexFormat[1].s = 1.0f;
411         vertexFormat[1].t = 1.0f;
412         
413         vertexFormat[2].x = +1.0f;
414         vertexFormat[2].y = +1.0f;
415         vertexFormat[2].z = -0.9f;
416         vertexFormat[2].s = 1.0f;
417         vertexFormat[2].t = 0.0f;
418         
419         vertexFormat[3].x = -1.0f;
420         vertexFormat[3].y = +1.0f;
421         vertexFormat[3].z = -0.9f;
422         vertexFormat[3].s = 0.0f;
423         vertexFormat[3].t = 0.0f;
424
425         if(using_flags->is_use_one_board_computer() || (using_flags->get_max_button() > 0)) {
426                 initPackedGLObject(&main_pass,
427                                                    using_flags->get_screen_width() * 2, using_flags->get_screen_height() * 2,
428                                                    ":/gl3/vertex_shader.glsl" , ":/gl3/chromakey_fragment_shader2.glsl",
429                                                    "Main Shader");
430         } else {
431                 initPackedGLObject(&main_pass,
432                                                    using_flags->get_screen_width() * 2, using_flags->get_screen_height() * 2,
433                                                    ":/gl3/vertex_shader.glsl" , ":/gl3/fragment_shader.glsl",
434                                                    "Main Shader");
435         }               
436         if(main_pass != NULL) {
437                 setNormalVAO(main_pass->getShader(), main_pass->getVAO(),
438                                          main_pass->getVertexBuffer(),
439                                          vertexFormat, 4);
440         }
441         initPackedGLObject(&std_pass,
442                                            using_flags->get_screen_width(), using_flags->get_screen_height(),
443                                            ":/gl3/vertex_shader.glsl" , ":/gl3/chromakey_fragment_shader.glsl",
444                                            "Standard Shader");
445         initPackedGLObject(&led_pass,
446                                            10, 10,
447                                            ":/gl3/led_vertex_shader.glsl" , ":/gl3/led_fragment_shader.glsl",
448                                            "LED Shader");
449         for(int i = 0; i < 32; i++) {
450                 led_pass_vao[i] = new QOpenGLVertexArrayObject;
451                 led_pass_vbuffer[i] = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
452                 if(led_pass_vao[i]->create()) {
453                         if(led_pass_vbuffer[i]->create()) {
454                                 led_pass_vbuffer[i]->setUsagePattern(QOpenGLBuffer::DynamicDraw);
455                                 led_pass_vao[i]->bind();
456                                 led_pass_vbuffer[i]->bind();
457                                 led_pass_vbuffer[i]->allocate(sizeof(VertexTexCoord_t) * 4);
458                                 led_pass_vbuffer[i]->release();
459                                 led_pass_vao[i]->release();
460                                 set_led_vertex(i);
461                         }
462                 }
463         }
464         initPackedGLObject(&osd_pass,
465                                            48.0, 48.0,
466                                            ":/gl3/vertex_shader.glsl" , ":/gl3/icon_fragment_shader.glsl",
467                                            "OSD Shader");
468         for(int i = 0; i < 32; i++) {
469                 osd_pass_vao[i] = new QOpenGLVertexArrayObject;
470                 osd_pass_vbuffer[i] = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
471                 if(osd_pass_vao[i]->create()) {
472                         if(osd_pass_vbuffer[i]->create()) {
473                                 osd_pass_vbuffer[i]->setUsagePattern(QOpenGLBuffer::DynamicDraw);
474                                 osd_pass_vao[i]->bind();
475                                 osd_pass_vbuffer[i]->bind();
476                                 osd_pass_vbuffer[i]->allocate(sizeof(VertexTexCoord_t) * 4);
477                                 osd_pass_vbuffer[i]->release();
478                                 osd_pass_vao[i]->release();
479                                 set_osd_vertex(i);
480                         }
481                 }
482         }
483
484         initPackedGLObject(&ntsc_pass1,
485                                            _width, _height,
486                                            ":/gl3/vertex_shader.glsl" , ":/gl3/ntsc_pass1.glsl",
487                                            "NTSC Shader Pass1");
488         initPackedGLObject(&ntsc_pass2,
489                                            _width / 2, _height,
490                                            ":/gl3/vertex_shader.glsl" , ":/gl3/ntsc_pass2.glsl",
491                                            "NTSC Shader Pass2");
492         {
493                 int ii;
494                 QOpenGLShaderProgram *shader = ntsc_pass2->getShader();
495                 shader->bind();
496                 ii = shader->uniformLocation("luma_filter");
497                 if(ii >= 0) {
498                         shader->setUniformValueArray(ii, luma_filter, 24 + 1, 1);
499                 }
500                 ii = shader->uniformLocation("chroma_filter");
501                 if(ii >= 0) {
502                         shader->setUniformValueArray(ii, chroma_filter, 24 + 1, 1);
503                 }
504                 shader->release();
505         }
506    
507         if(using_flags->is_use_one_board_computer()) {
508                 initBitmapVertex();
509                 initPackedGLObject(&bitmap_block,
510                                                    _width * 2, _height * 2,
511                                                    ":/gl3/vertex_shader.glsl", ":/gl3/normal_fragment_shader.glsl",
512                                                    "Background Bitmap Shader");
513                 if(bitmap_block != NULL) {
514                         setNormalVAO(bitmap_block->getShader(), bitmap_block->getVAO(),
515                                                  bitmap_block->getVertexBuffer(),
516                                                  vertexBitmap, 4);
517                 }
518         }
519         initGridShaders(":/gl3/grids_vertex_shader_fixed.glsl", ":/gl3/grids_vertex_shader.glsl", ":/gl3/grids_fragment_shader.glsl");
520         
521         initGridVertexObject(&grids_horizonal_buffer, &grids_horizonal_vertex, using_flags->get_real_screen_height() + 3);
522         doSetGridsHorizonal(using_flags->get_real_screen_height(), true);
523         
524         initGridVertexObject(&grids_vertical_buffer, &grids_vertical_vertex, using_flags->get_real_screen_width() + 3);
525         doSetGridsVertical(using_flags->get_real_screen_width(), true);
526
527         do_set_texture_size(NULL, -1, -1);
528         p_wid->doneCurrent();
529 }
530
531 void GLDraw_3_0::updateGridsVAO(QOpenGLBuffer *bp,
532                                                                 QOpenGLVertexArrayObject *vp,
533                                                                 GLfloat *tp,
534                                                                 int number)
535
536 {
537         bool checkf = false;
538         if((bp != NULL) && (vp != NULL)) {
539                 if(bp->isCreated()) {
540                         if(bp->size() < (int)((number + 1) * sizeof(GLfloat) * 3 * 2)) {
541                                 bp->destroy();
542                                 bp->create();
543                                 checkf = true;
544                         }
545                 } else {
546                         bp->create();
547                         checkf = true;
548                 }
549                 vp->bind();
550                 bp->bind();
551                 if(checkf) {
552                         bp->allocate((number + 1) * sizeof(GLfloat) * 3 * 2);
553                 }
554                 if(tp != NULL) {
555                         bp->write(0, tp, (number + 1) * sizeof(GLfloat) * 3 * 2);
556                 }
557                 bp->release();
558                 vp->release();
559         }
560 }
561 void GLDraw_3_0::drawGridsMain_3(QOpenGLShaderProgram *prg,
562                                                                  QOpenGLBuffer *bp,
563                                                                  QOpenGLVertexArrayObject *vp,
564                                                                  int number,
565                                                                  GLfloat lineWidth,
566                                                                  QVector4D color)
567 {
568         if(number <= 0) return;
569         extfunc->glDisable(GL_TEXTURE_2D);
570         extfunc->glDisable(GL_DEPTH_TEST);
571         extfunc->glDisable(GL_BLEND);
572
573         if((bp == NULL) || (vp == NULL) || (prg == NULL)) return;
574         if((!bp->isCreated()) || (!vp->isCreated()) || (!prg->isLinked())) return;
575         {
576                 bp->bind();
577                 vp->bind();
578                 prg->bind();
579                 //GLfloat ff[2];
580                 //bp->read(8, &ff, sizeof(GLfloat) * 2);
581                 //printf("%d %f %f\n", number, ff[0], ff[1]);
582                 
583                 prg->setUniformValue("color", color);
584                 prg->enableAttributeArray("vertex");
585                 int vertex_loc = prg->attributeLocation("vertex");
586                 
587                 extfunc->glViewport(0, 0, p_wid->width(), p_wid->height());
588                 extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
589                 //extfunc->glEnable(GL_BLEND);
590                 //extfunc->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
591                 //extfunc->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
592                 extfunc->glVertexAttribPointer(vertex_loc, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, 0); 
593                 extfunc->glEnableVertexAttribArray(vertex_loc);
594                 
595                 extfunc->glEnableClientState(GL_VERTEX_ARRAY);
596                 extfunc->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
597                 extfunc->glLineWidth(lineWidth);
598                 extfunc->glVertexPointer(3, GL_FLOAT, 0, 0);
599                 extfunc->glDrawArrays(GL_LINES, 0, (number + 1) * 2);
600                 extfunc->glDisableClientState(GL_VERTEX_ARRAY);
601                 //extfunc->glDisable(GL_BLEND);
602                 prg->release();
603                 vp->release();
604                 bp->release();
605         }
606 }
607
608 void GLDraw_3_0::doSetGridsVertical(int pixels, bool force)
609 {
610         GLDraw_2_0::doSetGridsVertical(pixels, force);
611         updateGridsVAO(grids_vertical_buffer,
612                                    grids_vertical_vertex,
613                                    glVertGrids,
614                                    pixels);
615         
616 }
617 void GLDraw_3_0::doSetGridsHorizonal(int lines, bool force)
618 {
619         if((lines == vert_lines) && !force) return;
620         GLDraw_2_0::doSetGridsHorizonal(lines, force);
621         updateGridsVAO(grids_horizonal_buffer,
622                                    grids_horizonal_vertex,
623                                    glHorizGrids,
624                                    lines);
625 }
626
627 void GLDraw_3_0::drawGridsHorizonal(void)
628 {
629         QVector4D c= QVector4D(0.0f, 0.0f, 0.0f, 1.0f);
630         drawGridsMain_3(grids_shader,
631                                         grids_horizonal_buffer,
632                                         grids_horizonal_vertex,
633                                         vert_lines,
634                                         0.15f,
635                                         c);
636 }
637
638 void GLDraw_3_0::drawGridsVertical(void)
639 {
640         QVector4D c= QVector4D(0.0f, 0.0f, 0.0f, 1.0f);
641         drawGridsMain_3(grids_shader,
642                                         grids_vertical_buffer,
643                                         grids_vertical_vertex,
644                                         horiz_pixels,
645                                         0.5f,
646                                         c);
647 }
648
649 void GLDraw_3_0::renderToTmpFrameBuffer_nPass(GLuint src_texture,
650                                                                                           GLuint src_w,
651                                                                                           GLuint src_h,
652                                                                                           GLScreenPack *renderObject,
653                                                                                           GLuint dst_w,
654                                                                                           GLuint dst_h,
655                                                                                           bool use_chromakey)
656 {
657         {
658                 QOpenGLShaderProgram *shader = renderObject->getShader();
659                 int ii;
660                 // NTSC_PASSx
661
662                 extfunc->glClearColor(0.0, 0.0, 0.0, 1.0);
663                 extfunc->glClearDepth(1.0f);
664                 extfunc->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
665                 {
666                         if((src_texture != 0) && (shader != NULL)) {
667                                 extfunc->glEnable(GL_TEXTURE_2D);
668                                 renderObject->bind();
669                                 //extfunc->glViewport(0, 0, src_w, src_h);
670                                 extfunc->glViewport(0, 0, dst_w, dst_h);
671                                 extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
672                                 extfunc->glActiveTexture(GL_TEXTURE0);
673                                 extfunc->glBindTexture(GL_TEXTURE_2D, src_texture);
674                                 extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
675                                 extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
676                                 //extfunc->glColor4f(1.0, 1.0, 1.0, 1.0);
677                                 shader->setUniformValue("a_texture", 0);
678                                 //shader->setUniformValue("a_texture", src_texture);
679                                 {
680                                         ii = shader->uniformLocation("source_size");
681                                         if(ii >= 0) {
682                                                 QVector4D source_size = QVector4D((float)src_w, (float)src_h, 0, 0);
683                                                 shader->setUniformValue(ii, source_size);
684                                         }
685                                         ii = shader->uniformLocation("target_size");
686                                         if(ii >= 0) {
687                                                 QVector4D target_size = QVector4D((float)dst_w, (float)dst_h, 0, 0);
688                                                 shader->setUniformValue(ii, target_size);
689                                         }
690                                         ii = shader->uniformLocation("phase");
691                                         if(ii >= 0) {
692                                                 ringing_phase = ringing_phase + 0.093;
693                                                 if(ringing_phase > 1.0) ringing_phase = ringing_phase - 1.0;
694                                                 shader->setUniformValue(ii,  ringing_phase);
695                                         }
696                                         //ii = shader->uniformLocation("luma_filter");
697                                         //if(ii >= 0) {
698                                         //      shader->setUniformValueArray(ii, luma_filter, 24 + 1, 1);
699                                         //}
700                                         //ii = shader->uniformLocation("chroma_filter");
701                                         //if(ii >= 0) {
702                                         //      shader->setUniformValueArray(ii, chroma_filter, 24 + 1, 1);
703                                         //}
704                                 }
705                                 {
706                                         QVector4D c(fBrightR, fBrightG, fBrightB, 1.0);
707                                         QVector3D chromakey(0.0, 0.0, 0.0);
708                 
709                                         ii = shader->uniformLocation("color");
710                                         if(ii >= 0) {
711                                                 shader->setUniformValue(ii, c);
712                                         }
713                                         ii = shader->uniformLocation("do_chromakey");
714                                         if(ii >= 0) {
715                                                 if(use_chromakey) {
716                                                         int ij;
717                                                         ij = shader->uniformLocation("chromakey");
718                                                         if(ij >= 0) {
719                                                                 shader->setUniformValue(ij, chromakey);
720                                                         }
721                                                         shader->setUniformValue(ii, GL_TRUE);
722                                                 } else {
723                                                         shader->setUniformValue(ii, GL_FALSE);
724                                                 }
725                                         }
726                                         //shader->setUniformValue("tex_width",  (float)w); 
727                                         //shader->setUniformValue("tex_height", (float)h);
728                                 }
729                                 shader->enableAttributeArray("texcoord");
730                                 shader->enableAttributeArray("vertex");
731                                 
732                                 int vertex_loc = shader->attributeLocation("vertex");
733                                 int texcoord_loc = shader->attributeLocation("texcoord");
734                                 extfunc->glEnableVertexAttribArray(vertex_loc);
735                                 extfunc->glEnableVertexAttribArray(texcoord_loc);
736                                 extfunc->glEnable(GL_VERTEX_ARRAY);
737
738                                 extfunc->glDrawArrays(GL_POLYGON, 0, 4);
739                                 
740                                 extfunc->glViewport(0, 0, dst_w, dst_h);
741                                 extfunc->glOrtho(0.0f, (float)dst_w, 0.0f, (float)dst_h, -1.0, 1.0);
742                                 renderObject->release();
743                                 extfunc->glBindTexture(GL_TEXTURE_2D, 0);
744                                 extfunc->glDisable(GL_TEXTURE_2D);
745                         }
746                 }
747         }
748 }
749
750
751 void GLDraw_3_0::uploadMainTexture(QImage *p, bool use_chromakey)
752 {
753         // set vertex
754         redraw_required = true;
755         if(p == NULL) return;
756         //redraw_required = true;
757         imgptr = p;
758         if(uVramTextureID == NULL) {
759                 uVramTextureID = new QOpenGLTexture(*p);
760         }
761
762         {
763                 // Upload to main texture
764                 extfunc->glBindTexture(GL_TEXTURE_2D, uVramTextureID->textureId());
765                 extfunc->glTexSubImage2D(GL_TEXTURE_2D, 0,
766                                                                  0, 0,
767                                                                  //screen_texture_width * 2,
768                                                                  //screen_texture_height * 2,
769                                                                  p->width(), p->height(),
770                                                                  GL_BGRA, GL_UNSIGNED_BYTE, p->constBits());
771                 extfunc->glBindTexture(GL_TEXTURE_2D, 0);
772         }
773         if(using_flags->is_support_tv_render() && (config.rendering_type == CONFIG_RENDER_TYPE_TV)) {
774                 renderToTmpFrameBuffer_nPass(uVramTextureID->textureId(),
775                                                                          screen_texture_width,
776                                                                          screen_texture_height,
777                                                                          ntsc_pass1,
778                                                                          ntsc_pass1->getViewportWidth(),
779                                                                          ntsc_pass1->getViewportHeight());
780                 
781                 renderToTmpFrameBuffer_nPass(ntsc_pass1->getTexture(),
782                                                                          ntsc_pass1->getViewportWidth(),
783                                                                          ntsc_pass1->getViewportHeight(),
784                                                                          ntsc_pass2,
785                                                                          ntsc_pass2->getViewportWidth(),
786                                                                          ntsc_pass2->getViewportHeight());
787                 uTmpTextureID = ntsc_pass2->getTexture();
788         } else {
789                 renderToTmpFrameBuffer_nPass(uVramTextureID->textureId(),
790                                                                          screen_texture_width,
791                                                                          screen_texture_height,
792                                                                          std_pass,
793                                                                          std_pass->getViewportWidth(),
794                                                                          std_pass->getViewportHeight(),
795                                                                          use_chromakey);
796
797                 //std_pass->bind();
798                 uTmpTextureID = std_pass->getTexture();
799                 //std_pass->release();
800         }
801         crt_flag = true;
802 }
803
804 void GLDraw_3_0::drawScreenTexture(void)
805 {
806         if(using_flags->is_use_one_board_computer()) {
807                 extfunc->glEnable(GL_BLEND);
808                 extfunc->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
809         } else {
810                 extfunc->glDisable(GL_BLEND);
811         }
812         
813         QVector4D color;
814         smoosing = config.use_opengl_filters;
815         if(set_brightness) {
816                 color = QVector4D(fBrightR, fBrightG, fBrightB, 1.0);
817         } else {
818                 color = QVector4D(1.0, 1.0, 1.0, 1.0);
819         }
820         if(using_flags->is_use_one_board_computer()) {
821                 drawMain(main_pass,
822                                  uTmpTextureID, // v2.0
823                                  color, smoosing,
824                                  true, QVector3D(0.0, 0.0, 0.0));       
825                 extfunc->glDisable(GL_BLEND);
826         } else {
827                 drawMain(main_pass,
828                                  uTmpTextureID, // v2.0
829                                  color, smoosing);      
830         }
831 }
832
833 void GLDraw_3_0::drawMain(QOpenGLShaderProgram *prg,
834                                                   QOpenGLVertexArrayObject *vp,
835                                                   QOpenGLBuffer *bp,
836                                                   GLuint texid,
837                                                   QVector4D color,
838                                                   bool f_smoosing,
839                                                   bool do_chromakey,
840                                                   QVector3D chromakey)
841 {
842         int ii;
843
844    if(texid != 0) {
845                 extfunc->glEnable(GL_TEXTURE_2D);
846                 vp->bind();
847                 bp->bind();
848                 prg->bind();
849                 extfunc->glViewport(0, 0, p_wid->width(), p_wid->height());
850                 extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
851                 extfunc->glActiveTexture(GL_TEXTURE0);
852                 extfunc->glBindTexture(GL_TEXTURE_2D, texid);
853                 if(!f_smoosing) {
854                         extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
855                         extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
856                 } else {
857                         extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
858                         extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
859                 }
860                 prg->setUniformValue("a_texture", 0);
861                 
862                 ii = prg->uniformLocation("color");
863                 if(ii >= 0) {
864                         prg->setUniformValue(ii,  color);
865                 }
866                 
867                 ii = prg->uniformLocation("tex_width");
868                 if(ii >= 0) {
869                         prg->setUniformValue(ii,  (float)screen_texture_width);
870                 }
871                 
872                 ii = prg->uniformLocation("tex_height");
873                 if(ii >= 0) {
874                         prg->setUniformValue(ii,  (float)screen_texture_height);
875                 }
876                 if(using_flags->is_use_screen_rotate()) {
877                         if(config.rotate_type) {
878                                 prg->setUniformValue("rotate", GL_TRUE);
879                         } else {
880                                 prg->setUniformValue("rotate", GL_FALSE);
881                         }
882                 } else {
883                         prg->setUniformValue("rotate", GL_FALSE);
884                 }
885
886                 if(do_chromakey) {
887                         ii = prg->uniformLocation("chromakey");
888                         if(ii >= 0) {
889                                 prg->setUniformValue(ii, chromakey);
890                         }
891                         ii = prg->uniformLocation("do_chromakey");
892                         if(ii >= 0) {
893                                 prg->setUniformValue(ii, GL_TRUE);
894                         }
895                 } else {
896                         ii = prg->uniformLocation("do_chromakey");
897                         if(ii >= 0) {
898                                 prg->setUniformValue(ii, GL_FALSE);
899                         }
900                 }
901                 
902                 prg->enableAttributeArray("texcoord");
903                 prg->enableAttributeArray("vertex");
904                 int vertex_loc = prg->attributeLocation("vertex");
905                 int texcoord_loc = prg->attributeLocation("texcoord");
906                 extfunc->glEnableVertexAttribArray(vertex_loc);
907                 extfunc->glEnableVertexAttribArray(texcoord_loc);
908                 extfunc->glEnable(GL_VERTEX_ARRAY);
909                 extfunc->glDrawArrays(GL_POLYGON, 0, 4);
910                 bp->release();
911                 vp->release();
912                 
913                 prg->release();
914                 extfunc->glBindTexture(GL_TEXTURE_2D, 0);
915                 extfunc->glDisable(GL_TEXTURE_2D);
916         }
917         else {
918                 extfunc->glDisable(GL_TEXTURE_2D);
919                 vp->bind();
920                 bp->bind();
921                 prg->bind();
922                 extfunc->glViewport(0, 0, p_wid->width(), p_wid->height());
923                 extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
924                 ii = prg->uniformLocation("color");
925                 if(ii >= 0) {
926                         prg->setUniformValue(ii,  color);
927                 }
928                 if(using_flags->is_use_screen_rotate()) {
929                         if(config.rotate_type) {
930                                 prg->setUniformValue("rotate", GL_TRUE);
931                         } else {
932                                 prg->setUniformValue("rotate", GL_FALSE);
933                         }
934                 } else {
935                         prg->setUniformValue("rotate", GL_FALSE);
936                 }
937
938                 if(do_chromakey) {
939                         ii = prg->uniformLocation("chromakey");
940                         if(ii >= 0) {
941                                 prg->setUniformValue(ii, chromakey);
942                         }
943                         ii = prg->uniformLocation("do_chromakey");
944                         if(ii >= 0) {
945                                 prg->setUniformValue(ii, GL_TRUE);
946                         }
947                 } else {
948                         ii = prg->uniformLocation("do_chromakey");
949                         if(ii >= 0) {
950                                 prg->setUniformValue(ii, GL_FALSE);
951                         }
952                 }
953                 
954                 prg->enableAttributeArray("vertex");
955                 int vertex_loc = prg->attributeLocation("vertex");
956                 extfunc->glEnableVertexAttribArray(vertex_loc);
957                 extfunc->glEnable(GL_VERTEX_ARRAY);
958                 extfunc->glDrawArrays(GL_POLYGON, 0, 4);
959                 extfunc->glDisable(GL_VERTEX_ARRAY);
960                 bp->release();
961                 vp->release();
962                 prg->release();
963                 extfunc->glBindTexture(GL_TEXTURE_2D, 0);
964                 extfunc->glDisable(GL_TEXTURE_2D);
965         }
966 }
967
968 void GLDraw_3_0::drawMain(GLScreenPack *obj,
969                                                   GLuint texid,
970                                                   QVector4D color,
971                                                   bool f_smoosing,
972                                                   bool do_chromakey,
973                                                   QVector3D chromakey)
974                                                    
975 {
976         QOpenGLShaderProgram *prg = obj->getShader();
977         QOpenGLVertexArrayObject *vp = obj->getVAO();
978         QOpenGLBuffer *bp = obj->getVertexBuffer();
979
980         drawMain(prg, vp, bp, texid, color, f_smoosing, do_chromakey, chromakey);
981 }
982
983 void GLDraw_3_0::drawButtonsMain(int num, bool f_smoosing)
984 {
985         GLuint texid;
986         QOpenGLBuffer *bp = buffer_button_vertex[num];
987         QOpenGLShaderProgram  *prg = button_shader;
988         QOpenGLVertexArrayObject *vp = vertex_button[num];
989         QVector4D color;
990         int ii;
991         if(uButtonTextureID[num] == NULL) return;
992         texid = uButtonTextureID[num]->textureId();
993         color = QVector4D(1.0, 1.0, 1.0, 1.0);
994         if((bp != NULL) && (vp != NULL) && (prg != NULL)) {
995                 if((bp->isCreated()) && (vp->isCreated()) && (prg->isLinked())) {
996                         bp->bind();
997                         vp->bind();
998                         prg->bind();
999                         extfunc->glViewport(0, 0, p_wid->width(), p_wid->height());
1000                         extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
1001                         extfunc->glActiveTexture(GL_TEXTURE0);
1002                         extfunc->glBindTexture(GL_TEXTURE_2D, texid);
1003                         if(!f_smoosing) {
1004                                 extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1005                                 extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
1006                         } else {
1007                                 extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1008                                 extfunc->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
1009                         }
1010                         prg->setUniformValue("a_texture", 0);
1011                         
1012                         ii = prg->uniformLocation("color");
1013                         if(ii >= 0) {
1014                                 prg->setUniformValue(ii,  color);
1015                         }
1016                         ii = prg->uniformLocation("do_chromakey");
1017                         if(ii >= 0) {
1018                                 prg->setUniformValue(ii, GL_FALSE);
1019                         }
1020                         if(using_flags->is_use_screen_rotate()) {
1021                                 if(config.rotate_type) {
1022                                         prg->setUniformValue("rotate", GL_TRUE);
1023                                 } else {
1024                                         prg->setUniformValue("rotate", GL_FALSE);
1025                                 }
1026                         } else {
1027                                 prg->setUniformValue("rotate", GL_FALSE);
1028                         }
1029                         int vertex_loc = prg->attributeLocation("vertex");
1030                         int texcoord_loc = prg->attributeLocation("texcoord");
1031                         prg->setAttributeBuffer(vertex_loc, GL_FLOAT, 0, 3, sizeof(VertexTexCoord_t));
1032                         prg->setAttributeBuffer(texcoord_loc, GL_FLOAT, 3 * sizeof(GLfloat), 2, sizeof(VertexTexCoord_t));
1033                         prg->enableAttributeArray(vertex_loc);
1034                         prg->enableAttributeArray(texcoord_loc);
1035                         extfunc->glEnableVertexAttribArray(vertex_loc);
1036                         extfunc->glEnableVertexAttribArray(texcoord_loc);
1037                         extfunc->glEnable(GL_VERTEX_ARRAY);
1038                         extfunc->glDrawArrays(GL_POLYGON, 0, 4);
1039                         bp->release();
1040                         vp->release();
1041                         prg->release();
1042                         extfunc->glBindTexture(GL_TEXTURE_2D, 0);
1043                         extfunc->glDisable(GL_TEXTURE_2D);
1044                         return;
1045                         }
1046                 }
1047
1048 }
1049
1050 void GLDraw_3_0::drawButtons(void)
1051 {
1052         for(int i = 0; i < using_flags->get_max_button(); i++) {
1053                 drawButtonsMain(i, false);
1054         }
1055 }
1056
1057 void GLDraw_3_0::drawBitmapTexture(void)
1058 {
1059         QVector4D color = QVector4D(1.0f, 1.0f, 1.0f, 1.0f);
1060         smoosing = config.use_opengl_filters;
1061         if(uBitmapTextureID == NULL) return;
1062         if(using_flags->is_use_one_board_computer()) {
1063                 extfunc->glDisable(GL_BLEND);
1064                 drawMain(bitmap_block,
1065                                  uBitmapTextureID->textureId(),
1066                                  color, smoosing);
1067         }
1068 }
1069
1070 void GLDraw_3_0::drawLedMain(GLScreenPack *obj, int num, QVector4D color)
1071 {
1072         QOpenGLShaderProgram *prg = obj->getShader();
1073         QOpenGLVertexArrayObject *vp = led_pass_vao[num];
1074         QOpenGLBuffer *bp = led_pass_vbuffer[num];
1075         int ii;
1076                 
1077         {
1078                 extfunc->glDisable(GL_TEXTURE_2D);
1079                 extfunc->glEnable(GL_BLEND);
1080                 extfunc->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1081                 vp->bind();
1082                 bp->bind();
1083                 prg->bind();
1084                 extfunc->glViewport(0, 0, p_wid->width(), p_wid->height());
1085                 extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
1086                 ii = prg->uniformLocation("color");
1087                 if(ii >= 0) {
1088                         prg->setUniformValue(ii,  color);
1089                 }
1090                 
1091                 prg->enableAttributeArray("vertex");
1092                 int vertex_loc = prg->attributeLocation("vertex");
1093                 prg->setAttributeBuffer(vertex_loc, GL_FLOAT, 0, 3, sizeof(VertexTexCoord_t));
1094                 extfunc->glVertexAttribPointer(vertex_loc, 3, GL_FLOAT, GL_FALSE, sizeof(VertexTexCoord_t), 0); 
1095
1096                 extfunc->glEnableVertexAttribArray(vertex_loc);
1097                 extfunc->glEnable(GL_VERTEX_ARRAY);
1098                 extfunc->glDrawArrays(GL_POLYGON, 0, 4);
1099                 bp->release();
1100                 vp->release();
1101                 
1102                 prg->release();
1103                 extfunc->glDisable(GL_TEXTURE_2D);
1104                 extfunc->glDisable(GL_BLEND);
1105         }
1106
1107 }
1108
1109
1110 void GLDraw_3_0::drawOsdLeds()
1111 {
1112         QVector4D color_on;
1113         QVector4D color_off;
1114         uint32_t bit = 0x00000001;
1115         if(osd_onoff) {
1116                 color_on = QVector4D(0.95, 0.0, 0.05, 1.0);
1117                 color_off = QVector4D(0.05,0.05, 0.05, 0.10);
1118         } else {
1119                 color_on = QVector4D(0.00,0.00, 0.00, 0.0);
1120                 color_off = QVector4D(0.00,0.00, 0.00, 0.0);
1121         }
1122         if(osd_onoff) {
1123                 //if(osd_led_status_bak != osd_led_status) {
1124                         for(int i = 0; i < osd_led_bit_width; i++) {
1125                                 if((bit & osd_led_status) == (bit & osd_led_status_bak)) {
1126                                         bit <<= 1;
1127                                         continue;
1128                                 }
1129                                 drawLedMain(led_pass, i,
1130                                                         ((osd_led_status & bit) != 0) ? color_on : color_off);
1131                                 bit <<= 1;
1132                         }
1133                         osd_led_status_bak = osd_led_status;
1134                 //}
1135         }
1136 }
1137
1138 void GLDraw_3_0::drawOsdIcons()
1139 {
1140         QVector4D color_on;
1141         QVector4D color_off;
1142         uint32_t bit = 0x00000001;
1143         if(osd_onoff) {
1144                 color_on = QVector4D(1.0,  1.0, 1.0, 0.8);
1145                 color_off = QVector4D(1.0, 1.0, 1.0, 0.00);
1146         } else {
1147                 color_on = QVector4D(0.00,0.00, 0.00, 0.0);
1148                 color_off = QVector4D(0.00,0.00, 0.00, 0.0);
1149         }
1150         if(osd_onoff) {
1151                 int major, minor;
1152                 //if(osd_led_status_bak != osd_led_status) {
1153                         for(int i = 0; i < osd_led_bit_width; i++) {
1154                                 if((bit & osd_led_status) == (bit & osd_led_status_bak)) {
1155                                         if((bit & osd_led_status) == 0) { 
1156                                                 bit <<= 1;
1157                                                 continue;
1158                                         }
1159                                 }
1160                                 if((i >= 2) && (i < 10)) { // FD
1161                                         major = 2;
1162                                         minor = i - 2;
1163                                 } else if((i >= 10) && (i < 12)) { // QD
1164                                         major = 3;
1165                                         minor = i - 10;
1166                                 } else if((i >= 12) && (i < 14)) { // CMT(R)
1167                                         major = 4;
1168                                         minor = i - 12;
1169                                 } else if((i >= 14) && (i < 16)) { // CMT(W)
1170                                         major = 5;
1171                                         minor = i - 14;
1172                                 } else {
1173                                         major = 0;
1174                                         minor = 0;
1175                                 }
1176                                 if((major != 0) && (icon_texid[major][minor] != NULL)) {
1177                                         drawMain(osd_pass->getShader(), osd_pass_vao[i], osd_pass_vbuffer[i],
1178                                                          icon_texid[major][minor]->textureId(),
1179                                                          ((osd_led_status & bit) != 0) ? color_on : color_off,
1180                                                          false, false, QVector3D(0.0, 0.0, 0.0));
1181                                 }
1182                                 bit <<= 1;
1183                         }
1184                         osd_led_status_bak = osd_led_status;
1185                 //}
1186         }
1187 }
1188
1189 void GLDraw_3_0::paintGL(void)
1190 {
1191         //p_wid->makeCurrent();
1192
1193 //      if(crt_flag || redraw_required) { //return;
1194                 if(emu_launched) {
1195                         crt_flag = false;
1196                 }
1197                 redraw_required = false;
1198                 extfunc->glViewport(0, 0, p_wid->width(), p_wid->height());
1199                 extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
1200                 
1201                 extfunc->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1202                 extfunc->glEnable(GL_DEPTH_TEST);
1203                 extfunc->glDisable(GL_BLEND);
1204                 if(using_flags->is_use_one_board_computer() || using_flags->is_use_bitmap()) {
1205                         extfunc->glEnable(GL_BLEND);
1206                         drawBitmapTexture();
1207                 }
1208                 if(using_flags->get_max_button() > 0) {
1209                         extfunc->glEnable(GL_BLEND);
1210                         drawButtons();
1211                 }
1212                 drawScreenTexture();
1213                 extfunc->glEnable(GL_BLEND);
1214                 extfunc->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1215                 extfunc->glDisable(GL_DEPTH_TEST);
1216                 //drawOsdLeds();
1217                 if(config.use_osd_virtual_media) drawOsdIcons();
1218                 extfunc->glDisable(GL_BLEND);
1219                 if(!using_flags->is_use_one_board_computer() && (using_flags->get_max_button() <= 0)) {
1220                         drawGrids();
1221                 }
1222                 extfunc->glFlush();
1223 //      } else {
1224 //              extfunc->glViewport(0, 0, p_wid->width(), p_wid->height());
1225 ///             extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
1226 //              //drawOsdLeds();
1227 //              extfunc->glClear(GL_DEPTH_BUFFER_BIT);
1228 //              extfunc->glEnable(GL_DEPTH_TEST);
1229 //              extfunc->glEnable(GL_BLEND);
1230 //              //drawOsdLeds();
1231 //              drawOsdIcons();
1232 //              extfunc->glFlush();
1233 //      }               
1234         //p_wid->doneCurrent();
1235 }
1236
1237 void GLDraw_3_0::setBrightness(GLfloat r, GLfloat g, GLfloat b)
1238 {
1239         fBrightR = r;
1240         fBrightG = g;
1241         fBrightB = b;
1242
1243         if(imgptr != NULL) {
1244                 p_wid->makeCurrent();
1245                 if(uVramTextureID == NULL) {
1246                         uVramTextureID = new QOpenGLTexture(*imgptr);
1247                 }
1248                 if(using_flags->is_use_one_board_computer() || (using_flags->get_max_button() > 0)) {
1249                         uploadMainTexture(imgptr, true);
1250                 } else {
1251                         uploadMainTexture(imgptr, false);
1252                 }
1253                 crt_flag = true;
1254                 p_wid->doneCurrent();
1255         }
1256 }
1257
1258 void GLDraw_3_0::set_texture_vertex(float wmul, float hmul)
1259 {
1260         float wfactor = 1.0f;
1261         float hfactor = 1.0f;
1262
1263         vertexTmpTexture[0].x = -1.0f;
1264         vertexTmpTexture[0].y = -1.0f;
1265         vertexTmpTexture[0].z = -0.1f;
1266         vertexTmpTexture[0].s = 0.0f;
1267         vertexTmpTexture[0].t = 0.0f;
1268         
1269         vertexTmpTexture[1].x = wfactor;
1270         vertexTmpTexture[1].y = -1.0f;
1271         vertexTmpTexture[1].z = -0.1f;
1272         vertexTmpTexture[1].s = wmul;
1273         vertexTmpTexture[1].t = 0.0f;
1274         
1275         vertexTmpTexture[2].x = wfactor;
1276         vertexTmpTexture[2].y = hfactor;
1277         vertexTmpTexture[2].z = -0.1f;
1278         vertexTmpTexture[2].s = wmul;
1279         vertexTmpTexture[2].t = hmul;
1280         
1281         vertexTmpTexture[3].x = -1.0f;
1282         vertexTmpTexture[3].y = hfactor;
1283         vertexTmpTexture[3].z = -0.1f;
1284         vertexTmpTexture[3].s = 0.0f;
1285         vertexTmpTexture[3].t = hmul;
1286 }
1287
1288
1289 void GLDraw_3_0::set_osd_vertex(int xbit)
1290 {
1291         float xbase, ybase, zbase;
1292         VertexTexCoord_t vertex[4];
1293         int major, minor, nl;
1294         int i = xbit;
1295         if((xbit < 0) || (xbit >= 32)) return;
1296         if((i >= 2) && (i < 10)) { // FD
1297                 major = 0;
1298                 minor = i - 2;
1299                 nl = using_flags->get_max_drive();
1300         } else if((i >= 10) && (i < 12)) { // QD
1301                 major = 2;
1302                 minor = i - 10;
1303                 nl = using_flags->get_max_qd();
1304         } else if((i >= 12) && (i < 14)) { // CMT(R)
1305                 major = 1;
1306                 minor = i - 12;
1307                 nl = using_flags->get_max_tape();
1308         } else if((i >= 14) && (i < 16)) { // CMT(W)
1309                 major = 1;
1310                 minor = i - 14;
1311                 nl = using_flags->get_max_tape();
1312         } else if(i >= 16) {
1313                 major = 4 + (i / 8) - 2;
1314                 minor = i % 8;
1315                 nl = 8;
1316         } else {
1317                 major = 6;
1318                 minor = i;
1319                 nl = 2;
1320         }
1321         xbase =  1.0f - (1.0f * 48.0f / 640.0f) * (float)(nl - minor) - (4.0f / 640.0f);;
1322         ybase = -1.0f + (1.0f * 48.0f / 400.0f) * (float)(major + 1) + (4.0f / 400.0f);
1323         zbase = -0.998f;
1324         vertex[0].x = xbase;
1325         vertex[0].y = ybase;
1326         vertex[0].z = zbase;
1327         vertex[0].s = 0.0f;
1328         vertex[0].t = 0.0f;
1329         
1330         vertex[1].x = xbase + (48.0f / 640.0f);
1331         vertex[1].y = ybase;
1332         vertex[1].z = zbase;
1333         vertex[1].s = 1.0f;
1334         vertex[1].t = 0.0f;
1335         
1336         vertex[2].x = xbase + (48.0f / 640.0f);
1337         vertex[2].y = ybase - (48.0f / 400.0f);
1338         vertex[2].z = zbase;
1339         vertex[2].s = 1.0f;
1340         vertex[2].t = 1.0f;
1341         
1342         vertex[3].x = xbase;
1343         vertex[3].y = ybase - (48.0f / 400.0f);
1344         vertex[3].z = zbase;
1345         vertex[3].s = 0.0f;
1346         vertex[3].t = 1.0f;
1347         
1348         setNormalVAO(osd_pass->getShader(), osd_pass_vao[xbit],
1349                                  osd_pass_vbuffer[xbit],
1350                                  vertex, 4);
1351 }
1352
1353 void GLDraw_3_0::set_led_vertex(int xbit)
1354 {
1355         float xbase, ybase, zbase;
1356         VertexTexCoord_t vertex[4];
1357
1358         if((xbit < 0) || (xbit >=32)) return;
1359         xbase = 0.0f + (1.0f / 32.0f) * 31.0f - ((1.0f * (float)xbit) / 32.0f) + (1.0f / 128.0f);
1360         ybase = -1.0f + (2.0f / 64.0f) * 1.5f;
1361         zbase = -0.999f;
1362         vertex[0].x = xbase;
1363         vertex[0].y = ybase;
1364         vertex[0].z = zbase;
1365         vertex[0].s = 0.0f;
1366         vertex[0].t = 0.0f;
1367         
1368         vertex[1].x = xbase + (1.0f / 64.0f);
1369         vertex[1].y = ybase;
1370         vertex[1].z = zbase;
1371         vertex[1].s = 1.0f;
1372         vertex[1].t = 0.0f;
1373         
1374         vertex[2].x = xbase + (1.0f / 64.0f);
1375         vertex[2].y = ybase - (1.0f / 64.0f);
1376         vertex[2].z = zbase;
1377         vertex[2].s = 1.0f;
1378         vertex[2].t = 1.0f;
1379         
1380         vertex[3].x = xbase;
1381         vertex[3].y = ybase - (1.0f / 64.0f);
1382         vertex[3].z = zbase;
1383         vertex[3].s = 0.0f;
1384         vertex[3].t = 1.0f;
1385         
1386         setNormalVAO(led_pass->getShader(), led_pass_vao[xbit],
1387                                  led_pass_vbuffer[xbit],
1388                                  vertex, 4);
1389 }
1390
1391 void GLDraw_3_0::do_set_screen_multiply(float mul)
1392 {
1393         screen_multiply = mul;
1394         do_set_texture_size(imgptr, screen_texture_width, screen_texture_height);
1395 }
1396
1397 void GLDraw_3_0::do_set_texture_size(QImage *p, int w, int h)
1398 {
1399         if(w <= 0) w = using_flags->get_real_screen_width();
1400         if(h <= 0) h = using_flags->get_real_screen_height();
1401         imgptr = p;
1402         float iw, ih;
1403         if(p != NULL) {
1404                 iw = (float)p->width();
1405                 ih = (float)p->height();
1406         } else {
1407                 iw = (float)using_flags->get_real_screen_width();
1408                 ih = (float)using_flags->get_real_screen_height();
1409         }
1410         //if((w == screen_texture_width) && (h == screen_texture_height)) return;
1411         //printf("%dx%d -> %fx%f\n", w, h, iw, ih);
1412         if(p_wid != NULL) {
1413                 screen_texture_width = w;
1414                 screen_texture_height = h;
1415
1416                 p_wid->makeCurrent();
1417                 {
1418                         set_texture_vertex((float)w / iw, (float)h / ih);
1419                         setNormalVAO(std_pass->getShader(), std_pass->getVAO(),
1420                                                  std_pass->getVertexBuffer(),
1421                                                  vertexTmpTexture, 4);
1422                         //set_texture_vertex(p, p_wid->width(), p_wid->height(), w, h);
1423                         set_texture_vertex((float)w / iw, (float)h / ih);
1424                         setNormalVAO(ntsc_pass1->getShader(), ntsc_pass1->getVAO(),
1425                                                  ntsc_pass1->getVertexBuffer(),
1426                                                  vertexTmpTexture, 4);
1427
1428                         set_texture_vertex(1.0f, 1.0f);
1429                         setNormalVAO(ntsc_pass2->getShader(), ntsc_pass2->getVAO(),
1430                                                  ntsc_pass2->getVertexBuffer(),
1431                                                  vertexTmpTexture, 4);
1432                         
1433                 }
1434                 if(p != NULL) {
1435                         if(uVramTextureID != NULL) delete uVramTextureID;
1436                         uVramTextureID = new QOpenGLTexture(*p);
1437                 }
1438                 vertexFormat[0].x = -1.0f;
1439                 vertexFormat[0].y = -1.0f;
1440                 vertexFormat[0].z = -0.9f;
1441                 vertexFormat[1].x =  1.0f;
1442                 vertexFormat[1].y = -1.0f;
1443                 vertexFormat[1].z = -0.9f;
1444                 vertexFormat[2].x =  1.0f;
1445                 vertexFormat[2].y =  1.0f;
1446                 vertexFormat[2].z = -0.9f;
1447                 vertexFormat[3].x = -1.0f;
1448                 vertexFormat[3].y =  1.0f;
1449                 vertexFormat[3].z = -0.9f;
1450
1451                 vertexFormat[0].s = 0.0f;
1452                 //vertexFormat[0].t = (float)h / ih;
1453                 //vertexFormat[1].s = (float)w / iw;
1454                 //vertexFormat[1].t = (float)h / ih;
1455                 //vertexFormat[2].s = (float)w / iw;
1456                 vertexFormat[0].t = 1.0f;
1457                 vertexFormat[1].s = 1.0f;
1458                 vertexFormat[1].t = 1.0f;
1459                 vertexFormat[2].s = 1.0f;
1460                 vertexFormat[2].t = 0.0f;
1461                 vertexFormat[3].s = 0.0f;
1462                 vertexFormat[3].t = 0.0f;
1463                 
1464                 setNormalVAO(main_pass->getShader(), main_pass->getVAO(),
1465                                          main_pass->getVertexBuffer(),
1466                                          vertexFormat, 4);
1467                 
1468                 if(w > using_flags->get_real_screen_width()) {
1469                         w = using_flags->get_real_screen_width();
1470                 }                       
1471                 if(h > using_flags->get_real_screen_height()) {
1472                         h = using_flags->get_real_screen_height();
1473                 }
1474                 this->doSetGridsHorizonal(h, false);
1475                 this->doSetGridsVertical(w, false);
1476                 p_wid->doneCurrent();
1477         }
1478 }
1479 void GLDraw_3_0::do_set_horiz_lines(int lines)
1480 {
1481         if(lines > using_flags->get_real_screen_height()) {
1482                 lines = using_flags->get_real_screen_height();
1483         }                       
1484         this->doSetGridsHorizonal(lines, false);
1485 }
1486
1487 void GLDraw_3_0::resizeGL_Screen(void)
1488 {
1489         if(main_pass != NULL) {
1490                 setNormalVAO(main_pass->getShader(), main_pass->getVAO(),
1491                                          main_pass->getVertexBuffer(),
1492                                          vertexFormat, 4);
1493         }
1494 }       
1495
1496 void GLDraw_3_0::resizeGL(int width, int height)
1497 {
1498         //int side = qMin(width, height);
1499         p_wid->makeCurrent();
1500         extfunc->glViewport(0, 0, width, height);
1501         extfunc->glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
1502         crt_flag = true;
1503         if(!using_flags->is_use_one_board_computer() && (using_flags->get_max_button() <= 0)) {
1504                 doSetGridsHorizonal(vert_lines, true);
1505                 if(using_flags->is_use_vertical_pixel_lines()) {
1506                         doSetGridsVertical(horiz_pixels, true);
1507                 }
1508         }
1509         resizeGL_SetVertexs();
1510         resizeGL_Screen();
1511         if(using_flags->is_use_one_board_computer()) {
1512                 setNormalVAO(bitmap_block->getShader(), bitmap_block->getVAO(),
1513                                          bitmap_block->getVertexBuffer(),
1514                                          vertexBitmap, 4);
1515         }       
1516         if(using_flags->get_max_button() > 0) {
1517                 updateButtonTexture();
1518         }
1519         p_wid->doneCurrent();
1520 }