src/gl/vertexprogram.cpp

Tue, 27 Jul 2021 16:29:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 27 Jul 2021 16:29:00 +0300
changeset 118
8e1c9f18ae15
child 119
24275a4064f4
permissions
-rw-r--r--

Add vertex rendering

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

mercurial