src/gl/basicshaderprogram.cpp

Tue, 26 Jan 2021 18:02:17 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 26 Jan 2021 18:02:17 +0200
changeset 100
43ce3672648a
parent 70
f21b800b02a4
child 118
8e1c9f18ae15
permissions
-rw-r--r--

fix build

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

AbstractBasicShaderProgram::AbstractBasicShaderProgram(QObject* parent) :
	QObject{parent},
	buffer{QOpenGLBuffer::VertexBuffer},
	vertexShader{QOpenGLShader::Vertex},
	fragmentShader{QOpenGLShader::Fragment}
{
}

void AbstractBasicShaderProgram::initialize()
{
	if (not isInitialized)
	{
		this->initializeOpenGLFunctions();
		this->isInitialized = true;
		this->program.emplace(this);
		gl::buildShaders(&*this->program, this->vertexShaderSource(), this->fragmentShaderSource());
		this->program->bind();
		this->buffer.create();
		this->buffer.bind();
		this->buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
		this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize());
		this->vertexArrayObject.create();
		this->vertexArrayObject.bind();
		this->setupVertexArrays();
		this->vertexArrayObject.release();
		this->buffer.release();
		this->program->release();
		this->checkForGLErrors();
	}
}

void AbstractBasicShaderProgram::setViewMatrix(const glm::mat4& newViewMatrix)
{
	this->setMatrix("view", newViewMatrix);
}

void AbstractBasicShaderProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix)
{
	this->setMatrix("projection", newProjectionMatrix);
}

void AbstractBasicShaderProgram::setModelMatrix(const glm::mat4& newModelMatrix)
{
	this->setMatrix("model", newModelMatrix);
}

void AbstractBasicShaderProgram::setMatrix(const char* name, const glm::mat4& matrix)
{
	Q_ASSERT(this->isInitialized);
	this->program->bind();
	this->program->setUniformMatrix(name, matrix);
	this->program->release();
	this->checkForGLErrors();
}

void AbstractBasicShaderProgram::draw()
{
	this->program->bind();
	this->vertexArrayObject.bind();
	glDrawArrays(this->drawMode(), 0, this->vertexCount());
	this->vertexArrayObject.release();
	this->program->release();
	this->checkForGLErrors();
}

void AbstractBasicShaderProgram::teardown()
{
	this->vertexArrayObject.destroy();
	this->buffer.destroy();
	this->program.reset();
}

void AbstractBasicShaderProgram::checkForGLErrors()
{
	gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent()));
}

mercurial