GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/render/modernopenglgraphics.cpp Lines: 0 514 0.0 %
Date: 2020-06-04 Branches: 0 546 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifdef USE_OPENGL
24
#if !defined(ANDROID) && !defined(__native_client__) && !defined(__SWITCH__)
25
26
#include "render/modernopenglgraphics.h"
27
28
#include "graphicsmanager.h"
29
30
#include "render/opengl/mgl.h"
31
#ifdef __native_client__
32
#include "render/opengl/naclglfunctions.h"
33
#endif  // __native_client__
34
35
#include "render/shaders/shaderprogram.h"
36
#include "render/shaders/shadersmanager.h"
37
38
#include "render/vertexes/imagecollection.h"
39
40
#include "resources/imagerect.h"
41
#include "resources/openglimagehelper.h"
42
43
#include "resources/image/image.h"
44
45
#include "utils/delete2.h"
46
#include "utils/foreach.h"
47
#include "utils/sdlcheckutils.h"
48
#include "utils/sdlhelper.h"
49
50
#include "debug.h"
51
52
#define vertFill2D(var, x1, y1, x2, y2, dstX, dstY, w, h) \
53
    var[vp + 0] = dstX; \
54
    var[vp + 1] = dstY; \
55
    var[vp + 2] = x1; \
56
    var[vp + 3] = y1; \
57
    \
58
    var[vp + 4] = dstX + w; \
59
    var[vp + 5] = dstY; \
60
    var[vp + 6] = x2; \
61
    var[vp + 7] = y1; \
62
    \
63
    var[vp + 8] = dstX + w; \
64
    var[vp + 9] = dstY + h; \
65
    var[vp + 10] = x2; \
66
    var[vp + 11] = y2; \
67
    \
68
    var[vp + 12] = dstX; \
69
    var[vp + 13] = dstY; \
70
    var[vp + 14] = x1; \
71
    var[vp + 15] = y1; \
72
    \
73
    var[vp + 16] = dstX; \
74
    var[vp + 17] = dstY + h; \
75
    var[vp + 18] = x1; \
76
    var[vp + 19] = y2; \
77
    \
78
    var[vp + 20] = dstX + w; \
79
    var[vp + 21] = dstY + h; \
80
    var[vp + 22] = x2; \
81
    var[vp + 23] = y2
82
83
GLuint ModernOpenGLGraphics::mTextureBinded = 0;
84
#ifdef DEBUG_DRAW_CALLS
85
unsigned int ModernOpenGLGraphics::mDrawCalls = 0;
86
unsigned int ModernOpenGLGraphics::mLastDrawCalls = 0;
87
#endif  // DEBUG_DRAW_CALLS
88
89
ModernOpenGLGraphics::ModernOpenGLGraphics() :
90
    mIntArray(nullptr),
91
    mIntArrayCached(nullptr),
92
    mProgram(nullptr),
93
    mAlphaCached(1.0F),
94
    mVpCached(0),
95
    mFloatColor(1.0F),
96
    mMaxVertices(500),
97
    mProgramId(0U),
98
    mSimpleColorUniform(0U),
99
    mPosAttrib(0),
100
    mTextureColorUniform(0U),
101
    mScreenUniform(0U),
102
    mDrawTypeUniform(0U),
103
    mVao(0U),
104
    mVbo(0U),
105
    mEbo(0U),
106
    mVboBinded(0U),
107
    mEboBinded(0U),
108
    mAttributesBinded(0U),
109
    mColorAlpha(false),
110
    mTextureDraw(false),
111
#ifdef DEBUG_BIND_TEXTURE
112
    mOldTexture(),
113
    mOldTextureId(0),
114
#endif  // DEBUG_BIND_TEXTURE
115
    mFbo()
116
{
117
    mOpenGL = RENDER_MODERN_OPENGL;
118
    mName = "modern OpenGL";
119
}
120
121
ModernOpenGLGraphics::~ModernOpenGLGraphics()
122
{
123
    deleteArraysInternal();
124
    deleteGLObjects();
125
}
126
127
void ModernOpenGLGraphics::deleteGLObjects() restrict2
128
{
129
    delete2(mProgram)
130
    if (mVbo != 0U)
131
    {
132
//        logger->log("delete buffer vbo: %u", mVbo);
133
        mglDeleteBuffers(1, &mVbo);
134
    }
135
    if (mEbo != 0U)
136
    {
137
//        logger->log("delete buffer ebo: %u", mEbo);
138
        mglDeleteBuffers(1, &mEbo);
139
    }
140
    if (mVao != 0U)
141
        mglDeleteVertexArrays(1, &mVao);
142
}
143
144
void ModernOpenGLGraphics::initArrays(const int vertCount) restrict2
145
{
146
    mMaxVertices = vertCount;
147
    if (mMaxVertices < 500)
148
        mMaxVertices = 500;
149
    else if (mMaxVertices > 1024)
150
        mMaxVertices = 1024;
151
152
    // need alocate small size, after if limit reached reallocate to double size
153
    const size_t sz = mMaxVertices * 4 + 30;
154
    vertexBufSize = mMaxVertices;
155
    if (mIntArray == nullptr)
156
        mIntArray = new GLint[sz];
157
    if (mIntArrayCached == nullptr)
158
        mIntArrayCached = new GLint[sz];
159
}
160
161
void ModernOpenGLGraphics::postInit() restrict2
162
{
163
    mglGenVertexArrays(1, &mVao);
164
    mglBindVertexArray(mVao);
165
    mglGenBuffers(1, &mVbo);
166
//    logger->log("gen vbo buffer: %u", mVbo);
167
    bindArrayBuffer(mVbo);
168
    mglGenBuffers(1, &mEbo);
169
//    logger->log("gen ebo buffer: %u", mEbo);
170
    bindElementBuffer(mEbo);
171
172
    logger->log("Compiling shaders");
173
    mProgram = shaders.getSimpleProgram();
174
    if (mProgram == nullptr)
175
    {
176
        graphicsManager.logError();
177
        logger->safeError("Shader creation error. See manaplus.log.");
178
    }
179
    mProgramId = mProgram->getProgramId();
180
    if (mProgramId == 0U)
181
        logger->error("Shaders compilation error.");
182
183
    logger->log("Shaders compilation done.");
184
    mglUseProgram(mProgramId);
185
186
    mPosAttrib = mglGetAttribLocation(mProgramId, "position");
187
    mglEnableVertexAttribArray(mPosAttrib);
188
    mglVertexAttribIFormat(mPosAttrib, 4, GL_INT, 0);
189
190
    mSimpleColorUniform = mglGetUniformLocation(mProgramId, "color");
191
    mScreenUniform = mglGetUniformLocation(mProgramId, "screen");
192
    mDrawTypeUniform = mglGetUniformLocation(mProgramId, "drawType");
193
    mTextureColorUniform = mglGetUniformLocation(mProgramId, "alpha");
194
195
    mglUniform1f(mTextureColorUniform, 1.0F);
196
197
    mglBindVertexBuffer(0, mVbo, 0, 4 * sizeof(GLint));
198
    mglVertexAttribBinding(mPosAttrib, 0);
199
//    mglVertexAttribIPointer(mPosAttrib, 4, GL_INT, 4 * sizeof(GLint), 0);
200
    mAttributesBinded = mVbo;
201
202
    mglUniform2f(mScreenUniform,
203
        static_cast<float>(mWidth) / 2.0F,
204
        static_cast<float>(mHeight) / 2.0F);
205
    mglUniform4f(mSimpleColorUniform,
206
        0.0F,
207
        0.0F,
208
        0.0F,
209
        0.0F);
210
}
211
212
void ModernOpenGLGraphics::screenResized() restrict2
213
{
214
    deleteGLObjects();
215
    mVboBinded = 0U;
216
    mEboBinded = 0U;
217
    mAttributesBinded = 0U;
218
    mColor = Color(0, 0, 0, 0);
219
    postInit();
220
}
221
222
void ModernOpenGLGraphics::deleteArrays() restrict2
223
{
224
    deleteArraysInternal();
225
}
226
227
void ModernOpenGLGraphics::deleteArraysInternal() restrict2
228
{
229
    delete [] mIntArray;
230
    mIntArray = nullptr;
231
    delete [] mIntArrayCached;
232
    mIntArrayCached = nullptr;
233
}
234
235
bool ModernOpenGLGraphics::setVideoMode(const int w, const int h,
236
                                        const int scale,
237
                                        const int bpp,
238
                                        const bool fs,
239
                                        const bool hwaccel,
240
                                        const bool resize,
241
                                        const bool noFrame,
242
                                        const bool allowHighDPI) restrict2
