|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | + |
| 3 | +#include <scratchcpp/costume.h> |
| 4 | + |
| 5 | +#include "skin.h" |
| 6 | +#include "texture.h" |
| 7 | + |
| 8 | +using namespace scratchcpprender; |
| 9 | + |
| 10 | +Skin::Skin(libscratchcpp::Costume *costume) |
| 11 | +{ |
| 12 | + if (!costume) |
| 13 | + return; |
| 14 | +} |
| 15 | + |
| 16 | +Texture Skin::createAndPaintTexture(int width, int height, bool multisampled) |
| 17 | +{ |
| 18 | + QOpenGLContext *context = QOpenGLContext::currentContext(); |
| 19 | + |
| 20 | + if (!context || !context->isValid() || (width <= 0 || height <= 0)) |
| 21 | + return Texture(); |
| 22 | + |
| 23 | + // Create offscreen surface |
| 24 | + QOffscreenSurface surface; |
| 25 | + surface.setFormat(context->format()); |
| 26 | + surface.create(); |
| 27 | + Q_ASSERT(surface.isValid()); |
| 28 | + |
| 29 | + // Save old surface |
| 30 | + QSurface *oldSurface = context->surface(); |
| 31 | + |
| 32 | + // Make context active on the surface |
| 33 | + context->makeCurrent(&surface); |
| 34 | + |
| 35 | + const QRectF drawRect(0, 0, width, height); |
| 36 | + const QSize drawRectSize = drawRect.size().toSize(); |
| 37 | + |
| 38 | + // Create multisampled FBO (if the multisampled parameter is set) |
| 39 | + QOpenGLFramebufferObjectFormat format; |
| 40 | + format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); |
| 41 | + |
| 42 | + if (multisampled) |
| 43 | + format.setSamples(16); |
| 44 | + |
| 45 | + QOpenGLFramebufferObject fbo(drawRectSize, format); |
| 46 | + fbo.bind(); |
| 47 | + |
| 48 | + // Create paint device |
| 49 | + QOpenGLPaintDevice device(drawRectSize); |
| 50 | + QPainter painter(&device); |
| 51 | + painter.beginNativePainting(); |
| 52 | + painter.setRenderHint(QPainter::Antialiasing, false); |
| 53 | + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 54 | + glClear(GL_COLOR_BUFFER_BIT); |
| 55 | + |
| 56 | + // Call the skin-specific paint method |
| 57 | + paint(&painter); |
| 58 | + |
| 59 | + // Done with the painting |
| 60 | + painter.endNativePainting(); |
| 61 | + painter.end(); |
| 62 | + fbo.release(); |
| 63 | + |
| 64 | + GLuint textureHandle; |
| 65 | + |
| 66 | + if (multisampled) { |
| 67 | + // Create non-multisampled FBO (we can't take the texture from the multisampled FBO) |
| 68 | + format.setSamples(0); |
| 69 | + |
| 70 | + QOpenGLFramebufferObject targetFbo(drawRectSize, format); |
| 71 | + targetFbo.bind(); |
| 72 | + |
| 73 | + // Blit the multisampled FBO to target FBO |
| 74 | + QOpenGLFramebufferObject::blitFramebuffer(&targetFbo, &fbo); |
| 75 | + |
| 76 | + // Take the texture (will call targetFbo.release()) |
| 77 | + textureHandle = targetFbo.takeTexture(); |
| 78 | + } else { |
| 79 | + // Take the texture |
| 80 | + textureHandle = fbo.takeTexture(); |
| 81 | + } |
| 82 | + |
| 83 | + // Restore old surface |
| 84 | + context->doneCurrent(); |
| 85 | + |
| 86 | + if (oldSurface) |
| 87 | + context->makeCurrent(oldSurface); |
| 88 | + |
| 89 | + return Texture(textureHandle, drawRectSize); |
| 90 | +} |
0 commit comments