src/gl/axesprogram.cpp

Wed, 15 Jun 2022 12:32:40 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 15 Jun 2022 12:32:40 +0300
changeset 224
54fd83d4db9c
parent 216
c7241f504117
child 234
87ee9824210b
permissions
-rw-r--r--

GCC doesn't like me using QStringLiteral here

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

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

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

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

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

out vec4 color;
smooth in vec3 ex_color;

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

void AxesLayer::initializeGL()
{
	constexpr struct VertexType
	{
		glm::vec3 position;
		glm::vec3 color;
	} data[] = {
		{{10000, 0, 0}, {1, 0, 0}},
		{{0, 0, 0}, {1, 0, 0}},
		{{-10000, 0, 0}, {0.5, 0, 0}},
		{{0, 0, 0}, {0.5, 0, 0}},
		{{0, 10000, 0}, {0, 1, 0}},
		{{0, 0, 0}, {0, 1, 0}},
		{{0, -10000, 0}, {0, 0.5, 0}},
		{{0, 0, 0}, {0, 0.5, 0}},
		{{0, 0, 10000}, {0, 0, 1}},
		{{0, 0, 0}, {0, 0, 1}},
		{{0, 0, -10000}, {0, 0, 0.5}},
		{{0, 0, 0}, {0, 0, 0.5}},
	};
	constexpr int stride = sizeof(VertexType);
	this->shader.initialize(
		::vertexShaderSource,
		::fragmentShaderSource,
		QOpenGLBuffer::StaticDraw,
		{
			GLAttributeSpec{
				.type = GL_FLOAT,
				.offset = offsetof(VertexType, position),
				.tuplesize = 3,
				.stride = stride,
			},
			{
				.type = GL_FLOAT,
				.offset = offsetof(VertexType, color),
				.tuplesize = 3,
				.stride = stride,
			},
		});
	this->shader.bufferData(&data[0], countof(data), sizeof data[0]);
}

void AxesLayer::paintGL()
{
	glLineWidth(5);
	glEnable(GL_LINE_SMOOTH);
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
	this->shader.draw(GL_LINES);
	glDisable(GL_LINE_SMOOTH);
}

void AxesLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix)
{
	this->shader.setMvpMatrix(mvpMatrix);
}

mercurial