243
{
244
    setMainFlags(w, h,
245
        scale,
246
        bpp,
247
        fs,
248
        hwaccel,
249
        resize,
250
        noFrame,
251
        allowHighDPI);
252
253
    return setOpenGLMode();
254
}
255
256
void ModernOpenGLGraphics::setColor(const Color &restrict color) restrict2
257
{
258
    mColorAlpha = (color.a != 255);
259
    if (mColor != color)
260
    {
261
        mColor = color;
262
        mglUniform4f(mSimpleColorUniform,
263
            static_cast<float>(color.r) / 255.0F,
264
            static_cast<float>(color.g) / 255.0F,
265
            static_cast<float>(color.b) / 255.0F,
266
            static_cast<float>(color.a) / 255.0F);
267
    }
268
}
269
270
void ModernOpenGLGraphics::setColorAlpha(const float alpha) restrict2
271
{
272
    if (mAlphaCached != alpha)
273
    {
274
        mAlphaCached = alpha;
275
        mglUniform1f(mTextureColorUniform, alpha);
276
    }
277
}
278
279
void ModernOpenGLGraphics::drawQuad(const int srcX,
280
                                    const int srcY,
281
                                    const int dstX,
282
                                    const int dstY,
283
                                    const int width,
284
                                    const int height) restrict2
285
{
286
    const int texX2 = srcX + width;
287
    const int texY2 = srcY + height;
288
    const int x2 = dstX + width;
289
    const int y2 = dstY + height;
290
291
    GLint vertices[] =
292
    {
293
        dstX, dstY, srcX, srcY,
294
        x2, dstY, texX2, srcY,
295
        dstX, y2, srcX, texY2,
296
        x2, y2, texX2, texY2
297
    };
298
299
//    logger->log("allocate: %d, %ld", mVboBinded, sizeof(vertices));
300
    mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
301
        vertices, GL_STREAM_DRAW);
302
#ifdef DEBUG_DRAW_CALLS
303
    mDrawCalls ++;
304
#endif  // DEBUG_DRAW_CALLS
305
    mglDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
306
#ifdef OPENGLERRORS
307
    graphicsManager.logError();
308
#endif  // OPENGLERRORS
309
}
310
311
void ModernOpenGLGraphics::drawRescaledQuad(const int srcX, const int srcY,
312
                                            const int dstX, const int dstY,
313
                                            const int width, const int height,
314
                                            const int desiredWidth,
315
                                            const int desiredHeight) restrict2
316
{
317
    const int texX2 = srcX + width;
318
    const int texY2 = srcY + height;
319
    const int x2 = dstX + desiredWidth;
320
    const int y2 = dstY + desiredHeight;
321
322
    GLint vertices[] =
323
    {
324
        dstX, dstY, srcX, srcY,
325
        x2, dstY, texX2, srcY,
326
        dstX, y2, srcX, texY2,
327
        x2, y2, texX2, texY2
328
    };
329
330
//    logger->log("allocate: %d, %ld", mVboBinded, sizeof(vertices));
331
    mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
332
        vertices, GL_STREAM_DRAW);
333
#ifdef DEBUG_DRAW_CALLS
334
    mDrawCalls ++;
335
#endif  // DEBUG_DRAW_CALLS
336
    mglDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
337
#ifdef OPENGLERRORS
338
    graphicsManager.logError();
339
#endif  // OPENGLERRORS
340
}
341
342
void ModernOpenGLGraphics::drawImage(const Image *restrict const image,
343
                                     int dstX, int dstY) restrict2
344
{
345
    drawImageInline(image, dstX, dstY);
346
}
347
348
void ModernOpenGLGraphics::drawImageInline(const Image *restrict const image,
349
                                           int dstX, int dstY) restrict2
350
{
351
    FUNC_BLOCK("Graphics::drawImage", 1)
352
    if (image == nullptr)
353
        return;
354
355
#ifdef DEBUG_BIND_TEXTURE
356
    debugBindTexture(image);
357
#endif  // DEBUG_BIND_TEXTURE
358
    bindTexture(GL_TEXTURE_2D, image->mGLImage);
359
    enableTexturingAndBlending();
360
    bindArrayBufferAndAttributes(mVbo);
361
    setColorAlpha(image->mAlpha);
362
363
    const ClipRect &clipArea = mClipStack.top();
364
    const SDL_Rect &imageRect = image->mBounds;
365
    drawQuad(imageRect.x, imageRect.y,
366
        dstX + clipArea.xOffset, dstY + clipArea.yOffset,
367
        imageRect.w, imageRect.h);
368
}
369
370
void ModernOpenGLGraphics::copyImage(const Image *restrict const image,
371
                                     int dstX, int dstY) restrict2
372
{
373
    drawImageInline(image, dstX, dstY);
374
}
375
376
void ModernOpenGLGraphics::testDraw() restrict2
377
{
378
/*
379
    GLint vertices[] =
380
    {
381
        0, 0, 0, 0,
382
        800, 0, 800, 0,
383
        0, 600, 0, 600,
384
        800, 600, 800, 600
385
    };
386
*/
387
//    logger->log("allocate: %d, %ld", mVboBinded, sizeof(vertices));
388
//    logger->log("allocate ebo: %d, %ld", mEboBinded, sizeof(elements));
389
//    mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
390
//        vertices, GL_STREAM_DRAW);
391
//    mglBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements),
392
//        elements, GL_STREAM_DRAW);
393
#ifdef DEBUG_DRAW_CALLS
394
    mDrawCalls ++;
395
#endif  // DEBUG_DRAW_CALLS
396
    mglDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
397
//    glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, 0);
398
#ifdef OPENGLERRORS
399
    graphicsManager.logError();
400
#endif  // OPENGLERRORS
401
}
402
403
void ModernOpenGLGraphics::drawImageCached(const Image *restrict const image
404
                                           A_UNUSED,
405
                                           int A_UNUSED x,
406
                                           int y A_UNUSED) restrict2
407
{
408
}
409
410
void ModernOpenGLGraphics::drawPatternCached(const Image *restrict const image
411
                                             A_UNUSED,
412
                                             const int x A_UNUSED,
413
                                             const int y A_UNUSED,
414
                                             const int w A_UNUSED,
415
                                             const int h A_UNUSED) restrict2
416
{
417
}
418
419
void ModernOpenGLGraphics::completeCache() restrict2
420
{
421
}
422
423
void ModernOpenGLGraphics::drawRescaledImage(const Image *restrict const image,
424
                                             int dstX, int dstY,
425
                                             const int desiredWidth,
426
                                             const int desiredHeight) restrict2
427
{
428
    if (image == nullptr)
429
        return;
430
431
    const SDL_Rect &imageRect = image->mBounds;
432
433
    // Just draw the image normally when no resizing is necessary,
434
    if (imageRect.w == desiredWidth && imageRect.h == desiredHeight)
435
    {
436
        drawImageInline(image, dstX, dstY);
437
        return;
438
    }
439
440
    setColorAlpha(image->mAlpha);
441
#ifdef DEBUG_BIND_TEXTURE
442
    debugBindTexture(image);
443
#endif  // DEBUG_BIND_TEXTURE
444
    bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage);
445
    enableTexturingAndBlending();
446
    bindArrayBufferAndAttributes(mVbo);
447
448
    const ClipRect &clipArea = mClipStack.top();
449
    // Draw a textured quad.
450
    drawRescaledQuad(imageRect.x, imageRect.y,
451
        dstX + clipArea.xOffset, dstY + clipArea.yOffset,
452
        imageRect.w, imageRect.h,
453
        desiredWidth, desiredHeight);
454
}
455
456
void ModernOpenGLGraphics::drawPattern(const Image *restrict const image,
457
                                       const int x, const int y,
458
                                       const int w, const int h) restrict2
459
{
460
    drawPatternInline(image, x, y, w, h);
461
}
462
463
void ModernOpenGLGraphics::drawPatternInline(const Image *restrict const image,
464
                                             const int x,
465
                                             const int y,
466
                                             const int w,
467
                                             const int h) restrict2
468
{
469
    if (image == nullptr)
470
        return;
471
472
    const SDL_Rect &imageRect = image->mBounds;
473
    const int srcX = imageRect.x;
474
    const int srcY = imageRect.y;
475
    const int iw = imageRect.w;
476
    const int ih = imageRect.h;
477
478
    if (iw == 0 || ih == 0)
479
        return;
480
481
    const ClipRect &clipArea = mClipStack.top();
482
    const int x2 = x + clipArea.xOffset;
483
    const int y2 = y + clipArea.yOffset;
484
485
#ifdef DEBUG_BIND_TEXTURE
486
    debugBindTexture(image);
487
#endif  // DEBUG_BIND_TEXTURE
488
489
    bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage);
490
491
    enableTexturingAndBlending();
492
    bindArrayBufferAndAttributes(mVbo);
493
    setColorAlpha(image->mAlpha);
494
495
    unsigned int vp = 0;
496
    const unsigned int vLimit = mMaxVertices * 4;
497
498
    for (int py = 0; py < h; py += ih)
499
    {
500
        const int height = (py + ih >= h) ? h - py : ih;
501
        const int texY2 = srcY + height;
502
        const int dstY = y2 + py;
503
        for (int px = 0; px < w; px += iw)
504
        {
505
            const int width = (px + iw >= w) ? w - px : iw;
506
            const int dstX = x2 + px;
507
508
            const int texX2 = srcX + width;
509
510
            vertFill2D(mIntArray,
511
                srcX, srcY, texX2, texY2,
512
                dstX, dstY, width, height);
513
514
            vp += 24;
515
            if (vp >= vLimit)
516
            {
517
                drawTriangleArray(vp);
518
                vp = 0;
519
            }
520
        }
521
    }
522
    if (vp > 0)
523
        drawTriangleArray(vp);
524
}
525
526
void ModernOpenGLGraphics::drawRescaledPattern(const Image *
527
                                               restrict const image,
528
                                               const int x, const int y,
529
                                               const int w, const int h,
530
                                               const int scaledWidth,
531
                                               const int scaledHeight)
532
                                               restrict2
533
{
534
    if (image == nullptr)
535
        return;
536
537
    if (scaledWidth == 0 || scaledHeight == 0)
538
        return;
539
540
    const SDL_Rect &imageRect = image->mBounds;
541
    const int srcX = imageRect.x;
542
    const int srcY = imageRect.y;
543
    const int iw = imageRect.w;
544
    const int ih = imageRect.h;
545
    if (iw == 0 || ih == 0)
546
        return;
547
548
#ifdef DEBUG_BIND_TEXTURE
549
    debugBindTexture(image);
550
#endif  // DEBUG_BIND_TEXTURE
551
552
    bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage);
553
554
    enableTexturingAndBlending();
555
    bindArrayBufferAndAttributes(mVbo);
556
    setColorAlpha(image->mAlpha);
557
558
    unsigned int vp = 0;
559
    const unsigned int vLimit = mMaxVertices * 4;
560
561
    const ClipRect &clipArea = mClipStack.top();
562
    const int x2 = x + clipArea.xOffset;
