Wed, 22 Jan 2020 22:41:17 +0200
modelview matrix set up
/* * LDForge: LDraw parts authoring CAD * Copyright (C) 2013 - 2020 Teemu Piippo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <GL/glut.h> #include <glm/ext/matrix_transform.hpp> #include <glm/ext/matrix_clip_space.hpp> #include <QMouseEvent> #include <QMessageBox> #include "partrenderer.h" PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) : QOpenGLWidget{parent}, model{model}, documents{documents}, colorTable{colorTable}, compiler{new gl::Compiler{this->colorTable, this}} { this->setMouseTracking(true); } PartRenderer::~PartRenderer() { } void PartRenderer::initializeGL() { this->initializeOpenGLFunctions(); if (glGetError() != GL_NO_ERROR) { abort(); } glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); this->compiler->initialize(); this->compiler->build(this->model, this->documents); this->initializeLighting(); this->initialized = true; this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); glLineWidth(2.0); } /* * 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) { glViewport(0, 0, width, height); this->projectionMatrix = glm::perspective( glm::radians(90.0f), static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.f); } static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) { switch (vboClass) { case gl::ArrayClass::Lines: case gl::ArrayClass::ConditionalLines: return GL_LINES; case gl::ArrayClass::Triangles: return GL_TRIANGLES; case gl::ArrayClass::Quads: return GL_QUADS; } throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"}; } void PartRenderer::paintGL() { /* glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); */ glEnable (GL_DEPTH_TEST); glShadeModel (GL_SMOOTH); glEnable (GL_MULTISAMPLE); glEnable (GL_LINE_SMOOTH); glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); this->renderScene(); } void PartRenderer::renderScene() { glClearColor(0.8f, 0.8f, 0.8f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glLoadIdentity(); //glTranslated(0.0, 0.0, -4.5 * this->compiler->modelDistance()); //glTranslated(0.0, 0.0, -4.5); //glMultMatrixf(padMatrix(this->rotation.toRotationMatrix()).constData()); //xyz(glTranslatef, -this->compiler->modelCenter()); auto rotationMatrix = padMatrix(this->rotation.toRotationMatrix()); rotationMatrix(2, 3) = 0; glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1.0f, 1.0f); 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; } this->compiler->setUniform("CameraTransformation", gl::toQMatrix(this->projectionMatrix * this->viewMatrix)); // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. renderVao(gl::ArrayClass::Triangles); renderVao(gl::ArrayClass::Quads); renderVao(gl::ArrayClass::Lines); glDisable(GL_POLYGON_OFFSET_FILL); } void PartRenderer::renderVao(const gl::ArrayClass arrayClass) { this->compiler->bindVertexArray(arrayClass); const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX; glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); this->compiler->releaseVertexArray(arrayClass); this->checkForGLErrors(); } void PartRenderer::checkForGLErrors() { GLenum glError; QStringList errors; while ((glError = glGetError()) != GL_NO_ERROR) { const QString glErrorString = QString::fromLatin1(reinterpret_cast<const char*>(::gluErrorString(glError))); errors.append(glErrorString); } if (not errors.isEmpty()) { QMessageBox::critical( this, tr("Rendering error"), QString{"Failed to render.\n%1"}.arg(errors.join("\n"))); } } 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.6f * static_cast<float>(std::hypot(move.x(), move.y())) ); this->rotation = versor * this->rotation; QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, 4.5}); glm::vec3 cameraPosition_glm = {cameraPosition.x(), cameraPosition.y(), cameraPosition.z()}; this->viewMatrix = glm::lookAt(cameraPosition_glm, {0, 0, 0}, {0, -1, 0}); this->update(); } this->lastMousePosition = pointToPointF(event->pos()); } void PartRenderer::setCompiler(gl::Compiler* compiler) { this->compiler = compiler; } void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) { this->renderStyle = newStyle; this->update(); }