summaryrefslogtreecommitdiffstats
path: root/examples/opengl/paintedwindow
diff options
context:
space:
mode:
authorSamuel Rødal <samuel.rodal@nokia.com>2011-08-22 10:49:28 +0200
committerSamuel Rødal <samuel.rodal@nokia.com>2011-08-29 10:25:24 +0200
commit6e28e8441b698c3397c2c78125c877f2e9867cb1 (patch)
tree1e3ad0e43cb775854835817cd04bdc8b5e047e15 /examples/opengl/paintedwindow
parentaaa4a26f82f99fa8724841eba91bad029306e0ce (diff)
Copy core GL functionality to QtGui with QGL -> QOpenGL naming.
Change-Id: Ibc989afa4a30dd184d41d1a1cd89f97196e48855 Reviewed-on: http://codereview.qt.nokia.com/3710 Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
Diffstat (limited to 'examples/opengl/paintedwindow')
-rw-r--r--examples/opengl/paintedwindow/main.cpp27
-rw-r--r--examples/opengl/paintedwindow/paintedwindow.cpp124
-rw-r--r--examples/opengl/paintedwindow/paintedwindow.h30
-rw-r--r--examples/opengl/paintedwindow/paintedwindow.pro18
4 files changed, 199 insertions, 0 deletions
diff --git a/examples/opengl/paintedwindow/main.cpp b/examples/opengl/paintedwindow/main.cpp
new file mode 100644
index 0000000000..c77cf41f0f
--- /dev/null
+++ b/examples/opengl/paintedwindow/main.cpp
@@ -0,0 +1,27 @@
+#include <QGuiApplication>
+#include <QRect>
+#include <QScreen>
+
+#include "paintedwindow.h"
+
+int main(int argc, char **argv)
+{
+ QGuiApplication app(argc, argv);
+
+ QScreen *screen = QGuiApplication::primaryScreen();
+
+ QRect screenGeometry = screen->availableGeometry();
+
+ QSurfaceFormat format;
+ format.setDepthBufferSize(16);
+ format.setSamples(4);
+
+ QPoint center = screenGeometry.center();
+ QRect windowRect(0, 0, 640, 480);
+
+ PaintedWindow window;
+ window.setGeometry(QRect(center - windowRect.center(), windowRect.size()));
+ window.show();
+
+ app.exec();
+}
diff --git a/examples/opengl/paintedwindow/paintedwindow.cpp b/examples/opengl/paintedwindow/paintedwindow.cpp
new file mode 100644
index 0000000000..b9ecac1293
--- /dev/null
+++ b/examples/opengl/paintedwindow/paintedwindow.cpp
@@ -0,0 +1,124 @@
+#include "paintedwindow.h"
+
+#include <QOpenGLContext>
+#include <QOpenGLFunctions>
+#include <QPainter>
+#include <QTimer>
+
+#include <qmath.h>
+
+PaintedWindow::PaintedWindow()
+ : m_fbo(0)
+{
+ QSurfaceFormat format;
+ format.setStencilBufferSize(8);
+
+ setSurfaceType(QWindow::OpenGLSurface);
+ setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
+ setFormat(format);
+
+ create();
+
+ 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);
+
+ delete m_fbo;
+ m_fbo = new QOpenGLFramebufferObject(size());
+}
+
+void PaintedWindow::paint()
+{
+ if (!m_fbo)
+ return;
+
+ m_context->makeCurrent(this);
+
+ QPainterPath path;
+ path.addEllipse(0, 0, m_fbo->width(), m_fbo->height());
+
+ QPainter painter;
+ painter.begin(m_fbo);
+ painter.fillRect(0, 0, m_fbo->width(), m_fbo->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);
+}
diff --git a/examples/opengl/paintedwindow/paintedwindow.h b/examples/opengl/paintedwindow/paintedwindow.h
new file mode 100644
index 0000000000..9e1b43638c
--- /dev/null
+++ b/examples/opengl/paintedwindow/paintedwindow.h
@@ -0,0 +1,30 @@
+#include <QWindow>
+
+#include <QtGui/qopengl.h>
+#include <QtGui/qopenglshaderprogram.h>
+#include <QtGui/qopenglframebufferobject.h>
+
+#include <QColor>
+#include <QTime>
+
+class QOpenGLContext;
+
+class PaintedWindow : public QWindow
+{
+ Q_OBJECT
+public:
+ PaintedWindow();
+
+private slots:
+ void paint();
+
+private:
+ void resizeEvent(QResizeEvent *);
+
+ QOpenGLContext *m_context;
+ QOpenGLFramebufferObject *m_fbo;
+ QOpenGLShaderProgram *m_program;
+
+ GLuint m_vertexAttribute;
+ GLuint m_texCoordsAttribute;
+};
diff --git a/examples/opengl/paintedwindow/paintedwindow.pro b/examples/opengl/paintedwindow/paintedwindow.pro
new file mode 100644
index 0000000000..a3ae3bace5
--- /dev/null
+++ b/examples/opengl/paintedwindow/paintedwindow.pro
@@ -0,0 +1,18 @@
+######################################################################
+# Automatically generated by qmake (2.01a) Wed Apr 27 16:40:46 2011
+######################################################################
+
+TEMPLATE = app
+DEPENDPATH += .
+INCLUDEPATH += .
+
+# Input
+HEADERS += paintedwindow.h
+SOURCES += paintedwindow.cpp main.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/opengl/paintedwindow
+sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS paintedwindow.pro
+sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/opengl/paintedwindow
+INSTALLS += target sources
+