diff -r 3a9e761e4faa -r c57fb7a5ffa3 src/gl/compiler.cpp --- a/src/gl/compiler.cpp Wed Jan 22 00:23:29 2020 +0200 +++ b/src/gl/compiler.cpp Wed Jan 22 01:17:11 2020 +0200 @@ -25,6 +25,33 @@ #include "invert.h" #include "ring.h" +static const char* vertexShaderSource = R"( +#version 330 core + +layout(location=0) in vec3 position; +layout(location=1) in vec4 color; +out vec4 vColor; +uniform mat4 CameraTransformation; + +void main() +{ + vColor = color; + gl_Position = projection * modelview * CameraTransformation * vec4(position, 1.0); +} +)"; + +static const char* fragmentShaderSource = R"( +#version 330 core + +in vec4 vColor; +out vec4 fColor; + +void main() +{ + fColor = vColor; +} +)"; + gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) : QObject{parent}, colorTable{colorTable} @@ -33,15 +60,6 @@ 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() @@ -49,16 +67,28 @@ if (not this->initialized) { this->initializeOpenGLFunctions(); - for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1) + for (int i = 0; i < gl::NUM_ARRAY_CLASSES; i += 1) { - QOpenGLVertexArrayObject& vao = this->vertexArrays[arrayId]; - vao.create(); - vao.bind(); - glGenBuffers(gl::numVboSubclasses, &this->storedVbo[gl::numVboSubclasses * arrayId]); - this->buildShaders(arrayId); - vao.release(); + auto& object = this->glObjects[i]; + object.program = new QOpenGLShaderProgram; + object.program->create(); + object.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource); + object.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource); + object.program->link(); + object.program->bind(); + object.buffer.create(); + object.buffer.bind(); + object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); + object.vertexArray.create(); + 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); + object.vertexArray.release(); + object.buffer.release(); + object.program->release(); } - //glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]); this->initialized = true; } } @@ -66,26 +96,20 @@ void gl::Compiler::build(Model* model, DocumentManager* context) { this->boundingBox = {}; - std::vector vboData[gl::numVbos]; + std::vector vboData[gl::NUM_ARRAY_CLASSES]; const std::vector 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(arrayId), static_cast(i)}); - this->upload(vboIndex, vboData[vboIndex]); - } - this->vertexArrays[arrayId].release(); + auto& buffer = this->glObjects[arrayId].buffer; + auto& vector = vboData[arrayId]; + this->storedVboSizes[arrayId] = vector.size(); + buffer.bind(); + buffer.allocate(vector.data(), static_cast(vector.size() * sizeof vector[0])); + buffer.release(); } } @@ -119,7 +143,9 @@ void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector* vboData) { const gl::ArrayClass vboClass = classifyPolygon(polygon); - std::vector& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})]; + //std::vector& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})]; + std::vector& vertexBuffer = vboData[static_cast(vboClass)]; + /* std::vector& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})]; auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z); @@ -132,6 +158,7 @@ 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 @@ -184,74 +211,21 @@ void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass) { - this->vertexArrays[static_cast(arrayClass)].bind(); - glUseProgram(this->glObjects[static_cast(arrayClass)].program); + auto& object = this->glObjects[static_cast(arrayClass)]; + object.vertexArray.bind(); + object.program->bind(); } void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass) { - this->vertexArrays[static_cast(arrayClass)].release(); + auto& object = this->glObjects[static_cast(arrayClass)]; + object.program->release(); + object.vertexArray.release(); } -void gl::Compiler::buildShaders(int arrayId) +std::size_t gl::Compiler::vboSize(const gl::ArrayClass arrayClass) const { - /* - 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(glGetAttribLocation(this->glObjects[arrayId].program, "position")); - glEnableVertexAttribArray(posAttrib); - glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, size, gl::offset(0)); - const GLuint colAttrib = static_cast(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& data) -{ - glBindBuffer(GL_ARRAY_BUFFER, this->storedVbo[vboIndex]); - glBufferData(GL_ARRAY_BUFFER, static_cast(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)]; + return this->storedVboSizes[static_cast(arrayClass)]; } int gl::vboIndex(const VboAddress vboAddress)