src/gl/basicshaderprogram.cpp

Thu, 15 Jun 2023 16:18:03 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 15 Jun 2023 16:18:03 +0300
changeset 383
530d23cd4e97
parent 377
e1c5e4310f8b
permissions
-rw-r--r--

Refactor, make selecting elements from the model select the corresponding line from the editor as well

/*
 *  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 "src/gl/basicshaderprogram.h"

void gl::initialize_basic_shader(
	gl::basic_shader* shader,
	const char* vertexShaderSource,
	const char* fragmentShaderSource,
	QOpenGLBuffer::UsagePattern usagePattern,
	const std::vector<GLAttributeSpec>& attributeSpecs)
{
	shader->program = std::make_unique<gl::ShaderProgram>();
	gl::buildShaders(shader->program.get(), vertexShaderSource, fragmentShaderSource);
	shader->program->bind();
	shader->buffer.create();
	shader->buffer.bind();
	shader->buffer.setUsagePattern(usagePattern);
	shader->vertexArrayObject.create();
	shader->vertexArrayObject.bind();
	for (std::size_t i = 0; i < attributeSpecs.size(); ++i) {
		const auto& spec = attributeSpecs[i];
		const int attr = narrow<int>(signed_cast(i));
		shader->program->enableAttributeArray(attr);
		shader->program->setAttributeBuffer(attr, spec.type, spec.offset, spec.tuplesize, spec.stride);
	}
	shader->vertexArrayObject.release();
	shader->buffer.release();
	shader->program->release();
	gl::checkForGLErrors(nullptr);
}

void gl::set_shader_matrix_uniform(
	gl::basic_shader* shader,	
	const char* name,
	const glm::mat4& value)
{
	Q_ASSERT(shader->program != nullptr);
	shader->program->bind();
	shader->program->setUniformMatrix(name, value);
	shader->program->release();
	gl::checkForGLErrors(nullptr);
}

void gl::set_shader_vector_uniform(
	gl::basic_shader* shader,
	const char* name,
	const glm::vec4& value)
{
	Q_ASSERT(shader->program != nullptr);
	shader->program->bind();
	shader->program->setUniformVector(name, value);
	shader->program->release();
	gl::checkForGLErrors(nullptr);
}

void gl::buffer_shader_data(
	gl::basic_shader* shader,
	const void* data,
	std::size_t count,
	std::size_t size)
{
	shader->buffer.bind();
	shader->buffer.allocate(data, narrow<int>(signed_cast(count * size)));
	shader->buffer.release();
	shader->vertexCount = narrow<int>(signed_cast(count));
}

void gl::draw_shader(
	gl::basic_shader* shader,
	GLenum drawMode)
{
	shader->program->bind();
	shader->vertexArrayObject.bind();
	glDrawArrays(drawMode, 0, shader->vertexCount);
	shader->vertexArrayObject.release();
	shader->program->release();
	gl::checkForGLErrors(nullptr);
}

mercurial