src/gl/gridprogram.cpp

Sat, 05 Mar 2022 13:32:58 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:32:58 +0200
changeset 163
36ea1a8aee33
parent 156
65b75beed7e0
child 167
c1ff4f107569
permissions
-rw-r--r--

Add icons for tools

/*
 *  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;
uniform mat4 grid;

void main()
{
	gl_Position = projection * view * model * grid * 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;

void main(void)
{
	float dx = fract(ex_uv.y);
	float dy = fract(ex_uv.x);
	/* fade the grid towards extreme co-ordinates */
	float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y)));
	color = vec4(gridColor.xyz, gridColor.w * d);
}
)";

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)
{
	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 this->gridData.data();
}

int GridProgram::vertexSize() const
{
	return sizeof this->gridData[0];
}

int GridProgram::vertexCount() const
{
	return this->gridData.size();
}

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

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

mercurial