summaryrefslogtreecommitdiffstats
path: root/examples/opengl/hellowindow/hellowindow.cpp
diff options
context:
space:
mode:
authorSamuel Rødal <samuel.rodal@nokia.com>2011-06-07 17:25:22 +0200
committerSamuel Rødal <samuel.rodal@nokia.com>2011-06-10 09:24:56 +0200
commit4a189c188ccd2fb5f8d1d5ddadf06cbd6bc0916f (patch)
tree99bff9f015e869b5521836ea5667590939b22a53 /examples/opengl/hellowindow/hellowindow.cpp
parent4d10e64f2a78e32418a98e1c80c6579ae0779dfc (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/opengl/hellowindow/hellowindow.cpp')
-rw-r--r--examples/opengl/hellowindow/hellowindow.cpp88
1 files changed, 48 insertions, 40 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);