GLuint Scene::InitTexture(QString path) { GLuint textureId(0); QImage pm(path); if (pm.isNull()) // проверка на загрузку текстуры { setWindowTitle("no image: " + path); _isAllLoad = false; return 0; } pm = QGLWidget::convertToGLFormat(pm); glGenTextures(1, &textureId); glBindTexture(GL_TEXTURE_2D, textureId); glTexImage2D(GL_TEXTURE_2D, 0, 4, (GLsizei)pm.width(), (GLsizei)pm.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pm.bits()); glBindTexture(GL_TEXTURE_2D, textureId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); return textureId; }
void KisTextureTile::bindToActiveTexture() { glBindTexture(GL_TEXTURE_2D, textureId()); if (m_needsMipmapRegeneration) { glGenerateMipmap(GL_TEXTURE_2D); m_needsMipmapRegeneration = false; } }
void SSGQuickLayer::grab() { if (!m_item || m_size.isNull()) { delete m_fbo; delete m_secondaryFbo; m_fbo = m_secondaryFbo = 0; m_depthStencilBuffer.clear(); m_dirtyTexture = false; return; } QSGNode *root = m_item; while (root->firstChild() && root->type() != QSGNode::RootNodeType) root = root->firstChild(); if (root->type() != QSGNode::RootNodeType) return; if (!m_renderer) { m_renderer = m_context->createRenderer(); connect(m_renderer, SIGNAL(sceneGraphChanged()), this, SLOT(markDirtyTexture())); } m_renderer->setDevicePixelRatio(m_device_pixel_ratio); m_renderer->setRootNode(static_cast<QSGRootNode *>(root)); QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); bool deleteFboLater = false; if (!m_fbo || m_fbo->size() != m_size || m_fbo->format().internalTextureFormat() != m_format || (!m_fbo->format().mipmap() && m_mipmap)) { if (!m_multisamplingChecked) { if (m_context->openglContext()->format().samples() <= 1) { m_multisampling = false; } else { const QSet<QByteArray> extensions = m_context->openglContext()->extensions(); m_multisampling = extensions.contains(QByteArrayLiteral("GL_EXT_framebuffer_multisample")) && extensions.contains(QByteArrayLiteral("GL_EXT_framebuffer_blit")); } m_multisamplingChecked = true; } if (m_multisampling) { // Don't delete the FBO right away in case it is used recursively. deleteFboLater = true; delete m_secondaryFbo; QOpenGLFramebufferObjectFormat format; format.setInternalTextureFormat(m_format); format.setSamples(m_context->openglContext()->format().samples()); m_secondaryFbo = new QOpenGLFramebufferObject(m_size, format); m_depthStencilBuffer = m_context->depthStencilBufferForFbo(m_secondaryFbo); } else { QOpenGLFramebufferObjectFormat format; format.setInternalTextureFormat(m_format); format.setMipmap(m_mipmap); if (m_recursive) { deleteFboLater = true; delete m_secondaryFbo; m_secondaryFbo = new QOpenGLFramebufferObject(m_size, format); funcs->glBindTexture(GL_TEXTURE_2D, m_secondaryFbo->texture()); updateBindOptions(true); m_depthStencilBuffer = m_context->depthStencilBufferForFbo(m_secondaryFbo); } else { delete m_fbo; delete m_secondaryFbo; m_fbo = new QOpenGLFramebufferObject(m_size, format); m_secondaryFbo = 0; funcs->glBindTexture(GL_TEXTURE_2D, m_fbo->texture()); updateBindOptions(true); m_depthStencilBuffer = m_context->depthStencilBufferForFbo(m_fbo); } } } if (m_recursive && !m_secondaryFbo) { // m_fbo already created, m_recursive was just set. Q_ASSERT(m_fbo); Q_ASSERT(!m_multisampling); m_secondaryFbo = new QOpenGLFramebufferObject(m_size, m_fbo->format()); funcs->glBindTexture(GL_TEXTURE_2D, m_secondaryFbo->texture()); updateBindOptions(true); } // Render texture. root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip and opacity update. m_renderer->nodeChanged(root, QSGNode::DirtyForceUpdate); // Force render list update. #ifdef QSG_DEBUG_FBO_OVERLAY if (qmlFboOverlay()) { if (!m_debugOverlay) m_debugOverlay = new QSGSimpleRectNode(); m_debugOverlay->setRect(QRectF(0, 0, m_size.width(), m_size.height())); m_debugOverlay->setColor(QColor(0xff, 0x00, 0x80, 0x40)); root->appendChildNode(m_debugOverlay); } #endif m_dirtyTexture = false; m_renderer->setDeviceRect(m_size); m_renderer->setViewportRect(m_size); QRectF mirrored(m_mirrorHorizontal ? m_rect.right() : m_rect.left(), m_mirrorVertical ? m_rect.bottom() : m_rect.top(), m_mirrorHorizontal ? -m_rect.width() : m_rect.width(), m_mirrorVertical ? -m_rect.height() : m_rect.height()); m_renderer->setProjectionMatrixToRect(mirrored); m_renderer->setClearColor(Qt::transparent); if (m_multisampling) { m_renderer->renderScene(BindableFbo(m_secondaryFbo, m_depthStencilBuffer.data())); if (deleteFboLater) { delete m_fbo; QOpenGLFramebufferObjectFormat format; format.setInternalTextureFormat(m_format); format.setAttachment(QOpenGLFramebufferObject::NoAttachment); format.setMipmap(m_mipmap); format.setSamples(0); m_fbo = new QOpenGLFramebufferObject(m_size, format); funcs->glBindTexture(GL_TEXTURE_2D, m_fbo->texture()); updateBindOptions(true); } QRect r(QPoint(), m_size); QOpenGLFramebufferObject::blitFramebuffer(m_fbo, r, m_secondaryFbo, r); } else { if (m_recursive) { m_renderer->renderScene(BindableFbo(m_secondaryFbo, m_depthStencilBuffer.data())); if (deleteFboLater) { delete m_fbo; QOpenGLFramebufferObjectFormat format; format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); format.setInternalTextureFormat(m_format); format.setMipmap(m_mipmap); m_fbo = new QOpenGLFramebufferObject(m_size, format); funcs->glBindTexture(GL_TEXTURE_2D, m_fbo->texture()); updateBindOptions(true); } qSwap(m_fbo, m_secondaryFbo); } else { m_renderer->renderScene(BindableFbo(m_fbo, m_depthStencilBuffer.data())); } } if (m_mipmap) { funcs->glBindTexture(GL_TEXTURE_2D, textureId()); funcs->glGenerateMipmap(GL_TEXTURE_2D); } root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip, opacity and render list update. #ifdef QSG_DEBUG_FBO_OVERLAY if (qmlFboOverlay()) root->removeChildNode(m_debugOverlay); #endif if (m_recursive) markDirtyTexture(); // Continuously update if 'live' and 'recursive'. }
void SurfaceItem::render(const Map &map, const Camera &camera) { int zone = map.zone(vertices().at(0)); GLuint tex = textureId(); if (zone < 0) return; m_program->bind(); m_program->setUniformValue(m_matrixUniform, camera.viewProjectionMatrix()); QSize size = surface()->size(); m_program->setUniformValue(m_pixelSizeUniform, 5. / size.width(), 5. / size.height()); m_program->setUniformValue(m_eyeUniform, camera.viewPos()); m_program->setUniformValue(m_focusColorUniform, GLfloat(m_opacity)); m_program->setUniformValueArray(m_lightsUniform, map.lights(zone).constData(), map.lights(zone).size()); m_program->setUniformValue(m_numLightsUniform, map.lights(zone).size()); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex); QVector<QVector3D> v = vertices(); QVector3D va = v[0]; QVector3D vb = v[1]; QVector3D vc = v[2]; QVector3D vd = v[3]; QVector<QVector3D> vertexBuffer; vertexBuffer << va << vb << vd << vd << vb << vc; qreal y1 = 0; qreal y2 = 1; if (surface()->origin() == QWaylandSurface::OriginTopLeft) qSwap(y1, y2); QVector<QVector2D> texCoordBuffer; texCoordBuffer << QVector2D(0, y2) << QVector2D(1, y2) << QVector2D(0, y1) << QVector2D(0, y1) << QVector2D(1, y2) << QVector2D(1, y1); m_program->setUniformValue(m_normalUniform, -QVector3D::crossProduct(vb - va, vc - va).normalized()); m_program->enableAttributeArray(m_vertexAttr); m_program->setAttributeArray(m_vertexAttr, vertexBuffer.constData()); m_program->enableAttributeArray(m_texCoordAttr); m_program->setAttributeArray(m_texCoordAttr, texCoordBuffer.constData()); glEnable(GL_BLEND); glDisable(GL_CULL_FACE); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glDrawArrays(GL_TRIANGLES, 0, 6); glDisable(GL_BLEND); m_program->disableAttributeArray(m_texCoordAttr); m_program->disableAttributeArray(m_vertexAttr); #if 0 QOpenGLPaintDevice device(camera.viewSize()); QPainter p(&device); va = camera.viewProjectionMatrix().map(va); vb = camera.viewProjectionMatrix().map(vb); vc = camera.viewProjectionMatrix().map(vc); vd = camera.viewProjectionMatrix().map(vd); QVector3D c(camera.viewSize().width() * 0.5, camera.viewSize().height() * 0.5, 0); va = c + c * va * QVector3D(1, -1, 0); vb = c + c * vb * QVector3D(1, -1, 0); vc = c + c * vc * QVector3D(1, -1, 0); vd = c + c * vd * QVector3D(1, -1, 0); QPointF pa(va.x(), va.y()); QPointF pb(vb.x(), vb.y()); QPointF pc(vc.x(), vc.y()); QPointF pd(vd.x(), vd.y()); p.drawLine(pa, pb); p.drawLine(pb, pc); p.drawLine(pc, pd); p.drawLine(pd, pa); extern QVector3D debug; QVector3D d = camera.viewProjectionMatrix().map(debug); d = c + c * d * QVector3D(1, -1, 0); static QVector3D old; if (debug != old) old = debug; p.setPen(Qt::NoPen); p.setBrush(Qt::red); p.drawEllipse(QRectF(d.x() - 2, d.y() - 2, 4, 4)); p.end(); #endif }