void ScreenAlignedQuad::draw(QOpenGLFunctions &gl) { m_vertices->bind(); gl.glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 0, nullptr); gl.glEnableVertexAttribArray(0); gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); }
void CubeFace::draw() { m_vao->bind(); QOpenGLFunctions f = QOpenGLFunctions(QOpenGLContext::currentContext()); f.glDrawArrays(GL_TRIANGLES, 0, 12); m_vao->release(); }
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 CompasWidget::paintGL(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); m_world.setToIdentity(); m_world.rotate(180.0f - (m_xRot / 16.0f), 1, 0, 0); m_world.rotate(m_yRot / 16.0f, 0, 1, 0); m_world.rotate(m_zRot / 16.0f, 0, 0, 1); /* QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao); m_program->bind(); m_program->setUniformValue(m_projMatrixLoc, m_proj); m_program->setUniformValue(m_mvMatrixLoc, m_camera * m_world); QMatrix3x3 normalMatrix = m_world.normalMatrix(); m_program->setUniformValue(m_normalMatrixLoc, normalMatrix); glDrawArrays(GL_TRIANGLES, 0, m_logo.vertexCount()); m_program->release(); */ QOpenGLFunctions *f = context()->functions(); const bool newFrameReady=true; if (newFrameReady) {//new frame ready f->glFrontFace(GL_CW); // because our cube's vertex data is such f->glBindTexture(GL_TEXTURE_2D, m_fbo2->texture()); m_program2->bind(); QOpenGLVertexArrayObject::Binder vaoBinder(m_vao2); // If VAOs are not supported, set the vertex attributes every time. if (!m_vao2->isCreated()){ setupVertexAttribs2(); } static GLfloat angle = 0; QMatrix4x4 m; m.translate(0, 0, -20); m.rotate(90, 0, 0, 1); m.rotate(angle, 0.5, 1, 0); angle += 0.5f; m_program2->setUniformValue(m_matrixLoc, m_proj * m); // Draw the cube. f->glDrawArrays(GL_TRIANGLES, 0, 36); m_program2->release(); } }
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 GLImageProcessor::render() { vao.bind(); if (!texture.isNull()) texture->bind(); program->bind(); QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glDrawArrays(GL_QUADS, 0, 4); program->release(); vao.release(); }
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 GLShaderTextured::draw(unsigned int mode, const QMatrix4x4& transformMatrix, GLfloat *textureCoords, GLfloat *vertices, int nbVertices) { if (!m_texture) { qDebug("GLShaderTextured::draw: no texture defined. Doing nothing"); return; } QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); m_program->bind(); m_program->setUniformValue(m_matrixLoc, transformMatrix); m_texture->bind(); m_program->setUniformValue(m_textureLoc, 0); // Use texture unit 0 which magically contains our texture f->glEnableVertexAttribArray(0); // vertex f->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices); f->glEnableVertexAttribArray(1); // texture coordinates f->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, textureCoords); f->glDrawArrays(mode, 0, nbVertices); f->glDisableVertexAttribArray(0); m_program->release(); }
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; }