Sun, 26 Jan 2020 00:55:36 +0200
fix remaining rendering control issues
19 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #pragma once | |
20 | #include "main.h" | |
21 | #include "gl/common.h" | |
22 | #include "types/boundingbox.h" | |
28 | 23 | #include <glm/glm.hpp> |
30 | 24 | #include <glm/gtc/type_ptr.hpp> |
19 | 25 | #include <QMap> |
26 | #include <QSet> | |
26 | 27 | #include <QOpenGLVertexArrayObject> |
28 | #include <QOpenGLBuffer> | |
29 | #include <QOpenGLShaderProgram> | |
19 | 30 | |
21 | 31 | class Model; |
32 | class DocumentManager; | |
33 | ||
19 | 34 | namespace gl |
35 | { | |
36 | class Compiler; | |
37 | class Renderer; | |
21 | 38 | struct VboAddress |
39 | { | |
26 | 40 | ArrayClass vboClass; |
21 | 41 | VboSubclass vboSubclass; |
42 | }; | |
43 | int vboIndex(const VboAddress vboAddress); | |
28 | 44 | QMatrix4x4 toQMatrix(const glm::mat4& matrix); |
19 | 45 | } |
46 | ||
47 | class gl::Compiler : public QObject, protected QOpenGLFunctions | |
48 | { | |
49 | Q_OBJECT | |
50 | public: | |
26 | 51 | Compiler(const ColorTable& colorTable, QObject* parent); |
19 | 52 | ~Compiler(); |
21 | 53 | void build(Model* model, DocumentManager* context); |
54 | void buildPolygon(Polygon polygon, std::vector<GLfloat>* vboData); | |
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
|
55 | std::size_t vboSize(gl::ArrayClass arrayClass) const; |
26 | 56 | QColor getColorForPolygon(const gl::Polygon& polygon); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
57 | Point3D modelCenter() const; |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
58 | double modelDistance() const; |
26 | 59 | void initialize(); |
60 | void bindVertexArray(gl::ArrayClass arrayClass); | |
61 | void releaseVertexArray(gl::ArrayClass arrayClass); | |
62 | void buildShaders(int arrayId); | |
30 | 63 | |
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
|
64 | 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
|
65 | 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
|
66 | { |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
67 | 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
|
68 | 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
|
69 | { |
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 | 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
|
71 | 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
|
72 | 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
|
73 | 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
|
74 | 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
|
75 | } |
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 | } |
30 | 77 | |
78 | template<typename Float, glm::qualifier Prec> | |
79 | void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value) | |
80 | { | |
81 | const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value)); | |
82 | this->setUniform(uniformName, *array); | |
83 | } | |
19 | 84 | private: |
85 | bool m_vboChanged[gl::numVbos] = {true}; | |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
86 | std::size_t storedVboSizes[gl::numVbos] = {0_z}; |
21 | 87 | bool initialized = false; |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
88 | BoundingBox boundingBox; |
26 | 89 | const ColorTable& colorTable; |
90 | struct | |
91 | { | |
92 | QOpenGLShaderProgram* program = nullptr; | |
93 | QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer}; | |
94 | QOpenGLVertexArrayObject vertexArray; | |
95 | } glObjects[gl::NUM_ARRAY_CLASSES]; | |
19 | 96 | }; |
97 | ||
98 | #define CHECK_GL_ERROR() { checkGLError(__FILE__, __LINE__); } | |
99 | void checkGLError (QString file, int line); |