Browse Source

Shaders: ability to use RGBA colors with VertexColor shader.

It was just RGB before. In order to avoid breaking current code, the
attribute now requires explicit specification of number of components.
If building with deprecated APIs enabled, there is an additional
deprecated constructor that defaults to RGB so the current code should
keep working as-is.
pull/195/head
Vladimír Vondruš 9 years ago
parent
commit
0fe2ab0aa9
  1. 2
      src/Magnum/DebugTools/ObjectRenderer.cpp
  2. 29
      src/Magnum/Shaders/Generic.h
  3. 4
      src/Magnum/Shaders/VertexColor.frag
  4. 11
      src/Magnum/Shaders/VertexColor.h
  5. 4
      src/Magnum/Shaders/VertexColor2D.vert
  6. 6
      src/Magnum/Shaders/VertexColor3D.vert

2
src/Magnum/DebugTools/ObjectRenderer.cpp

@ -171,7 +171,7 @@ template<UnsignedInt dimensions> ObjectRenderer<dimensions>::ObjectRenderer(Scen
.setCount(Renderer<dimensions>::indices.size())
.addVertexBuffer(*vertexBuffer, 0,
typename Shaders::VertexColor<dimensions>::Position(),
typename Shaders::VertexColor<dimensions>::Color())
typename Shaders::VertexColor<dimensions>::Color{Shaders::VertexColor<dimensions>::Color::Components::Four})
.setIndexBuffer(*indexBuffer, 0, Mesh::IndexType::UnsignedByte, 0, Renderer<dimensions>::positions.size());
ResourceManager::instance().set<Mesh>(_mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
}

29
src/Magnum/Shaders/Generic.h

@ -70,9 +70,25 @@ template<UnsignedInt dimensions> struct Generic {
/**
* @brief Vertex color
*
* @ref Color3.
* @ref Color4, however defaults to @ref Color3 if @ref MAGNUM_BUILD_DEPRECATED
* is defined. See the constructor documentation for more information.
*/
typedef Attribute<3, Color3> Color;
struct Color: Attribute<3, Color4> {
/**
* @brief Constructor
* @param components Component count
* @param dataType Type of passed data
* @param dataOptions Data options
*/
constexpr explicit Color(Components components, DataType dataType = DataType::Float, DataOptions dataOptions = {});
#ifdef MAGNUM_BUILD_DEPRECATED
/** @copybrief Color(Components, DataType, DataOptions)
* @deprecated Use @ref Color(Components, DataType, DataOptions) instead.
*/
CORRADE_DEPRECATED("use Color(Components, DataType, DataOptions) instead") constexpr explicit Color(DataType dataType = DataType::Float, DataOptions dataOptions = {});
#endif
};
};
#endif
@ -85,7 +101,14 @@ typedef Generic<3> Generic3D;
#ifndef DOXYGEN_GENERATING_OUTPUT
struct BaseGeneric {
typedef Attribute<1, Vector2> TextureCoordinates;
typedef Attribute<3, Color3> Color;
struct Color: Attribute<3, Color4> {
constexpr explicit Color(Components components, DataType dataType = DataType::Float, DataOptions dataOptions = DataOptions()): Attribute<3, Color4>{components, dataType, dataOptions} {}
#ifdef MAGNUM_BUILD_DEPRECATED
CORRADE_DEPRECATED("use Color(Components, DataType, DataOptions) instead") constexpr explicit Color(DataType dataType = DataType::Float, DataOptions dataOptions = DataOptions()): Attribute<3, Color4>{Components::Three, dataType, dataOptions} {}
#endif
};
};
template<> struct Generic<2>: BaseGeneric {

4
src/Magnum/Shaders/VertexColor.frag

@ -28,12 +28,12 @@
#define fragmentColor gl_FragColor
#endif
in lowp vec3 interpolatedColor;
in lowp vec4 interpolatedColor;
#ifdef NEW_GLSL
out lowp vec4 fragmentColor;
#endif
void main() {
fragmentColor = vec4(interpolatedColor, 1.0);
fragmentColor = interpolatedColor;
}

11
src/Magnum/Shaders/VertexColor.h

@ -51,7 +51,10 @@ attributes in your triangle mesh and call at least
## Example usage
Common mesh setup:
Common mesh setup. Note the explicit specification of components for the color
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
struct Vertex {
Vector3 position;
@ -65,7 +68,7 @@ vertices.setData(data, BufferUsage::StaticDraw);
Mesh mesh;
mesh.addVertexBuffer(vertices, 0,
Shaders::VertexColor3D::Position{},
Shaders::VertexColor3D::Color{});
Shaders::VertexColor3D::Color{Shaders::VertexColor3D::Color::Components::Three});
@endcode
Common rendering setup:
@ -94,7 +97,9 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VertexColor: public
/**
* @brief Vertex color
*
* @ref shaders-generic "Generic attribute", @ref Color3.
* @ref shaders-generic "Generic attribute", @ref Color4, however
* defaults to @ref Color3 if @ref MAGNUM_BUILD_DEPRECATED is defined.
* See the @ref Generic::Color for more information.
*/
typedef typename Generic<dimensions>::Color Color;

4
src/Magnum/Shaders/VertexColor2D.vert

@ -45,9 +45,9 @@ in highp vec2 position;
#ifdef EXPLICIT_ATTRIB_LOCATION
layout(location = 3)
#endif
in lowp vec3 color;
in lowp vec4 color;
out lowp vec3 interpolatedColor;
out lowp vec4 interpolatedColor;
void main() {
gl_Position.xywz = vec4(transformationProjectionMatrix*vec3(position, 1.0), 0.0);

6
src/Magnum/Shaders/VertexColor3D.vert

@ -40,13 +40,13 @@ uniform highp mat4 transformationProjectionMatrix;
#ifdef EXPLICIT_ATTRIB_LOCATION
layout(location = POSITION_ATTRIBUTE_LOCATION) in highp vec4 position;
layout(location = 3) in lowp vec3 color;
layout(location = 3) in lowp vec4 color;
#else
in highp vec4 position;
in lowp vec3 color;
in lowp vec4 color;
#endif
out lowp vec3 interpolatedColor;
out lowp vec4 interpolatedColor;
void main() {
gl_Position = transformationProjectionMatrix*position;

Loading…
Cancel
Save