--- a/src/gl/compiler.cpp Sun Feb 02 00:58:59 2020 +0200 +++ b/src/gl/compiler.cpp Thu Feb 06 20:33:05 2020 +0200 @@ -31,6 +31,7 @@ layout(location=0) in vec3 position; layout(location=1) in vec4 color; layout(location=2) in vec3 normal; +layout(location=3) in vec3 id; out vec4 vColor; out vec3 vFragPos; out vec3 vNormal; @@ -43,6 +44,7 @@ const int FRAGSTYLE_BfcGreen = 1; const int FRAGSTYLE_BfcRed = 2; const int FRAGSTYLE_Random = 3; +const int FRAGSTYLE_Id = 4; void main() { @@ -56,6 +58,10 @@ { vColor = vec4(0.9, 0.2, 0.2, 1.0); } + else if (fragmentStyle == FRAGSTYLE_Id) + { + vColor = vec4(id, 1.0); + } else { vColor = color; @@ -75,13 +81,21 @@ const vec3 lightPos = vec3(0.5, 0.5, 0.5); const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0); const float ambientStrength = 0.7; +uniform bool useLighting; void main() { - vec4 ambient = ambientStrength * lightColor; - vec3 lightDirection = normalize(lightPos - vFragPos); - vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor; - fColor = (ambient + diffuse) * vColor; + if (useLighting) + { + vec4 ambient = ambientStrength * lightColor; + vec3 lightDirection = normalize(lightPos - vFragPos); + vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor; + fColor = (ambient + diffuse) * vColor; + } + else + { + fColor = vColor; + } } )"; @@ -131,10 +145,12 @@ object.program->enableAttributeArray(0); object.program->enableAttributeArray(1); object.program->enableAttributeArray(2); + object.program->enableAttributeArray(3); constexpr int stride = sizeof(gl::Vertex); object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride); object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride); object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(gl::Vertex, normal), 3, stride); + object.program->setAttributeBuffer(3, GL_FLOAT, offsetof(gl::Vertex, id), 3, stride); object.vertexArray.release(); object.buffer.release(); object.program->release(); @@ -179,15 +195,14 @@ return gl::ArrayClass::Lines; } -[[maybe_unused]] -static QColor colorFromId(ldraw::Id id) +static glm::vec3 colorFromId(ldraw::Id id) { // Calculate a color based from this index. This method caters for // 16777216 objects. I don't think that will be exceeded anytime soon. const int r = (id.value / 0x10000) % 0x100; const int g = (id.value / 0x100) % 0x100; const int b = id.value % 0x100; - return {r, g, b}; + return {r / 255.0f, g / 255.0f, b / 255.0f}; } void gl::Compiler::buildPolygon( @@ -210,6 +225,7 @@ vertex.position = polygon.vertices[i]; vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2)); vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; + vertex.id = colorFromId(polygon.id); } }