template<UnsignedInt dimensions>
Magnum::Shaders::VectorGL class new in Git master

Vector OpenGL shader.

Renders vector art in plain grayscale form. See also DistanceFieldVectorGL for more advanced effects. For rendering an unchanged texture you can use the FlatGL shader. You need to provide the Position and TextureCoordinates attributes in your triangle mesh and call at least bindVectorTexture(). By default, the shader renders the texture with a white color in an identity transformation. Use setTransformationProjectionMatrix(), setColor() and others to configure the shader.

Image

Alpha / transparency is supported by the shader implicitly, but to have it working on the framebuffer, you need to enable GL::Renderer::Feature::Blending and set up the blending function. See GL::Renderer::setBlendFunction() for details.

Example usage

Common mesh setup:

struct Vertex {
    Vector2 position;
    Vector2 textureCoordinates;
};
Vertex data[60]{
    // ...
};

GL::Buffer vertices;
vertices.setData(data, GL::BufferUsage::StaticDraw);

GL::Mesh mesh;
mesh.addVertexBuffer(vertices, 0,
    Shaders::VectorGL2D::Position{},
    Shaders::VectorGL2D::TextureCoordinates{});

Common rendering setup:

Matrix3 transformationMatrix, projectionMatrix;
GL::Texture2D texture;

Shaders::VectorGL2D shader;
shader.setColor(0x2f83cc_rgbf)
    .bindVectorTexture(texture)
    .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix)
    .draw(mesh);

Uniform buffers

See Using uniform buffers for a high-level overview that applies to all shaders. In this particular case, because the shader doesn't need a separate projection and transformation matrix, a combined one is supplied via a TransformationProjectionUniform2D / TransformationProjectionUniform3D buffer. To maximize use of the limited uniform buffer memory, materials are supplied separately in a VectorMaterialUniform buffer and then referenced via materialId from a VectorDrawUniform; for optional texture transformation a per-draw TextureTransformationUniform can be supplied as well. A uniform buffer setup equivalent to the above would look like this:

GL::Buffer projectionTransformationUniform, materialUniform, drawUniform;
projectionTransformationUniform.setData({
    Shaders::TransformationProjectionUniform2D{}
        .setTransformationProjectionMatrix(transformationMatrix*projectionMatrix)
});
materialUniform.setData({
    Shaders::VectorMaterialUniform{}
        .setColor(0x2f83cc_rgbf)
});
drawUniform.setData({
    Shaders::VectorDrawUniform{}
        .setMaterialId(0)
});

Shaders::VectorGL2D shader{Shaders::VectorGL2D::Flag::UniformBuffers};
shader
    .bindTransformationProjectionBuffer(projectionTransformationUniform)
    .bindMaterialBuffer(materialUniform)
    .bindDrawBuffer(drawUniform)
    .bindVectorTexture(texture)
    .draw(mesh);

For a multidraw workflow enable Flag::MultiDraw, supply desired material and draw count in the VectorGL(Flags, UnsignedInt, UnsignedInt) constructor and specify material references and texture offsets for every draw. Texture arrays aren't currently supported for this shader. Besides that, the usage is similar for all shaders, see Multidraw and reducing driver overhead for an example.

Base classes

class Magnum::GL::AbstractShaderProgram
Base for shader program implementations.

Public types

enum (anonymous): UnsignedInt { ColorOutput = GenericGL<dimensions>::ColorOutput }
enum class Flag: UnsignedByte { TextureTransformation = 1 << 0 new in 2020.06, UniformBuffers = 1 << 1 new in Git master, MultiDraw = UniformBuffers|(1 << 2) new in Git master } new in 2020.06
Flag.
using Position = GenericGL<dimensions>::Position
Vertex position.
using TextureCoordinates = GenericGL<dimensions>::TextureCoordinates
2D texture coordinates
using Flags = Containers::EnumSet<Flag> new in 2020.06
Flags.

Constructors, destructors, conversion operators

VectorGL(Flags flags = {}) explicit
Constructor.
VectorGL(Flags flags, UnsignedInt materialCount, UnsignedInt drawCount) explicit
Construct for a multi-draw scenario.
VectorGL(NoCreateT) explicit noexcept
Construct without creating the underlying OpenGL object.
VectorGL(const VectorGL<dimensions>&) deleted
Copying is not allowed.
VectorGL(VectorGL<dimensions>&&) defaulted noexcept
Move constructor.

Public functions

auto operator=(const VectorGL<dimensions>&) -> VectorGL<dimensions>& deleted
Copying is not allowed.
auto operator=(VectorGL<dimensions>&&) -> VectorGL<dimensions>& defaulted noexcept
Move assignment.
auto flags() const -> Flags new in 2020.06
Flags.
auto materialCount() const -> UnsignedInt new in Git master
Material count.
auto drawCount() const -> UnsignedInt new in Git master
Draw count.

Uniform setters

Used only if Flag::UniformBuffers is not set.

