diff -r cef43609a374 -r 0133e565e072 src/gl/compiler.cpp --- a/src/gl/compiler.cpp Sat Dec 14 23:00:01 2019 +0200 +++ b/src/gl/compiler.cpp Wed Jan 01 17:45:56 2020 +0200 @@ -24,7 +24,222 @@ #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(); + } + std::vector vboData[gl::numVbos]; + const std::vector 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& data, const Point3D& point) +{ + data.push_back(point.x); + data.push_back(point.y); + data.push_back(point.z); +} + +void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector* vboData) +{ + const gl::VboClass vboClass = classifyPolygon(polygon); + auto vboBuffer = [&](VboSubclass subclass) -> std::vector& + { + return vboData[gl::vboIndex({vboClass, subclass})]; + }; + auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); + for (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& 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 (int i = 0; i < polygon.numPolygonVertices(); i += 1) + { + polygon.vertices[i].y = -polygon.vertices[i].y; + polygon.vertices[i].z = -polygon.vertices[i].z; +#if 0 + // Add these vertices to the bounding box (unless we're going to do it over + // from scratch afterwards anyway) + if (not this->needBoundingBoxRebuild) + { + this->boundingBox.consider(poly.vertices[i]); + } +#endif + 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* data, const gl::Polygon& polygon, VboSubclass subclass) +{ + std::vector& buffer = data[gl::vboIndex({classifyPolygon(polygon), subclass})]; + const QColor color = this->getColorForPolygon(polygon, subclass); + buffer.reserve(data->size() + 4 * polygon.numPolygonVertices()); + for (int i = 0; i < polygon.numPolygonVertices(); i += 1) + { + buffer.push_back(color.redF() / 255.0f); + buffer.push_back(color.greenF() / 255.0f); + buffer.push_back(color.blueF() / 255.0f); + buffer.push_back(color.alphaF() / 255.0f); + } +} + +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& data) +{ + glBindBuffer(GL_ARRAY_BUFFER, this->storedVbo[vboIndex]); + glBufferData(GL_ARRAY_BUFFER, 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)]; +} + +int gl::Compiler::vboSize(const VboAddress vboAddress) const +{ + return this->storedVboSizes[gl::vboIndex(vboAddress)]; +} + +int gl::vboIndex(const VboAddress vboAddress) +{ + return static_cast(vboAddress.vboClass) * gl::numVboSubclasses + + static_cast(vboAddress.vboSubclass); +}