#include <QApplication>
#include <QGLWidget>
#include "scene.h"
#include "view.h"
int main(int argc, char *argv[])
{
widget->setAutoFillBackground(false);
widget->makeCurrent();
Scene *scene= new Scene();
View *mainView = new View();
mainView->setViewport(widget);
mainView
->setViewportUpdateMode
(QGraphicsView::FullViewportUpdate);
mainView->setScene(scene);
mainView->show();
mainView->resize(800,800);
return a.exec();
}
#include <QApplication>
#include <QGLWidget>
#include "scene.h"
#include "view.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGLWidget *widget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
widget->setAutoFillBackground(false);
widget->makeCurrent();
Scene *scene= new Scene();
View *mainView = new View();
mainView->setViewport(widget);
mainView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
mainView->setScene(scene);
mainView->show();
mainView->resize(800,800);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#include "scene.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QTimer>
Scene::Scene() : angularSpeed(0)
{
initializeGLFunctions();
glClearColor(0.0f,0.0f,0.0f,1.0f);
initShaders();
initTextures();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
geometries.init();
timer.start(12, this);
}
{
painter->beginNativePainting();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 matrix;
matrix.translate(0.0, 0.0, -5.0);
matrix.rotate(rotation);
program.setUniformValue("mvp_matrix", projection * matrix);
program.setUniformValue("texture", 0);
geometries.drawCubeGeometry(&program);
}
void Scene::initShaders()
{
program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl");
program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl");
program.link();
program.bind();
}
void Scene::initTextures()
{
glEnable(GL_TEXTURE_2D);
textureData
= new QOpenGLTexture
(QImage(":/cube.png"));
textureData->bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
{
angularSpeed *= 0.99;
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
update();
}
}
{
mousePressPosition = QVector2D(event->scenePos());
}
{
QVector2D diff = QVector2D(e->scenePos()) - mousePressPosition;
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
qreal acc = diff.length() / 100.0;
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
angularSpeed += acc;
}
#include "scene.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QTimer>
Scene::Scene() : angularSpeed(0)
{
initializeGLFunctions();
glClearColor(0.0f,0.0f,0.0f,1.0f);
initShaders();
initTextures();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
geometries.init();
timer.start(12, this);
}
void Scene::drawBackground(QPainter *painter, const QRectF &rect)
{
painter->beginNativePainting();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 matrix;
matrix.translate(0.0, 0.0, -5.0);
matrix.rotate(rotation);
program.setUniformValue("mvp_matrix", projection * matrix);
program.setUniformValue("texture", 0);
geometries.drawCubeGeometry(&program);
}
void Scene::initShaders()
{
program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl");
program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl");
program.link();
program.bind();
}
void Scene::initTextures()
{
glEnable(GL_TEXTURE_2D);
textureData = new QOpenGLTexture(QImage(":/cube.png"));
textureData->bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
void Scene::timerEvent(QTimerEvent *)
{
angularSpeed *= 0.99;
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
update();
}
}
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
mousePressPosition = QVector2D(event->scenePos());
}
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
QVector2D diff = QVector2D(e->scenePos()) - mousePressPosition;
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
qreal acc = diff.length() / 100.0;
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
angularSpeed += acc;
}
To copy to clipboard, switch view to plain text mode
#include <QGraphicsView>
#include <QResizeEvent>
#include "geometryengine.h"
{
Q_OBJECT
protected:
if (scene())
scene()->setSceneRect(
}
public:
View(){}
};
#endif // VIEW_H
#include <QGraphicsView>
#include <QResizeEvent>
#include "geometryengine.h"
class View : public QGraphicsView
{
Q_OBJECT
protected:
void resizeEvent(QResizeEvent *event){
if (scene())
scene()->setSceneRect(
QRect(QPoint(0, 0), event->size()));
QGraphicsView::resizeEvent(event);
}
public:
View(){}
};
#endif // VIEW_H
To copy to clipboard, switch view to plain text mode
Any ideas where it went wrong?
Bookmarks