Sat, 14 Dec 2019 23:00:01 +0200
fixed build
#include <QMouseEvent> #include <GL/glut.h> // teapot #include "partrenderer.h" PartRenderer::PartRenderer(QWidget* parent) : QOpenGLWidget{parent} { this->setMouseTracking(true); } void PartRenderer::initializeGL() { this->initializeOpenGLFunctions(); if (this->glGetError() != GL_NO_ERROR) { abort(); } this->initializeLighting(); this->initialized = true; this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); } /* * Pads a 3×3 matrix into a 4×4 one by adding cells from the identity matrix. */ static QMatrix4x4 padMatrix(const QMatrix3x3& stub) { return { stub(0, 0), stub(0, 1), stub(0, 2), 0, stub(1, 0), stub(1, 1), stub(1, 2), 0, stub(2, 0), stub(2, 1), stub(2, 2), 0, 0, 0, 0, 1 }; } void PartRenderer::initializeLighting() { GLfloat materialShininess[] = {5.0}; GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; glShadeModel(GL_SMOOTH); glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glEnable(GL_DEPTH_TEST); } void PartRenderer::resizeGL(int width, int height) { constexpr GLfloat near = 1.0f; constexpr GLfloat far = 1e+05f; glViewport (0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, static_cast<double>(width) / static_cast<double>(height), near, far); glMatrixMode(GL_MODELVIEW); } // https://www.codemiles.com/c-opengl-examples/drawing-teapot-using-opengl-t9010.html?mobile=on void PartRenderer::paintGL() { switch (this->renderStyle) { case gl::RenderStyle::Normal: case gl::RenderStyle::BfcRedGreen: case gl::RenderStyle::RandomColors: break; case gl::RenderStyle::Wireframe: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); break; } glMatrixMode(GL_MODELVIEW); // clear the drawing buffer. glClear(GL_COLOR_BUFFER_BIT); // clear the identity matrix. glLoadIdentity(); // traslate the draw by z = -4.0 // Note this when you decrease z like -8.0 the drawing will looks far , or smaller. glTranslatef(0.0,0.0,-4.5); // Red color used to draw. glColor3f(0.8, 0.2, 0.1); glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); glutSolidTeapot(1.0); glFlush(); } static QPointF pointToPointF(const QPoint& point) { return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())}; } /* static QPoint pointFToPoint(const QPointF& point) { return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))}; } */ void PartRenderer::mouseMoveEvent(QMouseEvent* event) { const bool left = event->buttons() & Qt::LeftButton; const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; if (left and not move.isNull()) { const QQuaternion versor = QQuaternion::fromAxisAndAngle( QVector3D{static_cast<float>(move.y()), static_cast<float>(move.x()), 0.0f}, 0.6 * std::hypot(move.x(), move.y()) ); this->rotation = versor * this->rotation; this->update(); } this->lastMousePosition = pointToPointF(event->pos()); } void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) { this->renderStyle = newStyle; this->update(); }