Sun, 19 Jan 2020 14:25:57 +0200
added debug and release to hgignore
/* * LDForge: LDraw parts authoring CAD * Copyright (C) 2013 - 2018 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/>. */ #define GL_GLEXT_PROTOTYPES #include <GL/glu.h> #include <GL/glext.h> #include "gl/compiler.h" #include "documentmanager.h" #include "invert.h" #include "ring.h" gl::Compiler::Compiler(QObject* parent) : QObject{parent} { } gl::Compiler::~Compiler() { } void gl::Compiler::build(Model* model, DocumentManager* context) { if (not this->initialized) { this->initializeVbo(); } this->boundingBox = {}; std::vector<GLfloat> vboData[gl::numVbos]; const std::vector<gl::Polygon> polygons = model->getPolygons(context); for (const gl::Polygon& polygon : polygons) { this->buildPolygon(polygon, vboData); } for (int i = 0; i < gl::numVbos; i += 1) { this->upload(i, vboData[i]); } } gl::VboClass classifyPolygon(const gl::Polygon& polygon) { switch (polygon.type) { case gl::Polygon::EdgeLine: return gl::VboClass::Lines; case gl::Polygon::Triangle: return gl::VboClass::Triangles; case gl::Polygon::Quadrilateral: return gl::VboClass::Quads; case gl::Polygon::ConditionalEdge: return gl::VboClass::ConditionalLines; } return gl::VboClass::Lines; } QColor colorFromId(linetypes::Id id) { // Calculate a color based from this index. This method caters for // 16777216 objects. I don't think that will be exceeded anytime soon. const int r = (id.value / 0x10000) % 0x100; const int g = (id.value / 0x100) % 0x100; const int b = id.value % 0x100; return {r, g, b}; } void writeVertex(std::vector<GLfloat>& data, const Point3D& point) { data.push_back(static_cast<GLfloat>(point.x)); data.push_back(static_cast<GLfloat>(point.y)); data.push_back(static_cast<GLfloat>(point.z)); } void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData) { const gl::VboClass vboClass = classifyPolygon(polygon); auto vboBuffer = [&](VboSubclass subclass) -> std::vector<GLfloat>& { return vboData[gl::vboIndex({vboClass, subclass})]; }; auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) { const Point3D& v1 = vertexRing[i - 1]; const Point3D& v2 = vertexRing[i]; const Point3D& v3 = vertexRing[i + 1]; const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized(); std::vector<GLfloat>& data = vboBuffer(VboSubclass::Normals); for (const GLfloat coord : {normal.x(), normal.y(), normal.z()}) data.push_back(coord); } vboBuffer(VboSubclass::Surfaces).reserve(vboBuffer(VboSubclass::Surfaces).size() + polygon.numPolygonVertices()); // Transform vertices so that they're suitable for GL rendering for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) { polygon.vertices[i].y = -polygon.vertices[i].y; polygon.vertices[i].z = -polygon.vertices[i].z; this->boundingBox.consider(polygon.vertices[i]); writeVertex(vboBuffer(VboSubclass::Surfaces), polygon.vertices[i]); } this->writeColor(vboData, polygon, VboSubclass::RegularColors); this->writeColor(vboData, polygon, VboSubclass::PickColors); this->writeColor(vboData, polygon, VboSubclass::BfcFrontColors); this->writeColor(vboData, polygon, VboSubclass::BfcBackColors); } QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, VboSubclass subclass) { QColor color; switch (subclass) { case VboSubclass::Surfaces: case VboSubclass::Normals: case VboSubclass::InvertedNormals: // Surface and normal VBOs contain vertex data, not colors. So we can't return anything meaningful. return {}; case VboSubclass::BfcFrontColors: // Use the constant green color for BFC front colors return {64, 192, 80}; case VboSubclass::BfcBackColors: // Use the constant red color for BFC back colors return {208, 64, 64}; case VboSubclass::PickColors: // For the picking scene, use unique picking colors provided by the model. return colorFromId(polygon.id); case VboSubclass::RandomColors: // For the random color scene, the owner object has rolled up a random color. Use that. color = {255, 64, 255}; // polygonOwner->randomColor(); break; case VboSubclass::RegularColors: // For normal colors, use the polygon's color. if (polygon.color == colors::main) { color = {255, 255, 64}; // mainColorRepresentation(); } else if (polygon.color == colors::edge) { // Edge color is black, unless we have a dark background, in which case lines need to be bright. color = Qt::black; //luma(config::backgroundColor()) > 40 ? Qt::black : Qt::white; } else { // Not main or edge color, use the polygon's color as is. color = {255, 255, 64}; //polygon.color.faceColor(); } break; } if (color.isValid()) { // We may wish to apply blending on the color to indicate selection or highlight. /* const double blendAlpha = 0.0; if (blendAlpha != 0.0) { QColor selectedColor = config::selectColorBlend(); double denominator = blendAlpha + 1.0; color.setRed((color.red() + (selectedColor.red() * blendAlpha)) / denominator); color.setGreen((color.green() + (selectedColor.green() * blendAlpha)) / denominator); color.setBlue((color.blue() + (selectedColor.blue() * blendAlpha)) / denominator); } */ } else { if (polygon.numPolygonVertices() == 2) color = Qt::black; else color = {255, 255, 64}; } return color; } void gl::Compiler::writeColor(std::vector<GLfloat>* data, const gl::Polygon& polygon, VboSubclass subclass) { std::vector<GLfloat>& buffer = data[gl::vboIndex({classifyPolygon(polygon), subclass})]; const QColor color = this->getColorForPolygon(polygon, subclass); buffer.reserve(data->size() + 4 * polygon.numPolygonVertices()); for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) { buffer.push_back(static_cast<GLfloat>(color.redF())); buffer.push_back(static_cast<GLfloat>(color.greenF())); buffer.push_back(static_cast<GLfloat>(color.blueF())); buffer.push_back(static_cast<GLfloat>(color.alphaF())); } } Point3D gl::Compiler::modelCenter() const { return boxCenter(this->boundingBox); } double gl::Compiler::modelDistance() const { return longestMeasure(this->boundingBox); } void gl::Compiler::initializeVbo() { this->initializeOpenGLFunctions(); glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]); this->initialized = true; } /// /// \brief Uploads data to a vbo /// \param vboAddress Which vbo to upload to /// \param data Data to upload to vbo /// void gl::Compiler::upload(const int vboIndex, const std::vector<GLfloat>& data) { glBindBuffer(GL_ARRAY_BUFFER, this->storedVbo[vboIndex]); glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof data[0]), data.data(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); this->storedVboSizes[vboIndex] = data.size(); } GLuint gl::Compiler::vbo(const VboAddress vboAddress) const { return this->storedVbo[gl::vboIndex(vboAddress)]; } std::size_t gl::Compiler::vboSize(const VboAddress vboAddress) const { return this->storedVboSizes[gl::vboIndex(vboAddress)]; } int gl::vboIndex(const VboAddress vboAddress) { return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses + static_cast<std::uint8_t>(vboAddress.vboSubclass); }