563
    const int y2 = y + clipArea.yOffset;
564
565
    const float scaleFactorW = static_cast<float>(scaledWidth) / iw;
566
    const float scaleFactorH = static_cast<float>(scaledHeight) / ih;
567
568
    for (int py = 0; py < h; py += scaledHeight)
569
    {
570
        const int height = (py + scaledHeight >= h)
571
            ? h - py : scaledHeight;
572
        const int dstY = y2 + py;
573
        const int scaledY = srcY + height / scaleFactorH;
574
        for (int px = 0; px < w; px += scaledWidth)
575
        {
576
            const int width = (px + scaledWidth >= w)
577
                ? w - px : scaledWidth;
578
            const int dstX = x2 + px;
579
            const int scaledX = srcX + width / scaleFactorW;
580
581
            vertFill2D(mIntArray,
582
                srcX, srcY, scaledX, scaledY,
583
                dstX, dstY, width, height);
584
585
            vp += 24;
586
            if (vp >= vLimit)
587
            {
588
                drawTriangleArray(vp);
589
                vp = 0;
590
            }
591
        }
592
    }
593
    if (vp > 0)
594
        drawTriangleArray(vp);
595
}
596
597
inline void ModernOpenGLGraphics::drawVertexes(const
598
                                               OpenGLGraphicsVertexes &
599
                                               restrict ogl) restrict2
600
{
601
    const STD_VECTOR<int> &vp = ogl.mVp;
602
    const STD_VECTOR<GLuint> &vbos = ogl.mVbo;
603
    STD_VECTOR<int>::const_iterator ivp;
604
    STD_VECTOR<GLuint>::const_iterator ivbo;
605
    const STD_VECTOR<int>::const_iterator ivp_end = vp.end();
606
607
/*
608
    if (vp.size() != vbos.size())
609
        logger->log("different size in vp and vbos");
610
*/
611
612
    for (ivp = vp.begin(), ivbo = vbos.begin();
613
         ivp != ivp_end;
614
         ++ ivp, ++ ivbo)
615
    {
616
        bindArrayBufferAndAttributes(*ivbo);
617
#ifdef DEBUG_DRAW_CALLS
618
        mDrawCalls ++;
619
#endif  // DEBUG_DRAW_CALLS
620
//        logger->log("draw from array: %u", *ivbo);
621
        mglDrawArrays(GL_TRIANGLES, 0, *ivp / 4);
622
#ifdef OPENGLERRORS
623
        graphicsManager.logError();
624
#endif  // OPENGLERRORS
625
    }
626
}
627
628
void ModernOpenGLGraphics::calcPattern(ImageVertexes *restrict const vert,
629
                                       const Image *restrict const image,
630
                                       const int x,
631
                                       const int y,
632
                                       const int w,
633
                                       const int h) const restrict2
634
{
635
    calcPatternInline(vert, image, x, y, w, h);
636
}
637
638
void ModernOpenGLGraphics::calcPatternInline(ImageVertexes *
639
                                             restrict const vert,
640
                                             const Image *restrict const image,
641
                                             const int x,
642
                                             const int y,
643
                                             const int w,
644
                                             const int h) const restrict2
645
{
646
    if (image == nullptr || vert == nullptr)
647
        return;
648
649
    const SDL_Rect &imageRect = image->mBounds;
650
    const int srcX = imageRect.x;
651
    const int srcY = imageRect.y;
652
    const int iw = imageRect.w;
653
    const int ih = imageRect.h;
654
655
    if (iw == 0 || ih == 0)
656
        return;
657
658
    const ClipRect &clipArea = mClipStack.top();
659
    const int x2 = x + clipArea.xOffset;
660
    const int y2 = y + clipArea.yOffset;
661
662
    const unsigned int vLimit = mMaxVertices * 4;
663
664
    OpenGLGraphicsVertexes &ogl = vert->ogl;
665
    unsigned int vp = ogl.continueVp();
666
667
    GLint *intArray = ogl.continueIntTexArray();
668
669
    for (int py = 0; py < h; py += ih)
670
    {
671
        const int height = (py + ih >= h) ? h - py : ih;
672
        const int dstY = y2 + py;
673
        const int texY2 = srcY + height;
674
        for (int px = 0; px < w; px += iw)
675
        {
676
            const int width = (px + iw >= w) ? w - px : iw;
677
            const int dstX = x2 + px;
678
            const int texX2 = srcX + width;
679
680
            vertFill2D(intArray,
681
                srcX, srcY, texX2, texY2,
682
                dstX, dstY, width, height);
683
684
            vp += 24;
685
            if (vp >= vLimit)
686
            {
687
                intArray = ogl.switchIntTexArray();
688
                ogl.switchVp(vp);
689
                vp = 0;
690
            }
691
        }
692
    }
693
    ogl.switchVp(vp);
694
}
695
696
void ModernOpenGLGraphics::calcTileCollection(ImageCollection *
697
                                              restrict const vertCol,
698
                                              const Image *
699
                                              restrict const image,
700
                                              int x, int y) restrict2
701
{
702
    if (vertCol == nullptr || image == nullptr)
703
        return;
704
    if (vertCol->currentGLImage != image->mGLImage)
705
    {
706
        ImageVertexes *const vert = new ImageVertexes;
707
        vertCol->currentGLImage = image->mGLImage;
708
        vertCol->currentVert = vert;
709
        vert->image = image;
710
        vertCol->draws.push_back(vert);
711
        calcTileVertexesInline(vert, image, x, y);
712
    }
713
    else
714
    {
715
        calcTileVertexesInline(vertCol->currentVert, image, x, y);
716
    }
717
}
718
719
void ModernOpenGLGraphics::drawTileCollection(const ImageCollection
720
                                              *restrict const vertCol)
721
                                              restrict2
722
{
723
    enableTexturingAndBlending();
724
/*
725
    if (!vertCol)
726
    {
727
        logger->log("ModernOpenGLGraphics::drawTileCollection"
728
            " vertCol is nullptr");
729
    }
730
*/
731
    const ImageVertexesVector &draws = vertCol->draws;
732
    const ImageCollectionCIter it_end = draws.end();
733
    for (ImageCollectionCIter it = draws.begin(); it != it_end; ++ it)
734
    {
735
        const ImageVertexes *const vert = *it;
736
        const Image *const image = vert->image;
737
738
        setColorAlpha(image->mAlpha);
739
#ifdef DEBUG_BIND_TEXTURE
740
        debugBindTexture(image);
741
#endif  // DEBUG_BIND_TEXTURE
742
743
        bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage);
744
        drawVertexes(vert->ogl);
745
    }
