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 or to PhongGL with zero lights and PhongGL::Flag::VertexColor enabled. However, the 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 bound with bindTransformationProjectionBuffer(). 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 transformationProjectionUniform, materialUniform, drawUniform;
transformationProjectionUniform.setData({
    Shaders::TransformationProjectionUniform2D{}
        .setTransformationProjectionMatrix(projectionMatrix*transformationMatrix)
});
materialUniform.setData({
    Shaders::VectorMaterialUniform{}
        .setColor(0x2f83cc_rgbf)
});
drawUniform.setData({
    Shaders::VectorDrawUniform{}
        .setMaterialId(0)
});

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

For a multidraw workflow enable Flag::MultiDraw and supply desired draw count via Configuration::setDrawCount(). 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.

Derived classes

class Magnum::Shaders::VertexColorGL::CompileState new in Git master
Asynchronous compilation state.

Public types

class CompileState new in Git master
Asynchronous compilation state.
class Configuration new in Git master
Configuration.
enum (anonymous): UnsignedInt { ColorOutput = GenericGL<dimensions>::ColorOutput }
enum class Flag: UnsignedByte { UniformBuffers = 1 << 0 new in Git master, ShaderStorageBuffers = UniformBuffers|(1 << 2) 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.

Public static functions

static auto compile(const Configuration& configuration = Configuration{}) -> CompileState new in Git master
Compile asynchronously.
static auto compile(Flags flags) -> CompileState deprecated in Git master
Compile asynchronously.
static auto compile(Flags flags, UnsignedInt drawCount) -> CompileState deprecated in Git master
Compile for a multi-draw scenario asynchronously.

Constructors, destructors, conversion operators

VertexColorGL(const Configuration& configuration = Configuration{}) explicit new in Git master
Constructor.
VertexColorGL(Flags flags) deprecated in Git master explicit
Constructor.
VertexColorGL(Flags flags, UnsignedInt drawCount) deprecated in Git master explicit
Construct for a multi-draw scenario.
VertexColorGL(CompileState&& state) explicit new in Git master
Finalize an asynchronous compilation.
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.

ShaderStorageBuffers new in Git master

Use shader storage buffers. Superset of functionality provided by Flag::UniformBuffers, compared to it doesn't have any size limits on setDrawCount() in exchange for potentially more costly access and narrower platform support.

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(const Containers::Iterable<MeshView>&) and related APIs 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>
static CompileState Magnum::Shaders::VertexColorGL<dimensions>::compile(const Configuration& configuration = Configuration{}) new in Git master

Compile asynchronously.

Compared to VertexColorGL(const Configuration&) can perform an asynchronous compilation and linking. See Async shader compilation and linking for more information.

template<UnsignedInt dimensions>
static CompileState Magnum::Shaders::VertexColorGL<dimensions>::compile(Flags flags)

Compile asynchronously.

template<UnsignedInt dimensions>
static CompileState Magnum::Shaders::VertexColorGL<dimensions>::compile(Flags flags, UnsignedInt drawCount)

Compile for a multi-draw scenario asynchronously.

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

Constructor.

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

Construct for a multi-draw scenario.

template<UnsignedInt dimensions>
Magnum::Shaders::VertexColorGL<dimensions>::VertexColorGL(CompileState&& state) explicit new in Git master

Finalize an asynchronous compilation.

Takes an asynchronous compilation state returned by compile() and forms a ready-to-use shader object. See Async shader compilation and linking for more information.

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>
Flags Magnum::Shaders::VertexColorGL<dimensions>::flags() const

Flags.

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 bound with bindTransformationProjectionBuffer(). Has use only if Flag::UniformBuffers is set and Flag::ShaderStorageBuffers is not 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(const Containers::Iterable<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

Bind a transformation and projection uniform / shader storage 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.