Browse Source

doc: compiled code snippets for the Shaders namespace.

Also not really without errors.
pull/205/head
Vladimír Vondruš 8 years ago
parent
commit
3906f533bb
  1. 49
      doc/shaders.dox
  2. 3
      doc/snippets/CMakeLists.txt
  3. 426
      doc/snippets/MagnumShaders.cpp
  4. 30
      src/Magnum/Shaders/DistanceFieldVector.h
  5. 51
      src/Magnum/Shaders/Flat.h
  6. 61
      src/Magnum/Shaders/MeshVisualizer.h
  7. 73
      src/Magnum/Shaders/Phong.h
  8. 28
      src/Magnum/Shaders/Vector.h
  9. 26
      src/Magnum/Shaders/VertexColor.h

49
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 position in the buffer. See @ref Mesh::addVertexBuffer() for details and usage
examples. Example mesh configuration for @ref Shaders::Phong shader: examples. Example mesh configuration for @ref Shaders::Phong shader:
@code{.cpp} @snippet MagnumShaders.cpp shaders-setup
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
Each shader then has its own set of configuration functions. Some configuration Each shader then has its own set of configuration functions. Some configuration
is static, specified commonly as flags in constructor, directly affecting 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 various binding points, commonly exposed through various setters. Example
configuration and rendering using @ref Shaders::Phong : configuration and rendering using @ref Shaders::Phong :
@code{.cpp} @snippet MagnumShaders.cpp shaders-rendering
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
@section shaders-generic Generic vertex attributes @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 Configuration of the above mesh using generic attributes could then look like
this: this:
@code{.cpp} @snippet MagnumShaders.cpp shaders-generic
mesh.addVertexBuffer(vertices, 0,
Shaders::Generic3D::Position{},
Shaders::Generic3D::Normal{},
Shaders::Generic3D::TextureCoordinates{});
@endcode
Note that in this particular case both configurations are equivalent, because Note that in this particular case both configurations are equivalent, because
@ref Shaders::Phong also uses generic vertex attribute definitions. @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 use for example @ref Shaders::Flat3D or even @ref Shaders::MeshVisualizer with
the same mesh reconfiguration. The unused attributes will be simply ignored. the same mesh reconfiguration. The unused attributes will be simply ignored.
@code{.cpp} @snippet MagnumShaders.cpp shaders-meshvisualizer
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
The @ref MeshTools::compile() utility configures meshes using generic vertex The @ref MeshTools::compile() utility configures meshes using generic vertex
attribute definitions to make them usable with any shader. attribute definitions to make them usable with any shader.

3
doc/snippets/CMakeLists.txt

@ -34,7 +34,8 @@ if(CORRADE_TARGET_EMSCRIPTEN AND NOT TARGET_GLES2)
endif() endif()
add_library(snippets STATIC add_library(snippets STATIC
Magnum.cpp) Magnum.cpp
MagnumShaders.cpp)
target_link_libraries(snippets PRIVATE Magnum) target_link_libraries(snippets PRIVATE Magnum)
set_target_properties(snippets PROPERTIES FOLDER "Magnum/doc/snippets") set_target_properties(snippets PROPERTIES FOLDER "Magnum/doc/snippets")

426
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š <mosra@centrum.cz>
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 <numeric>
#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<UnsignedInt> indices{
// ...
};
std::vector<Vector3> 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] */
}
}

30
src/Magnum/Shaders/DistanceFieldVector.h

@ -56,37 +56,11 @@ value passed to @ref setSmoothness(). You need to provide @ref Position and
Common mesh setup: Common mesh setup:
@code{.cpp} @snippet MagnumShaders.cpp DistanceFieldVector-usage1
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
Common rendering setup: Common rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp 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);
@endcode
@see @ref shaders, @ref DistanceFieldVector2D, @ref DistanceFieldVector3D @see @ref shaders, @ref DistanceFieldVector2D, @ref DistanceFieldVector3D
@todo Use fragment shader derivations to have proper smoothness in perspective/ @todo Use fragment shader derivations to have proper smoothness in perspective/

51
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: Common mesh setup:
@code{.cpp} @snippet MagnumShaders.cpp Flat-usage-colored1
struct Vertex {
Vector3 position;
};
Vertex data[] = { ... };
Buffer vertices;
vertices.setData(data, BufferUsage::StaticDraw);
Mesh mesh;
mesh.addVertexBuffer(vertices, 0, Shaders::Flat3D::Position{});
@endcode
Common rendering setup: Common rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp 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);
@endcode
@subsection Shaders-Flat-usage-textured Textured mesh @subsection Shaders-Flat-usage-textured Textured mesh
Common mesh setup: Common mesh setup:
@code{.cpp} @snippet MagnumShaders.cpp Flat-usage-textured1
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
Common rendering setup: Common rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp Flat-usage-textured2
Matrix4 transformationMatrix, projectionMatrix;
Texture2D texture;
Shaders::Flat3D shader{Shaders::Flat3D::Textured};
shader.setTransformationProjectionMatrix(projectionMatrix*transformationMatrix)
.setTexture(texture);
mesh.draw(shader);
@endcode
@see @ref shaders, @ref Flat2D, @ref Flat3D @see @ref shaders, @ref Flat2D, @ref Flat3D
*/ */

