void Renderer::render() { QMutexLocker locker(&m_windowLock); if (m_windows.isEmpty()) return; HelloWindow *surface = m_windows.at(m_currentWindow); QColor color = surface->color(); m_currentWindow = (m_currentWindow + 1) % m_windows.size(); if (!m_context->makeCurrent(surface)) return; QSize viewSize = surface->size(); locker.unlock(); if (!m_initialized) { initialize(); m_initialized = true; } QOpenGLFunctions *f = m_context->functions(); f->glViewport(0, 0, viewSize.width() * surface->devicePixelRatio(), viewSize.height() * surface->devicePixelRatio()); f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); f->glClearColor(m_backgroundColor.redF(), m_backgroundColor.greenF(), m_backgroundColor.blueF(), m_backgroundColor.alphaF()); f->glFrontFace(GL_CW); f->glCullFace(GL_FRONT); f->glEnable(GL_CULL_FACE); f->glEnable(GL_DEPTH_TEST); m_program->bind(); m_vbo.bind(); m_program->enableAttributeArray(vertexAttr); m_program->enableAttributeArray(normalAttr); m_program->setAttributeBuffer(vertexAttr, GL_FLOAT, 0, 3); const int verticesSize = vertices.count() * 3 * sizeof(GLfloat); m_program->setAttributeBuffer(normalAttr, GL_FLOAT, verticesSize, 3); QMatrix4x4 modelview; modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f); modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f); modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f); modelview.translate(0.0f, -0.2f, 0.0f); m_program->setUniformValue(matrixUniform, modelview); m_program->setUniformValue(colorUniform, color); m_context->functions()->glDrawArrays(GL_TRIANGLES, 0, vertices.size()); m_context->swapBuffers(surface); m_fAngle += 1.0f; QTimer::singleShot(0, this, SLOT(render())); }
void Renderer::render() { QMutexLocker locker(&m_windowLock); if (m_windows.isEmpty()) return; HelloWindow *surface = m_windows.at(m_currentWindow); QColor color = surface->color(); m_currentWindow = (m_currentWindow + 1) % m_windows.size(); if (!m_context->makeCurrent(surface)) return; QSize viewSize = surface->size(); locker.unlock(); if (!m_initialized) { initialize(); m_initialized = true; } QOpenGLFunctions *f = m_context->functions(); f->glViewport(0, 0, viewSize.width() * surface->devicePixelRatio(), viewSize.height() * surface->devicePixelRatio()); f->glClearColor(0.1f, 0.1f, 0.2f, 1.0f); f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); f->glFrontFace(GL_CW); f->glCullFace(GL_FRONT); f->glEnable(GL_CULL_FACE); f->glEnable(GL_DEPTH_TEST); QMatrix4x4 modelview; modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f); modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f); modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f); modelview.translate(0.0f, -0.2f, 0.0f); m_program->bind(); m_program->setUniformValue(matrixUniform, modelview); m_program->setUniformValue(colorUniform, color); paintQtLogo(); m_program->release(); f->glDisable(GL_DEPTH_TEST); f->glDisable(GL_CULL_FACE); m_context->swapBuffers(surface); m_fAngle += 1.0f; QTimer::singleShot(0, this, SLOT(render())); }
void exposeEvent(QExposeEvent *) { if (!isExposed()) return; if (!gl) { gl = new QOpenGLContext(); gl->setFormat(requestedFormat()); gl->create(); } gl->makeCurrent(this); QOpenGLShaderProgram prog; prog.addShaderFromSourceCode(QOpenGLShader::Vertex, "attribute highp vec4 a_Pos;" "attribute lowp vec4 a_Color;" "varying lowp vec4 v_Color;" "void main() {" " gl_Position = a_Pos;" " v_Color = a_Color;" "}"); prog.addShaderFromSourceCode(QOpenGLShader::Fragment, "varying lowp vec4 v_Color;" "void main() {" " gl_FragColor = v_Color;" "}"); prog.bind(); QOpenGLFunctions *functions = gl->functions(); functions->glClearColor(0, 0, 0, 0); functions->glClear(GL_COLOR_BUFFER_BIT); functions->glViewport(0, 0, width(), height()); prog.enableAttributeArray("a_Pos"); prog.enableAttributeArray("a_Color"); float coords[] = { -0.7f, 0.7f, 0.8f, 0.8f, -0.8f, -0.8f, 0.7f, -0.7f }; float colors[] = { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0 }; prog.setAttributeArray("a_Pos", GL_FLOAT, coords, 2, 0); prog.setAttributeArray("a_Color", GL_FLOAT, colors, 4, 0); functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); prog.disableAttributeArray("a_Pos"); prog.disableAttributeArray("a_Color"); gl->swapBuffers(this); }
void GLImageProcessor::plot(int w, int h) { vao.bind(); plotProgram->bind(); QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glActiveTexture(GL_TEXTURE0); f->glBindTexture(GL_TEXTURE_2D, renderFbo->texture()); f->glViewport(0, 0, w, h); f->glDrawArrays(GL_QUADS, 0, 4); plotProgram->release(); vao.release(); }
void QtRenderer::render() { QMutexLocker locker(&m_windowLock); if (m_windows.isEmpty()) return; HelloWindow *surface = m_windows.at(m_currentWindow); QColor color = surface->color(); m_currentWindow = (m_currentWindow + 1) % m_windows.size(); if (!m_context->makeCurrent(surface)) return; m_fboController->bind(surface->size()); m_fboController->bindAndDraw(); m_fboController->release(); QSize viewSize = surface->size(); locker.unlock(); if (!m_initialized) { initialize(); m_initialized = true; } //f->glClearColor(0.1f, 0.1f, 0.2f, 1.0f); QOpenGLFunctions *f = m_context->functions(); f->glViewport(0, 0, viewSize.width() * surface->devicePixelRatio(), viewSize.height() * surface->devicePixelRatio()); f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 modelview; modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f); modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f); modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f); modelview.translate(0.0f, -0.2f, 0.0f); m_program->setUniformValue(matrixUniform, modelview); m_program->setUniformValue(colorUniform, color); m_context->functions()->glDrawArrays(GL_TRIANGLES, 0, vertices.size()); m_context->swapBuffers(surface); m_fAngle += 1.0f; QTimer::singleShot(250, this, SLOT(render())); }
void TFInteg2DGL::integrate(const float *colormap, int resolution, float basesize, float stepsize) { if (!texFull || resolution != texFull->width()) newResources(resolution); QOpenGLFunctions* f = QOpenGLContext::currentContext()->functions(); tex1d->setData(QOpenGLTexture::RGBA, QOpenGLTexture::Float32, colormap); // framebuffer object GLint oFbo, viewport[4], activeTex, oTex; newFbo(); f->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oFbo); f->glBindFramebuffer(GL_FRAMEBUFFER, *fbo); f->glClear(GL_COLOR_BUFFER_BIT); // viewport f->glGetIntegerv(GL_VIEWPORT, viewport); f->glViewport(0, 0, resolution, resolution); // 1d texture f->glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex); f->glActiveTexture(GL_TEXTURE0); f->glGetIntegerv(GL_TEXTURE_1D, &oTex); f->glBindTexture(GL_TEXTURE_1D, tex1d->textureId()); // paint painter.recreateVAO(); painter.paint("tf1d", 0, "resolution", resolution, "basesize", basesize, "segLen", stepsize); // clean f->glActiveTexture(GL_TEXTURE0); f->glBindTexture(GL_TEXTURE_1D, oTex); f->glActiveTexture(activeTex); // std::unique_ptr<GLubyte[]> pixels(new GLubyte [resolution * resolution * 4]); // f->glReadPixels(0, 0, resolution, resolution, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); // QImage image(pixels.get(), resolution, resolution, QImage::Format_RGBA8888); // static QLabel label; // label.resize(resolution, resolution); // label.setPixmap(QPixmap::fromImage(image.mirrored())); // label.show(); f->glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); f->glBindFramebuffer(GL_FRAMEBUFFER, oFbo); }
void GLImageProcessor::run() { renderFbo->bind(); { QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glViewport(0, 0, renderFbo->width(), renderFbo->height()); texture->bind(); vao.bind(); program->bind(); f->glDrawArrays(GL_QUADS, 0, 4); program->release(); vao.release(); } renderFbo->bindDefault(); }
void QSGDistanceFieldGlyphCache::saveTexture(GLuint textureId, int width, int height) const { QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions(); GLuint fboId; functions->glGenFramebuffers(1, &fboId); GLuint tmpTexture = 0; functions->glGenTextures(1, &tmpTexture); functions->glBindTexture(GL_TEXTURE_2D, tmpTexture); functions->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); functions->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); functions->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); functions->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); functions->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); functions->glBindTexture(GL_TEXTURE_2D, 0); functions->glBindFramebuffer(GL_FRAMEBUFFER_EXT, fboId); functions->glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tmpTexture, 0); functions->glActiveTexture(GL_TEXTURE0); functions->glBindTexture(GL_TEXTURE_2D, textureId); functions->glDisable(GL_STENCIL_TEST); functions->glDisable(GL_DEPTH_TEST); functions->glDisable(GL_SCISSOR_TEST); functions->glDisable(GL_BLEND); GLfloat textureCoordinateArray[8]; textureCoordinateArray[0] = 0.0f; textureCoordinateArray[1] = 0.0f; textureCoordinateArray[2] = 1.0f; textureCoordinateArray[3] = 0.0f; textureCoordinateArray[4] = 1.0f; textureCoordinateArray[5] = 1.0f; textureCoordinateArray[6] = 0.0f; textureCoordinateArray[7] = 1.0f; GLfloat vertexCoordinateArray[8]; vertexCoordinateArray[0] = -1.0f; vertexCoordinateArray[1] = -1.0f; vertexCoordinateArray[2] = 1.0f; vertexCoordinateArray[3] = -1.0f; vertexCoordinateArray[4] = 1.0f; vertexCoordinateArray[5] = 1.0f; vertexCoordinateArray[6] = -1.0f; vertexCoordinateArray[7] = 1.0f; functions->glViewport(0, 0, width, height); functions->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertexCoordinateArray); functions->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinateArray); { static const char *vertexShaderSource = "attribute vec4 vertexCoordsArray; \n" "attribute vec2 textureCoordArray; \n" "varying vec2 textureCoords; \n" "void main(void) \n" "{ \n" " gl_Position = vertexCoordsArray; \n" " textureCoords = textureCoordArray; \n" "} \n"; static const char *fragmentShaderSource = "varying vec2 textureCoords; \n" "uniform sampler2D texture; \n" "void main() \n" "{ \n" " gl_FragColor = texture2D(texture, textureCoords); \n" "} \n"; GLuint vertexShader = functions->glCreateShader(GL_VERTEX_SHADER); GLuint fragmentShader = functions->glCreateShader(GL_FRAGMENT_SHADER); if (vertexShader == 0 || fragmentShader == 0) { GLenum error = functions->glGetError(); qWarning("QSGDistanceFieldGlyphCache::saveTexture: Failed to create shaders. (GL error: %x)", error); return; } functions->glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); functions->glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); functions->glCompileShader(vertexShader); GLint len = 1; functions->glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &len); char infoLog[2048]; functions->glGetShaderInfoLog(vertexShader, 2048, NULL, infoLog); if (qstrlen(infoLog) > 0) qWarning("Problems compiling vertex shader:\n %s", infoLog); functions->glCompileShader(fragmentShader); functions->glGetShaderInfoLog(fragmentShader, 2048, NULL, infoLog); if (qstrlen(infoLog) > 0) qWarning("Problems compiling fragment shader:\n %s", infoLog); GLuint shaderProgram = functions->glCreateProgram(); functions->glAttachShader(shaderProgram, vertexShader); functions->glAttachShader(shaderProgram, fragmentShader); functions->glBindAttribLocation(shaderProgram, 0, "vertexCoordsArray"); functions->glBindAttribLocation(shaderProgram, 1, "textureCoordArray"); functions->glLinkProgram(shaderProgram); functions->glGetProgramInfoLog(shaderProgram, 2048, NULL, infoLog); if (qstrlen(infoLog) > 0) qWarning("Problems linking shaders:\n %s", infoLog); functions->glUseProgram(shaderProgram); functions->glEnableVertexAttribArray(0); functions->glEnableVertexAttribArray(1); int textureUniformLocation = functions->glGetUniformLocation(shaderProgram, "texture"); functions->glUniform1i(textureUniformLocation, 0); } functions->glDrawArrays(GL_TRIANGLE_FAN, 0, 4); { GLenum error = functions->glGetError(); if (error != GL_NO_ERROR) qWarning("glDrawArrays reported error 0x%x", error); } uchar *data = new uchar[width * height * 4]; functions->glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); QImage image(data, width, height, QImage::Format_ARGB32); QByteArray fileName = m_referenceFont.familyName().toLatin1() + '_' + QByteArray::number(textureId); fileName = fileName.replace('/', '_').replace(' ', '_') + ".png"; image.save(QString::fromLocal8Bit(fileName)); { GLenum error = functions->glGetError(); if (error != GL_NO_ERROR) qWarning("glReadPixels reported error 0x%x", error); } functions->glDisableVertexAttribArray(0); functions->glDisableVertexAttribArray(1); functions->glDeleteFramebuffers(1, &fboId); functions->glDeleteTextures(1, &tmpTexture); delete[] data; }
void QOpenGLTextureBlitWindow::render() { m_context->makeCurrent(this); QRect viewport(0,0,dWidth(),dHeight()); QOpenGLFunctions *functions = m_context->functions(); functions->glViewport(0,0,dWidth(), dHeight()); functions->glClearColor(0.f, .6f, .0f, 0.f); functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QOpenGLTexture texture(m_image); texture.setWrapMode(QOpenGLTexture::ClampToEdge); texture.create(); QOpenGLTexture texture_mirrored(m_image_mirrord); texture_mirrored.setWrapMode(QOpenGLTexture::ClampToEdge); texture_mirrored.create(); QRectF topLeftOriginTopLeft(QPointF(0,0), QPointF(dWidth()/2.0, dHeight()/2.0)); QRectF topRightOriginTopLeft(QPointF(dWidth()/2.0,0), QPointF(dWidth(), dHeight()/2.0)); QRectF bottomLeftOriginTopLeft(QPointF(0, dHeight()/2.0), QPointF(dWidth() /2.0, dHeight())); QRectF bottomRightOriginTopLeft(QPoint(dWidth()/2.0, dHeight()/2.0), QPointF(dWidth(), dHeight())); QRectF topLeftOriginBottomLeft = bottomLeftOriginTopLeft; Q_UNUSED(topLeftOriginBottomLeft); QRectF topRightOriginBottomLeft = bottomRightOriginTopLeft; Q_UNUSED(topRightOriginBottomLeft); QRectF bottomLeftOriginBottomLeft = topLeftOriginTopLeft; QRectF bottomRightOriginBottomLeft = topRightOriginTopLeft; QOpenGLTextureBlitter::Origin topLeftOrigin = QOpenGLTextureBlitter::OriginTopLeft; QOpenGLTextureBlitter::Origin bottomLeftOrigin = QOpenGLTextureBlitter::OriginBottomLeft; QMatrix4x4 topRightOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(topRightOriginTopLeft, viewport); QMatrix4x4 bottomLeftOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(bottomLeftOriginTopLeft, viewport); QMatrix4x4 bottomRightOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(bottomRightOriginTopLeft, viewport); QMatrix3x3 texTopLeftOriginTopLeft = QOpenGLTextureBlitter::sourceTransform(topLeftOriginTopLeft, m_image.size(), topLeftOrigin); QMatrix3x3 texTopRightOriginBottomLeft = QOpenGLTextureBlitter::sourceTransform(topRightOriginBottomLeft, m_image.size(), bottomLeftOrigin); QMatrix3x3 texBottomLeftOriginBottomLeft = QOpenGLTextureBlitter::sourceTransform(bottomLeftOriginBottomLeft, m_image.size(), bottomLeftOrigin); QMatrix3x3 texBottomRightOriginBottomLeft = QOpenGLTextureBlitter::sourceTransform(bottomRightOriginBottomLeft, m_image.size(), bottomLeftOrigin); QSizeF subSize(topLeftOriginTopLeft.width()/2, topLeftOriginTopLeft.height()/2); QRectF subTopLeftOriginTopLeft(topLeftOriginTopLeft.topLeft(), subSize); QRectF subTopRightOriginTopLeft(QPointF(topLeftOriginTopLeft.topLeft().x() + topLeftOriginTopLeft.width() / 2, topLeftOriginTopLeft.topLeft().y()), subSize); QRectF subBottomLeftOriginTopLeft(QPointF(topLeftOriginTopLeft.topLeft().x(), topLeftOriginTopLeft.topLeft().y() + topLeftOriginTopLeft.height() / 2), subSize); QRectF subBottomRightOriginTopLeft(QPointF(topLeftOriginTopLeft.topLeft().x() + topLeftOriginTopLeft.width() / 2, topLeftOriginTopLeft.topLeft().y() + topLeftOriginTopLeft.height() / 2), subSize); QMatrix4x4 subTopLeftOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(subTopLeftOriginTopLeft, viewport); QMatrix4x4 subTopRightOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(subTopRightOriginTopLeft, viewport); QMatrix4x4 subBottomLeftOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(subBottomLeftOriginTopLeft, viewport); QMatrix4x4 subBottomRightOriginTopLeftVertex = QOpenGLTextureBlitter::targetTransform(subBottomRightOriginTopLeft, viewport); m_blitter.bind(); m_blitter.blit(texture_mirrored.textureId(), subTopLeftOriginTopLeftVertex, texBottomRightOriginBottomLeft); m_blitter.blit(texture_mirrored.textureId(), subTopRightOriginTopLeftVertex, texBottomLeftOriginBottomLeft); m_blitter.blit(texture.textureId(), subBottomLeftOriginTopLeftVertex, texTopRightOriginBottomLeft); m_blitter.blit(texture.textureId(), subBottomRightOriginTopLeftVertex, texTopLeftOriginTopLeft); m_blitter.blit(texture.textureId(), topRightOriginTopLeftVertex, topLeftOrigin); m_blitter.blit(texture_mirrored.textureId(), bottomLeftOriginTopLeftVertex, topLeftOrigin); m_blitter.setSwizzleRB(true); m_blitter.blit(texture.textureId(), bottomRightOriginTopLeftVertex, texTopLeftOriginTopLeft); m_blitter.setSwizzleRB(false); m_blitter.release(); if (m_blitter.supportsExternalOESTarget()) { // Cannot do much testing here, just verify that bind and release work, meaning that the program is present. m_blitter.bind(0x8D65); m_blitter.release(); } m_context->swapBuffers(this); }