auto setTransformationProjectionMatrix(const MatrixTypeFor<dimensions, Float>& matrix) -> VectorGL<dimensions>&
Set transformation and projection matrix.
auto setTextureMatrix(const Matrix3& matrix) -> VectorGL<dimensions>& new in 2020.06
Set texture coordinate transformation matrix.
auto setBackgroundColor(const Color4& color) -> VectorGL<dimensions>&
Set background color.
auto setColor(const Color4& color) -> VectorGL<dimensions>&
Set fill color.

Texture binding

auto bindVectorTexture(GL::Texture2D& texture) -> VectorGL<dimensions>&
Bind vector texture.

Enum documentation

template<UnsignedInt dimensions>
enum Magnum::Shaders::VectorGL<dimensions>::(anonymous): UnsignedInt

Enumerators
ColorOutput

Color shader output. Generic output, present always. Expects three- or four-component floating-point or normalized buffer attachment.

template<UnsignedInt dimensions>
enum class Magnum::Shaders::VectorGL<dimensions>::Flag: UnsignedByte new in 2020.06

Flag.

Enumerators
TextureTransformation new in 2020.06

Enable texture coordinate transformation.

UniformBuffers new in Git master

Use uniform buffers. Expects that uniform data are supplied via bindTransformationProjectionBuffer(), bindDrawBuffer(), bindTextureTransformationBuffer() and bindMaterialBuffer() instead of direct uniform setters.

MultiDraw new in Git master

Enable multidraw functionality. Implies Flag::UniformBuffers and adds the value from setDrawOffset() with the gl_DrawID builtin, which makes draws submitted via GL::AbstractShaderProgram::draw(Containers::ArrayView<const Containers::Reference<MeshView>>) pick up per-draw parameters directly, without having to rebind the uniform buffers or specify setDrawOffset() before each draw. In a non-multidraw scenario, gl_DrawID is 0, which means a shader with this flag enabled can be used for regular draws as well.

Typedef documentation

template<UnsignedInt dimensions>
typedef GenericGL<dimensions>::Position Magnum::Shaders::VectorGL<dimensions>::Position

Vertex position.

Generic attribute, Vector2 in 2D, Vector3 in 3D.

template<UnsignedInt dimensions>
typedef GenericGL<dimensions>::TextureCoordinates Magnum::Shaders::VectorGL<dimensions>::TextureCoordinates

2D texture coordinates

Generic attribute, Vector2.

template<UnsignedInt dimensions>
typedef Containers::EnumSet<Flag> Magnum::Shaders::VectorGL<dimensions>::Flags new in 2020.06

Flags.

Function documentation

template<UnsignedInt dimensions>
Magnum::Shaders::VectorGL<dimensions>::VectorGL(Flags flags = {}) explicit

Constructor.

Parameters
flags Flags

While this function is meant mainly for the classic uniform scenario (without Flag::UniformBuffers set), it's equivalent to VectorGL(Flags, UnsignedInt, UnsignedInt) with materialCount and drawCount set to 1.

template<UnsignedInt dimensions>
Magnum::Shaders::VectorGL<dimensions>::VectorGL(Flags flags, UnsignedInt materialCount, UnsignedInt drawCount) explicit

Construct for a multi-draw scenario.

Parameters
flags Flags
materialCount Size of a VectorMaterialUniform buffer bound with bindMaterialBuffer()
drawCount Size of a TransformationProjectionUniform2D / TransformationProjectionUniform3D / VectorDrawUniform / TextureTransformationUniform buffer bound with bindTransformationProjectionBuffer(), bindDrawBuffer() and bindTextureTransformationBuffer()

If flags contains Flag::UniformBuffers, materialCount and drawCount describe the uniform buffer sizes as these are required to have a statically defined size. The draw offset is then set via setDrawOffset() and the per-draw materials specified via VectorDrawUniform::materialId.

If flags don't contain Flag::UniformBuffers, drawCount is ignored and the constructor behaves the same as VectorGL(Flags).

template<UnsignedInt dimensions>
Magnum::Shaders::VectorGL<dimensions>::VectorGL(NoCreateT) explicit noexcept

Construct without creating the underlying OpenGL object.

The constructed instance is equivalent to a moved-from state. Useful in cases where you will overwrite the instance later anyway. Move another object over it to make it useful.

This function can be safely used for constructing (and later destructing) objects even without any OpenGL context being active. However note that this is a low-level and a potentially dangerous API, see the documentation of NoCreate for alternatives.

template<UnsignedInt dimensions>
UnsignedInt Magnum::Shaders::VectorGL<dimensions>::materialCount() const new in Git master

Material count.

Statically defined size of the VectorMaterialUniform uniform buffer. Has use only if Flag::UniformBuffers is set.

template<UnsignedInt dimensions>
UnsignedInt Magnum::Shaders::VectorGL<dimensions>::drawCount() const new in Git master

Draw count.

