Make OpenGL legacy examples hellogl and overpainting work with Dynamic GL.

Call GL functions using QOpenGLFunctions_1_1.

Task-number: QTBUG-46103
Change-Id: I1cbacf9c192c17d96d96aa861bb16e2918a0c053
Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2015-05-18 10:31:33 +02:00
parent 4476966e04
commit cab7e7858a
8 changed files with 33 additions and 25 deletions

View File

@ -128,6 +128,8 @@ void GLWidget::setZRotation(int angle)
//! [6] //! [6]
void GLWidget::initializeGL() void GLWidget::initializeGL()
{ {
initializeOpenGLFunctions();
qglClearColor(qtPurple.dark()); qglClearColor(qtPurple.dark());
logo = new QtLogo(this, 64); logo = new QtLogo(this, 64);
@ -153,7 +155,7 @@ void GLWidget::paintGL()
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0); glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0); glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0); glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
logo->draw(); logo->draw(static_cast<QOpenGLFunctions_1_1 *>(this));
} }
//! [7] //! [7]

View File

@ -42,11 +42,12 @@
#define GLWIDGET_H #define GLWIDGET_H
#include <QGLWidget> #include <QGLWidget>
#include <QOpenGLFunctions_1_1>
class QtLogo; class QtLogo;
//! [0] //! [0]
class GLWidget : public QGLWidget class GLWidget : public QGLWidget, public QOpenGLFunctions_1_1
{ {
Q_OBJECT Q_OBJECT

View File

@ -14,4 +14,4 @@ QT += opengl widgets
target.path = $$[QT_INSTALL_EXAMPLES]/opengl/legacy/hellogl target.path = $$[QT_INSTALL_EXAMPLES]/opengl/legacy/hellogl
INSTALLS += target INSTALLS += target
contains(QT_CONFIG, opengles.|angle|dynamicgl):error("This example requires Qt to be configured with -opengl desktop") contains(QT_CONFIG, opengles.|angle):error("This example requires Qt to be configured with -opengl desktop")

View File

@ -115,6 +115,8 @@ void GLWidget::setZRotation(int angle)
//! [2] //! [2]
void GLWidget::initializeGL() void GLWidget::initializeGL()
{ {
initializeOpenGLFunctions();
glEnable(GL_MULTISAMPLE); glEnable(GL_MULTISAMPLE);
logo = new QtLogo(this); logo = new QtLogo(this);
@ -173,7 +175,7 @@ void GLWidget::paintEvent(QPaintEvent *event)
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0); glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0); glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
logo->draw(); logo->draw(static_cast<QOpenGLFunctions_1_1 *>(this));
//! [7] //! [7]
//! [8] //! [8]

View File

@ -42,13 +42,14 @@
#define GLWIDGET_H #define GLWIDGET_H
#include <QGLWidget> #include <QGLWidget>
#include <QOpenGLFunctions_1_1>
#include <QTimer> #include <QTimer>
class Bubble; class Bubble;
class QtLogo; class QtLogo;
//! [0] //! [0]
class GLWidget : public QGLWidget class GLWidget : public QGLWidget, public QOpenGLFunctions_1_1
{ {
Q_OBJECT Q_OBJECT

View File

@ -16,4 +16,4 @@ SOURCES = bubble.cpp \
target.path = $$[QT_INSTALL_EXAMPLES]/opengl/legacy/overpainting target.path = $$[QT_INSTALL_EXAMPLES]/opengl/legacy/overpainting
INSTALLS += target INSTALLS += target
contains(QT_CONFIG, opengles.|angle|dynamicgl):error("This example requires Qt to be configured with -opengl desktop") contains(QT_CONFIG, opengles.|angle):error("This example requires Qt to be configured with -opengl desktop")

View File

@ -43,6 +43,7 @@
#include <QGLWidget> #include <QGLWidget>
#include <QMatrix4x4> #include <QMatrix4x4>
#include <QVector3D> #include <QVector3D>
#include <QOpenGLFunctions_1_1>
#include <qmath.h> #include <qmath.h>
@ -60,7 +61,7 @@ struct Geometry
void appendSmooth(const QVector3D &a, const QVector3D &n, int from); void appendSmooth(const QVector3D &a, const QVector3D &n, int from);
void appendFaceted(const QVector3D &a, const QVector3D &n); void appendFaceted(const QVector3D &a, const QVector3D &n);
void finalize(); void finalize();
void loadArrays() const; void loadArrays(QOpenGLFunctions_1_1 *functions) const;
}; };
//! [0] //! [0]
@ -73,7 +74,7 @@ public:
void setSmoothing(Smoothing s) { sm = s; } void setSmoothing(Smoothing s) { sm = s; }
void translate(const QVector3D &t); void translate(const QVector3D &t);
void rotate(qreal deg, QVector3D axis); void rotate(qreal deg, QVector3D axis);
void draw() const; void draw(QOpenGLFunctions_1_1 *functions) const;
void addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n); void addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n);
void addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &d); void addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &d);
@ -96,10 +97,10 @@ static inline void qSetColor(float colorVec[], QColor c)
colorVec[3] = c.alphaF(); colorVec[3] = c.alphaF();
} }
void Geometry::loadArrays() const void Geometry::loadArrays(QOpenGLFunctions_1_1 *functions) const
{ {
glVertexPointer(3, GL_FLOAT, 0, vertices.constData()); functions->glVertexPointer(3, GL_FLOAT, 0, vertices.constData());
glNormalPointer(GL_FLOAT, 0, normals.constData()); functions->glNormalPointer(GL_FLOAT, 0, normals.constData());
} }
void Geometry::finalize() void Geometry::finalize()
@ -170,15 +171,15 @@ void Patch::translate(const QVector3D &t)
} }
//! [2] //! [2]
void Patch::draw() const void Patch::draw(QOpenGLFunctions_1_1 *functions) const
{ {
glPushMatrix(); functions->glPushMatrix();
glMultMatrixf(mat.constData()); functions->glMultMatrixf(mat.constData());
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, faceColor); functions->glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, faceColor);
const GLushort *indices = geom->faces.constData(); const GLushort *indices = geom->faces.constData();
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, indices + start); functions->glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, indices + start);
glPopMatrix(); functions->glPopMatrix();
} }
//! [2] //! [2]
@ -371,17 +372,17 @@ void QtLogo::buildGeometry(int divisions, qreal scale)
//! [3] //! [3]
//! [4] //! [4]
void QtLogo::draw() const void QtLogo::draw(QOpenGLFunctions_1_1 *functions) const
{ {
geom->loadArrays(); geom->loadArrays(functions);
glEnableClientState(GL_VERTEX_ARRAY); functions->glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY); functions->glEnableClientState(GL_NORMAL_ARRAY);
for (int i = 0; i < parts.count(); ++i) for (int i = 0; i < parts.count(); ++i)
parts[i]->draw(); parts[i]->draw(functions);
glDisableClientState(GL_VERTEX_ARRAY); functions->glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY); functions->glDisableClientState(GL_NORMAL_ARRAY);
} }
//! [4] //! [4]

View File

@ -44,6 +44,7 @@
#include <QObject> #include <QObject>
#include <QColor> #include <QColor>
class QOpenGLFunctions_1_1;
class Patch; class Patch;
struct Geometry; struct Geometry;
@ -54,7 +55,7 @@ public:
explicit QtLogo(QObject *parent, int d = 64, qreal s = 1.0); explicit QtLogo(QObject *parent, int d = 64, qreal s = 1.0);
~QtLogo(); ~QtLogo();
void setColor(QColor c); void setColor(QColor c);
void draw() const; void draw(QOpenGLFunctions_1_1 *functions) const;
private: private:
void buildGeometry(int d, qreal s); void buildGeometry(int d, qreal s);