src/gl/compiler.cpp

changeset 46
98645c8e7704
parent 43
08dc62e03a6d
child 47
cd6704009eb9
equal deleted inserted replaced
45:272c84c7c87e 46:98645c8e7704
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 id;
34 out vec4 vColor; 35 out vec4 vColor;
35 out vec3 vFragPos; 36 out vec3 vFragPos;
36 out vec3 vNormal; 37 out vec3 vNormal;
37 uniform mat4 modelMatrix; 38 uniform mat4 modelMatrix;
38 uniform mat4 viewMatrix; 39 uniform mat4 viewMatrix;
41 42
42 const int FRAGSTYLE_Normal = 0; 43 const int FRAGSTYLE_Normal = 0;
43 const int FRAGSTYLE_BfcGreen = 1; 44 const int FRAGSTYLE_BfcGreen = 1;
44 const int FRAGSTYLE_BfcRed = 2; 45 const int FRAGSTYLE_BfcRed = 2;
45 const int FRAGSTYLE_Random = 3; 46 const int FRAGSTYLE_Random = 3;
47 const int FRAGSTYLE_Id = 4;
46 48
47 void main() 49 void main()
48 { 50 {
49 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); 51 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
50 vNormal = normalize(normalMatrix * normal); 52 vNormal = normalize(normalMatrix * normal);
53 vColor = vec4(0.2, 0.9, 0.2, 1.0); 55 vColor = vec4(0.2, 0.9, 0.2, 1.0);
54 } 56 }
55 else if (fragmentStyle == FRAGSTYLE_BfcRed) 57 else if (fragmentStyle == FRAGSTYLE_BfcRed)
56 { 58 {
57 vColor = vec4(0.9, 0.2, 0.2, 1.0); 59 vColor = vec4(0.9, 0.2, 0.2, 1.0);
60 }
61 else if (fragmentStyle == FRAGSTYLE_Id)
62 {
63 vColor = vec4(id, 1.0);
58 } 64 }
59 else 65 else
60 { 66 {
61 vColor = color; 67 vColor = color;
62 } 68 }
73 in vec3 vNormal; 79 in vec3 vNormal;
74 out vec4 fColor; 80 out vec4 fColor;
75 const vec3 lightPos = vec3(0.5, 0.5, 0.5); 81 const vec3 lightPos = vec3(0.5, 0.5, 0.5);
76 const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0); 82 const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0);
77 const float ambientStrength = 0.7; 83 const float ambientStrength = 0.7;
84 uniform bool useLighting;
78 85
79 void main() 86 void main()
80 { 87 {
81 vec4 ambient = ambientStrength * lightColor; 88 if (useLighting)
82 vec3 lightDirection = normalize(lightPos - vFragPos); 89 {
83 vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor; 90 vec4 ambient = ambientStrength * lightColor;
84 fColor = (ambient + diffuse) * vColor; 91 vec3 lightDirection = normalize(lightPos - vFragPos);
92 vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor;
93 fColor = (ambient + diffuse) * vColor;
94 }
95 else
96 {
97 fColor = vColor;
98 }
85 } 99 }
86 )"; 100 )";
87 101
88 gl::Compiler::Compiler(const ldraw::ColorTable& colorTable, QObject* parent) : 102 gl::Compiler::Compiler(const ldraw::ColorTable& colorTable, QObject* parent) :
89 QObject{parent}, 103 QObject{parent},
129 object.vertexArray.create(); 143 object.vertexArray.create();
130 object.vertexArray.bind(); 144 object.vertexArray.bind();
131 object.program->enableAttributeArray(0); 145 object.program->enableAttributeArray(0);
132 object.program->enableAttributeArray(1); 146 object.program->enableAttributeArray(1);
133 object.program->enableAttributeArray(2); 147 object.program->enableAttributeArray(2);
148 object.program->enableAttributeArray(3);
134 constexpr int stride = sizeof(gl::Vertex); 149 constexpr int stride = sizeof(gl::Vertex);
135 object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride); 150 object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride);
136 object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride); 151 object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride);
137 object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(gl::Vertex, normal), 3, stride); 152 object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(gl::Vertex, normal), 3, stride);
153 object.program->setAttributeBuffer(3, GL_FLOAT, offsetof(gl::Vertex, id), 3, stride);
138 object.vertexArray.release(); 154 object.vertexArray.release();
139 object.buffer.release(); 155 object.buffer.release();
140 object.program->release(); 156 object.program->release();
141 } 157 }
142 this->initialized = true; 158 this->initialized = true;
177 return gl::ArrayClass::ConditionalLines; 193 return gl::ArrayClass::ConditionalLines;
178 } 194 }
179 return gl::ArrayClass::Lines; 195 return gl::ArrayClass::Lines;
180 } 196 }
181 197
182 [[maybe_unused]] 198 static glm::vec3 colorFromId(ldraw::Id id)
183 static QColor colorFromId(ldraw::Id id)
184 { 199 {
185 // Calculate a color based from this index. This method caters for 200 // Calculate a color based from this index. This method caters for
186 // 16777216 objects. I don't think that will be exceeded anytime soon. 201 // 16777216 objects. I don't think that will be exceeded anytime soon.
187 const int r = (id.value / 0x10000) % 0x100; 202 const int r = (id.value / 0x10000) % 0x100;
188 const int g = (id.value / 0x100) % 0x100; 203 const int g = (id.value / 0x100) % 0x100;
189 const int b = id.value % 0x100; 204 const int b = id.value % 0x100;
190 return {r, g, b}; 205 return {r / 255.0f, g / 255.0f, b / 255.0f};
191 } 206 }
192 207
193 void gl::Compiler::buildPolygon( 208 void gl::Compiler::buildPolygon(
194 gl::Polygon polygon, 209 gl::Polygon polygon,
195 std::vector<gl::Vertex>* vboData, 210 std::vector<gl::Vertex>* vboData,
208 this->boundingBox.consider(polygon.vertices[i]); 223 this->boundingBox.consider(polygon.vertices[i]);
209 gl::Vertex& vertex = vertexBuffer.emplace_back(); 224 gl::Vertex& vertex = vertexBuffer.emplace_back();
210 vertex.position = polygon.vertices[i]; 225 vertex.position = polygon.vertices[i];
211 vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2)); 226 vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2));
212 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; 227 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()};
228 vertex.id = colorFromId(polygon.id);
213 } 229 }
214 } 230 }
215 231
216 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, const gl::RenderPreferences& preferences) 232 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, const gl::RenderPreferences& preferences)
217 { 233 {

mercurial