746
}
747
748
void ModernOpenGLGraphics::calcPattern(ImageCollection *restrict const vertCol,
749
                                       const Image *restrict const image,
750
                                       const int x,
751
                                       const int y,
752
                                       const int w,
753
                                       const int h) const restrict2
754
{
755
    if (vertCol == nullptr || image == nullptr)
756
        return;
757
    ImageVertexes *vert = nullptr;
758
    if (vertCol->currentGLImage != image->mGLImage)
759
    {
760
        vert = new ImageVertexes;
761
        vertCol->currentGLImage = image->mGLImage;
762
        vertCol->currentVert = vert;
763
        vert->image = image;
764
        vertCol->draws.push_back(vert);
765
    }
766
    else
767
    {
768
        vert = vertCol->currentVert;
769
    }
770
771
    calcPatternInline(vert, image, x, y, w, h);
772
}
773
774
void ModernOpenGLGraphics::calcTileVertexes(ImageVertexes *restrict const vert,
775
                                            const Image *restrict const image,
776
                                            int dstX, int dstY) const restrict2
777
{
778
    calcTileVertexesInline(vert, image, dstX, dstY);
779
}
780
781
void ModernOpenGLGraphics::calcTileVertexesInline(ImageVertexes *
782
                                                  restrict const vert,
783
                                                  const Image *
784
                                                  restrict const image,
785
                                                  int dstX,
786
                                                  int dstY) const restrict2
787
{
788
    const SDL_Rect &imageRect = image->mBounds;
789
    const int srcX = imageRect.x;
790
    const int srcY = imageRect.y;
791
    const int w = imageRect.w;
792
    const int h = imageRect.h;
793
794
    if (w == 0 || h == 0)
795
        return;
796
797
    const ClipRect &clipArea = mClipStack.top();
798
    const int x2 = dstX + clipArea.xOffset;
799
    const int y2 = dstY + clipArea.yOffset;
800
801
    const unsigned int vLimit = mMaxVertices * 4;
802
803
    OpenGLGraphicsVertexes &ogl = vert->ogl;
804
805
    unsigned int vp = ogl.continueVp();
806
807
    int texX2 = srcX + w;
808
    int texY2 = srcY + h;
809
810
    GLint *const intArray = ogl.continueIntTexArray();
811
812
    vertFill2D(intArray,
813
        srcX, srcY, texX2, texY2,
814
        x2, y2, w, h);
815
816
    vp += 24;
817
    if (vp >= vLimit)
818
    {
819
        ogl.switchIntTexArray();
820
        ogl.switchVp(vp);
821
        vp = 0;
822
    }
823
824
    ogl.switchVp(vp);
825
}
826
827
void ModernOpenGLGraphics::drawTileVertexes(const ImageVertexes *
828
                                            restrict const vert) restrict2
829
{
830
    if (vert == nullptr)
831
        return;
832
    const Image *const image = vert->image;
833
834
    setColorAlpha(image->mAlpha);
835
#ifdef DEBUG_BIND_TEXTURE
836
    debugBindTexture(image);
837
#endif  // DEBUG_BIND_TEXTURE
838
839
    bindTexture(OpenGLImageHelper::mTextureType, image->mGLImage);
840
    enableTexturingAndBlending();
841
    bindArrayBufferAndAttributes(mVbo);
842
843
    drawVertexes(vert->ogl);
844
}
845
846
void ModernOpenGLGraphics::calcWindow(ImageCollection *restrict const vertCol,
847
                                      const int x, const int y,
848
                                      const int w, const int h,
849
                                      const ImageRect &restrict imgRect)
850
                                      restrict2
851
{
852
    ImageVertexes *vert = nullptr;
853
    const Image *const image = imgRect.grid[4];
854
    if (image == nullptr)
855
        return;
856
    if (vertCol->currentGLImage != image->mGLImage)
857
    {
858
        vert = new ImageVertexes;
859
        vertCol->currentGLImage = image->mGLImage;
860
        vertCol->currentVert = vert;
861
        vert->image = image;
862
        vertCol->draws.push_back(vert);
863
    }
864
    else
865
    {
866
        vert = vertCol->currentVert;
867
    }
868
    calcImageRect(vert, x, y, w, h, imgRect);
869
}
870
871
void ModernOpenGLGraphics::updateScreen() restrict2
872
{
873
    BLOCK_START("Graphics::updateScreen")
874
#ifdef DEBUG_DRAW_CALLS
875
    mLastDrawCalls = mDrawCalls;
876
    mDrawCalls = 0;
877
#endif  // DEBUG_DRAW_CALLS
878
#ifdef USE_SDL2
879
    SDL_GL_SwapWindow(mWindow);
880
#else  // USE_SDL2
881
    SDL_GL_SwapBuffers();
882
#endif  // USE_SDL2
883
#ifdef DEBUG_OPENGL
884
    if (isGLNotNull(mglFrameTerminator))
885
        mglFrameTerminator();
886
#endif  // DEBUG_OPENGL
887
    BLOCK_END("Graphics::updateScreen")
888
}
889
890
void ModernOpenGLGraphics::beginDraw() restrict2
891
{
892
    setOpenGLFlags();
893
    mglDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
894
    mglHint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT_ARB, GL_FASTEST);
895
    pushClipArea(Rect(0, 0, mRect.w, mRect.h));
896
}
897
898
void ModernOpenGLGraphics::endDraw() restrict2
899
{
900
    popClipArea();
901
}
902
903
void ModernOpenGLGraphics::pushClipArea(const Rect &restrict area) restrict2
904
{
905
    Graphics::pushClipArea(area);
906
    const ClipRect &clipArea = mClipStack.top();
907
908
    mglScissor(clipArea.x * mScale,
909
        (mRect.h - clipArea.y - clipArea.height) * mScale,
910
        clipArea.width * mScale,
911
        clipArea.height * mScale);
912
}
913
914
void ModernOpenGLGraphics::popClipArea() restrict2
915
{
916
    if (mClipStack.empty())
917
        return;
918
    Graphics::popClipArea();
919
    if (mClipStack.empty())
920
        return;
921
922
    const ClipRect &clipArea = mClipStack.top();
923
    mglScissor(clipArea.x * mScale,
924
        (mRect.h - clipArea.y - clipArea.height) * mScale,
925
        clipArea.width * mScale,
926
        clipArea.height * mScale);
927
}
928
929
void ModernOpenGLGraphics::drawPoint(int x, int y) restrict2
930
{
931
    disableTexturingAndBlending();
932
    bindArrayBufferAndAttributes(mVbo);
933
    const ClipRect &clipArea = mClipStack.top();
934
    GLint vertices[] =
935
    {
936
        x + clipArea.xOffset, y + clipArea.yOffset, 0, 0
937
    };
938
//    logger->log("allocate: %d, %ld", mVboBinded, sizeof(vertices));
939
    mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
940
        vertices, GL_STREAM_DRAW);
