From 0fe2ab0aa9402c5da7675b80ec7d69e2a8daf760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 22 Feb 2017 16:17:40 +0100 Subject: [PATCH] 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. --- src/Magnum/DebugTools/ObjectRenderer.cpp | 2 +- src/Magnum/Shaders/Generic.h | 29 +++++++++++++++++++++--- src/Magnum/Shaders/VertexColor.frag | 4 ++-- src/Magnum/Shaders/VertexColor.h | 11 ++++++--- src/Magnum/Shaders/VertexColor2D.vert | 4 ++-- src/Magnum/Shaders/VertexColor3D.vert | 6 ++--- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Magnum/DebugTools/ObjectRenderer.cpp b/src/Magnum/DebugTools/ObjectRenderer.cpp index aa71cec98..bebbc4c95 100644 --- a/src/Magnum/DebugTools/ObjectRenderer.cpp +++ b/src/Magnum/DebugTools/ObjectRenderer.cpp @@ -171,7 +171,7 @@ template ObjectRenderer::ObjectRenderer(Scen .setCount(Renderer::indices.size()) .addVertexBuffer(*vertexBuffer, 0, typename Shaders::VertexColor::Position(), - typename Shaders::VertexColor::Color()) + typename Shaders::VertexColor::Color{Shaders::VertexColor::Color::Components::Four}) .setIndexBuffer(*indexBuffer, 0, Mesh::IndexType::UnsignedByte, 0, Renderer::positions.size()); ResourceManager::instance().set(_mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual); } diff --git a/src/Magnum/Shaders/Generic.h b/src/Magnum/Shaders/Generic.h index 66c0ae48f..5f3e90223 100644 --- a/src/Magnum/Shaders/Generic.h +++ b/src/Magnum/Shaders/Generic.h @@ -70,9 +70,25 @@ template 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 { diff --git a/src/Magnum/Shaders/VertexColor.frag b/src/Magnum/Shaders/VertexColor.frag index 9e281bebf..8d48c3fd3 100644 --- a/src/Magnum/Shaders/VertexColor.frag +++ b/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; } diff --git a/src/Magnum/Shaders/VertexColor.h b/src/Magnum/Shaders/VertexColor.h index 5c527bfe3..3449456bd 100644 --- a/src/Magnum/Shaders/VertexColor.h +++ b/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 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::Color Color; diff --git a/src/Magnum/Shaders/VertexColor2D.vert b/src/Magnum/Shaders/VertexColor2D.vert index c803f83a9..b5ab39bfb 100644 --- a/src/Magnum/Shaders/VertexColor2D.vert +++ b/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); diff --git a/src/Magnum/Shaders/VertexColor3D.vert b/src/Magnum/Shaders/VertexColor3D.vert index 5542ef6dd..4cf67a095 100644 --- a/src/Magnum/Shaders/VertexColor3D.vert +++ b/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;