--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gl/vertexprogram.cpp Tue Jul 27 16:29:00 2021 +0300 @@ -0,0 +1,115 @@ +#include "vertexprogram.h" +#include "document.h" + +static const char vertexShaderSource[] = R"( +#version 330 core + +layout (location = 0) in vec3 in_position; +layout (location = 1) in vec3 in_color; +uniform mat4 view; +uniform mat4 projection; +uniform mat4 model; +smooth out vec3 ex_color; + +void main() +{ + gl_Position = projection * view * model * vec4(in_position, 1.0); + ex_color = in_color; +} +)"; + +static const char fragmentShaderSource[] = R"( +#version 330 core + +out vec4 color; +smooth in vec3 ex_color; + +void main(void) +{ + color = vec4(ex_color, 1); +} +)"; + +static constexpr int VERTICES_PER_OBJECT = 16; + + +VertexProgram::VertexProgram(QObject *parent) : + AbstractBasicShaderProgram{parent} +{ +} + +const char *VertexProgram::vertexShaderSource() const +{ + return ::vertexShaderSource; +} + +const char *VertexProgram::fragmentShaderSource() const +{ + return ::fragmentShaderSource; +} + +const void *VertexProgram::vertexData() const +{ + return this->data.data(); +} + +int VertexProgram::vertexSize() const +{ + return sizeof(Vertex); +} + +int VertexProgram::vertexCount() const +{ + return this->data.size(); +} + +void VertexProgram::setupVertexArrays() +{ + for (int i : {0, 1}) + { + this->program->enableAttributeArray(i); + } + const int stride = this->vertexSize(); + this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride); + this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 3, stride); +} + +GLenum VertexProgram::drawMode() const +{ + return GL_LINES; +} + +QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const +{ + return QOpenGLBuffer::DynamicDraw; +} + +void VertexProgram::build(const Document *document) +{ + constexpr float size = 1; + constexpr glm::vec3 color = {0.0, 1.0, 1.0}; + this->data.clear(); + document->applyToVertices([&](const glm::vec3& vertex, const std::set<ldraw::id_t>&) + { + reserveMore(this->data, VERTICES_PER_OBJECT); + this->data.push_back({vertex, color}); + this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); + this->data.push_back({vertex, color}); + this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); + this->data.push_back({vertex, color}); + this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); + this->data.push_back({vertex, color}); + this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); + this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); + this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); + this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); + this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); + this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); + this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); + this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); + this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); + }); + this->buffer.bind(); + this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize()); + this->buffer.release(); +}