29 #version 330 core |
29 #version 330 core |
30 |
30 |
31 layout(location=0) in vec3 position; |
31 layout(location=0) in vec3 position; |
32 layout(location=1) in vec4 color; |
32 layout(location=1) in vec4 color; |
33 layout(location=2) in vec3 normal; |
33 layout(location=2) in vec3 normal; |
34 layout(location=3) in vec3 idColor; |
34 layout(location=3) in int id; |
35 layout(location=4) in int id; |
|
36 out vec4 vColor; |
35 out vec4 vColor; |
37 out vec3 vFragPos; |
36 out vec3 vFragPos; |
38 out vec3 vNormal; |
37 out vec3 vNormal; |
39 uniform mat4 modelMatrix; |
38 uniform mat4 modelMatrix; |
40 uniform mat4 viewMatrix; |
39 uniform mat4 viewMatrix; |
53 { |
52 { |
54 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); |
53 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); |
55 vNormal = normalize(normalMatrix * normal); |
54 vNormal = normalize(normalMatrix * normal); |
56 if (fragmentStyle == FRAGSTYLE_Id) |
55 if (fragmentStyle == FRAGSTYLE_Id) |
57 { |
56 { |
58 vColor = vec4(idColor, 1.0); |
57 /* Calculate a color based from this index. This method caters for |
|
58 * 16777216 objects. I don't think that will be exceeded anytime soon. |
|
59 */ |
|
60 int r = (id / 0x10000) % 0x100; |
|
61 int g = (id / 0x100) % 0x100; |
|
62 int b = id % 0x100; |
|
63 vColor = vec4(r / 255.0, g / 255.0, b / 255.0, 1.0); |
59 } |
64 } |
60 else |
65 else |
61 { |
66 { |
62 if (fragmentStyle == FRAGSTYLE_BfcGreen) |
67 if (fragmentStyle == FRAGSTYLE_BfcGreen) |
63 { |
68 { |
151 object.buffer.create(); |
156 object.buffer.create(); |
152 object.buffer.bind(); |
157 object.buffer.bind(); |
153 object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); |
158 object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); |
154 object.vertexArray.create(); |
159 object.vertexArray.create(); |
155 object.vertexArray.bind(); |
160 object.vertexArray.bind(); |
156 for (int k : {0, 1, 2, 3, 4}) |
161 for (int k : {0, 1, 2, 3}) |
157 { |
162 { |
158 object.program->enableAttributeArray(k); |
163 object.program->enableAttributeArray(k); |
159 } |
164 } |
160 constexpr int stride = sizeof(gl::Vertex); |
165 constexpr int stride = sizeof(gl::Vertex); |
161 object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride); |
166 object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride); |
162 object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride); |
167 object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride); |
163 object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(gl::Vertex, normal), 3, stride); |
168 object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(gl::Vertex, normal), 3, stride); |
164 object.program->setAttributeBuffer(3, GL_FLOAT, offsetof(gl::Vertex, idColor), 3, stride); |
169 glVertexAttribIPointer(3, 1, GL_INT, stride, (void*)offsetof(gl::Vertex, id)); |
165 glVertexAttribIPointer(4, 1, GL_INT, stride, (void*)offsetof(gl::Vertex, id)); |
|
166 object.vertexArray.release(); |
170 object.vertexArray.release(); |
167 object.buffer.release(); |
171 object.buffer.release(); |
168 object.program->release(); |
172 object.program->release(); |
169 } |
173 } |
170 this->initialized = true; |
174 this->initialized = true; |
205 return gl::ArrayClass::ConditionalLines; |
209 return gl::ArrayClass::ConditionalLines; |
206 } |
210 } |
207 return gl::ArrayClass::Lines; |
211 return gl::ArrayClass::Lines; |
208 } |
212 } |
209 |
213 |
210 static glm::vec3 colorFromId(ldraw::Id id) |
|
211 { |
|
212 // Calculate a color based from this index. This method caters for |
|
213 // 16777216 objects. I don't think that will be exceeded anytime soon. |
|
214 const int r = (id.value / 0x10000) % 0x100; |
|
215 const int g = (id.value / 0x100) % 0x100; |
|
216 const int b = id.value % 0x100; |
|
217 return {r / 255.0f, g / 255.0f, b / 255.0f}; |
|
218 } |
|
219 |
|
220 ldraw::Id gl::Compiler::idFromColor(const std::array<GLbyte, 3>& data) |
214 ldraw::Id gl::Compiler::idFromColor(const std::array<GLbyte, 3>& data) |
221 { |
215 { |
222 return {data[0] * std::int32_t{0x10000} + data[1] * std::int32_t{0x100} + data[2]}; |
216 return {data[0] * std::int32_t{0x10000} + data[1] * std::int32_t{0x100} + data[2]}; |
223 } |
217 } |
224 |
218 |
240 this->boundingBox.consider(polygon.vertices[i]); |
234 this->boundingBox.consider(polygon.vertices[i]); |
241 gl::Vertex& vertex = vertexBuffer.emplace_back(); |
235 gl::Vertex& vertex = vertexBuffer.emplace_back(); |
242 vertex.position = polygon.vertices[i]; |
236 vertex.position = polygon.vertices[i]; |
243 vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2)); |
237 vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2)); |
244 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; |
238 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; |
245 vertex.idColor = colorFromId(polygon.id); |
|
246 vertex.id = polygon.id.value; |
239 vertex.id = polygon.id.value; |
247 } |
240 } |
248 } |
241 } |
249 |
242 |
250 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, const gl::RenderPreferences& preferences) |
243 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, const gl::RenderPreferences& preferences) |