src/gl/compiler.cpp

changeset 27
c57fb7a5ffa3
parent 26
3a9e761e4faa
child 28
c92c1daf735f
equal deleted inserted replaced
26:3a9e761e4faa 27:c57fb7a5ffa3
23 #include "gl/compiler.h" 23 #include "gl/compiler.h"
24 #include "documentmanager.h" 24 #include "documentmanager.h"
25 #include "invert.h" 25 #include "invert.h"
26 #include "ring.h" 26 #include "ring.h"
27 27
28 static const char* vertexShaderSource = R"(
29 #version 330 core
30
31 layout(location=0) in vec3 position;
32 layout(location=1) in vec4 color;
33 out vec4 vColor;
34 uniform mat4 CameraTransformation;
35
36 void main()
37 {
38 vColor = color;
39 gl_Position = projection * modelview * CameraTransformation * vec4(position, 1.0);
40 }
41 )";
42
43 static const char* fragmentShaderSource = R"(
44 #version 330 core
45
46 in vec4 vColor;
47 out vec4 fColor;
48
49 void main()
50 {
51 fColor = vColor;
52 }
53 )";
54
28 gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) : 55 gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) :
29 QObject{parent}, 56 QObject{parent},
30 colorTable{colorTable} 57 colorTable{colorTable}
31 { 58 {
32 } 59 }
33 60
34 gl::Compiler::~Compiler() 61 gl::Compiler::~Compiler()
35 { 62 {
36 if (this->initialized) 63 }
37 { 64
38 for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1) 65 void gl::Compiler::initialize()
66 {
67 if (not this->initialized)
68 {
69 this->initializeOpenGLFunctions();
70 for (int i = 0; i < gl::NUM_ARRAY_CLASSES; i += 1)
39 { 71 {
40 glDeleteProgram(this->glObjects[arrayId].program); 72 auto& object = this->glObjects[i];
41 glDeleteShader(this->glObjects[arrayId].vertexShader); 73 object.program = new QOpenGLShaderProgram;
42 glDeleteProgram(this->glObjects[arrayId].fragmentShader); 74 object.program->create();
75 object.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource);
76 object.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource);
77 object.program->link();
78 object.program->bind();
79 object.buffer.create();
80 object.buffer.bind();
81 object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
82 object.vertexArray.create();
83 object.vertexArray.bind();
84 object.program->enableAttributeArray(0);
85 object.program->enableAttributeArray(1);
86 object.program->setAttributeBuffer(0, GL_FLOAT, 0, 3);
87 object.program->setAttributeBuffer(1, GL_FLOAT, 3, 4);
88 object.vertexArray.release();
89 object.buffer.release();
90 object.program->release();
43 } 91 }
44 }
45 }
46
47 void gl::Compiler::initialize()
48 {
49 if (not this->initialized)
50 {
51 this->initializeOpenGLFunctions();
52 for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
53 {
54 QOpenGLVertexArrayObject& vao = this->vertexArrays[arrayId];
55 vao.create();
56 vao.bind();
57 glGenBuffers(gl::numVboSubclasses, &this->storedVbo[gl::numVboSubclasses * arrayId]);
58 this->buildShaders(arrayId);
59 vao.release();
60 }
61 //glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]);
62 this->initialized = true; 92 this->initialized = true;
63 } 93 }
64 } 94 }
65 95
66 void gl::Compiler::build(Model* model, DocumentManager* context) 96 void gl::Compiler::build(Model* model, DocumentManager* context)
67 { 97 {
68 this->boundingBox = {}; 98 this->boundingBox = {};
69 std::vector<GLfloat> vboData[gl::numVbos]; 99 std::vector<GLfloat> vboData[gl::NUM_ARRAY_CLASSES];
70 const std::vector<gl::Polygon> polygons = model->getPolygons(context); 100 const std::vector<gl::Polygon> polygons = model->getPolygons(context);
71 for (const gl::Polygon& polygon : polygons) 101 for (const gl::Polygon& polygon : polygons)
72 { 102 {
73 this->buildPolygon(polygon, vboData); 103 this->buildPolygon(polygon, vboData);
74 } 104 }
75 /*
76 for (int index = 0; index < gl::numVbos; index += 1)
77 {
78 this->upload(index, vboData[index]);
79 }*/
80 for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1) 105 for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
81 { 106 {
82 this->vertexArrays[arrayId].bind(); 107 auto& buffer = this->glObjects[arrayId].buffer;
83 for (int i = 0; i < gl::numVboSubclasses; i += 1) 108 auto& vector = vboData[arrayId];
84 { 109 this->storedVboSizes[arrayId] = vector.size();
85 const int vboIndex = gl::vboIndex({static_cast<gl::ArrayClass>(arrayId), static_cast<gl::VboSubclass>(i)}); 110 buffer.bind();
86 this->upload(vboIndex, vboData[vboIndex]); 111 buffer.allocate(vector.data(), static_cast<int>(vector.size() * sizeof vector[0]));
87 } 112 buffer.release();
88 this->vertexArrays[arrayId].release();
89 } 113 }
90 } 114 }
91 115
92 gl::ArrayClass classifyPolygon(const gl::Polygon& polygon) 116 gl::ArrayClass classifyPolygon(const gl::Polygon& polygon)
93 { 117 {
117 } 141 }
118 142
119 void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData) 143 void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData)
120 { 144 {
121 const gl::ArrayClass vboClass = classifyPolygon(polygon); 145 const gl::ArrayClass vboClass = classifyPolygon(polygon);
122 std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})]; 146 //std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})];
147 std::vector<GLfloat>& vertexBuffer = vboData[static_cast<int>(vboClass)];
148 /*
123 std::vector<GLfloat>& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})]; 149 std::vector<GLfloat>& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})];
124 auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); 150 auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
125 reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z); 151 reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z);
126 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) 152 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
127 { 153 {
130 const Point3D& v3 = vertexRing[i + 1]; 156 const Point3D& v3 = vertexRing[i + 1];
131 const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized(); 157 const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized();
132 for (const GLfloat coord : {normal.x(), normal.y(), normal.z()}) 158 for (const GLfloat coord : {normal.x(), normal.y(), normal.z()})
133 normalsBuffer.push_back(coord); 159 normalsBuffer.push_back(coord);
134 } 160 }
161 */
135 reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7); 162 reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7);
136 const QColor color = this->getColorForPolygon(polygon); 163 const QColor color = this->getColorForPolygon(polygon);
137 // Transform vertices so that they're suitable for GL rendering 164 // Transform vertices so that they're suitable for GL rendering
138 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) 165 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
139 { 166 {
182 return longestMeasure(this->boundingBox); 209 return longestMeasure(this->boundingBox);
183 } 210 }
184 211
185 void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass) 212 void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass)
186 { 213 {
187 this->vertexArrays[static_cast<int>(arrayClass)].bind(); 214 auto& object = this->glObjects[static_cast<int>(arrayClass)];
188 glUseProgram(this->glObjects[static_cast<int>(arrayClass)].program); 215 object.vertexArray.bind();
216 object.program->bind();
189 } 217 }
190 218
191 void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass) 219 void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass)
192 { 220 {
193 this->vertexArrays[static_cast<int>(arrayClass)].release(); 221 auto& object = this->glObjects[static_cast<int>(arrayClass)];
194 } 222 object.program->release();
195 223 object.vertexArray.release();
196 void gl::Compiler::buildShaders(int arrayId) 224 }
197 { 225
198 /* 226 std::size_t gl::Compiler::vboSize(const gl::ArrayClass arrayClass) const
199 this->glObjects[arrayId].vertexShader = glCreateShader(GL_VERTEX_SHADER); 227 {
200 glShaderSource(this->glObjects[arrayId].vertexShader, 1, &vertexShaderSource, nullptr); 228 return this->storedVboSizes[static_cast<int>(arrayClass)];
201 glCompileShader(this->glObjects[arrayId].vertexShader);
202 this->glObjects[arrayId].fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
203 glShaderSource(this->glObjects[arrayId].fragmentShader, 1, &fragmentShaderSource, nullptr);
204 glCompileShader(this->glObjects[arrayId].fragmentShader);
205 for (auto&& pair : {
206 std::make_pair(this->glObjects[arrayId].vertexShader, tr("vertex shader")),
207 std::make_pair(this->glObjects[arrayId].fragmentShader, tr("fragment shader")),
208 })
209 {
210 GLint status;
211 glGetShaderiv(this->glObjects[arrayId].fragmentShader, GL_COMPILE_STATUS, &status);
212 if (status != GL_TRUE)
213 {
214 char compileLog[512];
215 glGetShaderInfoLog(pair.first, countof(compileLog), nullptr, compileLog);
216 QMessageBox::critical(nullptr, tr("Shader compile error"), tr("Unable to compile the %1. Compile log:\n\n%2").arg(pair.second).arg(compileLog));
217 abort();
218 }
219 }
220 this->glObjects[arrayId].program = glCreateProgram();
221 glAttachShader(this->glObjects[arrayId].program, this->glObjects[arrayId].vertexShader);
222 glAttachShader(this->glObjects[arrayId].program, this->glObjects[arrayId].fragmentShader);
223 glLinkProgram(this->glObjects[arrayId].program);
224 glUseProgram(this->glObjects[arrayId].program);
225 const std::size_t size = gl::FLOATS_PER_VERTEX * sizeof(GLfloat);
226 const GLuint posAttrib = static_cast<GLuint>(glGetAttribLocation(this->glObjects[arrayId].program, "position"));
227 glEnableVertexAttribArray(posAttrib);
228 glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, size, gl::offset(0));
229 const GLuint colAttrib = static_cast<GLuint>(glGetAttribLocation(this->glObjects[arrayId].program, "color"));
230 glEnableVertexAttribArray(colAttrib);
231 glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, size, gl::offset(3 * sizeof(GLfloat)));
232 */
233 }
234
235 ///
236 /// \brief Uploads data to a vbo
237 /// \param vboAddress Which vbo to upload to
238 /// \param data Data to upload to vbo
239 ///
240 void gl::Compiler::upload(const int vboIndex, const std::vector<GLfloat>& data)
241 {
242 glBindBuffer(GL_ARRAY_BUFFER, this->storedVbo[vboIndex]);
243 glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof data[0]), data.data(), GL_STATIC_DRAW);
244 this->storedVboSizes[vboIndex] = data.size();
245 }
246
247 GLuint gl::Compiler::vbo(const VboAddress vboAddress) const
248 {
249 return this->storedVbo[gl::vboIndex(vboAddress)];
250 }
251
252 std::size_t gl::Compiler::vboSize(const VboAddress vboAddress) const
253 {
254 return this->storedVboSizes[gl::vboIndex(vboAddress)];
255 } 229 }
256 230
257 int gl::vboIndex(const VboAddress vboAddress) 231 int gl::vboIndex(const VboAddress vboAddress)
258 { 232 {
259 return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses 233 return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses

mercurial