From 1eaf3b859188afb11b422e7970cd4e38da9d2875 Mon Sep 17 00:00:00 2001 From: Miguel Martin Date: Sat, 14 Dec 2013 19:33:15 +1030 Subject: [PATCH] Added colour option for Flat shader even when texturing is enabled This allows blending to be done (with the texture and colour) For: transparency and changing the colour of an object --- src/Shaders/Flat.cpp | 15 ++++++++------- src/Shaders/Flat.frag | 9 +++++---- src/Shaders/Flat.h | 13 +++++++------ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Shaders/Flat.cpp b/src/Shaders/Flat.cpp index 864890981..be697bce5 100644 --- a/src/Shaders/Flat.cpp +++ b/src/Shaders/Flat.cpp @@ -41,22 +41,23 @@ template Flat::Flat(const Flags flags): tran Utility::Resource rs("MagnumShaders"); #ifndef MAGNUM_TARGET_GLES - const Version version = Context::current()->supportedVersion({Version::GL310, Version::GL300, Version::GL210}); + const Version version = Context::current()->supportedVersion({Version::GL320, Version::GL310, Version::GL300, Version::GL210}); #else const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); #endif - Shader vert(version, Shader::Type::Fragment); + Shader vert(version, Shader::Type::Vertex); vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") .addSource(rs.get("compatibility.glsl")) - .addSource(rs.get("Flat.frag")); + .addSource(rs.get("generic.glsl")) + .addSource(rs.get(vertexShaderName())); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); attachShader(vert); - Shader frag(version, Shader::Type::Vertex); + Shader frag(version, Shader::Type::Fragment); frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") .addSource(rs.get("compatibility.glsl")) - .addSource(rs.get(vertexShaderName())); + .addSource(rs.get("Flat.frag")); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); attachShader(frag); @@ -77,11 +78,11 @@ template Flat::Flat(const Flags flags): tran #endif { transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); - if(!(flags & Flag::Textured)) colorUniform = uniformLocation("color"); + colorUniform = uniformLocation("color"); } #ifndef MAGNUM_TARGET_GLES - if(flags && !Context::current()->isExtensionSupported(version)) + if(!Context::current()->isExtensionSupported(version)) #endif { if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureLayer); diff --git a/src/Shaders/Flat.frag b/src/Shaders/Flat.frag index 665ed51d6..cff8496ad 100644 --- a/src/Shaders/Flat.frag +++ b/src/Shaders/Flat.frag @@ -25,6 +25,7 @@ #ifndef NEW_GLSL #define fragmentColor gl_FragColor #define texture texture2D +#define in varying #endif #ifdef TEXTURED @@ -33,16 +34,16 @@ layout(binding = 0) uniform sampler2D textureData; #else uniform sampler2D textureData; #endif -#else +#endif + #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 1) uniform vec4 color; #else uniform lowp vec4 color; #endif -#endif #ifdef TEXTURED -in mediump vec2 interpolatedTextureCoords; +in mediump vec2 interpolatedTextureCoordinates; #endif #ifdef NEW_GLSL @@ -51,7 +52,7 @@ out lowp vec4 fragmentColor; void main() { #ifdef TEXTURED - fragmentColor = texture(textureData, interpolatedTextureCoords); + fragmentColor = color * texture(textureData, interpolatedTextureCoordinates); #else fragmentColor = color; #endif diff --git a/src/Shaders/Flat.h b/src/Shaders/Flat.h index f36f106f8..955a62f2e 100644 --- a/src/Shaders/Flat.h +++ b/src/Shaders/Flat.h @@ -30,9 +30,9 @@ #include "Math/Matrix3.h" #include "Math/Matrix4.h" -#include "AbstractShaderProgram.h" #include "Color.h" #include "DimensionTraits.h" +#include "Shaders/Generic.h" #include "magnumShadersVisibility.h" @@ -44,7 +44,7 @@ namespace Implementation { } /** -@brief Flat shader +@brief %Flat shader Draws whole mesh with given unshaded color or texture. For colored mesh you need to provide @ref Position attribute in your triangle mesh and call at least @@ -67,14 +67,14 @@ myTexture.bind(Shaders::Flat2D::TextureLayer); template class MAGNUM_SHADERS_EXPORT Flat: public AbstractShaderProgram { public: /** @brief Vertex position */ - typedef Attribute<0, typename DimensionTraits::VectorType> Position; + typedef typename Generic::Position Position; /** * @brief Texture coordinates * * Used only if @ref Flag::Textured is set. */ - typedef Attribute<2, Vector2> TextureCoordinates; + typedef typename Generic::TextureCoordinates TextureCoordinates; enum: Int { /** Layer for color texture. Used only if @ref Flag::Textured is set. */ @@ -124,7 +124,8 @@ template class MAGNUM_SHADERS_EXPORT Flat: public Abstra * @brief Set color * @return Reference to self (for method chaining) * - * Has no effect if @ref Flag::Textured is set. + * Color will be multiplied with texture + * if @ref Flag::Textured is set. */ Flat& setColor(const Color4& color); @@ -144,7 +145,7 @@ typedef Flat<3> Flat3D; CORRADE_ENUMSET_OPERATORS(Implementation::FlatFlags) template inline Flat& Flat::setColor(const Color4& color) { - if(!(_flags & Flag::Textured)) setUniform(colorUniform, color); + setUniform(colorUniform, color); return *this; }