61
src/Magnum/Shaders/MeshVisualizer.h

@ -77,79 +77,28 @@ is optionally used for improving line appearance.
Common mesh setup: Common mesh setup:
@code{.cpp} @snippet MagnumShaders.cpp MeshVisualizer-usage-geom1
struct Vertex {
Vector3 position;
};
Vertex data[] = { ... };
Buffer vertices;
vertices.setData(data, BufferUsage::StaticDraw);
Mesh mesh;
mesh.addVertexBuffer(vertices, 0, Shaders::MeshVisualizer::Position{});
@endcode
Common rendering setup: Common rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp 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::Wireframe};
shader.setColor(0x2f83cc_rgbf)
.setWireframeColor(0xdcdcdc_rgbf)
.setViewportSize(defaultFramebuffer.viewport().size())
.setTransformationProjectionMatrix(projectionMatrix*transformationMatrix);
mesh.draw(shader);
@endcode
@subsection Shaders-MeshVisualizer-usage-wireframe-no-geom-old Wireframe visualization without geometry shader on older hardware @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 You need to provide also the @ref VertexIndex attribute. Mesh setup *in
addition to the above*: addition to the above*:
@code{.cpp} @snippet MagnumShaders.cpp MeshVisualizer-usage-no-geom-old1
constexpr std::size_t vertexCount = std::extent<decltype(data)>::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
Rendering setup: Rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp MeshVisualizer-usage-no-geom-old2
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
@subsection Shaders-MeshVisualizer-usage-wireframe-no-geom Wireframe visualization of indexed meshes without geometry shader @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: The vertices must be converted to non-indexed array. Mesh setup:
@code{.cpp} @snippet MagnumShaders.cpp MeshVisualizer-usage-no-geom
std::vector<UnsignedInt> indices{ ... };
std::vector<Vector3> 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
Rendering setup the same as above. Rendering setup the same as above.

73
src/Magnum/Shaders/Phong.h

@ -61,77 +61,21 @@ enabled textures.
Common mesh setup: Common mesh setup:
@code{.cpp} @snippet MagnumShaders.cpp Phong-usage-colored1
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
Common rendering setup: Common rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp 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);
@endcode
@subsection Shaders-Phong-usage-texture Diffuse and specular texture @subsection Shaders-Phong-usage-texture Diffuse and specular texture
Common mesh setup: Common mesh setup:
@code{.cpp} @snippet MagnumShaders.cpp Phong-usage-texture1
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
Common rendering setup: Common rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp Phong-usage-texture2
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
@subsection Shaders-Phong-usage-alpha Alpha-masked drawing @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 with the alpha mask, you can use that texture for both ambient and diffuse
part and then separate the alpha like this: part and then separate the alpha like this:
@code{.cpp} @snippet MagnumShaders.cpp Phong-usage-alpha
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
@see @ref shaders @see @ref shaders
*/ */

28
src/Magnum/Shaders/Vector.h

@ -55,35 +55,11 @@ attributes in your triangle mesh and call at least
Common mesh setup: Common mesh setup:
@code{.cpp} @snippet MagnumShaders.cpp Vector-usage1
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
Common rendering setup: Common rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp Vector-usage2
Matrix3 transformationMatrix, projectionMatrix;
Texture2D texture;
Shaders::Vector2D shader;
shader.setColor(0x2f83cc_rgbf)
.setVectorTexture(texture)
.setTransformationProjectionMatrix(projectionMatrix*transformationMatrix);
mesh.draw(shader);
@endcode
@see @ref shaders, @ref Vector2D, @ref Vector3D @see @ref shaders, @ref Vector2D, @ref Vector3D
*/ */

26
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 to all other attributes, it's possible to supply also three-component colors if
alpha is not important. alpha is not important.
@code{.cpp} @snippet MagnumShaders.cpp VertexColor-usage1
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
Common rendering setup: Common rendering setup:
@code{.cpp} @snippet MagnumShaders.cpp 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);
@endcode
@see @ref shaders, @ref VertexColor2D, @ref VertexColor3D @see @ref shaders, @ref VertexColor2D, @ref VertexColor3D
*/ */

Loading…
Cancel
Save