src/gl/compiler.h

Wed, 08 Jun 2022 19:33:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 08 Jun 2022 19:33:00 +0300
changeset 204
52e10e8d88cc
parent 200
ca23936b455b
child 205
1a4342d80de7
permissions
-rw-r--r--

Concentrate model editing into one coroutine inside main()

19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include "main.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include "gl/common.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include "types/boundingbox.h"
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
23 #include <glm/gtc/type_ptr.hpp>
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 #include <QMap>
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 #include <QSet>
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
26 #include <QOpenGLVertexArrayObject>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
27 #include <QOpenGLBuffer>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
28 #include <QOpenGLShaderProgram>
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
29 #include <QOpenGLExtraFunctions>
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 189
diff changeset
30 #include "model.h"
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
32 class Model;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
33 class DocumentManager;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
34
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 namespace gl
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
37 struct ModelShaders
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
38 {
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
39 struct Vertex
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
40 {
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
41 glm::vec3 position;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
42 glm::vec4 color;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
43 glm::vec3 normal;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
44 glm::int32 id;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
45 glm::int32 selected = 0;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
46 };
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
47 bool initialized = false;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
48 struct ShaderObject
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
49 {
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
50 std::unique_ptr<QOpenGLShaderProgram> program = nullptr;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
51 QOpenGLShaderProgram* pickSceneProgram = nullptr;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
52 QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer};
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
53 QOpenGLVertexArrayObject vertexArray;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
54 std::vector<Vertex> cachedData;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
55 std::size_t vertexCount;
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
56 } shaderObjects[gl::NUM_POLYGON_TYPES];
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
57 };
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
59 void build(
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
60 ModelShaders* shaders,
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
61 Model *model,
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
62 const ldraw::ColorTable& colorTable,
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
63 DocumentManager* context,
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
64 const RenderPreferences& preferences);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
65 void initializeModelShaders(ModelShaders* modelShaders);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
66 void bindModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
67 void releaseModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass);
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 189
diff changeset
68 void setModelShaderSelectedObjects(gl::ModelShaders* shaders, const QSet<ModelId>& ids);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
69 std::size_t vertexCount(const ModelShaders *shaders, gl::ArrayClass arrayClass);
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 189
diff changeset
70 ModelId idFromColor(const std::array<GLubyte, 3>& data);
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
71
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
72 template<typename T>
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
73 void setShaderUniform(gl::ModelShaders* shaders, const char* uniformName, T&& value)
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
74 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
75 for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects)
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
76 {
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
77 shader.program->bind();
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
78 const int location = glfunc.glGetUniformLocation(shader.program->programId(), uniformName);
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
79 Q_ASSERT(location != -1);
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
80 shader.program->setUniformValue(location, std::forward<T>(value));
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
81 shader.program->release();
27
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
82 }
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
83 }
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
84
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
85 template<typename Float, glm::qualifier Prec>
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
86 void setShaderUniformMatrix(
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
87 gl::ModelShaders* shaders,
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
88 const char* uniformName,
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
89 const glm::mat<4, 4, Float, Prec>& value)
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
90 {
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
91 const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value));
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
92 setShaderUniform(shaders, uniformName, *array);
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
93 }
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
94
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
95 BoundingBox boundingBoxForModel(Model* model, DocumentManager* context);
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
96 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 #define CHECK_GL_ERROR() { checkGLError(__FILE__, __LINE__); }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 void checkGLError (QString file, int line);

mercurial