src/gl/compiler.h

Fri, 06 Mar 2020 16:08:45 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 06 Mar 2020 16:08:45 +0200
changeset 68
ddb07bb6840c
parent 51
1a9eac27698d
child 73
97df974b5ed5
permissions
-rw-r--r--

default to XZ

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>
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
31 class Model;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
32 class DocumentManager;
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
33
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 namespace gl
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 class Compiler;
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
37
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
38 struct Vertex
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
39 {
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
40 glm::vec3 position;
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
41 glm::vec4 color;
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
42 glm::vec3 normal;
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
43 glm::int32 id;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
44 glm::int32 selected = 0;
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
45 };
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
48 class gl::Compiler : public QObject, protected QOpenGLExtraFunctions
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 {
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 Q_OBJECT
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 public:
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
52 Compiler(const ldraw::ColorTable& colorTable, QObject* parent);
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 ~Compiler();
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
54 void build(Model* model, DocumentManager* context, const RenderPreferences& preferences);
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
55 void buildPolygon(Polygon polygon, std::vector<Vertex>* vboData, const gl::RenderPreferences& preferences);
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
56 std::size_t vertexCount(gl::ArrayClass arrayClass) const;
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
57 QColor getColorForPolygon(const gl::Polygon& polygon, const RenderPreferences& preferences);
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 30
diff changeset
58 glm::vec3 modelCenter() const;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
59 double modelDistance() const;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
60 void initialize();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
61 void bindVertexArray(gl::ArrayClass arrayClass);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
62 void releaseVertexArray(gl::ArrayClass arrayClass);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
63 void buildShaders(int arrayId);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
64 void setSelectedObjects(const QSet<ldraw::Id> ids);
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
65
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
66 static ldraw::Id idFromColor(const std::array<GLbyte, 3>& data);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
67
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
68 template<typename T>
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
69 void setUniform(const char* uniformName, T&& value)
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
70 {
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
71 Q_ASSERT(this->initialized);
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 for (auto& object : this->glObjects)
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
73 {
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 object.program->bind();
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
75 const int location = glGetUniformLocation(object.program->programId(), uniformName);
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 Q_ASSERT(location != -1);
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
77 object.program->setUniformValue(location, std::forward<T>(value));
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
78 object.program->release();
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 }
c57fb7a5ffa3 commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
80 }
30
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
81
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
82 template<typename Float, glm::qualifier Prec>
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
83 void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value)
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 const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value));
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
86 this->setUniform(uniformName, *array);
1536f23cfab7 it works!
Teemu Piippo <teemu@hecknology.net>
parents: 28
diff changeset
87 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 private:
34
1de2b8d64e9f added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
89 std::size_t storedVertexCounts[gl::NUM_ARRAY_CLASSES] = {0_z};
21
Teemu Piippo <teemu@hecknology.net>
parents: 19
diff changeset
90 bool initialized = false;
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
91 BoundingBox boundingBox;
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 34
diff changeset
92 const ldraw::ColorTable& colorTable;
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
93 ldraw::Id hovered = ldraw::NULL_ID;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
94 struct
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
95 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
96 QOpenGLShaderProgram* program = nullptr;
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
97 QOpenGLShaderProgram* pickSceneProgram = nullptr;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
98 QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
99 QOpenGLVertexArrayObject vertexArray;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 50
diff changeset
100 std::vector<gl::Vertex> cachedData;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
101 } glObjects[gl::NUM_ARRAY_CLASSES];
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 };
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 #define CHECK_GL_ERROR() { checkGLError(__FILE__, __LINE__); }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 void checkGLError (QString file, int line);

mercurial