summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/opengl/hellowindow/hellowindow.cpp43
-rw-r--r--examples/opengl/hellowindow/hellowindow.h12
-rw-r--r--examples/opengl/hellowindow/hellowindow.pro2
-rw-r--r--examples/opengl/opengl.pro1
-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
8 files changed, 228 insertions, 29 deletions
diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp
index 3c4f88bdfe..cc1f52a92c 100644
--- a/examples/opengl/hellowindow/hellowindow.cpp
+++ b/examples/opengl/hellowindow/hellowindow.cpp
@@ -1,6 +1,6 @@
#include "hellowindow.h"
-#include <QGuiGLContext>
+#include <QOpenGLContext>
#include <QTimer>
@@ -10,7 +10,7 @@ Renderer::Renderer(const QSurfaceFormat &format, Renderer *share)
: m_initialized(false)
, m_format(format)
{
- m_context = new QGuiGLContext;
+ m_context = new QOpenGLContext;
m_context->setFormat(format);
if (share)
m_context->setShareContext(share->m_context);
@@ -91,11 +91,11 @@ void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewS
modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f);
modelview.translate(0.0f, -0.2f, 0.0f);
- program.bind();
- program.setUniformValue(matrixUniform, modelview);
- program.setUniformValue(colorUniform, color);
+ m_program->bind();
+ m_program->setUniformValue(matrixUniform, modelview);
+ m_program->setUniformValue(colorUniform, color);
paintQtLogo();
- program.release();
+ m_program->release();
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
@@ -107,20 +107,20 @@ void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewS
void Renderer::paintQtLogo()
{
- program.enableAttributeArray(normalAttr);
- program.enableAttributeArray(vertexAttr);
- program.setAttributeArray(vertexAttr, vertices.constData());
- program.setAttributeArray(normalAttr, normals.constData());
+ m_program->enableAttributeArray(normalAttr);
+ m_program->enableAttributeArray(vertexAttr);
+ m_program->setAttributeArray(vertexAttr, vertices.constData());
+ m_program->setAttributeArray(normalAttr, normals.constData());
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
- program.disableAttributeArray(normalAttr);
- program.disableAttributeArray(vertexAttr);
+ m_program->disableAttributeArray(normalAttr);
+ m_program->disableAttributeArray(vertexAttr);
}
void Renderer::initialize()
{
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
- QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
+ QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
const char *vsrc =
"attribute highp vec4 vertex;\n"
"attribute mediump vec3 normal;\n"
@@ -138,7 +138,7 @@ void Renderer::initialize()
"}\n";
vshader->compileSourceCode(vsrc);
- QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
+ QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
const char *fsrc =
"varying mediump vec4 color;\n"
"void main(void)\n"
@@ -147,14 +147,15 @@ void Renderer::initialize()
"}\n";
fshader->compileSourceCode(fsrc);
- program.addShader(vshader);
- program.addShader(fshader);
- program.link();
+ m_program = new QOpenGLShaderProgram;
+ m_program->addShader(vshader);
+ m_program->addShader(fshader);
+ m_program->link();
- vertexAttr = program.attributeLocation("vertex");
- normalAttr = program.attributeLocation("normal");
- matrixUniform = program.uniformLocation("matrix");
- colorUniform = program.uniformLocation("sourceColor");
+ vertexAttr = m_program->attributeLocation("vertex");
+ normalAttr = m_program->attributeLocation("normal");
+ matrixUniform = m_program->uniformLocation("matrix");
+ colorUniform = m_program->uniformLocation("sourceColor");
m_fAngle = 0;
createGeometry();
diff --git a/examples/opengl/hellowindow/hellowindow.h b/examples/opengl/hellowindow/hellowindow.h
index c114b09a24..6ff7411251 100644
--- a/examples/opengl/hellowindow/hellowindow.h
+++ b/examples/opengl/hellowindow/hellowindow.h
@@ -1,12 +1,12 @@
#include <QWindow>
-#include <QtOpenGL/qgl.h>
-#include <QtOpenGL/qglshaderprogram.h>
-#include <QtOpenGL/qglframebufferobject.h>
+#include <QtGui/qopengl.h>
+#include <QtGui/qopenglshaderprogram.h>
+#include <QColor>
#include <QTime>
-class QGuiGLContext;
+class QOpenGLContext;
class Renderer : public QObject
{
@@ -31,7 +31,6 @@ private:
void extrude(qreal x1, qreal y1, qreal x2, qreal y2);
QVector<QVector3D> vertices;
QVector<QVector3D> normals;
- QGLShaderProgram program;
int vertexAttr;
int normalAttr;
int matrixUniform;
@@ -39,7 +38,8 @@ private:
bool m_initialized;
QSurfaceFormat m_format;
- QGuiGLContext *m_context;
+ QOpenGLContext *m_context;
+ QOpenGLShaderProgram *m_program;
};
class HelloWindow : public QWindow
diff --git a/examples/opengl/hellowindow/hellowindow.pro b/examples/opengl/hellowindow/hellowindow.pro
index 44003d8863..555dc83574 100644
--- a/examples/opengl/hellowindow/hellowindow.pro
+++ b/examples/opengl/hellowindow/hellowindow.pro
@@ -6,8 +6,6 @@ TEMPLATE = app
DEPENDPATH += .
INCLUDEPATH += .
-QT += opengl widgets
-
# Input
HEADERS += hellowindow.h
SOURCES += hellowindow.cpp main.cpp
diff --git a/examples/opengl/opengl.pro b/examples/opengl/opengl.pro
index 59ba7b8bd5..5502ee270a 100644
--- a/examples/opengl/opengl.pro
+++ b/examples/opengl/opengl.pro
@@ -24,6 +24,7 @@ contains(QT_CONFIG, opengles1)|contains(QT_CONFIG, opengles2){
qpa {
SUBDIRS += hellowindow
+ SUBDIRS += paintedwindow
}
# install
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
+