src/gl/gridprogram.cpp

Tue, 28 Sep 2021 23:07:23 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 23:07:23 +0300
changeset 145
4dea24d3eda0
parent 118
8e1c9f18ae15
child 155
13713fadbf14
permissions
-rw-r--r--

Use QSaveFile to save the file more safely

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2020 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gridprogram.h"

// Based on https://stackoverflow.com/q/30842755
const char vertexShaderSource[] = R"(
#version 330 core

layout (location = 0) in vec2 in_position;
uniform mat4 view;
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);
	ex_uv = in_position;
}
)";

const char fragmentShaderSource[] = R"(
#version 330 core

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 - d, 50);
	/* 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 + cos(ex_uv.y / 0.0001f * pi)) * 0.5f;
	d *= (1 + cos(ex_uv.x / 0.0001f * pi)) * 0.5f;
	color = vec4(gridColor.xyz, gridColor.w * d);
}
)";

static const glm::vec2 data[] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};

void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix)
{
	this->setMatrix("grid", newGridMatrix);
}

void GridProgram::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;
	}
}

const char* GridProgram::vertexShaderSource() const
{
	return ::vertexShaderSource;
}

const char* GridProgram::fragmentShaderSource() const
{
	return ::fragmentShaderSource;
}

const void* GridProgram::vertexData() const
{
	return ::data;
}

int GridProgram::vertexSize() const
{
	return sizeof data[0];
}

int GridProgram::vertexCount() const
{
	return glm::countof(data);
}

void GridProgram::setupVertexArrays()
{
	this->program->enableAttributeArray(0);
	this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
	this->program->setUniformVector("gridColor", this->gridColor);
}

GLenum GridProgram::drawMode() const
{
	return GL_QUADS;
}

QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const
{
	return QOpenGLBuffer::StaticDraw;
}

mercurial