--- a/src/gl/compiler.cpp Sun Jan 26 14:29:30 2020 +0200 +++ b/src/gl/compiler.cpp Tue Jan 28 23:34:49 2020 +0200 @@ -30,13 +30,21 @@ layout(location=0) in vec3 position; layout(location=1) in vec4 color; +layout(location=2) in vec3 normal; out vec4 vColor; -uniform mat4 CameraTransformation; +out vec3 vFragPos; +out vec3 vNormal; +uniform mat4 modelMatrix; +uniform mat4 viewMatrix; +uniform mat4 projectionMatrix; void main() { + mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); + vNormal = normalize(normalMatrix * normal); vColor = color; - gl_Position = CameraTransformation * vec4(position, 1.0); + vFragPos = vec3(modelMatrix * vec4(position, 1.0)); + gl_Position = projectionMatrix * viewMatrix * vec4(vFragPos, 1.0); } )"; @@ -44,11 +52,19 @@ #version 330 core in vec4 vColor; +in vec3 vFragPos; +in vec3 vNormal; out vec4 fColor; +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; void main() { - fColor = vColor; + vec4 ambient = ambientStrength * lightColor; + vec3 lightDirection = normalize(lightPos - vFragPos); + vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor; + fColor = (ambient + diffuse) * vColor; } )"; @@ -97,9 +113,11 @@ object.vertexArray.bind(); object.program->enableAttributeArray(0); object.program->enableAttributeArray(1); - constexpr int stride = 7 * sizeof(GLfloat); - object.program->setAttributeBuffer(0, GL_FLOAT, 0 * sizeof(GLfloat), 3, stride); - object.program->setAttributeBuffer(1, GL_FLOAT, 3 * sizeof(GLfloat), 4, stride); + object.program->enableAttributeArray(2); + 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.vertexArray.release(); object.buffer.release(); object.program->release(); @@ -111,7 +129,7 @@ void gl::Compiler::build(Model* model, DocumentManager* context) { this->boundingBox = {}; - std::vector<GLfloat> vboData[gl::NUM_ARRAY_CLASSES]; + std::vector<gl::Vertex> vboData[gl::NUM_ARRAY_CLASSES]; const std::vector<gl::Polygon> polygons = model->getPolygons(context); for (const gl::Polygon& polygon : polygons) { @@ -121,7 +139,7 @@ { auto& buffer = this->glObjects[arrayId].buffer; auto& vector = vboData[arrayId]; - this->storedVboSizes[arrayId] = vector.size(); + this->storedVertexCounts[arrayId] = vector.size(); buffer.bind(); buffer.allocate(vector.data(), static_cast<int>(vector.size() * sizeof vector[0])); buffer.release(); @@ -155,39 +173,23 @@ return {r, g, b}; } -void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData) +void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<gl::Vertex>* vboData) { const gl::ArrayClass vboClass = classifyPolygon(polygon); - //std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})]; - std::vector<GLfloat>& vertexBuffer = vboData[static_cast<int>(vboClass)]; - /* - std::vector<GLfloat>& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})]; + std::vector<gl::Vertex>& vertexBuffer = vboData[static_cast<int>(vboClass)]; auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); - reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z); + reserveMore(vertexBuffer, polygon.numPolygonVertices()); + const QColor color = this->getColorForPolygon(polygon); for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) { const glm::vec3& v1 = vertexRing[i - 1]; const glm::vec3& v2 = vertexRing[i]; const glm::vec3& v3 = vertexRing[i + 1]; - const QVector3D normal = QVector3D::crossProduct(v3 - v2, v1 - v2).normalized(); - for (const GLfloat coord : {normal.x(), normal.y(), normal.z()}) - normalsBuffer.push_back(coord); - } - */ - reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7); - const QColor color = this->getColorForPolygon(polygon); - // Transform vertices so that they're suitable for GL rendering - for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) - { - glm::vec3& point = polygon.vertices[i]; this->boundingBox.consider(polygon.vertices[i]); - vertexBuffer.push_back(static_cast<GLfloat>(point.x)); - vertexBuffer.push_back(static_cast<GLfloat>(point.y)); - vertexBuffer.push_back(static_cast<GLfloat>(point.z)); - vertexBuffer.push_back(static_cast<GLfloat>(color.redF())); - vertexBuffer.push_back(static_cast<GLfloat>(color.greenF())); - vertexBuffer.push_back(static_cast<GLfloat>(color.blueF())); - vertexBuffer.push_back(static_cast<GLfloat>(color.alphaF())); + gl::Vertex& vertex = vertexBuffer.emplace_back(); + 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()}; } } @@ -236,35 +238,7 @@ object.vertexArray.release(); } -std::size_t gl::Compiler::vboSize(const gl::ArrayClass arrayClass) const -{ - return this->storedVboSizes[static_cast<int>(arrayClass)]; -} - -int gl::vboIndex(const VboAddress vboAddress) -{ - return static_cast<std::uint8_t>(vboAddress.vboClass) * gl::numVboSubclasses - + static_cast<std::uint8_t>(vboAddress.vboSubclass); -} - -QMatrix4x4 gl::toQMatrix(const glm::mat4& matrix) +std::size_t gl::Compiler::vertexCount(const gl::ArrayClass arrayClass) const { - return { - matrix[0][0], - matrix[0][1], - matrix[0][2], - matrix[0][3], - matrix[1][0], - matrix[1][1], - matrix[1][2], - matrix[1][3], - matrix[2][0], - matrix[2][1], - matrix[2][2], - matrix[2][3], - matrix[3][0], - matrix[3][1], - matrix[3][2], - matrix[3][3], - }; + return this->storedVertexCounts[static_cast<int>(arrayClass)]; }