Statically defined size of each of the TransformationProjectionUniform2D / TransformationProjectionUniform3D, VectorDrawUniform and TextureTransformationUniform uniform buffers. Has use only if Flag::UniformBuffers is set.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::setTransformationProjectionMatrix(const MatrixTypeFor<dimensions, Float>& matrix)

Set transformation and projection matrix.

Returns Reference to self (for method chaining)

Initial value is an identity matrix.

Expects that Flag::UniformBuffers is not set, in that case fill TransformationProjectionUniform2D::transformationProjectionMatrix / TransformationProjectionUniform3D::transformationProjectionMatrix and call bindTransformationProjectionBuffer() instead.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::setTextureMatrix(const Matrix3& matrix) new in 2020.06

Set texture coordinate transformation matrix.

Returns Reference to self (for method chaining)

Expects that the shader was created with Flag::TextureTransformation enabled. Initial value is an identity matrix.

Expects that Flag::UniformBuffers is not set, in that case fill TextureTransformationUniform::rotationScaling and TextureTransformationUniform::offset and call bindTextureTransformationBuffer() instead.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::setBackgroundColor(const Color4& color)

Set background color.

Returns Reference to self (for method chaining)

Initial value is 0x00000000_rgbaf.

Expects that Flag::UniformBuffers is not set, in that case fill VectorMaterialUniform::backgroundColor and call bindMaterialBuffer() instead.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::setColor(const Color4& color)

Set fill color.

Returns Reference to self (for method chaining)

Initial value is 0xffffffff_rgbaf.

Expects that Flag::UniformBuffers is not set, in that case fill VectorMaterialUniform::color and call bindMaterialBuffer() instead.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::setDrawOffset(UnsignedInt offset) new in Git master

Set a draw offset.

Returns Reference to self (for method chaining)

Specifies which item in the TransformationProjectionUniform2D / TransformationProjectionUniform3D, VectorDrawUniform and TextureTransformationUniform buffers bound with bindTransformationProjectionBuffer(), bindDrawBuffer() and bindTextureTransformationBuffer() should be used for current draw. Expects that Flag::UniformBuffers is set and offset is less than drawCount(). Initial value is 0, if drawCount() is 1, the function is a no-op as the shader assumes draw offset to be always zero.

If Flag::MultiDraw is set, gl_DrawID is added to this value, which makes each draw submitted via GL::AbstractShaderProgram::draw(Containers::ArrayView<const Containers::Reference<MeshView>>) pick up its own per-draw parameters.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindTransformationProjectionBuffer(GL::Buffer& buffer) new in Git master

Set a transformation and projection uniform buffer.

Returns Reference to self (for method chaining)

Expects that Flag::UniformBuffers is set. The buffer is expected to contain drawCount() instances of TransformationProjectionUniform2D / TransformationProjectionUniform3D. At the very least you need to call also bindDrawBuffer() and bindMaterialBuffer().

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindTransformationProjectionBuffer(GL::Buffer& buffer, GLintptr offset, GLsizeiptr size) new in Git master

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindDrawBuffer(GL::Buffer& buffer) new in Git master

Set a draw uniform buffer.

Returns Reference to self (for method chaining)

Expects that Flag::UniformBuffers is set. The buffer is expected to contain drawCount() instances of VectorDrawUniform. At the very least you need to call also bindTransformationProjectionBuffer() and bindMaterialBuffer().

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindDrawBuffer(GL::Buffer& buffer, GLintptr offset, GLsizeiptr size) new in Git master

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindTextureTransformationBuffer(GL::Buffer& buffer) new in Git master

Set a texture transformation uniform buffer.

Returns Reference to self (for method chaining)

Expects that both Flag::UniformBuffers and Flag::TextureTransformation is set. The buffer is expected to contain drawCount() instances of TextureTransformationUniform.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindTextureTransformationBuffer(GL::Buffer& buffer, GLintptr offset, GLsizeiptr size) new in Git master

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindMaterialBuffer(GL::Buffer& buffer) new in Git master

Set a material uniform buffer.

Returns Reference to self (for method chaining)

Expects that Flag::UniformBuffers is set. The buffer is expected to contain materialCount() instances of VectorMaterialUniform. At the very least you need to call also bindTransformationProjectionBuffer() and bindDrawBuffer().

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindMaterialBuffer(GL::Buffer& buffer, GLintptr offset, GLsizeiptr size) new in Git master

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<UnsignedInt dimensions>
VectorGL<dimensions>& Magnum::Shaders::VectorGL<dimensions>::bindVectorTexture(GL::Texture2D& texture)

Bind vector texture.

Returns Reference to self (for method chaining)

template<UnsignedInt dimensions> template<UnsignedInt dimensions>
Debug& operator<<(Debug& debug, VectorGL<dimensions>::Flag value)

Debug output operator.

template<UnsignedInt dimensions> template<UnsignedInt dimensions>
Debug& operator<<(Debug& debug, VectorGL<dimensions>::Flags value)

Debug output operator.