26 #version 330 core |
26 #version 330 core |
27 |
27 |
28 layout(location=0) in vec3 position; |
28 layout(location=0) in vec3 position; |
29 layout(location=1) in vec4 color; |
29 layout(location=1) in vec4 color; |
30 layout(location=2) in vec3 normal; |
30 layout(location=2) in vec3 normal; |
31 layout(location=3) in int id; |
31 layout(location=3) in vec3 pickcolor; |
32 layout(location=4) in int selected; |
32 layout(location=4) in float selected; |
33 out vec4 vColor; |
33 out vec4 vColor; |
34 out vec3 vFragPos; |
34 out vec3 vFragPos; |
35 out vec3 vNormal; |
35 out vec3 vNormal; |
36 uniform mat4 modelMatrix; |
36 uniform mat4 modelMatrix; |
37 uniform mat4 viewMatrix; |
37 uniform mat4 viewMatrix; |
38 uniform mat4 projectionMatrix; |
38 uniform mat4 projectionMatrix; |
39 uniform int fragmentStyle; |
39 uniform int fragmentStyle; |
40 uniform vec3 selectedColor; |
40 uniform vec4 selectedColor; |
41 uniform int highlighted; |
41 uniform int highlighted; |
42 |
42 |
43 const int FRAGSTYLE_Normal = 0; |
43 const int FRAGSTYLE_Normal = 0; |
44 const int FRAGSTYLE_BfcGreen = 1; |
44 const int FRAGSTYLE_BfcGreen = 1; |
45 const int FRAGSTYLE_BfcRed = 2; |
45 const int FRAGSTYLE_BfcRed = 2; |
51 { |
51 { |
52 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); |
52 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); |
53 vNormal = normalize(normalMatrix * normal); |
53 vNormal = normalize(normalMatrix * normal); |
54 if (fragmentStyle == FRAGSTYLE_Id) |
54 if (fragmentStyle == FRAGSTYLE_Id) |
55 { |
55 { |
56 /* Calculate a color based from this index. This method caters for |
56 vColor = vec4(pickcolor, 1.0); |
57 * 16777216 objects. I don't think that will be exceeded anytime soon. |
|
58 */ |
|
59 int r = (id / 0x10000) % 0x100; |
|
60 int g = (id / 0x100) % 0x100; |
|
61 int b = id % 0x100; |
|
62 vColor = vec4(r / 255.0, g / 255.0, b / 255.0, 1.0); |
|
63 } |
|
64 else if (selected == 1) |
|
65 { |
|
66 vColor = vec4(selectedColor, 1.0); |
|
67 } |
57 } |
68 else |
58 else |
69 { |
59 { |
70 if (fragmentStyle == FRAGSTYLE_BfcGreen) |
60 if (fragmentStyle == FRAGSTYLE_BfcGreen) |
71 { |
61 { |
204 shader.vertexArray.bind(); |
196 shader.vertexArray.bind(); |
205 for (int k : {0, 1, 2, 3, 4}) { |
197 for (int k : {0, 1, 2, 3, 4}) { |
206 shader.program->enableAttributeArray(k); |
198 shader.program->enableAttributeArray(k); |
207 } |
199 } |
208 using Vertex = ModelShaders::Vertex; |
200 using Vertex = ModelShaders::Vertex; |
209 constexpr int stride = sizeof(Vertex); |
201 constexpr std::size_t stride = sizeof(Vertex); |
210 shader.program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride); |
202 shader.program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride); |
211 shader.program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 4, stride); |
203 shader.program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 4, stride); |
212 shader.program->setAttributeBuffer(2, GL_FLOAT, offsetof(Vertex, normal), 3, stride); |
204 shader.program->setAttributeBuffer(2, GL_FLOAT, offsetof(Vertex, normal), 3, stride); |
213 const void* vertexptr = reinterpret_cast<const void*>(offsetof(Vertex, id)); |
205 shader.program->setAttributeBuffer(3, GL_FLOAT, offsetof(Vertex, pickcolor), 3, stride); |
214 glfunc().glVertexAttribPointer(3, 1, GL_INT, GL_FALSE, stride, vertexptr); |
206 shader.program->setAttributeBuffer(4, GL_FLOAT, offsetof(Vertex, selected), 1, stride); |
215 const void* selectedptr = reinterpret_cast<const void*>(offsetof(Vertex, selected)); |
|
216 glfunc().glVertexAttribPointer(4, 1, GL_INT, GL_FALSE, stride, selectedptr); |
|
217 shader.vertexArray.release(); |
207 shader.vertexArray.release(); |
218 shader.buffer.release(); |
208 shader.buffer.release(); |
219 shader.program->release(); |
209 shader.program->release(); |
220 } |
210 } |
221 modelShaders->initialized = true; |
211 modelShaders->initialized = true; |
305 gl::ModelShaders::Vertex& vertex = vertexBuffer.emplace_back(); |
295 gl::ModelShaders::Vertex& vertex = vertexBuffer.emplace_back(); |
306 vertex.position = point; |
296 vertex.position = point; |
307 vertex.normal = normal; |
297 vertex.normal = normal; |
308 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; |
298 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; |
309 vertex.id = polygon.id.value; |
299 vertex.id = polygon.id.value; |
|
300 vertex.pickcolor = idToColor(polygon.id.value); |
310 }); |
301 }); |
311 }); |
302 }); |
312 for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) |
303 for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) |
313 { |
304 { |
314 shader.vertexCount = shader.cachedData.size(); |
305 shader.vertexCount = shader.cachedData.size(); |
317 shader.buffer.allocate(shader.cachedData.data(), bytes); |
308 shader.buffer.allocate(shader.cachedData.data(), bytes); |
318 shader.buffer.release(); |
309 shader.buffer.release(); |
319 } |
310 } |
320 } |
311 } |
321 |
312 |
322 ModelId gl::idFromColor(const std::array<GLubyte, 3>& data) |
313 ModelId gl::idFromUcharColor(const std::array<GLubyte, 3>& data) |
323 { |
314 { |
324 return {data[0] * std::int32_t{0x10000} + data[1] * std::int32_t{0x100} + data[2]}; |
315 return { |
|
316 static_cast<std::int32_t>(data[0]) | |
|
317 static_cast<std::int32_t>(data[1]) << 8 | |
|
318 static_cast<std::int32_t>(data[2]) << 16 |
|
319 }; |
325 } |
320 } |
326 |
321 |
327 void gl::bindModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass) |
322 void gl::bindModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass) |
328 { |
323 { |
329 ModelShaders::ShaderObject& shaderObject = shaders->shaderObjects[static_cast<int>(arrayClass)]; |
324 ModelShaders::ShaderObject& shaderObject = shaders->shaderObjects[static_cast<int>(arrayClass)]; |