--- a/src/gl/axesprogram.cpp Fri Mar 06 16:08:53 2020 +0200 +++ b/src/gl/axesprogram.cpp Fri Mar 06 20:13:10 2020 +0200 @@ -16,7 +16,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "gridprogram.h" +#include "axesprogram.h" // Based on https://stackoverflow.com/q/30842755 const char vertexShaderSource[] = R"( @@ -31,8 +31,8 @@ void main() { - gl_Position = projection * view * model * vec4(in_position, 0.0, 1.0); - ex_uv = ex_color; + gl_Position = projection * view * model * vec4(in_position, 1.0); + ex_color = in_color; } )"; @@ -44,7 +44,7 @@ void main(void) { - color = vec4(ex_color, 1) + color = vec4(ex_color, 1); } )"; @@ -67,102 +67,43 @@ AxesVertex{{0, 0, 10000}, {0, 0, 1}}, }; -AxesProgram::AxesProgram(QObject* parent) : - QObject{parent}, - buffer{QOpenGLBuffer::VertexBuffer}, - vertexShader{QOpenGLShader::Vertex}, - fragmentShader{QOpenGLShader::Fragment} +const char* AxesProgram::vertexShaderSource() const { + return ::vertexShaderSource; } -void AxesProgram::initialize() +const char* AxesProgram::fragmentShaderSource() const { - if (not isInitialized) - { - this->initializeOpenGLFunctions(); - this->isInitialized = true; - this->program.emplace(this); - gl::buildShaders(&*this->program, ::vertexShaderSource, ::fragmentShaderSource); - this->program->bind(); - this->buffer.create(); - this->buffer.bind(); - this->buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); - this->buffer.allocate(data, countof(data) * sizeof data[0]); - this->vertexArrayObject.create(); - this->vertexArrayObject.bind(); - this->program->enableAttributeArray(0); - this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0); - this->program->setUniformVector("gridColor", this->gridColor); - this->vertexArrayObject.release(); - this->buffer.release(); - this->program->release(); - this->checkForGLErrors(); - } + return ::fragmentShaderSource; } -void AxesProgram::setViewMatrix(const glm::mat4& newViewMatrix) +const void* AxesProgram::vertexData() const { - this->setMatrix("view", newViewMatrix); + return ::data; } -void AxesProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix) +GLenum AxesProgram::drawMode() const { - this->setMatrix("projection", newProjectionMatrix); -} - -void AxesProgram::setModelMatrix(const glm::mat4& newModelMatrix) -{ - this->setMatrix("model", newModelMatrix); + return GL_LINES; } -void AxesProgram::setGridMatrix(const glm::mat4& newGridMatrix) +int AxesProgram::vertexSize() const { - this->setMatrix("grid", newGridMatrix); + return sizeof data[0]; } -void AxesProgram::setMatrix(const char* name, const glm::mat4& matrix) +int AxesProgram::vertexCount() const { - Q_ASSERT(this->isInitialized); - this->program->bind(); - this->program->setUniformMatrix(name, matrix); - this->program->release(); - this->checkForGLErrors(); + return countof(data); } -void GridProgram::setGridColor(const QColor& newGridColor) -{ - const glm::vec4 vec = gl::colorToVector4(newGridColor); - if (this->isInitialized) - { - this->program->bind(); - this->program->setUniformVector("gridColor", vec); - this->program->release(); - this->checkForGLErrors(); - } - else - { - this->gridColor = vec; - } -} - -void GridProgram::draw() +void AxesProgram::setupVertexArrays() { - this->program->bind(); - this->vertexArrayObject.bind(); - glDrawArrays(GL_QUADS, 0, countof(data)); - this->vertexArrayObject.release(); - this->program->release(); - this->checkForGLErrors(); + for (int i : {0, 1}) + { + this->program->enableAttributeArray(i); + } + const int stride = this->vertexSize(); + this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(AxesVertex, position), 3, stride); + this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(AxesVertex, color), 3, stride); } - -void GridProgram::teardown() -{ - this->vertexArrayObject.destroy(); - this->buffer.destroy(); - this->program.reset(); -} - -void GridProgram::checkForGLErrors() -{ - gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent())); -}