src/gl/gridprogram.cpp

Wed, 25 May 2022 20:36:34 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 20:36:34 +0300
changeset 199
6988973515d2
parent 167
c1ff4f107569
child 216
c7241f504117
permissions
-rw-r--r--

Fix pick() picking from weird places on the screen with high DPI scaling

glReadPixels reads data from the frame buffer, which contains data after
high DPI scaling, so any reads to that need to take this scaling into account

/*
 *  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"

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}
{
	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});
	}
	for (int i = -extent; i <= extent; i += 1)
	{
		this->gridData.push_back({-extent, i});
		this->gridData.push_back({extent, 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