src/gl/compiler.cpp

changeset 48
3c10f0e2fbe0
parent 47
cd6704009eb9
child 49
d56cc7387dad
equal deleted inserted replaced
47:cd6704009eb9 48:3c10f0e2fbe0
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 layout(location=3) in vec3 idColor;
35 layout(location=4) in int id;
35 out vec4 vColor; 36 out vec4 vColor;
36 out vec3 vFragPos; 37 out vec3 vFragPos;
37 out vec3 vNormal; 38 out vec3 vNormal;
38 uniform mat4 modelMatrix; 39 uniform mat4 modelMatrix;
39 uniform mat4 viewMatrix; 40 uniform mat4 viewMatrix;
40 uniform mat4 projectionMatrix; 41 uniform mat4 projectionMatrix;
41 uniform int fragmentStyle; 42 uniform int fragmentStyle;
43 uniform vec3 selectedColor;
44 uniform int highlighted;
42 45
43 const int FRAGSTYLE_Normal = 0; 46 const int FRAGSTYLE_Normal = 0;
44 const int FRAGSTYLE_BfcGreen = 1; 47 const int FRAGSTYLE_BfcGreen = 1;
45 const int FRAGSTYLE_BfcRed = 2; 48 const int FRAGSTYLE_BfcRed = 2;
46 const int FRAGSTYLE_Random = 3; 49 const int FRAGSTYLE_Random = 3;
48 51
49 void main() 52 void main()
50 { 53 {
51 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); 54 mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
52 vNormal = normalize(normalMatrix * normal); 55 vNormal = normalize(normalMatrix * normal);
53 if (fragmentStyle == FRAGSTYLE_BfcGreen) 56 if (fragmentStyle == FRAGSTYLE_Id)
54 { 57 {
55 vColor = vec4(0.2, 0.9, 0.2, 1.0); 58 vColor = vec4(idColor, 1.0);
56 }
57 else if (fragmentStyle == FRAGSTYLE_BfcRed)
58 {
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);
64 } 59 }
65 else 60 else
66 { 61 {
67 vColor = color; 62 if (fragmentStyle == FRAGSTYLE_BfcGreen)
68 } 63 {
64 vColor = vec4(0.2, 0.9, 0.2, 1.0);
65 }
66 else if (fragmentStyle == FRAGSTYLE_BfcRed)
67 {
68 vColor = vec4(0.9, 0.2, 0.2, 1.0);
69 }
70 else
71 {
72 vColor = color;
73 }
74 if (highlighted == id)
75 {
76 vColor = (vColor + vec4(selectedColor, 1.0) * 0.6) / 1.6;
77 }
78 }
79
69 vFragPos = vec3(modelMatrix * vec4(position, 1.0)); 80 vFragPos = vec3(modelMatrix * vec4(position, 1.0));
70 gl_Position = projectionMatrix * viewMatrix * vec4(vFragPos, 1.0); 81 gl_Position = projectionMatrix * viewMatrix * vec4(vFragPos, 1.0);
71 } 82 }
72 )"; 83 )";
73 84
140 object.buffer.create(); 151 object.buffer.create();
141 object.buffer.bind(); 152 object.buffer.bind();
142 object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); 153 object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
143 object.vertexArray.create(); 154 object.vertexArray.create();
144 object.vertexArray.bind(); 155 object.vertexArray.bind();
145 object.program->enableAttributeArray(0); 156 for (int k : {0, 1, 2, 3, 4})
146 object.program->enableAttributeArray(1); 157 {
147 object.program->enableAttributeArray(2); 158 object.program->enableAttributeArray(k);
148 object.program->enableAttributeArray(3); 159 }
149 constexpr int stride = sizeof(gl::Vertex); 160 constexpr int stride = sizeof(gl::Vertex);
150 object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride); 161 object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride);
151 object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride); 162 object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride);
152 object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(gl::Vertex, normal), 3, stride); 163 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); 164 object.program->setAttributeBuffer(3, GL_FLOAT, offsetof(gl::Vertex, idColor), 3, stride);
165 glVertexAttribIPointer(4, 1, GL_INT, stride, (void*)offsetof(gl::Vertex, id));
154 object.vertexArray.release(); 166 object.vertexArray.release();
155 object.buffer.release(); 167 object.buffer.release();
156 object.program->release(); 168 object.program->release();
157 } 169 }
158 this->initialized = true; 170 this->initialized = true;
228 this->boundingBox.consider(polygon.vertices[i]); 240 this->boundingBox.consider(polygon.vertices[i]);
229 gl::Vertex& vertex = vertexBuffer.emplace_back(); 241 gl::Vertex& vertex = vertexBuffer.emplace_back();
230 vertex.position = polygon.vertices[i]; 242 vertex.position = polygon.vertices[i];
231 vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2)); 243 vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2));
232 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; 244 vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()};
233 vertex.id = colorFromId(polygon.id); 245 vertex.idColor = colorFromId(polygon.id);
246 vertex.id = polygon.id.value;
234 } 247 }
235 } 248 }
236 249
237 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, const gl::RenderPreferences& preferences) 250 QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, const gl::RenderPreferences& preferences)
238 { 251 {

mercurial