--- 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; -}