src/gl/geometrypreview.cpp

Wed, 17 Feb 2021 16:49:35 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 17 Feb 2021 16:49:35 +0200
changeset 102
9f435f66bd0c
permissions
-rw-r--r--

stuff

102
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include "geometrypreview.h"
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 const char vertexShaderSource[] = R"(
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 #version 330 core
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 layout (location = 0) in vec3 in_position;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 layout (location = 1) in vec3 in_color;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 uniform mat4 view;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 uniform mat4 projection;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 uniform mat4 model;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 smooth out vec3 ex_color;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 void main()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 gl_Position = projection * view * model * vec4(in_position, 1.0);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 ex_color = in_color;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 )";
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 const char fragmentShaderSource[] = R"(
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #version 330 core
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 out vec4 color;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 smooth in vec3 ex_color;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 void main(void)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 color = vec4(ex_color, 1);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 )";
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 constexpr std::size_t vertexSize = sizeof(GeometryPreview::Vertex);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 GeometryPreview::GeometryPreview(QObject *parent) :
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 AbstractShaderProgram{{
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 {GL_LINES, vertexSize, QOpenGLBuffer::DynamicDraw},
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 {GL_TRIANGLES, vertexSize, QOpenGLBuffer::DynamicDraw}
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 }, parent}
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 GeometryPreview::~GeometryPreview()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 QVector<GeometryPreview::Vertex>& GeometryPreview::modifyLinesBuffer()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 this->needRebuild = true;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 return this->lines;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 QVector<GeometryPreview::Vertex>& GeometryPreview::modifyTrianglesBuffer()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 this->needRebuild = true;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 return this->triangles;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 void GeometryPreview::initialize()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 if (not this->isInitialized)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 this->isInitialized = true;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 this->program.emplace(this);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 gl::buildShaders(&*this->program, ::vertexShaderSource, ::fragmentShaderSource);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 this->program->bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 for (QOpenGLBuffer* buffer : {&this->linesBuffer, &this->trianglesBuffer})
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 buffer->create();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 buffer->bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 buffer->setUsagePattern(QOpenGLBuffer::DynamicDraw);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 this->vertexArrayObject.create();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 this->vertexArrayObject.bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 this->setupVertexArrays();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 this->vertexArrayObject.release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 for (auto& buffer : {this->linesBuffer, this->trianglesBuffer})
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 buffer->release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 this->program->release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 this->checkForGLErrors();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 void GeometryPreview::rebuildIfNecessary()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 if (this->needRebuild)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize());
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 this->needRebuild = false;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 void GeometryPreview::checkForGLErrors()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent()));
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 void GeometryPreview::setupVertexArrays()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 for (int i : {0, 1})
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 this->program->enableAttributeArray(i);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 const int stride = this->vertexSize();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 4, stride);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 }

mercurial