Mon, 13 Jun 2022 02:18:25 +0300
Reworked grid program into a render layer
--- a/CMakeLists.txt Sun Jun 12 23:59:37 2022 +0300 +++ b/CMakeLists.txt Mon Jun 13 02:18:25 2022 +0300 @@ -39,7 +39,7 @@ src/gl/axesprogram.cpp src/gl/basicshaderprogram.cpp src/gl/compiler.cpp -# src/gl/gridprogram.cpp + src/gl/gridprogram.cpp src/gl/partrenderer.cpp # src/gl/vertexprogram.cpp src/settingseditor/keyboardshortcutseditor.cpp @@ -77,7 +77,7 @@ src/gl/basicshaderprogram.h src/gl/common.h src/gl/compiler.h -# src/gl/gridprogram.h + src/gl/gridprogram.h src/gl/partrenderer.h # src/gl/vertexprogram.h src/settingseditor/keyboardshortcutseditor.h
--- a/src/gl/axesprogram.cpp Sun Jun 12 23:59:37 2022 +0300 +++ b/src/gl/axesprogram.cpp Mon Jun 13 02:18:25 2022 +0300 @@ -89,7 +89,11 @@ void AxesLayer::paintGL() { + glLineWidth(5); + glEnable(GL_LINE_SMOOTH); + glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); this->shader.draw(GL_LINES); + glDisable(GL_LINE_SMOOTH); } void AxesLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix)
--- a/src/gl/basicshaderprogram.cpp Sun Jun 12 23:59:37 2022 +0300 +++ b/src/gl/basicshaderprogram.cpp Mon Jun 13 02:18:25 2022 +0300 @@ -43,7 +43,7 @@ this->initializeOpenGLFunctions(); this->isInitialized = true; this->program = std::make_unique<gl::ShaderProgram>(); - gl::buildShaders(&*this->program, vertexShaderSource, fragmentShaderSource); + gl::buildShaders(this->program.get(), vertexShaderSource, fragmentShaderSource); this->program->bind(); this->buffer.create(); this->buffer.bind(); @@ -64,9 +64,23 @@ void BasicShader::setMvpMatrix(const glm::mat4& newMvpMatrix) { + this->setUniformMatrix("mvp", newMvpMatrix); +} + +void BasicShader::setUniformMatrix(const char* name, const glm::mat4& value) +{ Q_ASSERT(this->isInitialized); this->program->bind(); - this->program->setUniformMatrix("mvp", newMvpMatrix); + this->program->setUniformMatrix(name, value); + this->program->release(); + gl::checkForGLErrors(nullptr); +} + +void BasicShader::setUniformVector(const char* name, const glm::vec4& value) +{ + Q_ASSERT(this->isInitialized); + this->program->bind(); + this->program->setUniformVector(name, value); this->program->release(); gl::checkForGLErrors(nullptr); }
--- a/src/gl/basicshaderprogram.h Sun Jun 12 23:59:37 2022 +0300 +++ b/src/gl/basicshaderprogram.h Mon Jun 13 02:18:25 2022 +0300 @@ -29,6 +29,8 @@ const char* fragmentShaderSource, QOpenGLBuffer::UsagePattern usagePattern, const std::vector<GLAttributeSpec>& attributeSpecs); + void setUniformMatrix(const char* name, const glm::mat4& value); + void setUniformVector(const char* name, const glm::vec4& value); void setMvpMatrix(const glm::mat4& newMvpMatrix); void bufferData(const void* data, std::size_t count, std::size_t size); void draw(GLenum drawMode);
--- a/src/gl/gridprogram.cpp Sun Jun 12 23:59:37 2022 +0300 +++ b/src/gl/gridprogram.cpp Mon Jun 13 02:18:25 2022 +0300 @@ -18,24 +18,22 @@ #include "gridprogram.h" -const char vertexShaderSource[] = R"( +constexpr char vertexShaderSource[] = R"( #version 330 core layout (location = 0) in vec2 in_position; -uniform mat4 view; -uniform mat4 projection; -uniform mat4 model; +uniform mat4 mvp; smooth out vec2 ex_uv; uniform mat4 grid; void main() { - gl_Position = projection * view * model * grid * vec4(in_position, 0.0, 1.0); + gl_Position = mvp * grid * vec4(in_position, 0.0, 1.0); ex_uv = in_position; } )"; -const char fragmentShaderSource[] = R"( +constexpr char fragmentShaderSource[] = R"( #version 330 core out vec4 color; @@ -52,82 +50,70 @@ } )"; -GridProgram::GridProgram(QObject *parent) : - AbstractBasicShaderProgram{parent} +template<int extent> +constexpr auto calcGridData() { - constexpr int extent = 50; - this->gridData.reserve(8 * extent + 4); - for (int i = -extent; i <= extent; i += 1) - { - this->gridData.push_back({i, -extent}); - this->gridData.push_back({i, extent}); + std::array<glm::vec2, 8 * extent + 4> result; + int ix = 0; + for (int i = -extent; i <= extent; i += 1) { + result[ix++] = {i, -extent}; + result[ix++] = {i, extent}; } - for (int i = -extent; i <= extent; i += 1) - { - this->gridData.push_back({-extent, i}); - this->gridData.push_back({extent, i}); + for (int i = -extent; i <= extent; i += 1) { + result[ix++] = {-extent, i}; + result[ix++] = {extent, i}; } + return result; } -void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix) +void GridLayer::setGridMatrix(const glm::mat4& newGridMatrix) { - this->setMatrix("grid", newGridMatrix); + this->shader.setUniformMatrix("grid", newGridMatrix); } -void GridProgram::setGridColor(const QColor& newGridColor) +void GridLayer::setGridColor(const QColor& newGridColor) { - const glm::vec4 vec = gl::colorToVector4(newGridColor); - if (this->isInitialized) - { - this->program->bind(); - this->program->setUniformVector("gridColor", vec); - this->program->release(); - this->checkForGLErrors(); - } - else - { - this->gridColor = vec; + this->gridColor = gl::colorToVector4(newGridColor); + if (this->isInitialized) { + this->shader.setUniformVector("gridColor", this->gridColor); } } -const char* GridProgram::vertexShaderSource() const -{ - return ::vertexShaderSource; -} - -const char* GridProgram::fragmentShaderSource() const +void GridLayer::initializeGL() { - return ::fragmentShaderSource; -} - -const void* GridProgram::vertexData() const -{ - return this->gridData.data(); -} - -int GridProgram::vertexSize() const -{ - return sizeof this->gridData[0]; + this->shader.initialize( + ::vertexShaderSource, + ::fragmentShaderSource, + QOpenGLBuffer::StaticDraw, + { + GLAttributeSpec{ + .type = GL_FLOAT, + .offset = 0, + .tuplesize = 2, + .stride = 0, + }, + } + ); + this->isInitialized = true; + constexpr auto data = calcGridData<50>(); + this->shader.setUniformVector("gridColor", this->gridColor); + this->setGridMatrix({{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}}); + this->shader.bufferData(data.data(), data.size(), sizeof data[0]); } -int GridProgram::vertexCount() const +void GridLayer::paintGL() { - return this->gridData.size(); + glLineWidth(1); + glEnable(GL_BLEND); + glLineStipple(1, 0x8888); + glEnable(GL_LINE_STIPPLE); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + this->shader.draw(GL_LINES); + glDisable(GL_BLEND); + glDisable(GL_LINE_STIPPLE); } -void GridProgram::setupVertexArrays() +void GridLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix) { - this->program->enableAttributeArray(0); - this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0); - this->program->setUniformVector("gridColor", this->gridColor); + this->shader.setMvpMatrix(mvpMatrix); } - -GLenum GridProgram::drawMode() const -{ - return GL_LINES; -} - -QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const -{ - return QOpenGLBuffer::StaticDraw; -}
--- a/src/gl/gridprogram.h Sun Jun 12 23:59:37 2022 +0300 +++ b/src/gl/gridprogram.h Mon Jun 13 02:18:25 2022 +0300 @@ -20,23 +20,16 @@ #include "basicshaderprogram.h" #include "common.h" -class GridProgram : public AbstractBasicShaderProgram +class GridLayer final : public RenderLayer { - Q_OBJECT + BasicShader shader; + glm::vec4 gridColor = {1.0f, 1.0f, 1.0f, 0.75f}; + bool isInitialized = false; public: - GridProgram(QObject* parent = nullptr); void setGridMatrix(const glm::mat4& newGridMatrix); void setGridColor(const QColor& newGridColor); protected: - const char* vertexShaderSource() const override; - const char* fragmentShaderSource() const override; - const void* vertexData() const override; - int vertexSize() const override; - int vertexCount() const override; - void setupVertexArrays() override; - GLenum drawMode() const override; - QOpenGLBuffer::UsagePattern usagePattern() const override; -private: - glm::vec4 gridColor = {1.0f, 1.0f, 1.0f, 0.75f}; - std::vector<glm::vec2> gridData; + void initializeGL() override; + void paintGL() override; + void mvpMatrixChanged(const glm::mat4& mvpMatrix) override; };
--- a/src/main.cpp Sun Jun 12 23:59:37 2022 +0300 +++ b/src/main.cpp Mon Jun 13 02:18:25 2022 +0300 @@ -8,6 +8,7 @@ #include "ui_mainwindow.h" #include "version.h" #include "gl/axesprogram.h" +#include "gl/gridprogram.h" #include "gl/partrenderer.h" #include "document.h" #include "settingseditor/settingseditor.h" @@ -41,6 +42,7 @@ std::unique_ptr<QItemSelectionModel> itemSelectionModel; std::unique_ptr<EditTools> tools; std::unique_ptr<AxesLayer> axesLayer; + std::unique_ptr<GridLayer> gridLayer; Model* model; }; #include "main.moc" @@ -397,9 +399,11 @@ data->canvas = std::make_unique<PartRenderer>(model, &documents, colorTable); data->itemSelectionModel = std::make_unique<QItemSelectionModel>(model); data->axesLayer = std::make_unique<AxesLayer>(); + data->gridLayer = std::make_unique<GridLayer>(); data->model = model; data->canvas->addRenderLayer(data->axesLayer.get()); data->canvas->setLayerEnabled(data->axesLayer.get(), settings.drawAxes()); + data->canvas->addRenderLayer(data->gridLayer.get()); documents.setModelPayload(modelId, data); QObject::connect( data->tools.get(),