Wed, 28 Jul 2021 08:23:09 +0300
update
#include "vertexprogram.h" #include "document.h" static const char vertexShaderSource[] = R"( #version 330 core const int FRAGSTYLE_Normal = 0; const int FRAGSTYLE_Id = 1; 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 glm::vec3 o = {0, 0, 0}; static constexpr glm::vec3 c1 = {1, -1, 1}; static constexpr glm::vec3 c2 = {1, -1, -1}; static constexpr glm::vec3 c3 = {-1, -1, -1}; static constexpr glm::vec3 c4 = {-1, -1, 1}; static constexpr glm::vec3 markerGeometry[] = { o, c1, c2, o, c2, c3, o, c3, c4, o, c4, c1, }; 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_TRIANGLES; } QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const { return QOpenGLBuffer::DynamicDraw; } void VertexProgram::setFragmentStyle(const FragmentStyle newFragmentStyle) { this->fragmentStyle = newFragmentStyle; } 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, ::countof(::markerGeometry)); for (const glm::vec3& point : ::markerGeometry) { this->data.push_back({vertex + point * size, color}); } }); this->buffer.bind(); this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize()); this->buffer.release(); }