From ed489a1112372e6fb6f9898fa9368d5f321bbdff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 17 Jan 2022 09:42:21 +0100 Subject: [PATCH] doc: ew, a std::vector! Funny how even doing the *insanely complex* operation of extending a MeshData with one extra attribute is still shorter than manually populating the GL::Mesh. --- doc/generated/shaders.cpp | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/doc/generated/shaders.cpp b/doc/generated/shaders.cpp index 772959b7e..d91e5b48b 100644 --- a/doc/generated/shaders.cpp +++ b/doc/generated/shaders.cpp @@ -275,28 +275,22 @@ std::string ShaderVisualizer::flat() { std::string ShaderVisualizer::vertexColor() { Trade::MeshData sphere = Primitives::uvSphereSolid(32, 64); + /* Add a color attribute */ + Trade::MeshData sphereWithColors = MeshTools::interleave(std::move(sphere), { + Trade::MeshAttributeData{Trade::MeshAttribute::Color, VertexFormat::Vector3, nullptr} + }); + /* Color vertices nearest to given position */ auto target = Vector3{2.0f, 2.0f, 7.0f}.normalized(); - std::vector colors; - colors.reserve(sphere.vertexCount()); - for(Vector3 position: sphere.attribute(Trade::MeshAttribute::Position)) - colors.push_back(Color3::fromHsv({Math::lerp(240.0_degf, 420.0_degf, Math::max(1.0f - (position - target).length(), 0.0f)), 0.85f, 0.666f})); - - GL::Buffer vertices, indices; - vertices.setData(MeshTools::interleave(sphere.attribute(Trade::MeshAttribute::Position), colors), GL::BufferUsage::StaticDraw); - indices.setData(sphere.indices(), GL::BufferUsage::StaticDraw); - - GL::Mesh mesh; - mesh.setPrimitive(GL::MeshPrimitive::Triangles) - .setCount(sphere.indexCount()) - .addVertexBuffer(vertices, 0, - Shaders::VertexColorGL3D::Position{}, - Shaders::VertexColorGL3D::Color3{}) - .setIndexBuffer(indices, 0, GL::MeshIndexType::UnsignedInt); + Containers::StridedArrayView1D positions = sphereWithColors.attribute(Trade::MeshAttribute::Position); + Containers::StridedArrayView1D colors = sphereWithColors.mutableAttribute(Trade::MeshAttribute::Color); + for(std::size_t i = 0; i != sphereWithColors.vertexCount(); ++i) { + colors[i] = Color3::fromHsv({Math::lerp(240.0_degf, 420.0_degf, Math::max(1.0f - (positions[i] - target).length(), 0.0f)), 0.85f, 0.666f}); + } Shaders::VertexColorGL3D shader; shader.setTransformationProjectionMatrix(Projection*Transformation) - .draw(mesh); + .draw(MeshTools::compile(sphereWithColors)); return "vertexcolor.png"; }