# HG changeset patch # User Teemu Piippo # Date 1579648631 -7200 # Node ID c57fb7a5ffa3c05da65254b99079a5d7dd3e145c # Parent 3a9e761e4faa63acdf83c0ee4f373b585abad924 commit work done on plugging vao to the gl renderer, renders nonsense for now diff -r 3a9e761e4faa -r c57fb7a5ffa3 locale/fi.ts --- a/locale/fi.ts Wed Jan 22 00:23:29 2020 +0200 +++ b/locale/fi.ts Wed Jan 22 01:17:11 2020 +0200 @@ -88,27 +88,27 @@ LibraryManager - + Official library - + Unofficial library - + Working library - + Path - + Role @@ -161,27 +161,27 @@ - + Open model - + LDraw models (*.ldr *.dat) - + Problem loading references - + Problem opening file - + Could not open %1: %2 @@ -189,7 +189,7 @@ PartRenderer - + Rendering error 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) diff -r 3a9e761e4faa -r c57fb7a5ffa3 src/gl/compiler.h --- a/src/gl/compiler.h Wed Jan 22 00:23:29 2020 +0200 +++ b/src/gl/compiler.h Wed Jan 22 01:17:11 2020 +0200 @@ -49,9 +49,7 @@ ~Compiler(); void build(Model* model, DocumentManager* context); void buildPolygon(Polygon polygon, std::vector* vboData); - void upload(const int vboIndex, const std::vector& data); - GLuint vbo(const VboAddress vboAddress) const; - std::size_t vboSize(const VboAddress vboAddress) const; + std::size_t vboSize(gl::ArrayClass arrayClass) const; QColor getColorForPolygon(const gl::Polygon& polygon); Point3D modelCenter() const; double modelDistance() const; @@ -59,9 +57,20 @@ void bindVertexArray(gl::ArrayClass arrayClass); void releaseVertexArray(gl::ArrayClass arrayClass); void buildShaders(int arrayId); + template + void setUniform(const char* uniformName, T&& value) + { + Q_ASSERT(this->initialized); + for (auto& object : this->glObjects) + { + object.program->bind(); + const int location = glGetUniformLocation(object.program->programId(), uniformName); + Q_ASSERT(location != -1); + object.program->setUniformValue(location, std::forward(value)); + object.program->release(); + } + } private: - QOpenGLVertexArrayObject vertexArrays[gl::NUM_ARRAY_CLASSES]; - //GLuint storedVbo[gl::numVbos]; bool m_vboChanged[gl::numVbos] = {true}; std::size_t storedVboSizes[gl::numVbos] = {0_z}; bool initialized = false; diff -r 3a9e761e4faa -r c57fb7a5ffa3 src/gl/partrenderer.cpp --- a/src/gl/partrenderer.cpp Wed Jan 22 00:23:29 2020 +0200 +++ b/src/gl/partrenderer.cpp Wed Jan 22 01:17:11 2020 +0200 @@ -21,34 +21,6 @@ #include #include "partrenderer.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 = CameraTransformation * vec4(position, 1.0); -} -)"; - -static const char* fragmentShaderSource = R"( -#version 330 core - -in vec4 vColor; -out vec4 fColor; - -void main() -{ - fColor = vColor; -} -)"; - PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) : QOpenGLWidget{parent}, model{model}, @@ -61,7 +33,6 @@ PartRenderer::~PartRenderer() { - delete this->objects.program; } void PartRenderer::initializeGL() @@ -73,62 +44,13 @@ } glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); - //this->compiler->initialize(); - //this->compiler->build(this->model, this->documents); + this->compiler->initialize(); + this->compiler->build(this->model, this->documents); this->initializeLighting(); this->initialized = true; this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30); this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330); glLineWidth(2.0); - this->objects.program = new QOpenGLShaderProgram; - this->objects.program->create(); - this->checkForGLErrors(); - this->objects.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource); - this->checkForGLErrors(); - this->objects.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource); - this->checkForGLErrors(); - this->objects.program->link(); - this->checkForGLErrors(); - this->objects.program->bind(); - this->checkForGLErrors(); - this->objects.buffer.create(); - this->checkForGLErrors(); - this->objects.buffer.bind(); - this->checkForGLErrors(); - this->objects.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); - this->checkForGLErrors(); - /* - GLfloat data[] = { - 20.0f, 20.0f, 6.0f, 1.0f, 0.0f, 0.0f, 1.0f, - 30.0f, 20.0f, 6.0f, 0.0f, 1.0f, 0.0f, 1.0f, - 30.0f, 30.0f, 6.0f, 0.0f, 0.0f, 1.0f, 1.0f, - }; - */ - GLfloat data[] = { - 0.00f, 0.75f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, - -0.75f, -0.75f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, - 0.75f, -0.75f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f - }; - this->objects.buffer.allocate(data, sizeof data); - this->checkForGLErrors(); - this->objects.vertexArray.create(); - this->checkForGLErrors(); - this->objects.vertexArray.bind(); - this->checkForGLErrors(); - this->objects.program->enableAttributeArray(0); - this->checkForGLErrors(); - this->objects.program->enableAttributeArray(1); - this->checkForGLErrors(); - this->objects.program->setAttributeBuffer(0, GL_FLOAT, 0, 3); - this->checkForGLErrors(); - this->objects.program->setAttributeBuffer(1, GL_FLOAT, 3, 4); - this->checkForGLErrors(); - this->objects.vertexArray.release(); - this->checkForGLErrors(); - this->objects.buffer.release(); - this->checkForGLErrors(); - this->objects.program->release(); - this->checkForGLErrors(); } /* @@ -227,38 +149,21 @@ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); break; } - this->objects.program->bind(); - this->checkForGLErrors(); - const int cameraTransformationUniform = glGetUniformLocation(this->objects.program->programId(), "CameraTransformation"); - this->checkForGLErrors(); - this->objects.program->setUniformValue(cameraTransformationUniform, rotationMatrix); - this->checkForGLErrors(); - this->objects.vertexArray.bind(); - this->checkForGLErrors(); - glDrawArrays(GL_TRIANGLES, 0, 3); - this->checkForGLErrors(); - this->objects.vertexArray.release(); - this->checkForGLErrors(); - this->objects.program->release(); - this->checkForGLErrors(); -#if 0 + this->compiler->setUniform("CameraTransformation", rotationMatrix); // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. renderVao(gl::ArrayClass::Triangles); renderVao(gl::ArrayClass::Quads); renderVao(gl::ArrayClass::Lines); -#endif glDisable(GL_POLYGON_OFFSET_FILL); } -void PartRenderer::renderVao(const gl::ArrayClass /*arrayClass*/) +void PartRenderer::renderVao(const gl::ArrayClass arrayClass) { - /* this->compiler->bindVertexArray(arrayClass); - const std::size_t vertexCount = this->compiler->vboSize({arrayClass, gl::VboSubclass::VertexData}) / gl::FLOATS_PER_VERTEX; + const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX; glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast(vertexCount)); this->compiler->releaseVertexArray(arrayClass); this->checkForGLErrors(); - */ } void PartRenderer::checkForGLErrors() diff -r 3a9e761e4faa -r c57fb7a5ffa3 src/gl/partrenderer.h --- a/src/gl/partrenderer.h Wed Jan 22 00:23:29 2020 +0200 +++ b/src/gl/partrenderer.h Wed Jan 22 01:17:11 2020 +0200 @@ -33,12 +33,6 @@ QQuaternion rotation; gl::Compiler* compiler; gl::RenderStyle renderStyle = gl::RenderStyle::Normal; - struct - { - QOpenGLShaderProgram* program = nullptr; - QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer}; - QOpenGLVertexArrayObject vertexArray; - } objects; bool initialized = false; void initializeLighting(); void renderVao(const gl::ArrayClass arrayClass);