diff options
author | Samuel Rødal <samuel.rodal@nokia.com> | 2011-09-07 10:31:03 +0200 |
---|---|---|
committer | Paul Olav Tvete <paul.tvete@nokia.com> | 2011-09-08 11:07:56 +0200 |
commit | 1b1831500142c995137f5e4b1bc03d21aa08e1e1 (patch) | |
tree | 5fcd21ee23c37fbe3466fde353d0bcfd09a051e2 /examples/opengl/paintedwindow/paintedwindow.cpp | |
parent | 68974d8e647febb80a47d9cf6ce9452f3ce4fa21 (diff) |
Improved and made public the QOpenGLPaintDevice API.
The new API is more flexible, allowing the use of QOpenGLPaintDevice
with any QOpenGLContext, and putting more responsibility on the user for
ensuring the correct FBO is current (the user knows best anyhow).
Task-number: QTBUG-21263
Change-Id: I50b954125f552baef52fbb3fe2ed907a11ad519c
Reviewed-on: http://codereview.qt-project.org/4325
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Paul Olav Tvete <paul.tvete@nokia.com>
Diffstat (limited to 'examples/opengl/paintedwindow/paintedwindow.cpp')
-rw-r--r-- | examples/opengl/paintedwindow/paintedwindow.cpp | 95 |
1 files changed, 12 insertions, 83 deletions
diff --git a/examples/opengl/paintedwindow/paintedwindow.cpp b/examples/opengl/paintedwindow/paintedwindow.cpp index b9ecac1293..9b27c56914 100644 --- a/examples/opengl/paintedwindow/paintedwindow.cpp +++ b/examples/opengl/paintedwindow/paintedwindow.cpp @@ -1,17 +1,17 @@ #include "paintedwindow.h" #include <QOpenGLContext> -#include <QOpenGLFunctions> +#include <QOpenGLPaintDevice> #include <QPainter> #include <QTimer> #include <qmath.h> PaintedWindow::PaintedWindow() - : m_fbo(0) { QSurfaceFormat format; format.setStencilBufferSize(8); + format.setSamples(4); setSurfaceType(QWindow::OpenGLSurface); setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint); @@ -22,103 +22,32 @@ PaintedWindow::PaintedWindow() m_context = new QOpenGLContext(this); m_context->setFormat(format); m_context->create(); - - QTimer *timer = new QTimer(this); - timer->setInterval(16); - connect(timer, SIGNAL(timeout()), this, SLOT(paint())); - timer->start(); - - m_context->makeCurrent(this); - - QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); - const char *vsrc = - "attribute highp vec2 vertexCoordsInput;\n" - "attribute mediump vec2 texCoordsInput;\n" - "varying mediump vec2 texCoords;\n" - "void main(void)\n" - "{\n" - " texCoords = texCoordsInput;\n" - " gl_Position = vec4(vertexCoordsInput, 0, 1);\n" - "}\n"; - vshader->compileSourceCode(vsrc); - - QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); - const char *fsrc = - "uniform sampler2D tex;\n" - "varying mediump vec2 texCoords;\n" - "void main(void)\n" - "{\n" - " gl_FragColor = texture2D(tex, texCoords);\n" - "}\n"; - fshader->compileSourceCode(fsrc); - - m_program = new QOpenGLShaderProgram; - m_program->addShader(vshader); - m_program->addShader(fshader); - m_program->link(); - - m_vertexAttribute = m_program->attributeLocation("vertexCoordsInput"); - m_texCoordsAttribute = m_program->attributeLocation("texCoordsInput"); } void PaintedWindow::resizeEvent(QResizeEvent *) { - m_context->makeCurrent(this); + paint(); +} - delete m_fbo; - m_fbo = new QOpenGLFramebufferObject(size()); +void PaintedWindow::exposeEvent(QExposeEvent *) +{ + paint(); } void PaintedWindow::paint() { - if (!m_fbo) - return; - m_context->makeCurrent(this); QPainterPath path; - path.addEllipse(0, 0, m_fbo->width(), m_fbo->height()); + path.addEllipse(0, 0, width(), height()); + + QOpenGLPaintDevice device(size()); - QPainter painter; - painter.begin(m_fbo); - painter.fillRect(0, 0, m_fbo->width(), m_fbo->height(), Qt::white); + QPainter painter(&device); + painter.fillRect(0, 0, width(), height(), Qt::white); painter.setRenderHint(QPainter::Antialiasing); painter.fillPath(path, Qt::blue); painter.end(); - glViewport(0, 0, width(), height()); - - glDisable(GL_DEPTH_TEST); - glDisable(GL_STENCIL_TEST); - glDisable(GL_BLEND); - - glClearColor(0.1f, 0.1f, 0.2f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - GLfloat texCoords[] = { 0, 0, 1, 0, 0, 1, - 1, 0, 1, 1, 0, 1 }; - - GLfloat vertexCoords[] = { -1, -1, 1, -1, -1, 1, - 1, -1, 1, 1, -1, 1 }; - - m_program->bind(); - - m_context->functions()->glEnableVertexAttribArray(m_vertexAttribute); - m_context->functions()->glEnableVertexAttribArray(m_texCoordsAttribute); - - m_context->functions()->glVertexAttribPointer(m_vertexAttribute, 2, GL_FLOAT, GL_FALSE, 0, vertexCoords); - m_context->functions()->glVertexAttribPointer(m_texCoordsAttribute, 2, GL_FLOAT, GL_FALSE, 0, texCoords); - - glBindTexture(GL_TEXTURE_2D, m_fbo->texture()); - - glDrawArrays(GL_TRIANGLES, 0, 6); - - glBindTexture(GL_TEXTURE_2D, 0); - - m_context->functions()->glDisableVertexAttribArray(m_vertexAttribute); - m_context->functions()->glDisableVertexAttribArray(m_texCoordsAttribute); - - m_program->release(); - m_context->swapBuffers(this); } |