Sat, 05 Mar 2022 00:44:15 +0200
render the grid with lines
src/gl/gridprogram.cpp | file | annotate | diff | comparison | revisions | |
src/gl/gridprogram.h | file | annotate | diff | comparison | revisions |
--- a/src/gl/gridprogram.cpp Fri Mar 04 23:41:58 2022 +0200 +++ b/src/gl/gridprogram.cpp Sat Mar 05 00:44:15 2022 +0200 @@ -27,12 +27,11 @@ uniform mat4 projection; uniform mat4 model; smooth out vec2 ex_uv; -const mat4 stretch = mat4(vec4(1000, 0, 0, 0), vec4(0, 1000, 0, 0), vec4(0, 0, 1000, 0), vec4(0, 0, 0, 1)); uniform mat4 grid; void main() { - gl_Position = projection * view * model * grid * stretch * vec4(in_position, 0.0, 1.0); + gl_Position = projection * view * model * grid * vec4(in_position, 0.0, 1.0); ex_uv = in_position; } )"; @@ -43,26 +42,32 @@ out vec4 color; smooth in vec2 ex_uv; uniform vec4 gridColor; -const float pi = 3.14159265f; void main(void) { - float dx = fract(ex_uv.y / 0.001f); - float dy = fract(ex_uv.x / 0.001f); - /* compute distance to nearest unit line */ - float d = min(min(min(dy, dx), 1 - dy), 1 - dx); - /* use an extreme sigmoid to bring out the grid shape */ - d = pow(1.02 - d, 100); + float dx = fract(ex_uv.y); + float dy = fract(ex_uv.x); /* fade the grid towards extreme co-ordinates */ - d = (1.0f - 20 * max(abs(ex_uv.x), abs(ex_uv.y))) * d; - /* add dashes */ - d *= (1 + pow(cos((ex_uv.y / 0.0001f) * pi), 10)) * 0.5f; - d *= (1 + pow(cos((ex_uv.x / 0.0001f) * pi), 10)) * 0.5f; + float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y))); color = vec4(gridColor.xyz, gridColor.w * d); } )"; -static const glm::vec2 data[] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; +GridProgram::GridProgram(QObject *parent) : + AbstractBasicShaderProgram{parent} +{ + this->gridData.reserve(8004); + for (int i = -1000; i < 1000; i += 1) + { + this->gridData.push_back({i, -1000}); + this->gridData.push_back({i, 1000}); + } + for (int i = -1000; i < 1000; i += 1) + { + this->gridData.push_back({-1000, i}); + this->gridData.push_back({1000, i}); + } +} void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix) { @@ -97,17 +102,17 @@ const void* GridProgram::vertexData() const { - return ::data; + return this->gridData.data(); } int GridProgram::vertexSize() const { - return sizeof data[0]; + return sizeof this->gridData[0]; } int GridProgram::vertexCount() const { - return glm::countof(data); + return this->gridData.size(); } void GridProgram::setupVertexArrays() @@ -119,7 +124,7 @@ GLenum GridProgram::drawMode() const { - return GL_QUADS; + return GL_LINES; } QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const
--- a/src/gl/gridprogram.h Fri Mar 04 23:41:58 2022 +0200 +++ b/src/gl/gridprogram.h Sat Mar 05 00:44:15 2022 +0200 @@ -24,7 +24,7 @@ { Q_OBJECT public: - using AbstractBasicShaderProgram::AbstractBasicShaderProgram; + GridProgram(QObject* parent = nullptr); void setGridMatrix(const glm::mat4& newGridMatrix); void setGridColor(const QColor& newGridColor); protected: @@ -38,4 +38,5 @@ QOpenGLBuffer::UsagePattern usagePattern() const override; private: glm::vec4 gridColor = {1.0f, 1.0f, 1.0f, 0.75f}; + std::vector<glm::vec2> gridData; };