src/gl/axesprogram.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 118
8e1c9f18ae15
child 215
34c6e7bc4ee1
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 "axesprogram.h"

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

layout (location = 0) in vec3 in_position;
layout (location = 1) in vec3 in_color;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 model;
smooth out vec3 ex_color;

void main()
{
	gl_Position = projection * view * model * vec4(in_position, 1.0);
	ex_color = in_color;
}
)";

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

out vec4 color;
smooth in vec3 ex_color;

void main(void)
{
	color = vec4(ex_color, 1);
}
)";

namespace
{
	struct AxesVertex
	{
		glm::vec3 position;
		glm::vec3 color;
	};
}

static const AxesVertex data[] =
{
	AxesVertex{{10000, 0, 0}, {1, 0, 0}},
	AxesVertex{{0, 0, 0}, {1, 0, 0}},
	AxesVertex{{-10000, 0, 0}, {0.5, 0, 0}},
	AxesVertex{{0, 0, 0}, {0.5, 0, 0}},
	AxesVertex{{0, 10000, 0}, {0, 1, 0}},
	AxesVertex{{0, 0, 0}, {0, 1, 0}},
	AxesVertex{{0, -10000, 0}, {0, 0.5, 0}},
	AxesVertex{{0, 0, 0}, {0, 0.5, 0}},
	AxesVertex{{0, 0, 10000}, {0, 0, 1}},
	AxesVertex{{0, 0, 0}, {0, 0, 1}},
	AxesVertex{{0, 0, -10000}, {0, 0, 0.5}},
	AxesVertex{{0, 0, 0}, {0, 0, 0.5}},
};

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

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

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

GLenum AxesProgram::drawMode() const
{
	return GL_LINES;
}

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

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

void AxesProgram::setupVertexArrays()
{
	for (int i : {0, 1})
	{
		this->program->enableAttributeArray(i);
	}
	const int stride = this->vertexSize();
	this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(AxesVertex, position), 3, stride);
	this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(AxesVertex, color), 3, stride);
}

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

mercurial