Wed, 22 Sep 2021 00:25:31 +0300
Merge commit
/* * 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(); const QOpenGLBuffer::UsagePattern pattern = this->usagePattern(); this->buffer.setUsagePattern(pattern); if (pattern == 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())); }