Polish code of some opengl examples
Change-Id: If24ae1845176fc525cf6a239a5079f4802f8df3f Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
parent
4eac2c4728
commit
0136252cb2
@ -38,10 +38,11 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
#include "glwidget.h"
|
||||
#include "helper.h"
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
//! [0]
|
||||
GLWidget::GLWidget(Helper *helper, QWidget *parent)
|
||||
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent), helper(helper)
|
||||
|
@ -45,10 +45,6 @@
|
||||
|
||||
//! [0]
|
||||
class Helper;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QPaintEvent;
|
||||
class QWidget;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class GLWidget : public QGLWidget
|
||||
{
|
||||
|
@ -38,9 +38,12 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
#include "helper.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QPaintEvent>
|
||||
#include <QWidget>
|
||||
|
||||
//! [0]
|
||||
Helper::Helper()
|
||||
{
|
||||
@ -70,14 +73,15 @@ void Helper::paint(QPainter *painter, QPaintEvent *event, int elapsed)
|
||||
painter->setPen(circlePen);
|
||||
painter->rotate(elapsed * 0.030);
|
||||
|
||||
qreal r = elapsed/1000.0;
|
||||
qreal r = elapsed / 1000.0;
|
||||
int n = 30;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
painter->rotate(30);
|
||||
qreal radius = 0 + 120.0*((i+r)/n);
|
||||
qreal circleRadius = 1 + ((i+r)/n)*20;
|
||||
qreal factor = (i + r) / n;
|
||||
qreal radius = 0 + 120.0 * factor;
|
||||
qreal circleRadius = 1 + factor * 20;
|
||||
painter->drawEllipse(QRectF(radius, -circleRadius,
|
||||
circleRadius*2, circleRadius*2));
|
||||
circleRadius * 2, circleRadius * 2));
|
||||
}
|
||||
painter->restore();
|
||||
//! [2]
|
||||
@ -85,6 +89,6 @@ void Helper::paint(QPainter *painter, QPaintEvent *event, int elapsed)
|
||||
//! [3]
|
||||
painter->setPen(textPen);
|
||||
painter->setFont(textFont);
|
||||
painter->drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, "Qt");
|
||||
painter->drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, QStringLiteral("Qt"));
|
||||
}
|
||||
//! [3]
|
||||
|
@ -44,11 +44,7 @@
|
||||
#include <QBrush>
|
||||
#include <QFont>
|
||||
#include <QPen>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QPainter;
|
||||
class QPaintEvent;
|
||||
QT_END_NAMESPACE
|
||||
#include <QWidget>
|
||||
|
||||
//! [0]
|
||||
class Helper
|
||||
|
@ -38,9 +38,10 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QApplication>
|
||||
#include "window.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
@ -38,10 +38,11 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
#include "widget.h"
|
||||
#include "helper.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QTimer>
|
||||
|
||||
//! [0]
|
||||
Widget::Widget(Helper *helper, QWidget *parent)
|
||||
|
@ -45,9 +45,6 @@
|
||||
|
||||
//! [0]
|
||||
class Helper;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QPaintEvent;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class Widget : public QWidget
|
||||
{
|
||||
|
@ -38,15 +38,19 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
#include "glwidget.h"
|
||||
#include "widget.h"
|
||||
#include "window.h"
|
||||
|
||||
#include <QGridLayout>
|
||||
#include <QLabel>
|
||||
#include <QTimer>
|
||||
|
||||
//! [0]
|
||||
Window::Window()
|
||||
: QWidget()
|
||||
{
|
||||
setWindowTitle(tr("2D Painting on Native and OpenGL Widgets"));
|
||||
|
||||
Widget *native = new Widget(&helper, this);
|
||||
GLWidget *openGL = new GLWidget(&helper, this);
|
||||
QLabel *nativeLabel = new QLabel(tr("Native"));
|
||||
@ -65,7 +69,5 @@ Window::Window()
|
||||
connect(timer, SIGNAL(timeout()), native, SLOT(animate()));
|
||||
connect(timer, SIGNAL(timeout()), openGL, SLOT(animate()));
|
||||
timer->start(50);
|
||||
|
||||
setWindowTitle(tr("2D Painting on Native and OpenGL Widgets"));
|
||||
}
|
||||
//! [0]
|
||||
|
@ -41,14 +41,9 @@
|
||||
#ifndef WINDOW_H
|
||||
#define WINDOW_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include "helper.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QLabel;
|
||||
class QWidget;
|
||||
QT_END_NAMESPACE
|
||||
#include <QWidget>
|
||||
|
||||
//! [0]
|
||||
class Window : public QWidget
|
||||
|
@ -49,14 +49,13 @@ struct VertexData
|
||||
QVector2D texCoord;
|
||||
};
|
||||
|
||||
GeometryEngine::GeometryEngine() : vboIds(new GLuint[2])
|
||||
GeometryEngine::GeometryEngine()
|
||||
{
|
||||
}
|
||||
|
||||
GeometryEngine::~GeometryEngine()
|
||||
{
|
||||
glDeleteBuffers(2, vboIds);
|
||||
delete[] vboIds;
|
||||
}
|
||||
|
||||
void GeometryEngine::init()
|
||||
|
@ -41,8 +41,8 @@
|
||||
#ifndef GEOMETRYENGINE_H
|
||||
#define GEOMETRYENGINE_H
|
||||
|
||||
#include <QtOpenGL/QGLFunctions>
|
||||
#include <QtOpenGL/QGLShaderProgram>
|
||||
#include <QGLFunctions>
|
||||
#include <QGLShaderProgram>
|
||||
|
||||
class GeometryEngine : protected QGLFunctions
|
||||
{
|
||||
@ -51,14 +51,12 @@ public:
|
||||
virtual ~GeometryEngine();
|
||||
|
||||
void init();
|
||||
|
||||
void drawCubeGeometry(QGLShaderProgram *program);
|
||||
|
||||
private:
|
||||
void initCubeGeometry();
|
||||
|
||||
GLuint *vboIds;
|
||||
|
||||
GLuint vboIds[2];
|
||||
};
|
||||
|
||||
#endif // GEOMETRYENGINE_H
|
||||
|
@ -47,16 +47,15 @@
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
a.setApplicationName("cube");
|
||||
a.setApplicationVersion("0.1");
|
||||
#ifndef QT_NO_OPENGL
|
||||
MainWidget w;
|
||||
w.resize(640, 480);
|
||||
w.show();
|
||||
QApplication app(argc, argv);
|
||||
app.setApplicationName("cube");
|
||||
app.setApplicationVersion("0.1");
|
||||
#ifdef QT_NO_OPENGL
|
||||
MainWidget widget;
|
||||
widget.show();
|
||||
#else
|
||||
QLabel * notifyLabel = new QLabel("OpenGL Support required");
|
||||
notifyLabel->show();
|
||||
QLabel note("OpenGL Support required");
|
||||
note.show();
|
||||
#endif
|
||||
return a.exec();
|
||||
return app.exec();
|
||||
}
|
||||
|
@ -40,39 +40,26 @@
|
||||
|
||||
#include "mainwidget.h"
|
||||
|
||||
#include "geometryengine.h"
|
||||
|
||||
#include <QtOpenGL/QGLShaderProgram>
|
||||
|
||||
#include <QBasicTimer>
|
||||
#include <QMouseEvent>
|
||||
#include <QDebug>
|
||||
|
||||
#include <math.h>
|
||||
#include <locale.h>
|
||||
|
||||
MainWidget::MainWidget(QWidget *parent) :
|
||||
QGLWidget(parent),
|
||||
timer(new QBasicTimer),
|
||||
program(new QGLShaderProgram),
|
||||
geometries(new GeometryEngine),
|
||||
angularSpeed(0)
|
||||
{
|
||||
}
|
||||
|
||||
MainWidget::~MainWidget()
|
||||
{
|
||||
delete timer; timer = 0;
|
||||
delete program; program = 0;
|
||||
delete geometries; geometries = 0;
|
||||
|
||||
deleteTexture(texture);
|
||||
}
|
||||
|
||||
//! [0]
|
||||
void MainWidget::mousePressEvent(QMouseEvent *e)
|
||||
{
|
||||
// Saving mouse press position
|
||||
// Save mouse press position
|
||||
mousePressPosition = QVector2D(e->localPos());
|
||||
}
|
||||
|
||||
@ -97,17 +84,15 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *e)
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void MainWidget::timerEvent(QTimerEvent *e)
|
||||
void MainWidget::timerEvent(QTimerEvent *)
|
||||
{
|
||||
Q_UNUSED(e);
|
||||
|
||||
// Decrease angular speed (friction)
|
||||
angularSpeed *= 0.99;
|
||||
|
||||
// Stop rotation when speed goes below threshold
|
||||
if (angularSpeed < 0.01)
|
||||
if (angularSpeed < 0.01) {
|
||||
angularSpeed = 0.0;
|
||||
else {
|
||||
} else {
|
||||
// Update rotation
|
||||
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
|
||||
|
||||
@ -120,13 +105,8 @@ void MainWidget::timerEvent(QTimerEvent *e)
|
||||
void MainWidget::initializeGL()
|
||||
{
|
||||
initializeGLFunctions();
|
||||
|
||||
qglClearColor(Qt::black);
|
||||
|
||||
qDebug() << "Initializing shaders...";
|
||||
initShaders();
|
||||
|
||||
qDebug() << "Initializing textures...";
|
||||
initTextures();
|
||||
|
||||
//! [2]
|
||||
@ -137,33 +117,32 @@ void MainWidget::initializeGL()
|
||||
glEnable(GL_CULL_FACE);
|
||||
//! [2]
|
||||
|
||||
qDebug() << "Initializing geometries...";
|
||||
geometries->init();
|
||||
geometries.init();
|
||||
|
||||
// using QBasicTimer because its faster that QTimer
|
||||
timer->start(12, this);
|
||||
// Use QBasicTimer because its faster than QTimer
|
||||
timer.start(12, this);
|
||||
}
|
||||
|
||||
//! [3]
|
||||
void MainWidget::initShaders()
|
||||
{
|
||||
// Overriding system locale until shaders are compiled
|
||||
// Override system locale until shaders are compiled
|
||||
setlocale(LC_NUMERIC, "C");
|
||||
|
||||
// Compiling vertex shader
|
||||
if (!program->addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl"))
|
||||
// Compile vertex shader
|
||||
if (!program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl"))
|
||||
close();
|
||||
|
||||
// Compiling fragment shader
|
||||
if (!program->addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl"))
|
||||
// Compile fragment shader
|
||||
if (!program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl"))
|
||||
close();
|
||||
|
||||
// Linking shader pipeline
|
||||
if (!program->link())
|
||||
// Link shader pipeline
|
||||
if (!program.link())
|
||||
close();
|
||||
|
||||
// Binding shader pipeline for use
|
||||
if (!program->bind())
|
||||
// Bind shader pipeline for use
|
||||
if (!program.bind())
|
||||
close();
|
||||
|
||||
// Restore system locale
|
||||
@ -174,7 +153,7 @@ void MainWidget::initShaders()
|
||||
//! [4]
|
||||
void MainWidget::initTextures()
|
||||
{
|
||||
// Loading cube.png
|
||||
// Load cube.png image
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
texture = bindTexture(QImage(":/cube.png"));
|
||||
|
||||
@ -198,7 +177,7 @@ void MainWidget::resizeGL(int w, int h)
|
||||
glViewport(0, 0, w, h);
|
||||
|
||||
// Calculate aspect ratio
|
||||
qreal aspect = (qreal)w / ((qreal)h?h:1);
|
||||
qreal aspect = qreal(w) / qreal(h ? h : 1);
|
||||
|
||||
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
|
||||
const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
|
||||
@ -223,12 +202,12 @@ void MainWidget::paintGL()
|
||||
matrix.rotate(rotation);
|
||||
|
||||
// Set modelview-projection matrix
|
||||
program->setUniformValue("mvp_matrix", projection * matrix);
|
||||
program.setUniformValue("mvp_matrix", projection * matrix);
|
||||
//! [6]
|
||||
|
||||
// Using texture unit 0 which contains cube.png
|
||||
program->setUniformValue("texture", 0);
|
||||
// Use texture unit 0 which contains cube.png
|
||||
program.setUniformValue("texture", 0);
|
||||
|
||||
// Draw cube geometry
|
||||
geometries->drawCubeGeometry(program);
|
||||
geometries.drawCubeGeometry(&program);
|
||||
}
|
||||
|
@ -41,30 +41,26 @@
|
||||
#ifndef MAINWIDGET_H
|
||||
#define MAINWIDGET_H
|
||||
|
||||
#include <QtOpenGL/QGLWidget>
|
||||
#include <QtOpenGL/QGLFunctions>
|
||||
#include "geometryengine.h"
|
||||
|
||||
#include <QGLWidget>
|
||||
#include <QGLFunctions>
|
||||
#include <QMatrix4x4>
|
||||
#include <QQuaternion>
|
||||
#include <QVector2D>
|
||||
#include <QBasicTimer>
|
||||
#include <QGLShaderProgram>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QBasicTimer;
|
||||
class QGLShaderProgram;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class GeometryEngine;
|
||||
|
||||
class MainWidget : public QGLWidget, protected QGLFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MainWidget(QWidget *parent = 0);
|
||||
virtual ~MainWidget();
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
~MainWidget();
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *e);
|
||||
@ -79,9 +75,9 @@ protected:
|
||||
void initTextures();
|
||||
|
||||
private:
|
||||
QBasicTimer *timer;
|
||||
QGLShaderProgram *program;
|
||||
GeometryEngine *geometries;
|
||||
QBasicTimer timer;
|
||||
QGLShaderProgram program;
|
||||
GeometryEngine geometries;
|
||||
|
||||
GLuint texture;
|
||||
|
||||
|
@ -38,13 +38,13 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtOpenGL>
|
||||
#include "glwidget.h"
|
||||
|
||||
#include <QMouseEvent>
|
||||
#include <QTimer>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "glwidget.h"
|
||||
|
||||
GLWidget::GLWidget(QWidget *parent)
|
||||
: QGLWidget(parent)
|
||||
{
|
||||
@ -191,57 +191,52 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
|
||||
GLdouble r2 = outerRadius + toothSize / 2.0;
|
||||
GLdouble delta = (2.0 * Pi / toothCount) / 4.0;
|
||||
GLdouble z = thickness / 2.0;
|
||||
int i, j;
|
||||
|
||||
glShadeModel(GL_FLAT);
|
||||
|
||||
for (i = 0; i < 2; ++i) {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
GLdouble sign = (i == 0) ? +1.0 : -1.0;
|
||||
|
||||
glNormal3d(0.0, 0.0, sign);
|
||||
|
||||
glBegin(GL_QUAD_STRIP);
|
||||
for (j = 0; j <= toothCount; ++j) {
|
||||
for (int j = 0; j <= toothCount; ++j) {
|
||||
GLdouble angle = 2.0 * Pi * j / toothCount;
|
||||
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z);
|
||||
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z);
|
||||
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z);
|
||||
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta),
|
||||
sign * z);
|
||||
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z);
|
||||
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z);
|
||||
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z);
|
||||
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), sign * z);
|
||||
}
|
||||
glEnd();
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
for (j = 0; j < toothCount; ++j) {
|
||||
for (int j = 0; j < toothCount; ++j) {
|
||||
GLdouble angle = 2.0 * Pi * j / toothCount;
|
||||
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z);
|
||||
glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta),
|
||||
sign * z);
|
||||
glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta),
|
||||
sign * z);
|
||||
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta),
|
||||
sign * z);
|
||||
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z);
|
||||
glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), sign * z);
|
||||
glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), sign * z);
|
||||
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), sign * z);
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
|
||||
glBegin(GL_QUAD_STRIP);
|
||||
for (i = 0; i < toothCount; ++i) {
|
||||
for (j = 0; j < 2; ++j) {
|
||||
GLdouble angle = 2.0 * Pi * (i + (j / 2.0)) / toothCount;
|
||||
for (int i = 0; i < toothCount; ++i) {
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
GLdouble angle = 2.0 * Pi * (i + j / 2.0) / toothCount;
|
||||
GLdouble s1 = r1;
|
||||
GLdouble s2 = r2;
|
||||
if (j == 1)
|
||||
qSwap(s1, s2);
|
||||
|
||||
glNormal3d(cos(angle), sin(angle), 0.0);
|
||||
glVertex3d(s1 * cos(angle), s1 * sin(angle), +z);
|
||||
glVertex3d(s1 * cos(angle), s1 * sin(angle), -z);
|
||||
glNormal3d(cos(angle), sin(angle), 0.0);
|
||||
glVertex3d(s1 * cos(angle), s1 * sin(angle), +z);
|
||||
glVertex3d(s1 * cos(angle), s1 * sin(angle), -z);
|
||||
|
||||
glNormal3d(s2 * sin(angle + delta) - s1 * sin(angle),
|
||||
glNormal3d(s2 * sin(angle + delta) - s1 * sin(angle),
|
||||
s1 * cos(angle) - s2 * cos(angle + delta), 0.0);
|
||||
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z);
|
||||
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z);
|
||||
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z);
|
||||
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z);
|
||||
}
|
||||
}
|
||||
glVertex3d(r1, 0.0, +z);
|
||||
@ -251,11 +246,11 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius,
|
||||
glShadeModel(GL_SMOOTH);
|
||||
|
||||
glBegin(GL_QUAD_STRIP);
|
||||
for (i = 0; i <= toothCount; ++i) {
|
||||
GLdouble angle = i * 2.0 * Pi / toothCount;
|
||||
glNormal3d(-cos(angle), -sin(angle), 0.0);
|
||||
glVertex3d(r0 * cos(angle), r0 * sin(angle), +z);
|
||||
glVertex3d(r0 * cos(angle), r0 * sin(angle), -z);
|
||||
for (int i = 0; i <= toothCount; ++i) {
|
||||
GLdouble angle = i * 2.0 * Pi / toothCount;
|
||||
glNormal3d(-cos(angle), -sin(angle), 0.0);
|
||||
glVertex3d(r0 * cos(angle), r0 * sin(angle), +z);
|
||||
glVertex3d(r0 * cos(angle), r0 * sin(angle), -z);
|
||||
}
|
||||
glEnd();
|
||||
|
||||
|
@ -94,4 +94,4 @@ private:
|
||||
QPoint lastPos;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // GLWIDGET_H
|
||||
|
@ -38,14 +38,14 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
MainWindow mainWin;
|
||||
mainWin.show();
|
||||
MainWindow window;
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
||||
|
@ -38,12 +38,11 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtOpenGL>
|
||||
|
||||
#include "glwidget.h"
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
centralWidget = new QWidget;
|
||||
|
@ -44,12 +44,12 @@
|
||||
#include <QMainWindow>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QAction;
|
||||
class QLabel;
|
||||
class QMenu;
|
||||
class QScrollArea;
|
||||
class QSlider;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class GLWidget;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
@ -91,4 +91,4 @@ private:
|
||||
QAction *aboutQtAct;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // MAINWINDOW_H
|
||||
|
@ -41,9 +41,6 @@
|
||||
#include "hellowindow.h"
|
||||
|
||||
#include <QOpenGLContext>
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
#include <qmath.h>
|
||||
|
||||
Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
|
||||
@ -77,10 +74,8 @@ HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
|
||||
updateColor();
|
||||
}
|
||||
|
||||
void HelloWindow::exposeEvent(QExposeEvent *event)
|
||||
void HelloWindow::exposeEvent(QExposeEvent *)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
||||
render();
|
||||
|
||||
if (!m_timer) {
|
||||
@ -109,10 +104,7 @@ void HelloWindow::updateColor()
|
||||
};
|
||||
|
||||
m_color = colors[m_colorIndex];
|
||||
|
||||
m_colorIndex++;
|
||||
if (m_colorIndex >= int(sizeof(colors) / sizeof(colors[0])))
|
||||
m_colorIndex = 0;
|
||||
m_colorIndex = 1 - m_colorIndex;
|
||||
}
|
||||
|
||||
void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize)
|
||||
@ -171,31 +163,29 @@ void Renderer::initialize()
|
||||
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
|
||||
|
||||
QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
|
||||
const char *vsrc =
|
||||
"attribute highp vec4 vertex;\n"
|
||||
"attribute mediump vec3 normal;\n"
|
||||
"uniform mediump mat4 matrix;\n"
|
||||
"uniform lowp vec4 sourceColor;\n"
|
||||
"varying mediump vec4 color;\n"
|
||||
"void main(void)\n"
|
||||
"{\n"
|
||||
" vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
|
||||
" float angle = max(dot(normal, toLight), 0.0);\n"
|
||||
" vec3 col = sourceColor.rgb;\n"
|
||||
" color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n"
|
||||
" color = clamp(color, 0.0, 1.0);\n"
|
||||
" gl_Position = matrix * vertex;\n"
|
||||
"}\n";
|
||||
vshader->compileSourceCode(vsrc);
|
||||
vshader->compileSourceCode(
|
||||
"attribute highp vec4 vertex;"
|
||||
"attribute mediump vec3 normal;"
|
||||
"uniform mediump mat4 matrix;"
|
||||
"uniform lowp vec4 sourceColor;"
|
||||
"varying mediump vec4 color;"
|
||||
"void main(void)"
|
||||
"{"
|
||||
" vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));"
|
||||
" float angle = max(dot(normal, toLight), 0.0);"
|
||||
" vec3 col = sourceColor.rgb;"
|
||||
" color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);"
|
||||
" color = clamp(color, 0.0, 1.0);"
|
||||
" gl_Position = matrix * vertex;"
|
||||
"}");
|
||||
|
||||
QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
|
||||
const char *fsrc =
|
||||
"varying mediump vec4 color;\n"
|
||||
"void main(void)\n"
|
||||
"{\n"
|
||||
" gl_FragColor = color;\n"
|
||||
"}\n";
|
||||
fshader->compileSourceCode(fsrc);
|
||||
fshader->compileSourceCode(
|
||||
"varying mediump vec4 color;"
|
||||
"void main(void)"
|
||||
"{"
|
||||
" gl_FragColor = color;"
|
||||
"}");
|
||||
|
||||
m_program = new QOpenGLShaderProgram(this);
|
||||
m_program->addShader(vshader);
|
||||
|
@ -40,21 +40,15 @@
|
||||
|
||||
#include <QWindow>
|
||||
|
||||
#include <QtGui/qopengl.h>
|
||||
#include <QtGui/qopenglshaderprogram.h>
|
||||
|
||||
#include <QColor>
|
||||
#include <QTime>
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QSharedPointer>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QOpenGLContext;
|
||||
class QTimer;
|
||||
QT_END_NAMESPACE
|
||||
#include <QTimer>
|
||||
|
||||
class Renderer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Renderer(const QSurfaceFormat &format, Renderer *share = 0, QScreen *screen = 0);
|
||||
|
||||
@ -89,6 +83,7 @@ private:
|
||||
class HelloWindow : public QWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit HelloWindow(const QSharedPointer<Renderer> &renderer);
|
||||
|
||||
|
@ -38,21 +38,22 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include "hellowindow.h"
|
||||
|
||||
#include <qpa/qplatformintegration.h>
|
||||
#include <private/qguiapplication_p.h>
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QScreen>
|
||||
#include <QThread>
|
||||
|
||||
#include "hellowindow.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
const bool multipleWindows =
|
||||
QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedOpenGL)
|
||||
&& !QGuiApplication::arguments().contains(QLatin1String("--single"));
|
||||
&& !QGuiApplication::arguments().contains(QStringLiteral("--single"));
|
||||
|
||||
QScreen *screen = QGuiApplication::primaryScreen();
|
||||
|
||||
@ -71,7 +72,7 @@ int main(int argc, char **argv)
|
||||
|
||||
HelloWindow *windowA = new HelloWindow(rendererA);
|
||||
windowA->setGeometry(QRect(center, windowSize).translated(-windowSize.width() - delta / 2, 0));
|
||||
windowA->setTitle(QLatin1String("Thread A - Context A"));
|
||||
windowA->setTitle(QStringLiteral("Thread A - Context A"));
|
||||
windowA->setVisible(true);
|
||||
windows.prepend(windowA);
|
||||
|
||||
@ -85,13 +86,13 @@ int main(int argc, char **argv)
|
||||
|
||||
HelloWindow *windowB = new HelloWindow(rendererA);
|
||||
windowB->setGeometry(QRect(center, windowSize).translated(delta / 2, 0));
|
||||
windowB->setTitle(QLatin1String("Thread A - Context A"));
|
||||
windowB->setTitle(QStringLiteral("Thread A - Context A"));
|
||||
windowB->setVisible(true);
|
||||
windows.prepend(windowB);
|
||||
|
||||
HelloWindow *windowC = new HelloWindow(rendererB);
|
||||
windowC->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, windowSize.height() + delta));
|
||||
windowC->setTitle(QLatin1String("Thread B - Context B"));
|
||||
windowC->setTitle(QStringLiteral("Thread B - Context B"));
|
||||
windowC->setVisible(true);
|
||||
windows.prepend(windowC);
|
||||
|
||||
@ -113,7 +114,7 @@ int main(int argc, char **argv)
|
||||
window->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, -windowSize.height() / 2));
|
||||
|
||||
QChar id = QChar('B' + i);
|
||||
window->setTitle(QLatin1String("Thread ") + id + QLatin1String(" - Context ") + id);
|
||||
window->setTitle(QStringLiteral("Thread ") + id + QStringLiteral(" - Context ") + id);
|
||||
window->setVisible(true);
|
||||
windows.prepend(window);
|
||||
}
|
||||
@ -128,6 +129,7 @@ int main(int argc, char **argv)
|
||||
|
||||
for (int i = 0; i < renderThreads.size(); ++i)
|
||||
renderThreads.at(i)->wait();
|
||||
|
||||
qDeleteAll(windows);
|
||||
qDeleteAll(renderThreads);
|
||||
|
||||
|
@ -38,8 +38,6 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "bubble.h"
|
||||
|
||||
Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity)
|
||||
|
@ -41,15 +41,7 @@
|
||||
#ifndef BUBBLE_H
|
||||
#define BUBBLE_H
|
||||
|
||||
#include <QBrush>
|
||||
#include <QColor>
|
||||
#include <QPointF>
|
||||
#include <QRect>
|
||||
#include <QRectF>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QPainter;
|
||||
QT_END_NAMESPACE
|
||||
#include <QPainter>
|
||||
|
||||
class Bubble
|
||||
{
|
||||
@ -72,4 +64,4 @@ private:
|
||||
QColor outerColor;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // BUBBLE_H
|
||||
|
@ -38,16 +38,16 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtOpenGL>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "bubble.h"
|
||||
#include "qtlogo.h"
|
||||
#include "glwidget.h"
|
||||
|
||||
#include <QMouseEvent>
|
||||
#include <QTime>
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef GL_MULTISAMPLE
|
||||
#define GL_MULTISAMPLE 0x809D
|
||||
#endif
|
||||
|
@ -41,19 +41,11 @@
|
||||
#ifndef GLWIDGET_H
|
||||
#define GLWIDGET_H
|
||||
|
||||
#include <QBrush>
|
||||
#include <QFont>
|
||||
#include <QImage>
|
||||
#include <QPen>
|
||||
#include <QGLWidget>
|
||||
#include <QTimer>
|
||||
|
||||
class Bubble;
|
||||
class QtLogo;
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QPaintEvent;
|
||||
class QWidget;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class GLWidget : public QGLWidget
|
||||
@ -108,4 +100,4 @@ private:
|
||||
//! [4]
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // GLWIDGET_H
|
||||
|
@ -38,9 +38,10 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QApplication>
|
||||
#include "glwidget.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
@ -1,10 +1,12 @@
|
||||
QT += opengl widgets
|
||||
|
||||
VPATH += ../shared
|
||||
INCLUDEPATH += ../shared
|
||||
|
||||
QT += opengl widgets
|
||||
HEADERS = bubble.h \
|
||||
glwidget.h \
|
||||
qtlogo.h
|
||||
|
||||
SOURCES = bubble.cpp \
|
||||
glwidget.cpp \
|
||||
main.cpp \
|
||||
|
@ -38,14 +38,14 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "qtlogo.h"
|
||||
|
||||
#include <QGLWidget>
|
||||
#include <QMatrix4x4>
|
||||
#include <QVector3D>
|
||||
|
||||
#include <qmath.h>
|
||||
|
||||
#include "qtlogo.h"
|
||||
|
||||
static const qreal tee_height = 0.311126;
|
||||
static const qreal cross_width = 0.25;
|
||||
static const qreal bar_thickness = 0.113137;
|
||||
@ -117,7 +117,7 @@ void Geometry::finalize()
|
||||
|
||||
void Geometry::appendSmooth(const QVector3D &a, const QVector3D &n, int from)
|
||||
{
|
||||
// Smooth normals are acheived by averaging the normals for faces meeting
|
||||
// Smooth normals are achieved by averaging the normals for faces meeting
|
||||
// at a point. First find the point in geometry already generated
|
||||
// (working backwards, since most often the points shared are between faces
|
||||
// recently added).
|
||||
@ -125,28 +125,27 @@ void Geometry::appendSmooth(const QVector3D &a, const QVector3D &n, int from)
|
||||
for ( ; v >= from; --v)
|
||||
if (qFuzzyCompare(vertices[v], a))
|
||||
break;
|
||||
if (v < from)
|
||||
{
|
||||
// The vert was not found so add it as a new one, and initialize
|
||||
|
||||
if (v < from) {
|
||||
// The vertex was not found so add it as a new one, and initialize
|
||||
// its corresponding normal
|
||||
v = vertices.count();
|
||||
vertices.append(a);
|
||||
normals.append(n);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Vert found, accumulate normals into corresponding normal slot.
|
||||
// Must call finalize once finished accumulating normals
|
||||
normals[v] += n;
|
||||
}
|
||||
// In both cases (found or not) reference the vert via its index
|
||||
|
||||
// In both cases (found or not) reference the vertex via its index
|
||||
faces.append(v);
|
||||
}
|
||||
|
||||
void Geometry::appendFaceted(const QVector3D &a, const QVector3D &n)
|
||||
{
|
||||
// Faceted normals are achieved by duplicating the vert for every
|
||||
// normal, so that faces meeting at a vert get a sharp edge.
|
||||
// Faceted normals are achieved by duplicating the vertex for every
|
||||
// normal, so that faces meeting at a vertex get a sharp edge.
|
||||
int v = vertices.count();
|
||||
vertices.append(a);
|
||||
normals.append(n);
|
||||
@ -189,32 +188,29 @@ void Patch::draw() const
|
||||
void Patch::addTri(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &n)
|
||||
{
|
||||
QVector3D norm = n.isNull() ? QVector3D::normal(a, b, c) : n;
|
||||
if (sm == Smooth)
|
||||
{
|
||||
|
||||
if (sm == Smooth) {
|
||||
geom->appendSmooth(a, norm, initv);
|
||||
geom->appendSmooth(b, norm, initv);
|
||||
geom->appendSmooth(c, norm, initv);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
geom->appendFaceted(a, norm);
|
||||
geom->appendFaceted(b, norm);
|
||||
geom->appendFaceted(c, norm);
|
||||
}
|
||||
|
||||
count += 3;
|
||||
}
|
||||
|
||||
void Patch::addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c, const QVector3D &d)
|
||||
{
|
||||
QVector3D norm = QVector3D::normal(a, b, c);
|
||||
if (sm == Smooth)
|
||||
{
|
||||
|
||||
if (sm == Smooth) {
|
||||
addTri(a, b, c, norm);
|
||||
addTri(a, c, d, norm);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If faceted share the two common verts
|
||||
} else {
|
||||
// If faceted share the two common vertices
|
||||
addTri(a, b, c, norm);
|
||||
int k = geom->vertices.count();
|
||||
geom->appendSmooth(a, norm, k);
|
||||
@ -224,9 +220,9 @@ void Patch::addQuad(const QVector3D &a, const QVector3D &b, const QVector3D &c,
|
||||
}
|
||||
}
|
||||
|
||||
static inline QVector<QVector3D> extrude(const QVector<QVector3D> &verts, qreal depth)
|
||||
static inline QVector<QVector3D> extrude(const QVector<QVector3D> &vertices, qreal depth)
|
||||
{
|
||||
QVector<QVector3D> extr = verts;
|
||||
QVector<QVector3D> extr = vertices;
|
||||
for (int v = 0; v < extr.count(); ++v)
|
||||
extr[v].setZ(extr[v].z() - depth);
|
||||
return extr;
|
||||
@ -240,6 +236,7 @@ public:
|
||||
for (int i = 0; i < parts.count(); ++i)
|
||||
parts[i]->translate(t);
|
||||
}
|
||||
|
||||
void rotate(qreal deg, QVector3D axis)
|
||||
{
|
||||
for (int i = 0; i < parts.count(); ++i)
|
||||
@ -248,7 +245,7 @@ public:
|
||||
|
||||
// No special Rectoid destructor - the parts are fetched out of this member
|
||||
// variable, and destroyed by the new owner
|
||||
QList<Patch*> parts;
|
||||
QList<Patch *> parts;
|
||||
};
|
||||
|
||||
class RectPrism : public Rectoid
|
||||
|
Loading…
x
Reference in New Issue
Block a user