Tue, 17 Mar 2020 23:13:47 +0200
added doublespinbox
70 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #include "basicshaderprogram.h" | |
20 | ||
21 | AbstractBasicShaderProgram::AbstractBasicShaderProgram(QObject* parent) : | |
22 | QObject{parent}, | |
23 | buffer{QOpenGLBuffer::VertexBuffer}, | |
24 | vertexShader{QOpenGLShader::Vertex}, | |
25 | fragmentShader{QOpenGLShader::Fragment} | |
26 | { | |
27 | } | |
28 | ||
29 | void AbstractBasicShaderProgram::initialize() | |
30 | { | |
31 | if (not isInitialized) | |
32 | { | |
33 | this->initializeOpenGLFunctions(); | |
34 | this->isInitialized = true; | |
35 | this->program.emplace(this); | |
36 | gl::buildShaders(&*this->program, this->vertexShaderSource(), this->fragmentShaderSource()); | |
37 | this->program->bind(); | |
38 | this->buffer.create(); | |
39 | this->buffer.bind(); | |
40 | this->buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); | |
41 | this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize()); | |
42 | this->vertexArrayObject.create(); | |
43 | this->vertexArrayObject.bind(); | |
44 | this->setupVertexArrays(); | |
45 | this->vertexArrayObject.release(); | |
46 | this->buffer.release(); | |
47 | this->program->release(); | |
48 | this->checkForGLErrors(); | |
49 | } | |
50 | } | |
51 | ||
52 | void AbstractBasicShaderProgram::setViewMatrix(const glm::mat4& newViewMatrix) | |
53 | { | |
54 | this->setMatrix("view", newViewMatrix); | |
55 | } | |
56 | ||
57 | void AbstractBasicShaderProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix) | |
58 | { | |
59 | this->setMatrix("projection", newProjectionMatrix); | |
60 | } | |
61 | ||
62 | void AbstractBasicShaderProgram::setModelMatrix(const glm::mat4& newModelMatrix) | |
63 | { | |
64 | this->setMatrix("model", newModelMatrix); | |
65 | } | |
66 | ||
67 | void AbstractBasicShaderProgram::setMatrix(const char* name, const glm::mat4& matrix) | |
68 | { | |
69 | Q_ASSERT(this->isInitialized); | |
70 | this->program->bind(); | |
71 | this->program->setUniformMatrix(name, matrix); | |
72 | this->program->release(); | |
73 | this->checkForGLErrors(); | |
74 | } | |
75 | ||
76 | void AbstractBasicShaderProgram::draw() | |
77 | { | |
78 | this->program->bind(); | |
79 | this->vertexArrayObject.bind(); | |
80 | glDrawArrays(this->drawMode(), 0, this->vertexCount()); | |
81 | this->vertexArrayObject.release(); | |
82 | this->program->release(); | |
83 | this->checkForGLErrors(); | |
84 | } | |
85 | ||
86 | void AbstractBasicShaderProgram::teardown() | |
87 | { | |
88 | this->vertexArrayObject.destroy(); | |
89 | this->buffer.destroy(); | |
90 | this->program.reset(); | |
91 | } | |
92 | ||
93 | void AbstractBasicShaderProgram::checkForGLErrors() | |
94 | { | |
95 | gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent())); | |
96 | } |