--- a/src/glCompiler.cpp Thu Jan 11 11:41:40 2018 +0200 +++ b/src/glCompiler.cpp Thu Jan 11 15:09:44 2018 +0200 @@ -25,10 +25,12 @@ #include "ldDocument.h" #include "miscallenous.h" #include "glRenderer.h" -#include "dialogs.h" #include "guiutilities.h" #include "documentmanager.h" +static const QColor bfcFrontColor {64, 192, 80}; +static const QColor bfcBackColor {208, 64, 64}; + struct GLErrorInfo { GLenum value; @@ -49,8 +51,6 @@ ConfigOption(QString SelectColorBlend = "#0080FF") -// static QMap<LDObject*, String> g_objectOrigins; - void CheckGLErrorImpl(const char* file, int line) { QString errmsg; @@ -72,160 +72,160 @@ } -GLCompiler::GLCompiler(GLRenderer* renderer) : - HierarchyElement(renderer), - m_renderer(renderer) -{ - needMerge(); - memset(m_vboSizes, 0, sizeof m_vboSizes); -} +GLCompiler::GLCompiler(LDDocument* document, GLRenderer* renderer) : + document {document}, + renderer {renderer} {} void GLCompiler::initialize() { initializeOpenGLFunctions(); - glGenBuffers(NumVbos, &m_vbo[0]); + glGenBuffers(NumVbos, &vboIndices[0]); CHECK_GL_ERROR(); } GLCompiler::~GLCompiler() { - glDeleteBuffers(NumVbos, &m_vbo[0]); + glDeleteBuffers(NumVbos, &vboIndices[0]); CHECK_GL_ERROR(); } +/* + * Returns a color that represents this object index. There are 256³ possible + * colors, so that many indices can be bijectively addressed with colorss. + */ +QColor GLCompiler::indexColorForID(int id) const +{ + int red = (id / 0x10000) % 0x100; + int green = (id / 0x100) % 0x100; + int blue = id % 0x100; + + return {red, green, blue}; +} + -QColor GLCompiler::indexColorForID(int id) const +QColor blend(QColor baseColor, QColor blendColor, double intensity) { - // Calculate a color based from this index. This method caters for - // 16777216 objects. I don't think that will be exceeded anytime soon. :) - int r = (id / 0x10000) % 0x100, - g = (id / 0x100) % 0x100, - b = id % 0x100; - - return QColor(r, g, b); + double red = baseColor.redF(); + red += blendColor.redF() * intensity; + red /= (intensity + 1.0); + double green = baseColor.greenF(); + green += blendColor.greenF() * intensity; + green /= (intensity + 1.0); + double blue = baseColor.blueF(); + blue += blendColor.blueF() * intensity; + blue /= (intensity + 1.0); + return {int(round(red)), int(round(green)), int(round(blue))}; } QColor GLCompiler::getColorForPolygon(LDPolygon& poly, LDObject* topobj, ComplementVboType complement) const { - QColor qcol; - static const QColor bfcFrontColor(64, 192, 80); - static const QColor bfcBackColor(208, 64, 64); + QColor color; switch(complement) { case SurfacesVboComplement: case NumVboComplements: - return QColor(); + return {}; case BfcFrontColorsVboComplement: - qcol = bfcFrontColor; + color = bfcFrontColor; break; case BfcBackColorsVboComplement: - qcol = bfcBackColor; + color = bfcBackColor; break; case PickColorsVboComplement: return indexColorForID(topobj->id()); case RandomColorsVboComplement: - qcol = topobj->randomColor(); + color = topobj->randomColor(); break; case NormalColorsVboComplement: if (poly.color == MainColor) { if (topobj->color() == MainColor) - qcol = guiUtilities()->mainColorRepresentation(); + color = mainColorRepresentation(); else - qcol = topobj->color().faceColor(); + color = topobj->color().faceColor(); } else if (poly.color == EdgeColor) { - qcol = luma(QColor(config->backgroundColor())) > 40 ? Qt::black : Qt::white; + if (luma(config->backgroundColor()) > 40) + color = Qt::black; + else + color = Qt::white; } else { - LDColor col = poly.color; + LDColor colorInfo = poly.color; - if (col.isValid()) - qcol = col.faceColor(); + if (colorInfo.isValid()) + color = colorInfo.faceColor(); } break; } - if (not qcol.isValid()) + if (not color.isValid()) { // The color was unknown. Use main color to make the polygon at least // not appear pitch-black. if (poly.num != 2 and poly.num != 5) - qcol = guiUtilities()->mainColorRepresentation(); + color = mainColorRepresentation(); else - qcol = Qt::black; + color = Qt::black; // Warn about the unknown color, but only once. - static QList<int> warnedColors; + static QSet<int> warnedColors; if (not warnedColors.contains(poly.color)) { print("Unknown color %1!\n", poly.color); - warnedColors << poly.color; + warnedColors.insert(poly.color); } - return qcol; + return color; } double blendAlpha = 0.0; if (topobj->isSelected()) blendAlpha = 1.0; - else if (topobj == m_renderer->objectAtCursor()) + else if (topobj == renderer->objectAtCursor()) blendAlpha = 0.5; - if (blendAlpha != 0.0) - { - QColor selcolor(config->selectColorBlend()); - double denom = blendAlpha + 1.0; - qcol.setRed((qcol.red() +(selcolor.red() * blendAlpha)) / denom); - qcol.setGreen((qcol.green() +(selcolor.green() * blendAlpha)) / denom); - qcol.setBlue((qcol.blue() +(selcolor.blue() * blendAlpha)) / denom); - } - - return qcol; + color = blend(color, config->selectColorBlend(), blendAlpha); + return color; } void GLCompiler::needMerge() { - for (int i = 0; i < countof(m_vboChanged); ++i) - m_vboChanged[i] = true; + for (bool& changed_flag : this->vboChanged) + changed_flag = true; } void GLCompiler::stageForCompilation(LDObject* obj) { - /* - g_objectOrigins[obj] = format("%1:%2(%3)", - obj->document()->getDisplayName(), obj->lineNumber(), obj->typeName()); - */ - - m_staged << obj; + stagedObjects << obj; } void GLCompiler::unstage(LDObject* obj) { - m_staged.remove(obj); + stagedObjects.remove(obj); } -void GLCompiler::compileDocument(LDDocument* doc) +void GLCompiler::compileDocument(LDDocument* document) { - if (doc) + if (document) { - for (LDObject* obj : doc->objects()) + for (LDObject* obj : document->objects()) compileObject(obj); } } @@ -233,10 +233,10 @@ void GLCompiler::compileStaged() { - for (QSetIterator<LDObject*> it(m_staged); it.hasNext();) + for (QSetIterator<LDObject*> it(stagedObjects); it.hasNext();) compileObject(it.next()); - m_staged.clear(); + stagedObjects.clear(); } @@ -245,39 +245,45 @@ // Compile anything that still awaits it compileStaged(); - if (not m_vboChanged[vbonum]) - return; - - QVector<GLfloat> vbodata; + if (not vboChanged[vbonum]) + { + print("Merging %1", vbonum); + QVector<GLfloat> vbodata; - for (auto it = m_objectInfo.begin(); it != m_objectInfo.end();) - { - if (it.key() == nullptr) + for (auto it = objectInfo.begin(); it != objectInfo.end();) { - it = m_objectInfo.erase(it); - continue; + if (it.key() == nullptr) + { + it = objectInfo.erase(it); + } + else + { + if (not it.key()->isHidden()) + vbodata += it->data[vbonum]; + + ++it; + } } - if (it.key()->document() == currentDocument() and not it.key()->isHidden()) - vbodata += it->data[vbonum]; - - ++it; + glBindBuffer(GL_ARRAY_BUFFER, vboIndices[vbonum]); + glBufferData( + GL_ARRAY_BUFFER, + vbodata.size() * sizeof(GLfloat), + vbodata.constData(), + GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + CHECK_GL_ERROR(); + vboChanged[vbonum] = false; + vboSizes[vbonum] = vbodata.size(); } - - glBindBuffer(GL_ARRAY_BUFFER, m_vbo[vbonum]); - glBufferData(GL_ARRAY_BUFFER, vbodata.size() * sizeof(GLfloat), vbodata.constData(), GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - CHECK_GL_ERROR(); - m_vboChanged[vbonum] = false; - m_vboSizes[vbonum] = vbodata.size(); } void GLCompiler::dropObjectInfo(LDObject* obj) { - if (m_objectInfo.contains(obj)) + if (objectInfo.contains(obj)) { - m_objectInfo.remove(obj); + objectInfo.remove(obj); needMerge(); } } @@ -336,7 +342,7 @@ break; } - m_objectInfo[obj] = info; + objectInfo[obj] = info; needMerge(); } @@ -381,13 +387,6 @@ } } - -void GLCompiler::setRenderer(GLRenderer* renderer) -{ - m_renderer = renderer; -} - - int GLCompiler::vboNumber(SurfaceVboType surface, ComplementVboType complement) { return (surface * NumVboComplements) + complement; @@ -396,11 +395,11 @@ GLuint GLCompiler::vbo(int vbonum) const { - return m_vbo[vbonum]; + return vboIndices[vbonum]; } int GLCompiler::vboSize(int vbonum) const { - return m_vboSizes[vbonum]; -} \ No newline at end of file + return vboSizes[vbonum]; +}