diff options
author | Samuel Rødal <samuel.rodal@nokia.com> | 2011-06-07 17:25:22 +0200 |
---|---|---|
committer | Samuel Rødal <samuel.rodal@nokia.com> | 2011-06-10 09:24:56 +0200 |
commit | 4a189c188ccd2fb5f8d1d5ddadf06cbd6bc0916f (patch) | |
tree | 99bff9f015e869b5521836ea5667590939b22a53 /examples | |
parent | 4d10e64f2a78e32418a98e1c80c6579ae0779dfc (diff) |
QWindowContext / QWindowFormat refactor.
To enable having a single GL context used for multiple drawables we need
to de-couple the context class a bit more from the window class in the
plugin API. Now contexts are created stand-alone based on a GL format
and a share context, and when calling makeCurrent() a desired surface
is specified. This maps well to GLX, EGL, Cocoa, AGL, and WGL, which all
support this use case.
QWindowContext is renamed to QGuiGLContext, and QWindowFormat is renamed
to QGuiGLFormat. We have the ability to introduce a pbuffer or similar
other offscreen GL drawable abstraction in the future.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/opengl/hellowindow/hellowindow.cpp | 88 | ||||
-rw-r--r-- | examples/opengl/hellowindow/hellowindow.h | 40 | ||||
-rw-r--r-- | examples/opengl/hellowindow/main.cpp | 9 |
3 files changed, 85 insertions, 52 deletions
diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp index 32732f72b6..50cf8b0453 100644 --- a/examples/opengl/hellowindow/hellowindow.cpp +++ b/examples/opengl/hellowindow/hellowindow.cpp @@ -1,72 +1,82 @@ #include "hellowindow.h" -#include <QWindowContext> +#include <QGuiGLContext> #include <QTimer> #include <qmath.h> -HelloWindow::HelloWindow() - : colorIndex(0) +Renderer::Renderer() + : m_initialized(false) +{ + m_format.setDepthBufferSize(16); + m_format.setSamples(4); + + m_context = new QGuiGLContext(m_format); +} + +QGuiGLFormat Renderer::format() const +{ + return m_format; +} + +HelloWindow::HelloWindow(Renderer *renderer) + : m_colorIndex(0) + , m_renderer(renderer) { setSurfaceType(OpenGLSurface); setWindowTitle(QLatin1String("Hello Window")); - QWindowFormat format; - format.setDepthBufferSize(16); - format.setSamples(4); - - setWindowFormat(format); + setGLFormat(renderer->format()); setGeometry(QRect(10, 10, 640, 480)); create(); - initialize(); - QTimer *timer = new QTimer(this); timer->start(10); connect(timer, SIGNAL(timeout()), this, SLOT(render())); -} -void HelloWindow::mousePressEvent(QMouseEvent *) -{ updateColor(); } -void HelloWindow::resizeEvent(QResizeEvent *) +void HelloWindow::mousePressEvent(QMouseEvent *) { - glContext()->makeCurrent(); - - glViewport(0, 0, geometry().width(), geometry().height()); + updateColor(); } void HelloWindow::updateColor() { - float colors[][4] = + QColor colors[] = { - { 0.4, 1.0, 0.0, 0.0 }, - { 0.0, 0.4, 1.0, 0.0 } + QColor(100, 255, 0), + QColor(0, 100, 255) }; - glContext()->makeCurrent(); - - program.bind(); - program.setUniformValue(colorUniform, colors[colorIndex][0], colors[colorIndex][1], colors[colorIndex][2], colors[colorIndex][3]); - program.release(); + m_color = colors[m_colorIndex]; - colorIndex++; - if (colorIndex >= sizeof(colors) / sizeof(colors[0])) - colorIndex = 0; + m_colorIndex++; + if (m_colorIndex >= int(sizeof(colors) / sizeof(colors[0]))) + m_colorIndex = 0; } void HelloWindow::render() { - if (!glContext()) - return; + if (glSurface()) + m_renderer->render(glSurface(), m_color, geometry().size()); +} + +void Renderer::render(QPlatformGLSurface *surface, const QColor &color, const QSize &viewSize) +{ + m_context->makeCurrent(surface); + + if (!m_initialized) { + initialize(); + m_initialized = true; + } - glContext()->makeCurrent(); + glViewport(0, 0, viewSize.width(), viewSize.height()); glClearColor(0.1f, 0.1f, 0.2f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -84,18 +94,19 @@ void HelloWindow::render() program.bind(); program.setUniformValue(matrixUniform, modelview); + program.setUniformValue(colorUniform, color); paintQtLogo(); program.release(); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); - glContext()->swapBuffers(); + m_context->swapBuffers(surface); m_fAngle += 1.0f; } -void HelloWindow::paintQtLogo() +void Renderer::paintQtLogo() { program.enableAttributeArray(normalAttr); program.enableAttributeArray(vertexAttr); @@ -106,10 +117,8 @@ void HelloWindow::paintQtLogo() program.disableAttributeArray(vertexAttr); } -void HelloWindow::initialize() +void Renderer::initialize() { - glContext()->makeCurrent(); - glClearColor(0.1f, 0.1f, 0.2f, 1.0f); QGLShader *vshader = new QGLShader(QGLShader::Vertex, this); @@ -150,10 +159,9 @@ void HelloWindow::initialize() m_fAngle = 0; createGeometry(); - updateColor(); } -void HelloWindow::createGeometry() +void Renderer::createGeometry() { vertices.clear(); normals.clear(); @@ -204,7 +212,7 @@ void HelloWindow::createGeometry() vertices[i] *= 2.0f; } -void HelloWindow::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4) +void Renderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4) { vertices << QVector3D(x1, y1, -0.05f); vertices << QVector3D(x2, y2, -0.05f); @@ -245,7 +253,7 @@ void HelloWindow::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y normals << n; } -void HelloWindow::extrude(qreal x1, qreal y1, qreal x2, qreal y2) +void Renderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2) { vertices << QVector3D(x1, y1, +0.05f); vertices << QVector3D(x2, y2, +0.05f); diff --git a/examples/opengl/hellowindow/hellowindow.h b/examples/opengl/hellowindow/hellowindow.h index f0b8ee8b45..274dc9ca17 100644 --- a/examples/opengl/hellowindow/hellowindow.h +++ b/examples/opengl/hellowindow/hellowindow.h @@ -5,22 +5,19 @@ #include <QTime> -class HelloWindow : public QWindow +class QGuiGLContext; + +class Renderer : public QObject { - Q_OBJECT public: - HelloWindow(); + Renderer(); -protected: - void mousePressEvent(QMouseEvent *); - void resizeEvent(QResizeEvent *); + QGuiGLFormat format() const; -private slots: - void render(); + void render(QPlatformGLSurface *surface, const QColor &color, const QSize &viewSize); private: void initialize(); - void updateColor(); qreal m_fAngle; bool m_showBubbles; @@ -36,5 +33,28 @@ private: int normalAttr; int matrixUniform; int colorUniform; - uint colorIndex; + + bool m_initialized; + QGuiGLFormat m_format; + QGuiGLContext *m_context; +}; + +class HelloWindow : public QWindow +{ + Q_OBJECT +public: + HelloWindow(Renderer *renderer); + +private slots: + void render(); + +protected: + void mousePressEvent(QMouseEvent *); + +private: + void updateColor(); + + int m_colorIndex; + QColor m_color; + Renderer *m_renderer; }; diff --git a/examples/opengl/hellowindow/main.cpp b/examples/opengl/hellowindow/main.cpp index 1b80dc7523..af5943adf4 100644 --- a/examples/opengl/hellowindow/main.cpp +++ b/examples/opengl/hellowindow/main.cpp @@ -6,8 +6,13 @@ int main(int argc, char **argv) { QGuiApplication app(argc, argv); - HelloWindow window; - window.setVisible(true); + Renderer renderer; + + HelloWindow windowA(&renderer); + windowA.setVisible(true); + + HelloWindow windowB(&renderer); + windowB.setVisible(true); return app.exec(); } |