Wed, 22 Jan 2020 00:23:29 +0200
at least VAOs work now
/* * 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 <QMessageBox> #include "gl/compiler.h" #include "documentmanager.h" #include "invert.h" #include "ring.h" gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) : QObject{parent}, colorTable{colorTable} { } gl::Compiler::~Compiler() { if (this->initialized) { for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1) { glDeleteProgram(this->glObjects[arrayId].program); glDeleteShader(this->glObjects[arrayId].vertexShader); glDeleteProgram(this->glObjects[arrayId].fragmentShader); } } } void gl::Compiler::initialize() { if (not this->initialized) { this->initializeOpenGLFunctions(); for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1) { QOpenGLVertexArrayObject& vao = this->vertexArrays[arrayId]; vao.create(); vao.bind(); glGenBuffers(gl::numVboSubclasses, &this->storedVbo[gl::numVboSubclasses * arrayId]); this->buildShaders(arrayId); vao.release(); } //glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]); this->initialized = true; } } void gl::Compiler::build(Model* model, DocumentManager* context) { 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 index = 0; index < gl::numVbos; index += 1) { this->upload(index, vboData[index]); }*/ for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1) { this->vertexArrays[arrayId].bind(); for (int i = 0; i < gl::numVboSubclasses; i += 1) { const int vboIndex = gl::vboIndex({static_cast<gl::ArrayClass>(arrayId), static_cast<gl::VboSubclass>(i)}); this->upload(vboIndex, vboData[vboIndex]); } this->vertexArrays[arrayId].release(); } } gl::ArrayClass classifyPolygon(const gl::Polygon& polygon) { switch (polygon.type) { case gl::Polygon::EdgeLine: return gl::ArrayClass::Lines; case gl::Polygon::Triangle: return gl::ArrayClass::Triangles; case gl::Polygon::Quadrilateral: return gl::ArrayClass::Quads; case gl::Polygon::ConditionalEdge: return gl::ArrayClass::ConditionalLines; } return gl::ArrayClass::Lines; } [[maybe_unused]] static 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 gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData) { const gl::ArrayClass vboClass = classifyPolygon(polygon); std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})]; std::vector<GLfloat>& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})]; auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z); 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(); for (const GLfloat coord : {normal.x(), normal.y(), normal.z()}) normalsBuffer.push_back(coord); } reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7); const QColor color = this->getColorForPolygon(polygon); // Transform vertices so that they're suitable for GL rendering 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<GLfloat>(point.x)); vertexBuffer.push_back(static_cast<GLfloat>(point.y)); vertexBuffer.push_back(static_cast<GLfloat>(point.z)); vertexBuffer.push_back(static_cast<GLfloat>(color.redF())); vertexBuffer.push_back(static_cast<GLfloat>(color.greenF())); vertexBuffer.push_back(static_cast<GLfloat>(color.blueF())); vertexBuffer.push_back(static_cast<GLfloat>(color.alphaF())); } } QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon) { QColor color; // 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 = this->colorTable[polygon.color].faceColor; } return color; } Point3D gl::Compiler::modelCenter() const { return boxCenter(this->boundingBox); } double gl::Compiler::modelDistance() const { return longestMeasure(this->boundingBox); } void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass) { this->vertexArrays[static_cast<int>(arrayClass)].bind(); glUseProgram(this->glObjects[static_cast<int>(arrayClass)].program); } void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass) { this->vertexArrays[static_cast<int>(arrayClass)].release(); } void gl::Compiler::buildShaders(int arrayId) { /* this->glObjects[arrayId].vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(this->glObjects[arrayId].vertexShader, 1, &vertexShaderSource, nullptr); glCompileShader(this->glObjects[arrayId].vertexShader); this->glObjects[arrayId].fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(this->glObjects[arrayId].fragmentShader, 1, &fragmentShaderSource, nullptr); glCompileShader(this->glObjects[arrayId].fragmentShader); for (auto&& pair : { std::make_pair(this->glObjects[arrayId].vertexShader, tr("vertex shader")), std::make_pair(this->glObjects[arrayId].fragmentShader, tr("fragment shader")), }) { GLint status; glGetShaderiv(this->glObjects[arrayId].fragmentShader, GL_COMPILE_STATUS, &status); if (status != GL_TRUE) { char compileLog[512]; glGetShaderInfoLog(pair.first, countof(compileLog), nullptr, compileLog); QMessageBox::critical(nullptr, tr("Shader compile error"), tr("Unable to compile the %1. Compile log:\n\n%2").arg(pair.second).arg(compileLog)); abort(); } } this->glObjects[arrayId].program = glCreateProgram(); glAttachShader(this->glObjects[arrayId].program, this->glObjects[arrayId].vertexShader); glAttachShader(this->glObjects[arrayId].program, this->glObjects[arrayId].fragmentShader); glLinkProgram(this->glObjects[arrayId].program); glUseProgram(this->glObjects[arrayId].program); const std::size_t size = gl::FLOATS_PER_VERTEX * sizeof(GLfloat); const GLuint posAttrib = static_cast<GLuint>(glGetAttribLocation(this->glObjects[arrayId].program, "position")); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, size, gl::offset(0)); const GLuint colAttrib = static_cast<GLuint>(glGetAttribLocation(this->glObjects[arrayId].program, "color")); glEnableVertexAttribArray(colAttrib); glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, size, gl::offset(3 * sizeof(GLfloat))); */ } /// /// \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); 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); }