941
#ifdef DEBUG_DRAW_CALLS
942
    mDrawCalls ++;
943
#endif  // DEBUG_DRAW_CALLS
944
    mglDrawArrays(GL_POINTS, 0, 1);
945
#ifdef OPENGLERRORS
946
    graphicsManager.logError();
947
#endif  // OPENGLERRORS
948
}
949
950
void ModernOpenGLGraphics::drawLine(int x1, int y1,
951
                                    int x2, int y2) restrict2
952
{
953
    disableTexturingAndBlending();
954
    bindArrayBufferAndAttributes(mVbo);
955
    const ClipRect &clipArea = mClipStack.top();
956
    GLint vertices[] =
957
    {
958
        x1 + clipArea.xOffset, y1 + clipArea.yOffset, 0, 0,
959
        x2 + clipArea.xOffset, y2 + clipArea.yOffset, 0, 0
960
    };
961
//    logger->log("allocate: %d, %ld", mVboBinded, sizeof(vertices));
962
    mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
963
        vertices, GL_STREAM_DRAW);
964
#ifdef DEBUG_DRAW_CALLS
965
    mDrawCalls ++;
966
#endif  // DEBUG_DRAW_CALLS
967
    mglDrawArrays(GL_LINES, 0, 2);
968
#ifdef OPENGLERRORS
969
    graphicsManager.logError();
970
#endif  // OPENGLERRORS
971
}
972
973
void ModernOpenGLGraphics::drawRectangle(const Rect &restrict rect) restrict2
974
{
975
    disableTexturingAndBlending();
976
    bindArrayBufferAndAttributes(mVbo);
977
    const ClipRect &clipArea = mClipStack.top();
978
    const int x1 = rect.x + clipArea.xOffset;
979
    const int y1 = rect.y + clipArea.yOffset;
980
    const int x2 = x1 + rect.width;
981
    const int y2 = y1 + rect.height;
982
    GLint vertices[] =
983
    {
984
        x1, y1, 0, 0,
985
        x1, y2, 0, 0,
986
        x2, y2, 0, 0,
987
        x2, y1, 0, 0
988
    };
989
990
//    logger->log("allocate: %d, %ld", mVboBinded, sizeof(vertices));
991
    mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
992
        vertices, GL_STREAM_DRAW);
993
#ifdef DEBUG_DRAW_CALLS
994
    mDrawCalls ++;
995
#endif  // DEBUG_DRAW_CALLS
996
    mglDrawArrays(GL_LINE_LOOP, 0, 4);
997
#ifdef OPENGLERRORS
998
    graphicsManager.logError();
999
#endif  // OPENGLERRORS
1000
}
1001
1002
void ModernOpenGLGraphics::fillRectangle(const Rect &restrict rect) restrict2
1003
{
1004
    disableTexturingAndBlending();
1005
    bindArrayBufferAndAttributes(mVbo);
1006
    const ClipRect &clipArea = mClipStack.top();
1007
    const int x1 = rect.x + clipArea.xOffset;
1008
    const int y1 = rect.y + clipArea.yOffset;
1009
    const int x2 = x1 + rect.width;
1010
    const int y2 = y1 + rect.height;
1011
    GLint vertices[] =
1012
    {
1013
        x1, y1, 0, 0,
1014
        x2, y1, 0, 0,
1015
        x1, y2, 0, 0,
1016
        x2, y2, 0, 0
1017
    };
1018
1019
//    logger->log("allocate: %d, %ld", mVboBinded, sizeof(vertices));
1020
    mglBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
1021
        vertices, GL_STREAM_DRAW);
1022
#ifdef DEBUG_DRAW_CALLS
1023
    mDrawCalls ++;
1024
#endif  // DEBUG_DRAW_CALLS
1025
    mglDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1026
#ifdef OPENGLERRORS
1027
    graphicsManager.logError();
1028
#endif  // OPENGLERRORS
1029
}
1030
1031
void ModernOpenGLGraphics::enableTexturingAndBlending() restrict2
1032
{
1033
    if (!mTextureDraw)
1034
    {
1035
        mTextureDraw = true;
1036
        mglUniform1f(mDrawTypeUniform, 1.0F);
1037
    }
1038
    if (!mAlpha)
1039
    {
1040
        mglEnable(GL_BLEND);
1041
        mAlpha = true;
1042
    }
1043
}
1044
1045
void ModernOpenGLGraphics::disableTexturingAndBlending() restrict2
1046
{
1047
    if (mTextureDraw)
1048
    {
1049
        mTextureDraw = false;
1050
        mglUniform1f(mDrawTypeUniform, 0.0F);
1051
    }
1052
    if (mAlpha && !mColorAlpha)
1053
    {
1054
        mglDisable(GL_BLEND);
1055
        mAlpha = false;
1056
    }
1057
    else if (!mAlpha && mColorAlpha)
1058
    {
1059
        mglEnable(GL_BLEND);
1060
        mAlpha = true;
1061
    }
1062
}
1063
1064
void ModernOpenGLGraphics::drawRectangle(const Rect &restrict rect A_UNUSED,
1065
                                         const bool filled A_UNUSED) restrict2
1066
{
1067
}
1068
1069
void ModernOpenGLGraphics::drawNet(const int x1, const int y1,
1070
                                   const int x2, const int y2,
1071
                                   const int width, const int height) restrict2
