From f52a086f8eef37127cecf9f13296e1730f293137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 6 Sep 2015 11:02:22 +0200 Subject: [PATCH] Shaders: clean up Flat, set default color only for textured one. Also probably fixed a few issues when compiling the shader on older GLSL and GLSL ES (floating point literal suffixed, missing precision qualifiers). And less crazy preprocessor. --- src/Magnum/Shaders/Flat.cpp | 3 ++- src/Magnum/Shaders/Flat.frag | 32 +++++++++++++------------------- src/Magnum/Shaders/Flat.h | 25 +++++++++++-------------- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/Magnum/Shaders/Flat.cpp b/src/Magnum/Shaders/Flat.cpp index 912f3780c..5d62c9acf 100644 --- a/src/Magnum/Shaders/Flat.cpp +++ b/src/Magnum/Shaders/Flat.cpp @@ -100,7 +100,8 @@ template Flat::Flat(const Flags flags): tran /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES - setColor(Color4(1.0f)); // Default to white, with full transperancy (so we can see the texture) + /* Default to fully opaque white so we can see the texture */ + if(flags & Flag::Textured) setColor(Color4(1.0f)); #endif } diff --git a/src/Magnum/Shaders/Flat.frag b/src/Magnum/Shaders/Flat.frag index d98543db2..847a7c129 100644 --- a/src/Magnum/Shaders/Flat.frag +++ b/src/Magnum/Shaders/Flat.frag @@ -31,25 +31,19 @@ #ifdef TEXTURED #ifdef EXPLICIT_TEXTURE_LAYER -layout(binding = 0) uniform sampler2D textureData; -#else -uniform sampler2D textureData; +layout(binding = 0) #endif +uniform lowp sampler2D textureData; #endif #ifdef EXPLICIT_UNIFORM_LOCATION -# ifndef GL_ES -layout(location = 1) uniform vec4 color = vec4(1.0f, 1.0f, 1.0f, 1.0f); -# else -layout(location = 1) uniform vec4 color; -# endif -#else -# ifndef GL_ES -uniform lowp vec4 color = vec4(1.0f, 1.0f, 1.0f, 1.0f); -# else -uniform lowp vec4 color; -# endif +layout(location = 1) #endif +uniform lowp vec4 color + #if !defined(GL_ES) && defined(TEXTURED) + = vec4(1.0) + #endif + ; #ifdef TEXTURED in mediump vec2 interpolatedTextureCoordinates; @@ -60,9 +54,9 @@ out lowp vec4 fragmentColor; #endif void main() { - #ifdef TEXTURED - fragmentColor = color * texture(textureData, interpolatedTextureCoordinates); - #else - fragmentColor = color; - #endif + fragmentColor = + #ifdef TEXTURED + texture(textureData, interpolatedTextureCoordinates)* + #endif + color; } diff --git a/src/Magnum/Shaders/Flat.h b/src/Magnum/Shaders/Flat.h index a54c520c8..bf8c1a85c 100644 --- a/src/Magnum/Shaders/Flat.h +++ b/src/Magnum/Shaders/Flat.h @@ -50,11 +50,11 @@ 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 @ref setTransformationProjectionMatrix() and @ref setColor(). -If you want to use texture instead of color, you need to provide also -@ref TextureCoordinates attribute. Pass @ref Flag::Textured to constructor and -then at render time don't forget to set also the texture via @ref setTexture(). -The texture will be multiplied with the color (which is white by default, thus -it doesn't change texture color). +If you want to use texture, you need to provide also @ref TextureCoordinates +attribute. Pass @ref Flag::Textured to constructor and then at render time +don't forget to set also the texture via @ref setTexture(). The texture is +multipled by the color, which is by default set to fully opaque white if +texturing is enabled. For coloring the texture based on intensity you can use the @ref Vector shader. @@ -185,12 +185,14 @@ template class MAGNUM_SHADERS_EXPORT Flat: public Abstra * @brief Set color * @return Reference to self (for method chaining) * - * If not set, default value is fully opaque white. Color will be - * multiplied with texture if @ref Flag::Textured is set. + * If @ref Flag::Textured is set, default value is `{1.0f, 1.0f, 1.0f}` + * and the color will be multiplied with texture. * @see @ref setTexture() */ - /* MSVC needs inline also here to avoid linkage conflicts */ - inline Flat& setColor(const Color4& color); + Flat& setColor(const Color4& color){ + setUniform(colorUniform, color); + return *this; + } /** * @brief Set texture @@ -216,11 +218,6 @@ typedef Flat<3> Flat3D; CORRADE_ENUMSET_OPERATORS(Implementation::FlatFlags) -template inline Flat& Flat::setColor(const Color4& color) { - setUniform(colorUniform, color); - return *this; -} - }} #endif