Browse Source

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
pull/33/head
Miguel Martin 13 years ago
parent
commit
1eaf3b8591
  1. 15
      src/Shaders/Flat.cpp
  2. 9
      src/Shaders/Flat.frag
  3. 13
      src/Shaders/Flat.h

15
src/Shaders/Flat.cpp

@ -41,22 +41,23 @@ template<UnsignedInt dimensions> Flat<dimensions>::Flat(const Flags flags): tran
Utility::Resource rs("MagnumShaders"); Utility::Resource rs("MagnumShaders");
#ifndef MAGNUM_TARGET_GLES #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 #else
const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200}); const Version version = Context::current()->supportedVersion({Version::GLES300, Version::GLES200});
#endif #endif
Shader vert(version, Shader::Type::Fragment); Shader vert(version, Shader::Type::Vertex);
vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") vert.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "")
.addSource(rs.get("compatibility.glsl")) .addSource(rs.get("compatibility.glsl"))
.addSource(rs.get("Flat.frag")); .addSource(rs.get("generic.glsl"))
.addSource(rs.get(vertexShaderName<dimensions>()));
CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile());
attachShader(vert); attachShader(vert);
Shader frag(version, Shader::Type::Vertex); Shader frag(version, Shader::Type::Fragment);
frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "") frag.addSource(flags & Flag::Textured ? "#define TEXTURED\n" : "")
.addSource(rs.get("compatibility.glsl")) .addSource(rs.get("compatibility.glsl"))
.addSource(rs.get(vertexShaderName<dimensions>())); .addSource(rs.get("Flat.frag"));
CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile()); CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile());
attachShader(frag); attachShader(frag);
@ -77,11 +78,11 @@ template<UnsignedInt dimensions> Flat<dimensions>::Flat(const Flags flags): tran
#endif #endif
{ {
transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix"); transformationProjectionMatrixUniform = uniformLocation("transformationProjectionMatrix");
if(!(flags & Flag::Textured)) colorUniform = uniformLocation("color"); colorUniform = uniformLocation("color");
} }
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
if(flags && !Context::current()->isExtensionSupported<Extensions::GL::ARB::shading_language_420pack>(version)) if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::shading_language_420pack>(version))
#endif #endif
{ {
if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureLayer); if(flags & Flag::Textured) setUniform(uniformLocation("textureData"), TextureLayer);

9
src/Shaders/Flat.frag

@ -25,6 +25,7 @@
#ifndef NEW_GLSL #ifndef NEW_GLSL
#define fragmentColor gl_FragColor #define fragmentColor gl_FragColor
#define texture texture2D #define texture texture2D
#define in varying
#endif #endif
#ifdef TEXTURED #ifdef TEXTURED
@ -33,16 +34,16 @@ layout(binding = 0) uniform sampler2D textureData;
#else #else
uniform sampler2D textureData; uniform sampler2D textureData;
#endif #endif
#else #endif
#ifdef EXPLICIT_UNIFORM_LOCATION #ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 1) uniform vec4 color; layout(location = 1) uniform vec4 color;
#else #else
uniform lowp vec4 color; uniform lowp vec4 color;
#endif #endif
#endif
#ifdef TEXTURED #ifdef TEXTURED
in mediump vec2 interpolatedTextureCoords; in mediump vec2 interpolatedTextureCoordinates;
#endif #endif
#ifdef NEW_GLSL #ifdef NEW_GLSL
@ -51,7 +52,7 @@ out lowp vec4 fragmentColor;
void main() { void main() {
#ifdef TEXTURED #ifdef TEXTURED
fragmentColor = texture(textureData, interpolatedTextureCoords); fragmentColor = color * texture(textureData, interpolatedTextureCoordinates);
#else #else
fragmentColor = color; fragmentColor = color;
#endif #endif

13
src/Shaders/Flat.h

@ -30,9 +30,9 @@
#include "Math/Matrix3.h" #include "Math/Matrix3.h"
#include "Math/Matrix4.h" #include "Math/Matrix4.h"
#include "AbstractShaderProgram.h"
#include "Color.h" #include "Color.h"
#include "DimensionTraits.h" #include "DimensionTraits.h"
#include "Shaders/Generic.h"
#include "magnumShadersVisibility.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 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 need to provide @ref Position attribute in your triangle mesh and call at least
@ -67,14 +67,14 @@ myTexture.bind(Shaders::Flat2D::TextureLayer);
template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT Flat: public AbstractShaderProgram { template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT Flat: public AbstractShaderProgram {
public: public:
/** @brief Vertex position */ /** @brief Vertex position */
typedef Attribute<0, typename DimensionTraits<dimensions, Float>::VectorType> Position; typedef typename Generic<dimensions>::Position Position;
/** /**
* @brief Texture coordinates * @brief Texture coordinates
* *
* Used only if @ref Flag::Textured is set. * Used only if @ref Flag::Textured is set.
*/ */
typedef Attribute<2, Vector2> TextureCoordinates; typedef typename Generic<dimensions>::TextureCoordinates TextureCoordinates;
enum: Int { enum: Int {
/** Layer for color texture. Used only if @ref Flag::Textured is set. */ /** Layer for color texture. Used only if @ref Flag::Textured is set. */
@ -124,7 +124,8 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT Flat: public Abstra
* @brief Set color * @brief Set color
* @return Reference to self (for method chaining) * @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<dimensions>& setColor(const Color4& color); Flat<dimensions>& setColor(const Color4& color);
@ -144,7 +145,7 @@ typedef Flat<3> Flat3D;
CORRADE_ENUMSET_OPERATORS(Implementation::FlatFlags) CORRADE_ENUMSET_OPERATORS(Implementation::FlatFlags)
template<UnsignedInt dimensions> inline Flat<dimensions>& Flat<dimensions>::setColor(const Color4& color) { template<UnsignedInt dimensions> inline Flat<dimensions>& Flat<dimensions>::setColor(const Color4& color) {
if(!(_flags & Flag::Textured)) setUniform(colorUniform, color); setUniform(colorUniform, color);
return *this; return *this;
} }

Loading…
Cancel
Save