1072
{
1073
    unsigned int vp = 0;
1074
    const unsigned int vLimit = mMaxVertices * 4;
1075
1076
    disableTexturingAndBlending();
1077
    bindArrayBufferAndAttributes(mVbo);
1078
    const ClipRect &clipArea = mClipStack.top();
1079
    const GLint dx = clipArea.xOffset;
1080
    const GLint dy = clipArea.yOffset;
1081
1082
    const GLint xs1 = x1 + dx;
1083
    const GLint xs2 = x2 + dx;
1084
    const GLint ys1 = y1 + dy;
1085
    const GLint ys2 = y2 + dy;
1086
1087
    for (int y = y1; y < y2; y += height)
1088
    {
1089
        mIntArray[vp + 0] = xs1;
1090
        mIntArray[vp + 1] = y;
1091
        mIntArray[vp + 2] = 0;
1092
        mIntArray[vp + 3] = 0;
1093
1094
        mIntArray[vp + 4] = xs2;
1095
        mIntArray[vp + 5] = y;
1096
        mIntArray[vp + 6] = 0;
1097
        mIntArray[vp + 7] = 0;
1098
1099
        vp += 8;
1100
        if (vp >= vLimit)
1101
        {
1102
            drawLineArrays(vp);
1103
            vp = 0;
1104
        }
1105
    }
1106
1107
    for (int x = x1; x < x2; x += width)
1108
    {
1109
        mIntArray[vp + 0] = x;
1110
        mIntArray[vp + 1] = ys1;
1111
        mIntArray[vp + 2] = 0.0F;
1112
        mIntArray[vp + 3] = 0.0F;
1113
1114
        mIntArray[vp + 4] = x;
1115
        mIntArray[vp + 5] = ys2;
1116
        mIntArray[vp + 6] = 0.0F;
1117
        mIntArray[vp + 7] = 0.0F;
1118
1119
        vp += 8;
1120
        if (vp >= vLimit)
1121
        {
1122
            drawLineArrays(vp);
1123
            vp = 0;
1124
        }
1125
    }
1126
1127
    if (vp > 0)
1128
        drawLineArrays(vp);
1129
}
1130
1131
void ModernOpenGLGraphics::bindTexture(const GLenum target,
1132
                                       const GLuint texture)
1133
{
1134
    if (mTextureBinded != texture)
1135
    {
1136
        mTextureBinded = texture;
1137
        mglBindTexture(target, texture);
1138
    }
1139
}
1140
1141
void ModernOpenGLGraphics::removeArray(const uint32_t sz,
1142
                                       uint32_t *restrict const arr) restrict2
1143
{
1144
    mglDeleteBuffers(sz, arr);
1145
    for (size_t f = 0; f < sz; f ++)
1146
    {
1147
        if (arr[f] == mVboBinded)
1148
            mVboBinded = 0;
1149
//        logger->log("delete buffers: %u", arr[f]);
1150
    }
1151
}
1152
1153
void ModernOpenGLGraphics::bindArrayBuffer(const GLuint vbo) restrict2
1154
{
1155
    if (mVboBinded != vbo)
1156
    {
1157
        mVboBinded = vbo;
1158
//        logger->log("bind array: %u", vbo);
1159
        mglBindBuffer(GL_ARRAY_BUFFER, vbo);
1160
/*
1161
        if (mglIsBuffer(vbo) != GL_TRUE)
1162
            logger->log("bind wrong buffer: %u", vbo);
1163
*/
1164
        mAttributesBinded = 0U;
1165
    }
1166
}
1167
1168
void ModernOpenGLGraphics::bindElementBuffer(const GLuint ebo) restrict2
1169
{
1170
    if (mEboBinded != ebo)
1171
    {
1172
        mEboBinded = ebo;
1173
        logger->log("bind element: %u", ebo);
1174
        mglBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
1175
/*
1176
        if (mglIsBuffer(ebo) != GL_TRUE)
1177
            logger->log("bind wrong buffer: %u", vbo);
1178
*/
1179
    }
1180
}
1181
1182
void ModernOpenGLGraphics::bindArrayBufferAndAttributes(const GLuint vbo)
1183
                                                        restrict2
1184
{
1185
    if (mVboBinded != vbo)
1186
    {
1187
        mVboBinded = vbo;
1188
//        logger->log("bind array: %u", vbo);
1189
        mglBindBuffer(GL_ARRAY_BUFFER, vbo);
1190
/*
1191
        if (mglIsBuffer(vbo) != GL_TRUE)
1192
            logger->log("bind wrong buffer: %u", vbo);
1193
*/
1194
1195
        mAttributesBinded = mVboBinded;
1196
//        logger->log("bind vertex buffer: %u", mVboBinded);
1197
        mglBindVertexBuffer(0, mVboBinded, 0, 4 * sizeof(GLint));
1198
//        mglVertexAttribIPointer(mPosAttrib, 4, GL_INT, 4 * sizeof(GLint), 0);
1199
//        mglVertexAttribBinding(mPosAttrib, 0);
1200
    }
1201
    else if (mAttributesBinded != mVboBinded)
1202
    {
1203
        mAttributesBinded = mVboBinded;
1204
//        logger->log("bind vertex buffer: %u", mVboBinded);
1205
        mglBindVertexBuffer(0, mVboBinded, 0, 4 * sizeof(GLint));
1206
//        mglVertexAttribIPointer(mPosAttrib, 4, GL_INT, 4 * sizeof(GLint), 0);
1207
//        mglVertexAttribBinding(mPosAttrib, 0);
1208
    }
1209
}
1210
1211
void ModernOpenGLGraphics::bindAttributes() restrict2
1212
{
1213
    if (mAttributesBinded != mVboBinded)
1214
    {
1215
        mAttributesBinded = mVboBinded;
1216
        mglBindVertexBuffer(0, mVboBinded, 0, 4 * sizeof(GLint));
1217
//        mglVertexAttribIPointer(mPosAttrib, 4, GL_INT, 4 * sizeof(GLint), 0);
1218
//        mglVertexAttribBinding(mPosAttrib, 0);
1219
    }
1220
}
1221
1222
void ModernOpenGLGraphics::dumpSettings()
1223
{
1224
    GLint test[1000];
1225
    logger->log("\n\n");
1226
    logger->log("start opengl dump");
1227
    for (int f = 0; f < 65535; f ++)
1228
    {
1229
        test[0] = 0;
1230
        test[1] = 0;
1231
        test[2] = 0;
1232
        test[3] = 0;
1233
        mglGetIntegerv(f, &test[0]);
1234
        if (test[0] != 0 || test[1] != 0 || test[2] != 0 || test[3] != 0)
1235
        {
1236
            logger->log("\n%d = %d, %d, %d, %d", f,
1237
                test[0], test[1], test[2], test[3]);
1238
        }
1239
    }
1240
}
1241
1242
void ModernOpenGLGraphics::drawImageRect(const int x, const int y,
1243
                                         const int w, const int h,
1244
                                         const ImageRect &restrict imgRect)
1245
                                         restrict2
1246
{
1247
    #include "render/graphics_drawImageRect.hpp"
1248
}
1249
1250
void ModernOpenGLGraphics::calcImageRect(ImageVertexes *restrict const vert,
1251
                                         const int x, const int y,
1252
                                         const int w, const int h,
1253
                                         const ImageRect &restrict imgRect)
1254
                                         restrict2
