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

Vertex color OpenGL shader.

Draws a vertex-colored mesh. You need to provide Position and Color3 / Color4 attributes in your triangle mesh. By default, the shader renders the mesh in an identity transformation. Use setTransformationProjectionMatrix() to configure the shader.

Image

This shader is equivalent to FlatGL with FlatGL::Flag::VertexColor enabled; the 3D version of this shader is equivalent to PhongGL with PhongGL::Flag::VertexColor enabled. In both cases this implementation is much simpler and thus likely also faster.

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. The shader accepts either three- or four-component color attribute, use either Color3 or Color4 to specify which one you use.

struct Vertex {
    Vector3 position;
    Color3 color;
};
Vertex data[60]{
    // ...
};

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

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

Common rendering setup:

Matrix4 transformationMatrix = Matrix4::translation(Vector3::zAxis(-5.0f));
Matrix4 projectionMatrix =
    Matrix4::perspectiveProjection(35.0_degf, 1.0f, 0.001f, 100.0f);

Shaders::VertexColorGL3D shader;
shader.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. This is also the only buffer supplied, as there are no other draw parameters. 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 and supply desired draw count in the VertexColorGL(Flags, UnsignedInt) constructor. 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 { UniformBuffers = 1 << 0 new in Git master, MultiDraw = UniformBuffers|(1 << 1) new in Git master } new in 2020.06
Flag.
using Position = GenericGL<dimensions>::Position
Vertex position.
using Color3 = GenericGL<dimensions>::Color3
Three-component vertex color.
using Color4 = GenericGL<dimensions>::Color4
Four-component vertex color.
using Flags = Containers::EnumSet<Flag> new in 2020.06
Flags.

Constructors, destructors, conversion operators

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

Public functions

auto operator=(const VertexColorGL<dimensions>&) -> VertexColorGL<dimensions>& deleted
Copying is not allowed.
auto operator=(VertexColorGL<dimensions>&&) -> VertexColorGL<dimensions>& defaulted noexcept
Move assignment.
auto flags() const -> Flags
Flags.
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) -> VertexColorGL<dimensions>&
Set transformation and projection matrix.

Enum documentation

template<UnsignedInt dimensions>
enum Magnum::Shaders::VertexColorGL<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::VertexColorGL<dimensions>::Flag: UnsignedByte new in 2020.06

Flag.

Enumerators
UniformBuffers new in Git master

Use uniform buffers. Expects that uniform data are supplied via bindTransformationProjectionBuffer() 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::VertexColorGL<dimensions>::Position

Vertex position.

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

template<UnsignedInt dimensions>
typedef GenericGL<dimensions>::Color3 Magnum::Shaders::VertexColorGL<dimensions>::Color3

Three-component vertex color.

Generic attribute, Magnum::Color3. Use either this or the Color4 attribute.

template<UnsignedInt dimensions>
typedef GenericGL<dimensions>::Color4 Magnum::Shaders::VertexColorGL<dimensions>::Color4

Four-component vertex color.

Generic attribute, Magnum::Color4. Use either this or the Color3 attribute.

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

Flags.

Function documentation

template<UnsignedInt dimensions>
Magnum::Shaders::VertexColorGL<dimensions>::VertexColorGL(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 VertexColorGL(Flags, UnsignedInt) with drawCount set to 1.

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

Construct for a multi-draw scenario.

Parameters
flags Flags
drawCount Size of a TransformationProjectionUniform2D / TransformationProjectionUniform3D buffer bound with bindTransformationProjectionBuffer()

If flags contains Flag::UniformBuffers, drawCount describes the uniform buffer sizes as these are required to have a statically defined size. The draw offset is then set via setDrawOffset().

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

template<UnsignedInt dimensions>
Magnum::Shaders::VertexColorGL<dimensions>::VertexColorGL(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::VertexColorGL<dimensions>::drawCount() const new in Git master

Draw count.

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

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

Set transformation and projection matrix.

Returns Reference to self (for method chaining)

Default 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>
VertexColorGL<dimensions>& Magnum::Shaders::VertexColorGL<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 buffers bound with bindTransformationProjectionBuffer() 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>
VertexColorGL<dimensions>& Magnum::Shaders::VertexColorGL<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.

template<UnsignedInt dimensions>
VertexColorGL<dimensions>& Magnum::Shaders::VertexColorGL<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> template<UnsignedInt dimensions>
Debug& operator<<(Debug& debug, VertexColorGL<dimensions>::Flag value)

Debug output operator.

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

Debug output operator.