Tue, 28 Jan 2020 23:34:49 +0200
added some sort of lighting
locale/fi.ts | file | annotate | diff | comparison | revisions | |
src/gl/common.h | file | annotate | diff | comparison | revisions | |
src/gl/compiler.cpp | file | annotate | diff | comparison | revisions | |
src/gl/compiler.h | file | annotate | diff | comparison | revisions | |
src/gl/partrenderer.cpp | file | annotate | diff | comparison | revisions | |
src/gl/partrenderer.h | file | annotate | diff | comparison | revisions |
--- a/locale/fi.ts Sun Jan 26 14:29:30 2020 +0200 +++ b/locale/fi.ts Tue Jan 28 23:34:49 2020 +0200 @@ -189,7 +189,7 @@ <context> <name>PartRenderer</name> <message> - <location filename="../src/gl/partrenderer.cpp" line="168"/> + <location filename="../src/gl/partrenderer.cpp" line="152"/> <source>Rendering error</source> <translation type="unfinished"></translation> </message> @@ -236,22 +236,22 @@ <context> <name>gl::Compiler</name> <message> - <location filename="../src/gl/compiler.cpp" line="79"/> + <location filename="../src/gl/compiler.cpp" line="95"/> <source>Vertex shader:</source> <translation type="unfinished"></translation> </message> <message> - <location filename="../src/gl/compiler.cpp" line="84"/> + <location filename="../src/gl/compiler.cpp" line="100"/> <source>Fragment shader:</source> <translation type="unfinished"></translation> </message> <message> - <location filename="../src/gl/compiler.cpp" line="88"/> + <location filename="../src/gl/compiler.cpp" line="104"/> <source>Shader compile error</source> <translation type="unfinished"></translation> </message> <message> - <location filename="../src/gl/compiler.cpp" line="88"/> + <location filename="../src/gl/compiler.cpp" line="104"/> <source>Could not compile shaders.</source> <translation type="unfinished"></translation> </message>
--- a/src/gl/common.h Sun Jan 26 14:29:30 2020 +0200 +++ b/src/gl/common.h Tue Jan 28 23:34:49 2020 +0200 @@ -133,17 +133,6 @@ constexpr int NUM_ARRAY_CLASSES = countof(ARRAY_CLASSES); constexpr int FLOATS_PER_VERTEX = 7; - // Types of vbo per object - enum class VboSubclass : std::uint8_t - { - VertexData, - Normals - }; - constexpr int numVboSubclasses = 2; - - // Amount of vbos overall - constexpr int numVbos = gl::NUM_ARRAY_CLASSES * gl::numVboSubclasses; - enum class RenderStyle { Normal, @@ -151,9 +140,4 @@ BfcRedGreen, RandomColors }; - - inline void* offset(const int n) - { - return reinterpret_cast<void*>(n); - } }
--- 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)]; }
--- a/src/gl/compiler.h Sun Jan 26 14:29:30 2020 +0200 +++ b/src/gl/compiler.h Tue Jan 28 23:34:49 2020 +0200 @@ -33,14 +33,13 @@ namespace gl { class Compiler; - class Renderer; - struct VboAddress + + struct Vertex { - ArrayClass vboClass; - VboSubclass vboSubclass; + glm::vec3 position; + glm::vec4 color; + glm::vec3 normal; }; - int vboIndex(const VboAddress vboAddress); - QMatrix4x4 toQMatrix(const glm::mat4& matrix); } class gl::Compiler : public QObject, protected QOpenGLFunctions @@ -50,8 +49,8 @@ Compiler(const ColorTable& colorTable, QObject* parent); ~Compiler(); void build(Model* model, DocumentManager* context); - void buildPolygon(Polygon polygon, std::vector<GLfloat>* vboData); - std::size_t vboSize(gl::ArrayClass arrayClass) const; + void buildPolygon(Polygon polygon, std::vector<Vertex>* vboData); + std::size_t vertexCount(gl::ArrayClass arrayClass) const; QColor getColorForPolygon(const gl::Polygon& polygon); glm::vec3 modelCenter() const; double modelDistance() const; @@ -81,8 +80,7 @@ this->setUniform(uniformName, *array); } private: - bool m_vboChanged[gl::numVbos] = {true}; - std::size_t storedVboSizes[gl::numVbos] = {0_z}; + std::size_t storedVertexCounts[gl::NUM_ARRAY_CLASSES] = {0_z}; bool initialized = false; BoundingBox boundingBox; const ColorTable& colorTable;
--- a/src/gl/partrenderer.cpp Sun Jan 26 14:29:30 2020 +0200 +++ b/src/gl/partrenderer.cpp Tue Jan 28 23:34:49 2020 +0200 @@ -46,7 +46,6 @@ } this->compiler->initialize(); this->compiler->build(this->model, this->documents); - this->initializeLighting(); this->initialized = true; this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0}); this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0}); @@ -55,22 +54,6 @@ this->update(); } -void PartRenderer::initializeLighting() -{ - GLfloat materialShininess[] = {5.0}; - GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0}; - GLfloat ambientLightingLevel[] = {0.5, 0.5, 0.5, 1.0}; - glShadeModel(GL_SMOOTH); - glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); - glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLightingLevel); - glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLightingLevel); - glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glEnable(GL_COLOR_MATERIAL); - glEnable(GL_DEPTH_TEST); -} - void PartRenderer::resizeGL(int width, int height) { glViewport(0, 0, width, height); @@ -79,6 +62,7 @@ static_cast<float>(width) / static_cast<float>(height), 0.1f, 10000.f); + this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix); } static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass) @@ -128,7 +112,6 @@ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); break; } - this->compiler->setUniformMatrix("CameraTransformation", this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion)); // Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering. renderVao(gl::ArrayClass::Triangles); renderVao(gl::ArrayClass::Quads); @@ -141,12 +124,13 @@ // I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior const double z = 2 * std::exp(this->zoom) * (1 + this->compiler->modelDistance()); this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0}); + this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix); } void PartRenderer::renderVao(const gl::ArrayClass arrayClass) { this->compiler->bindVertexArray(arrayClass); - const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX; + const std::size_t vertexCount = this->compiler->vertexCount(arrayClass); glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount)); this->compiler->releaseVertexArray(arrayClass); this->checkForGLErrors(); @@ -185,6 +169,7 @@ const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0}); const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0}); this->modelQuaternion = q_x * q_y * this->modelQuaternion; + this->compiler->setUniformMatrix("modelMatrix", glm::mat4_cast(this->modelQuaternion)); this->update(); } this->lastMousePosition = pointToPointF(event->pos());
--- a/src/gl/partrenderer.h Sun Jan 26 14:29:30 2020 +0200 +++ b/src/gl/partrenderer.h Tue Jan 28 23:34:49 2020 +0200 @@ -41,8 +41,6 @@ static constexpr double MAX_ZOOM = 3.0; double zoom = 1.0; bool initialized = false; - void initializeLighting(); void renderVao(const gl::ArrayClass arrayClass); void checkForGLErrors(); - glm::mat4 modelMatrix() const; };