Tue, 27 Jul 2021 16:29:00 +0300
Add vertex rendering
118 | 1 | #include "vertexprogram.h" |
2 | #include "document.h" | |
3 | ||
4 | static const char vertexShaderSource[] = R"( | |
5 | #version 330 core | |
6 | ||
7 | layout (location = 0) in vec3 in_position; | |
8 | layout (location = 1) in vec3 in_color; | |
9 | uniform mat4 view; | |
10 | uniform mat4 projection; | |
11 | uniform mat4 model; | |
12 | smooth out vec3 ex_color; | |
13 | ||
14 | void main() | |
15 | { | |
16 | gl_Position = projection * view * model * vec4(in_position, 1.0); | |
17 | ex_color = in_color; | |
18 | } | |
19 | )"; | |
20 | ||
21 | static const char fragmentShaderSource[] = R"( | |
22 | #version 330 core | |
23 | ||
24 | out vec4 color; | |
25 | smooth in vec3 ex_color; | |
26 | ||
27 | void main(void) | |
28 | { | |
29 | color = vec4(ex_color, 1); | |
30 | } | |
31 | )"; | |
32 | ||
33 | static constexpr int VERTICES_PER_OBJECT = 16; | |
34 | ||
35 | ||
36 | VertexProgram::VertexProgram(QObject *parent) : | |
37 | AbstractBasicShaderProgram{parent} | |
38 | { | |
39 | } | |
40 | ||
41 | const char *VertexProgram::vertexShaderSource() const | |
42 | { | |
43 | return ::vertexShaderSource; | |
44 | } | |
45 | ||
46 | const char *VertexProgram::fragmentShaderSource() const | |
47 | { | |
48 | return ::fragmentShaderSource; | |
49 | } | |
50 | ||
51 | const void *VertexProgram::vertexData() const | |
52 | { | |
53 | return this->data.data(); | |
54 | } | |
55 | ||
56 | int VertexProgram::vertexSize() const | |
57 | { | |
58 | return sizeof(Vertex); | |
59 | } | |
60 | ||
61 | int VertexProgram::vertexCount() const | |
62 | { | |
63 | return this->data.size(); | |
64 | } | |
65 | ||
66 | void VertexProgram::setupVertexArrays() | |
67 | { | |
68 | for (int i : {0, 1}) | |
69 | { | |
70 | this->program->enableAttributeArray(i); | |
71 | } | |
72 | const int stride = this->vertexSize(); | |
73 | this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride); | |
74 | this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 3, stride); | |
75 | } | |
76 | ||
77 | GLenum VertexProgram::drawMode() const | |
78 | { | |
79 | return GL_LINES; | |
80 | } | |
81 | ||
82 | QOpenGLBuffer::UsagePattern VertexProgram::usagePattern() const | |
83 | { | |
84 | return QOpenGLBuffer::DynamicDraw; | |
85 | } | |
86 | ||
87 | void VertexProgram::build(const Document *document) | |
88 | { | |
89 | constexpr float size = 1; | |
90 | constexpr glm::vec3 color = {0.0, 1.0, 1.0}; | |
91 | this->data.clear(); | |
92 | document->applyToVertices([&](const glm::vec3& vertex, const std::set<ldraw::id_t>&) | |
93 | { | |
94 | reserveMore(this->data, VERTICES_PER_OBJECT); | |
95 | this->data.push_back({vertex, color}); | |
96 | this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); | |
97 | this->data.push_back({vertex, color}); | |
98 | this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); | |
99 | this->data.push_back({vertex, color}); | |
100 | this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); | |
101 | this->data.push_back({vertex, color}); | |
102 | this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); | |
103 | this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); | |
104 | this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); | |
105 | this->data.push_back({vertex + glm::vec3{size, -size, -size}, color}); | |
106 | this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); | |
107 | this->data.push_back({vertex + glm::vec3{-size, -size, -size}, color}); | |
108 | this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); | |
109 | this->data.push_back({vertex + glm::vec3{-size, -size, size}, color}); | |
110 | this->data.push_back({vertex + glm::vec3{size, -size, size}, color}); | |
111 | }); | |
112 | this->buffer.bind(); | |
113 | this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize()); | |
114 | this->buffer.release(); | |
115 | } |