1255
{
1256
    #include "render/graphics_calcImageRect.hpp"
1257
}
1258
1259
void ModernOpenGLGraphics::clearScreen() const restrict2
1260
{
1261
    mglClear(GL_COLOR_BUFFER_BIT |
1262
        GL_DEPTH_BUFFER_BIT |
1263
        GL_STENCIL_BUFFER_BIT);
1264
}
1265
1266
void ModernOpenGLGraphics::createGLContext(const bool custom) restrict2
1267
{
1268
    if (custom)
1269
    {
1270
        if (mGLContext != nullptr)
1271
            SDL::makeCurrentContext(mGLContext);
1272
        else
1273
            mGLContext = SDL::createGLContext(mWindow, 3, 3, 0x01);
1274
    }
1275
    else
1276
    {
1277
        Graphics::createGLContext(false);
1278
    }
1279
}
1280
1281
void ModernOpenGLGraphics::finalize(ImageCollection *restrict const col)
1282
                                    restrict2
1283
{
1284
    if (col == nullptr)
1285
        return;
1286
    FOR_EACH (ImageCollectionIter, it, col->draws)
1287
        finalize(*it);
1288
}
1289
1290
void ModernOpenGLGraphics::finalize(ImageVertexes *restrict const vert)
1291
                                    restrict2
1292
{
1293
    // in future need convert in each switchVp/continueVp
1294
1295
    if (vert == nullptr)
1296
        return;
1297
    OpenGLGraphicsVertexes &ogl = vert->ogl;
1298
    const STD_VECTOR<int> &vp = ogl.mVp;
1299
    STD_VECTOR<int>::const_iterator ivp;
1300
    const STD_VECTOR<int>::const_iterator ivp_end = vp.end();
1301
    STD_VECTOR<GLint*> &intTexPool = ogl.mIntTexPool;
1302
    STD_VECTOR<GLint*>::const_iterator ft;
1303
    const STD_VECTOR<GLint*>::const_iterator ft_end = intTexPool.end();
1304
    STD_VECTOR<GLuint> &vbos = ogl.mVbo;
1305
    STD_VECTOR<GLuint>::const_iterator ivbo;
1306
1307
    const int sz = CAST_S32(intTexPool.size());
1308
    if (sz == 0)
1309
        return;
1310
    vbos.resize(sz);
1311
    mglGenBuffers(sz, &vbos[0]);
1312
/*
1313
    for (int f = 0; f < sz; f ++)
1314
        logger->log("gen buffers: %u", vbos[f]);
1315
*/
1316
1317
    for (ft = intTexPool.begin(), ivp = vp.begin(), ivbo = vbos.begin();
1318
         ft != ft_end && ivp != ivp_end;
1319
         ++ ft, ++ ivp, ++ ivbo)
1320
    {
1321
        bindArrayBuffer(*ivbo);
1322
/*
1323
        logger->log("allocate: %d, %ld", mVboBinded,
1324
            (*ivp) * sizeof(GLint));
1325
*/
1326
        mglBufferData(GL_ARRAY_BUFFER, (*ivp) * sizeof(GLint),
1327
            *ft, GL_STATIC_DRAW);
1328
    }
1329
1330
    for (STD_VECTOR<GLint*>::iterator it = intTexPool.begin();
1331
        it != intTexPool.end(); ++ it)
1332
    {
1333
        delete [] (*it);
1334
    }
1335
    intTexPool.clear();
1336
}
1337
1338
void ModernOpenGLGraphics::drawTriangleArray(const int size) restrict2
1339
{
1340
//    logger->log("allocate: %d, %ld", mVboBinded, size * sizeof(GLint));
1341
    mglBufferData(GL_ARRAY_BUFFER, size * sizeof(GLint),
1342
        mIntArray, GL_STREAM_DRAW);
1343
#ifdef DEBUG_DRAW_CALLS
1344
    mDrawCalls ++;
1345
#endif  // DEBUG_DRAW_CALLS
1346
    mglDrawArrays(GL_TRIANGLES, 0, size / 4);
1347
#ifdef OPENGLERRORS
1348
    graphicsManager.logError();
1349
#endif  // OPENGLERRORS
1350
}
1351
1352
void ModernOpenGLGraphics::drawTriangleArray(const GLint *restrict const array,
1353
                                             const int size) restrict2
1354
{
1355
//    logger->log("allocate: %d, %ld", mVboBinded, size * sizeof(GLint));
1356
    mglBufferData(GL_ARRAY_BUFFER, size * sizeof(GLint),
1357
        array, GL_STREAM_DRAW);
1358
#ifdef DEBUG_DRAW_CALLS
1359
    mDrawCalls ++;
1360
#endif  // DEBUG_DRAW_CALLS
1361
    mglDrawArrays(GL_TRIANGLES, 0, size / 4);
1362
#ifdef OPENGLERRORS
1363
    graphicsManager.logError();
1364
#endif  // OPENGLERRORS
1365
}
1366
1367
void ModernOpenGLGraphics::drawLineArrays(const int size)
1368
{
1369
//    logger->log("allocate: %d, %ld", mVboBinded, size * sizeof(GLint));
1370
    mglBufferData(GL_ARRAY_BUFFER, size * sizeof(GLint),
1371
        mIntArray, GL_STREAM_DRAW);
1372
#ifdef DEBUG_DRAW_CALLS
1373
    mDrawCalls ++;
1374
#endif  // DEBUG_DRAW_CALLS
1375
    mglDrawArrays(GL_LINES, 0, size / 4);
1376
#ifdef OPENGLERRORS
1377
    graphicsManager.logError();
1378
#endif  // OPENGLERRORS
1379
}
1380
1381
#ifdef DEBUG_BIND_TEXTURE
1382
void ModernOpenGLGraphics::debugBindTexture(const Image *restrict const image)
1383
                                            restrict2
1384
{
1385
    const std::string texture = image->mIdPath;
1386
    if (mOldTexture != texture)
1387
    {
1388
        if ((!mOldTexture.empty() || !texture.empty())
1389
            && mOldTextureId != image->mGLImage)
1390
        {
1391
            logger->log("bind: %s (%d) to %s (%d)", mOldTexture.c_str(),
1392
                mOldTextureId, texture.c_str(), image->mGLImage);
1393
        }
1394
        mOldTextureId = image->mGLImage;
1395
        mOldTexture = texture;
1396
    }
1397
}
1398
#else  // DEBUG_BIND_TEXTURE
1399
void ModernOpenGLGraphics::debugBindTexture(const Image *restrict const
1400
                                            image A_UNUSED) restrict2
1401
{
1402
}
1403
#endif  // DEBUG_BIND_TEXTURE
1404
1405
#endif  // !defined(ANDROID) && !defined(__native_client__) &&
1406
        // !defined(__SWITCH__)
1407
#endif  // USE_OPENGL