# HG changeset patch # User Teemu Piippo # Date 1579732150 -7200 # Node ID 1536f23cfab77f39df6ca9580adf03f8a5eb61a1 # Parent 4cc6b582fde87bcd2de7f572f5b3080987529b84 it works! diff -r 4cc6b582fde8 -r 1536f23cfab7 locale/fi.ts --- a/locale/fi.ts Wed Jan 22 22:43:28 2020 +0200 +++ b/locale/fi.ts Thu Jan 23 00:29:10 2020 +0200 @@ -189,7 +189,7 @@ PartRenderer - + Rendering error @@ -233,4 +233,27 @@ + + gl::Compiler + + + Vertex shader: + + + + + Fragment shader: + + + + + Shader compile error + + + + + Could not compile shaders. + + + diff -r 4cc6b582fde8 -r 1536f23cfab7 locale/sv.ts --- a/locale/sv.ts Wed Jan 22 22:43:28 2020 +0200 +++ b/locale/sv.ts Thu Jan 23 00:29:10 2020 +0200 @@ -251,4 +251,23 @@ + + gl::Compiler + + Vertex shader: + + + + Fragment shader: + + + + Shader compile error + + + + Could not compile shaders. + + + diff -r 4cc6b582fde8 -r 1536f23cfab7 src/gl/compiler.cpp --- a/src/gl/compiler.cpp Wed Jan 22 22:43:28 2020 +0200 +++ b/src/gl/compiler.cpp Thu Jan 23 00:29:10 2020 +0200 @@ -97,8 +97,9 @@ object.vertexArray.bind(); object.program->enableAttributeArray(0); object.program->enableAttributeArray(1); - object.program->setAttributeBuffer(0, GL_FLOAT, 0, 3); - object.program->setAttributeBuffer(1, GL_FLOAT, 3, 4); + constexpr int stride = 7 * sizeof(GLfloat); + object.program->setAttributeBuffer(0, GL_FLOAT, 0 * sizeof(GLfloat), 3, stride); + object.program->setAttributeBuffer(1, GL_FLOAT, 3 * sizeof(GLfloat), 4, stride); object.vertexArray.release(); object.buffer.release(); object.program->release(); @@ -179,8 +180,6 @@ for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) { Point3D& point = polygon.vertices[i]; - polygon.vertices[i].y = -polygon.vertices[i].y; - polygon.vertices[i].z = -polygon.vertices[i].z; this->boundingBox.consider(polygon.vertices[i]); vertexBuffer.push_back(static_cast(point.x)); vertexBuffer.push_back(static_cast(point.y)); diff -r 4cc6b582fde8 -r 1536f23cfab7 src/gl/compiler.h --- a/src/gl/compiler.h Wed Jan 22 22:43:28 2020 +0200 +++ b/src/gl/compiler.h Thu Jan 23 00:29:10 2020 +0200 @@ -21,6 +21,7 @@ #include "gl/common.h" #include "types/boundingbox.h" #include +#include #include #include #include @@ -59,6 +60,7 @@ void bindVertexArray(gl::ArrayClass arrayClass); void releaseVertexArray(gl::ArrayClass arrayClass); void buildShaders(int arrayId); + template void setUniform(const char* uniformName, T&& value) { @@ -72,6 +74,13 @@ object.program->release(); } } + + template + void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value) + { + const float (*array)[4][4] = reinterpret_cast(glm::value_ptr(value)); + this->setUniform(uniformName, *array); + } private: bool m_vboChanged[gl::numVbos] = {true}; std::size_t storedVboSizes[gl::numVbos] = {0_z}; diff -r 4cc6b582fde8 -r 1536f23cfab7 src/gl/partrenderer.cpp --- a/src/gl/partrenderer.cpp Wed Jan 22 22:43:28 2020 +0200 +++ b/src/gl/partrenderer.cpp Thu Jan 23 00:29:10 2020 +0200 @@ -44,8 +44,6 @@ { abort(); } - glEnableClientState(GL_NORMAL_ARRAY); - glEnableClientState(GL_VERTEX_ARRAY); this->compiler->initialize(); this->compiler->build(this->model, this->documents); this->initializeLighting(); @@ -53,22 +51,9 @@ this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); glLineWidth(2.0); + this->update(); } -/* - * 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}; @@ -128,16 +113,8 @@ { 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) @@ -150,7 +127,7 @@ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); break; } - this->compiler->setUniform("CameraTransformation", gl::toQMatrix(this->projectionMatrix * this->viewMatrix)); + this->compiler->setUniformMatrix("CameraTransformation", 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); @@ -191,12 +168,13 @@ const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition; if (left and not move.isNull()) { + // FIXME: find a more elegant way to do this const QQuaternion versor = QQuaternion::fromAxisAndAngle( - QVector3D{static_cast(move.y()), static_cast(move.x()), 0.0f}, + QVector3D{static_cast(-move.y()), static_cast(move.x()), 0.0f}, 0.6f * static_cast(std::hypot(move.x(), move.y())) ); this->rotation = versor * this->rotation; - QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, 4.5}); + QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, static_cast(2 + 2 * this->compiler->modelDistance())}); glm::vec3 cameraPosition_glm = {cameraPosition.x(), cameraPosition.y(), cameraPosition.z()}; this->viewMatrix = glm::lookAt(cameraPosition_glm, {0, 0, 0}, {0, -1, 0}); this->update(); @@ -204,11 +182,6 @@ this->lastMousePosition = pointToPointF(event->pos()); } -void PartRenderer::setCompiler(gl::Compiler* compiler) -{ - this->compiler = compiler; -} - void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle) { this->renderStyle = newStyle; diff -r 4cc6b582fde8 -r 1536f23cfab7 src/gl/partrenderer.h --- a/src/gl/partrenderer.h Wed Jan 22 22:43:28 2020 +0200 +++ b/src/gl/partrenderer.h Thu Jan 23 00:29:10 2020 +0200 @@ -22,7 +22,6 @@ void resizeGL(int width, int height) override; void paintGL() override; void mouseMoveEvent(QMouseEvent* event) override; - void setCompiler(gl::Compiler* compiler); private slots: void setRenderStyle(const gl::RenderStyle newStyle); private: