From 3906f533bbcc36aa1711c2abf59ab0feee2ecedb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 27 Feb 2018 19:02:29 +0100 Subject: [PATCH] doc: compiled code snippets for the Shaders namespace. Also not really without errors. --- doc/shaders.dox | 49 +-- doc/snippets/CMakeLists.txt | 3 +- doc/snippets/MagnumShaders.cpp | 426 +++++++++++++++++++++++ src/Magnum/Shaders/DistanceFieldVector.h | 30 +- src/Magnum/Shaders/Flat.h | 51 +-- src/Magnum/Shaders/MeshVisualizer.h | 61 +--- src/Magnum/Shaders/Phong.h | 73 +--- src/Magnum/Shaders/Vector.h | 28 +- src/Magnum/Shaders/VertexColor.h | 26 +- 9 files changed, 452 insertions(+), 295 deletions(-) create mode 100644 doc/snippets/MagnumShaders.cpp diff --git a/doc/shaders.dox b/doc/shaders.dox index 68e0469a6..bb16fcad1 100644 --- a/doc/shaders.dox +++ b/doc/shaders.dox @@ -58,23 +58,7 @@ buffer into the mesh, you need to specify which shader attributes are on which position in the buffer. See @ref Mesh::addVertexBuffer() for details and usage examples. Example mesh configuration for @ref Shaders::Phong shader: -@code{.cpp} -struct Vertex { - Vector3 position; - Vector3 normal; - Vector2 textureCoordinates; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, - Shaders::Phong::Position{}, - Shaders::Phong::Normal{}, - Shaders::Phong::TextureCoordinates{}); -@endcode +@snippet MagnumShaders.cpp shaders-setup Each shader then has its own set of configuration functions. Some configuration is static, specified commonly as flags in constructor, directly affecting @@ -82,19 +66,7 @@ compiled shader code. Other configuration is specified through uniforms and various binding points, commonly exposed through various setters. Example configuration and rendering using @ref Shaders::Phong : -@code{.cpp} -Matrix4 transformationMatrix, projectionMatrix; -Texture2D diffuseTexture, specularTexture; - -Shaders::Phong shader{Shaders::Phong::DiffuseTexture}; -shader.setDiffuseTexture(diffuseTexture) - .setLightPosition({5.0f, 5.0f, 7.0f}) - .setTransformationMatrix(transformationMatrix) - .setNormalMatrix(transformationMatrix.rotation()) - .setProjectionMatrix(projectionMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp shaders-rendering @section shaders-generic Generic vertex attributes @@ -105,12 +77,7 @@ of generic attributes is available in @ref Shaders::Generic class. Configuration of the above mesh using generic attributes could then look like this: -@code{.cpp} -mesh.addVertexBuffer(vertices, 0, - Shaders::Generic3D::Position{}, - Shaders::Generic3D::Normal{}, - Shaders::Generic3D::TextureCoordinates{}); -@endcode +@snippet MagnumShaders.cpp shaders-generic Note that in this particular case both configurations are equivalent, because @ref Shaders::Phong also uses generic vertex attribute definitions. @@ -119,15 +86,7 @@ Then you can render the mesh using @ref Shaders::Phong shader like above, or use for example @ref Shaders::Flat3D or even @ref Shaders::MeshVisualizer with the same mesh reconfiguration. The unused attributes will be simply ignored. -@code{.cpp} -Shaders::MeshVisualizer visualizerShader{Shaders::MeshVisualizer::Wireframe}; -visualizerShader.setColor(Color3::fromHSV(216.0_degf, 0.85f, 1.0f)) - .setWireframeColor(Color3{0.95f}) - .setViewportSize(defaultFramebuffer.viewport().size()) - .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); - -mesh.draw(visualizerShader); -@endcode +@snippet MagnumShaders.cpp shaders-meshvisualizer The @ref MeshTools::compile() utility configures meshes using generic vertex attribute definitions to make them usable with any shader. diff --git a/doc/snippets/CMakeLists.txt b/doc/snippets/CMakeLists.txt index 22eaa9216..81a21617c 100644 --- a/doc/snippets/CMakeLists.txt +++ b/doc/snippets/CMakeLists.txt @@ -34,7 +34,8 @@ if(CORRADE_TARGET_EMSCRIPTEN AND NOT TARGET_GLES2) endif() add_library(snippets STATIC - Magnum.cpp) + Magnum.cpp + MagnumShaders.cpp) target_link_libraries(snippets PRIVATE Magnum) set_target_properties(snippets PROPERTIES FOLDER "Magnum/doc/snippets") diff --git a/doc/snippets/MagnumShaders.cpp b/doc/snippets/MagnumShaders.cpp new file mode 100644 index 000000000..ed5bc1ca2 --- /dev/null +++ b/doc/snippets/MagnumShaders.cpp @@ -0,0 +1,426 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include + +#include "Magnum/Buffer.h" +#include "Magnum/DefaultFramebuffer.h" +#include "Magnum/Mesh.h" +#include "Magnum/Texture.h" +#include "Magnum/Math/Color.h" +#include "Magnum/MeshTools/Duplicate.h" +#include "Magnum/Shaders/DistanceFieldVector.h" +#include "Magnum/Shaders/Flat.h" +#include "Magnum/Shaders/MeshVisualizer.h" +#include "Magnum/Shaders/Phong.h" +#include "Magnum/Shaders/Vector.h" +#include "Magnum/Shaders/VertexColor.h" + +using namespace Magnum; +using namespace Magnum::Math::Literals; + +int main() { + +{ +/* [shaders-setup] */ +struct Vertex { + Vector3 position; + Vector3 normal; + Vector2 textureCoordinates; +}; +Vertex data[60]{ + // ... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, + Shaders::Phong::Position{}, + Shaders::Phong::Normal{}, + Shaders::Phong::TextureCoordinates{}) + //... + ; +/* [shaders-setup] */ + +/* [shaders-rendering] */ +Matrix4 transformationMatrix, projectionMatrix; +Texture2D diffuseTexture, specularTexture; + +Shaders::Phong shader{Shaders::Phong::Flag::DiffuseTexture}; +shader.setDiffuseTexture(diffuseTexture) + .setLightPosition({5.0f, 5.0f, 7.0f}) + .setTransformationMatrix(transformationMatrix) + .setNormalMatrix(transformationMatrix.rotation()) + .setProjectionMatrix(projectionMatrix); + +mesh.draw(shader); +/* [shaders-rendering] */ + +/* [shaders-generic] */ +mesh.addVertexBuffer(vertices, 0, + Shaders::Generic3D::Position{}, + Shaders::Generic3D::Normal{}, + Shaders::Generic3D::TextureCoordinates{}); +/* [shaders-generic] */ + +/* [shaders-meshvisualizer] */ +Shaders::MeshVisualizer visualizerShader{Shaders::MeshVisualizer::Flag::Wireframe}; +visualizerShader + .setColor(0x2f83cc_rgbf) + .setWireframeColor(0xdcdcdc_rgbf) + .setViewportSize(Vector2{defaultFramebuffer.viewport().size()}) + .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); + +mesh.draw(visualizerShader); +/* [shaders-meshvisualizer] */ +} + +{ +/* [DistanceFieldVector-usage1] */ +struct Vertex { + Vector2 position; + Vector2 textureCoordinates; +}; +Vertex data[60]{ + // ... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, + Shaders::DistanceFieldVector2D::Position{}, + Shaders::DistanceFieldVector2D::TextureCoordinates{}) + // ... + ; +/* [DistanceFieldVector-usage1] */ +} + +{ +Mesh mesh; +/* [DistanceFieldVector-usage2] */ +Matrix3 transformationMatrix, projectionMatrix; +Texture2D texture; + +Shaders::DistanceFieldVector2D shader; +shader.setColor(0x2f83cc_rgbf) + .setOutlineColor(0xdcdcdc_rgbf) + .setOutlineRange(0.6f, 0.4f) + .setVectorTexture(texture) + .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); + +mesh.draw(shader); +/* [DistanceFieldVector-usage2] */ +} + +{ +/* [Flat-usage-colored1] */ +struct Vertex { + Vector3 position; +}; +Vertex data[60]{ + //... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, Shaders::Flat3D::Position{}) + // ... + ; +/* [Flat-usage-colored1] */ + +/* [Flat-usage-colored2] */ +Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f)); +Matrix4 projectionMatrix = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); + +Shaders::Flat3D shader; +shader.setColor(0x2f83cc_rgbf) + .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); + +mesh.draw(shader); +/* [Flat-usage-colored2] */ +} + +{ +/* [Flat-usage-textured1] */ +struct Vertex { + Vector3 position; + Vector2 textureCoordinates; +}; +Vertex data[60]{ + // ... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, + Shaders::Flat3D::Position{}, + Shaders::Flat3D::TextureCoordinates{}) + // ... + ; +/* [Flat-usage-textured1] */ + +/* [Flat-usage-textured2] */ +Matrix4 transformationMatrix, projectionMatrix; +Texture2D texture; + +Shaders::Flat3D shader{Shaders::Flat3D::Flag::Textured}; +shader.setTransformationProjectionMatrix(projectionMatrix*transformationMatrix) + .setTexture(texture); + +mesh.draw(shader); +/* [Flat-usage-textured2] */ +} + +{ +/* [MeshVisualizer-usage-geom1] */ +struct Vertex { + Vector3 position; +}; +Vertex data[60]{ + // ... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, Shaders::MeshVisualizer::Position{}); +/* [MeshVisualizer-usage-geom1] */ + +/* [MeshVisualizer-usage-geom2] */ +Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f)); +Matrix4 projectionMatrix = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); + +Shaders::MeshVisualizer shader{Shaders::MeshVisualizer::Flag::Wireframe}; +shader.setColor(0x2f83cc_rgbf) + .setWireframeColor(0xdcdcdc_rgbf) + .setViewportSize(Vector2{defaultFramebuffer.viewport().size()}) + .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); + +mesh.draw(shader); +/* [MeshVisualizer-usage-geom2] */ + +/* [MeshVisualizer-usage-no-geom-old1] */ +constexpr std::size_t vertexCount = Containers::arraySize(data); +Float vertexIndex[vertexCount]; +std::iota(vertexIndex, vertexIndex + vertexCount, 0.0f); + +Buffer vertexIndices; +vertexIndices.setData(vertexIndex, BufferUsage::StaticDraw); + +mesh.addVertexBuffer(vertexIndices, 0, Shaders::MeshVisualizer::VertexIndex{}); +/* [MeshVisualizer-usage-no-geom-old1] */ +} + +{ +Mesh mesh; +/* [MeshVisualizer-usage-no-geom-old2] */ +Matrix4 transformationMatrix, projectionMatrix; + +Shaders::MeshVisualizer shader{Shaders::MeshVisualizer::Flag::Wireframe| + Shaders::MeshVisualizer::Flag::NoGeometryShader}; +shader.setColor(0x2f83cc_rgbf) + .setWireframeColor(0xdcdcdc_rgbf) + .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); + +mesh.draw(shader); +/* [MeshVisualizer-usage-no-geom-old2] */ +} + +{ +/* [MeshVisualizer-usage-no-geom] */ +std::vector indices{ + // ... +}; +std::vector indexedPositions{ + // ... +}; + +/* De-indexing the position array */ +Buffer vertices; +vertices.setData(MeshTools::duplicate(indices, indexedPositions), + BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, Shaders::MeshVisualizer::Position{}); +/* [MeshVisualizer-usage-no-geom] */ +} + +{ +/* [Phong-usage-colored1] */ +struct Vertex { + Vector3 position; + Vector3 normal; +}; +Vertex data[60]{ + // ... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, + Shaders::Phong::Position{}, + Shaders::Phong::Normal{}); +/* [Phong-usage-colored1] */ + +/* [Phong-usage-colored2] */ +Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f)); +Matrix4 projectionMatrix = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); + +Shaders::Phong shader; +shader.setDiffuseColor(0x2f83cc_rgbf) + .setShininess(200.0f) + .setLightPosition({5.0f, 5.0f, 7.0f}) + .setTransformationMatrix(transformationMatrix) + .setNormalMatrix(transformationMatrix.rotation()) + .setProjectionMatrix(projectionMatrix); + +mesh.draw(shader); +/* [Phong-usage-colored2] */ +} + +{ +/* [Phong-usage-texture1] */ +struct Vertex { + Vector3 position; + Vector3 normal; + Vector2 textureCoordinates; +}; +Vertex data[60]{ + // ... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, + Shaders::Phong::Position{}, + Shaders::Phong::Normal{}, + Shaders::Phong::TextureCoordinates{}); +/* [Phong-usage-texture1] */ + +/* [Phong-usage-texture2] */ +Matrix4 transformationMatrix, projectionMatrix; +Texture2D diffuseTexture, specularTexture; + +Shaders::Phong shader{Shaders::Phong::Flag::DiffuseTexture| + Shaders::Phong::Flag::SpecularTexture}; +shader.setTextures(nullptr, &diffuseTexture, &specularTexture) + .setLightPosition({5.0f, 5.0f, 7.0f}) + .setTransformationMatrix(transformationMatrix) + .setNormalMatrix(transformationMatrix.rotation()) + .setProjectionMatrix(projectionMatrix); + +mesh.draw(shader); +/* [Phong-usage-texture2] */ +} + +{ +Texture2D ambientAlphaTexture, diffuseAlphaTexture; +Color3 diffuseRgb, specularRgb; +/* [Phong-usage-alpha] */ +Shaders::Phong shader{Shaders::Phong::Flag::AmbientTexture| + Shaders::Phong::Flag::DiffuseTexture}; +shader.setTextures(&ambientAlphaTexture, &diffuseAlphaTexture, nullptr) + .setAmbientColor(0x000000ff_rgbf) + .setDiffuseColor(Color4{diffuseRgb, 0.0f}) + .setSpecularColor(Color4{specularRgb, 0.0f}); +/* [Phong-usage-alpha] */ +} + +{ +/* [Vector-usage1] */ +struct Vertex { + Vector2 position; + Vector2 textureCoordinates; +}; +Vertex data[60]{ + // ... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, + Shaders::Vector2D::Position{}, + Shaders::Vector2D::TextureCoordinates{}); +/* [Vector-usage1] */ + +/* [Vector-usage2] */ +Matrix3 transformationMatrix, projectionMatrix; +Texture2D texture; + +Shaders::Vector2D shader; +shader.setColor(0x2f83cc_rgbf) + .setVectorTexture(texture) + .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); + +mesh.draw(shader); +/* [Vector-usage2] */ +} + +{ +/* [VertexColor-usage1] */ +struct Vertex { + Vector3 position; + Color3 color; +}; +Vertex data[60]{ + // ... +}; + +Buffer vertices; +vertices.setData(data, BufferUsage::StaticDraw); + +Mesh mesh; +mesh.addVertexBuffer(vertices, 0, + Shaders::VertexColor3D::Position{}, + Shaders::VertexColor3D::Color{Shaders::VertexColor3D::Color::Components::Three}); +/* [VertexColor-usage1] */ + +/* [VertexColor-usage2] */ +Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f)); +Matrix4 projectionMatrix = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); + +Shaders::VertexColor3D shader; +shader.setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); + +mesh.draw(shader); +/* [VertexColor-usage2] */ +} + +} diff --git a/src/Magnum/Shaders/DistanceFieldVector.h b/src/Magnum/Shaders/DistanceFieldVector.h index 4f6bbee2a..083cadb73 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.h +++ b/src/Magnum/Shaders/DistanceFieldVector.h @@ -56,37 +56,11 @@ value passed to @ref setSmoothness(). You need to provide @ref Position and Common mesh setup: -@code{.cpp} -struct Vertex { - Vector2 position; - Vector2 textureCoordinates; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, - Shaders::DistanceFieldVector2D::Position{}, - Shaders::DistanceFieldVector2D::TextureCoordinates{}); -@endcode +@snippet MagnumShaders.cpp DistanceFieldVector-usage1 Common rendering setup: -@code{.cpp} -Matrix3 transformationMatrix, projectionMatrix; -Texture2D texture; - -Shaders::DistanceFieldVector2D shader; -shader.setColor(0x2f83cc_rgbf) - .setOutlineColor(0xdcdcdc_rgbf) - .setOutlineRange(0.6f, 0.4f) - .setVectorTexture(texture) - .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp DistanceFieldVector-usage2 @see @ref shaders, @ref DistanceFieldVector2D, @ref DistanceFieldVector3D @todo Use fragment shader derivations to have proper smoothness in perspective/ diff --git a/src/Magnum/Shaders/Flat.h b/src/Magnum/Shaders/Flat.h index b61ad1be7..3d7ad3936 100644 --- a/src/Magnum/Shaders/Flat.h +++ b/src/Magnum/Shaders/Flat.h @@ -68,64 +68,21 @@ For coloring the texture based on intensity you can use the @ref Vector shader. Common mesh setup: -@code{.cpp} -struct Vertex { - Vector3 position; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, Shaders::Flat3D::Position{}); -@endcode +@snippet MagnumShaders.cpp Flat-usage-colored1 Common rendering setup: -@code{.cpp} -Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f)); -Matrix4 projectionMatrix = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); - -Shaders::Flat3D shader; -shader.setColor(0x2f83cc_rgbf) - .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp Flat-usage-colored2 @subsection Shaders-Flat-usage-textured Textured mesh Common mesh setup: -@code{.cpp} -struct Vertex { - Vector3 position; - Vector2 textureCoordinates; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, - Shaders::Flat3D::Position{}, - Shaders::Flat3D::TextureCoordinates{}); -@endcode +@snippet MagnumShaders.cpp Flat-usage-textured1 Common rendering setup: -@code{.cpp} -Matrix4 transformationMatrix, projectionMatrix; -Texture2D texture; - -Shaders::Flat3D shader{Shaders::Flat3D::Textured}; -shader.setTransformationProjectionMatrix(projectionMatrix*transformationMatrix) - .setTexture(texture); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp Flat-usage-textured2 @see @ref shaders, @ref Flat2D, @ref Flat3D */ diff --git a/src/Magnum/Shaders/MeshVisualizer.h b/src/Magnum/Shaders/MeshVisualizer.h index c389e134b..317c3b17a 100644 --- a/src/Magnum/Shaders/MeshVisualizer.h +++ b/src/Magnum/Shaders/MeshVisualizer.h @@ -77,79 +77,28 @@ is optionally used for improving line appearance. Common mesh setup: -@code{.cpp} -struct Vertex { - Vector3 position; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, Shaders::MeshVisualizer::Position{}); -@endcode +@snippet MagnumShaders.cpp MeshVisualizer-usage-geom1 Common rendering setup: -@code{.cpp} -Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f)); -Matrix4 projectionMatrix = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); - -Shaders::MeshVisualizer shader{Shaders::MeshVisualizer::Wireframe}; -shader.setColor(0x2f83cc_rgbf) - .setWireframeColor(0xdcdcdc_rgbf) - .setViewportSize(defaultFramebuffer.viewport().size()) - .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp MeshVisualizer-usage-geom2 @subsection Shaders-MeshVisualizer-usage-wireframe-no-geom-old Wireframe visualization without geometry shader on older hardware You need to provide also the @ref VertexIndex attribute. Mesh setup *in addition to the above*: -@code{.cpp} -constexpr std::size_t vertexCount = std::extent::value; -Float vertexIndex[vertexCount]; -std::iota(vertexIndex, vertexIndex + vertexCount, 0.0f); - -Buffer vertexIndices; -vertexIndices.setData(vertexIndex, BufferUsage::StaticDraw); - -mesh.addVertexBuffer(vertexIndices, 0, Shaders::MeshVisualizer::VertexIndex{}); -@endcode +@snippet MagnumShaders.cpp MeshVisualizer-usage-no-geom-old1 Rendering setup: -@code{.cpp} -Matrix4 transformationMatrix, projectionMatrix; - -Shaders::MeshVisualizer shader{Shaders::MeshVisualizer::Wireframe| - Shaders::MeshVisualizer::NoGeometryShader}; -shader.setColor(0x2f83cc_rgbf) - .setWireframeColor(0xdcdcdc_rgbf) - .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp MeshVisualizer-usage-no-geom-old2 @subsection Shaders-MeshVisualizer-usage-wireframe-no-geom Wireframe visualization of indexed meshes without geometry shader The vertices must be converted to non-indexed array. Mesh setup: -@code{.cpp} -std::vector indices{ ... }; -std::vector indexedPositions{ ... }; - -// De-indexing the position array -Buffer vertices; -vertices.setData(MeshTools::duplicate(indices, indexedPositions), BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, Shaders::MeshVisualizer::Position{}); -@endcode +@snippet MagnumShaders.cpp MeshVisualizer-usage-no-geom Rendering setup the same as above. diff --git a/src/Magnum/Shaders/Phong.h b/src/Magnum/Shaders/Phong.h index 1a0d58653..2abc63ab1 100644 --- a/src/Magnum/Shaders/Phong.h +++ b/src/Magnum/Shaders/Phong.h @@ -61,77 +61,21 @@ enabled textures. Common mesh setup: -@code{.cpp} -struct Vertex { - Vector3 position; - Vector3 normal; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, - Shaders::Phong::Position{}, - Shaders::Phong::Normal{}); -@endcode +@snippet MagnumShaders.cpp Phong-usage-colored1 Common rendering setup: -@code{.cpp} -Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f)); -Matrix4 projectionMatrix = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); - -Shaders::Phong shader; -shader.setDiffuseColor(0x2f83cc_rgbf) - .setShininess(200.0f) - .setLightPosition({5.0f, 5.0f, 7.0f}) - .setTransformationMatrix(transformationMatrix) - .setNormalMatrix(transformationMatrix.rotation()) - .setProjectionMatrix(projectionMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp Phong-usage-colored2 @subsection Shaders-Phong-usage-texture Diffuse and specular texture Common mesh setup: -@code{.cpp} -struct Vertex { - Vector3 position; - Vector3 normal; - Vector2 textureCoordinates; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, - Shaders::Phong::Position{}, - Shaders::Phong::Normal{}, - Shaders::Phong::TextureCoordinates{}); -@endcode +@snippet MagnumShaders.cpp Phong-usage-texture1 Common rendering setup: -@code{.cpp} -Matrix4 transformationMatrix, projectionMatrix; -Texture2D diffuseTexture, specularTexture; - -Shaders::Phong shader{Shaders::Phong::DiffuseTexture| - Shaders::Phong::SpecularTexture}; -shader.setTextures(nullptr, &diffuseTexture, &specularTexture) - .setLightPosition({5.0f, 5.0f, 7.0f}) - .setTransformationMatrix(transformationMatrix) - .setNormalMatrix(transformationMatrix.rotation()) - .setProjectionMatrix(projectionMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp Phong-usage-texture2 @subsection Shaders-Phong-usage-alpha Alpha-masked drawing @@ -141,14 +85,7 @@ ambient alpha will be taken into account. If you have diffuse texture combined with the alpha mask, you can use that texture for both ambient and diffuse part and then separate the alpha like this: -@code{.cpp} -Shaders::Phong shader{Shaders::Phong::AmbientTexture| - Shaders::Phong::DiffuseTexture}; -shader.setTextures(&diffuseAlphaTexture, &diffuseAlphaTexture, nullptr) - .setAmbientColor({0.0f, 0.0f, 0.0f, 1.0f}) - .setDiffuseColor(Color4{diffuseRgb, 0.0f}) - .setSpecularColor(Color4{specularRgb, 0.0f}); -@endcode +@snippet MagnumShaders.cpp Phong-usage-alpha @see @ref shaders */ diff --git a/src/Magnum/Shaders/Vector.h b/src/Magnum/Shaders/Vector.h index b6a534666..281ee7ab0 100644 --- a/src/Magnum/Shaders/Vector.h +++ b/src/Magnum/Shaders/Vector.h @@ -55,35 +55,11 @@ attributes in your triangle mesh and call at least Common mesh setup: -@code{.cpp} -struct Vertex { - Vector2 position; - Vector2 textureCoordinates; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, - Shaders::Vector2D::Position{}, - Shaders::Vector2D::TextureCoordinates{}); -@endcode +@snippet MagnumShaders.cpp Vector-usage1 Common rendering setup: -@code{.cpp} -Matrix3 transformationMatrix, projectionMatrix; -Texture2D texture; - -Shaders::Vector2D shader; -shader.setColor(0x2f83cc_rgbf) - .setVectorTexture(texture) - .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp Vector-usage2 @see @ref shaders, @ref Vector2D, @ref Vector3D */ diff --git a/src/Magnum/Shaders/VertexColor.h b/src/Magnum/Shaders/VertexColor.h index e08eda3ad..7e33030fc 100644 --- a/src/Magnum/Shaders/VertexColor.h +++ b/src/Magnum/Shaders/VertexColor.h @@ -56,33 +56,11 @@ attribute --- the shader accepts four-component color attribute but, similarly to all other attributes, it's possible to supply also three-component colors if alpha is not important. -@code{.cpp} -struct Vertex { - Vector3 position; - Color3 color; -}; -Vertex data[] = { ... }; - -Buffer vertices; -vertices.setData(data, BufferUsage::StaticDraw); - -Mesh mesh; -mesh.addVertexBuffer(vertices, 0, - Shaders::VertexColor3D::Position{}, - Shaders::VertexColor3D::Color{Shaders::VertexColor3D::Color::Components::Three}); -@endcode +@snippet MagnumShaders.cpp VertexColor-usage1 Common rendering setup: -@code{.cpp} -Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f)); -Matrix4 projectionMatrix = Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f); - -Shaders::VertexColor3D shader; -shader.setTransformationProjectionMatrix(projectionMatrix*transformationMatrix); - -mesh.draw(shader); -@endcode +@snippet MagnumShaders.cpp VertexColor-usage2 @see @ref shaders, @ref VertexColor2D, @ref VertexColor3D */