From 0e9cb6945e676c9fe2fe72c5766ff53d1af68f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 4 Aug 2018 15:17:06 +0200 Subject: [PATCH] Shaders: define reasonable uniform defaults for all shaders. Plus a bit of cleanup / fixing broken English. --- doc/changelog.dox | 2 + doc/shaders.dox | 7 ++- src/Magnum/Shaders/AbstractVector2D.vert | 6 ++- src/Magnum/Shaders/AbstractVector3D.vert | 6 ++- src/Magnum/Shaders/DistanceFieldVector.cpp | 2 + src/Magnum/Shaders/DistanceFieldVector.frag | 8 +++- src/Magnum/Shaders/DistanceFieldVector.h | 21 ++++++--- src/Magnum/Shaders/Flat.cpp | 4 +- src/Magnum/Shaders/Flat.h | 25 +++++----- src/Magnum/Shaders/Flat2D.vert | 6 ++- src/Magnum/Shaders/Flat3D.vert | 6 ++- src/Magnum/Shaders/Generic.h | 7 ++- src/Magnum/Shaders/MeshVisualizer.cpp | 4 +- src/Magnum/Shaders/MeshVisualizer.geom | 2 +- src/Magnum/Shaders/MeshVisualizer.h | 18 +++++--- src/Magnum/Shaders/MeshVisualizer.vert | 6 ++- src/Magnum/Shaders/Phong.cpp | 11 +++-- src/Magnum/Shaders/Phong.frag | 2 +- src/Magnum/Shaders/Phong.h | 51 +++++++++++++-------- src/Magnum/Shaders/Phong.vert | 22 +++++++-- src/Magnum/Shaders/Vector.cpp | 6 +++ src/Magnum/Shaders/Vector.frag | 8 +++- src/Magnum/Shaders/Vector.h | 17 ++++--- src/Magnum/Shaders/VertexColor.h | 7 +-- 24 files changed, 172 insertions(+), 82 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 2bc4b825f..d7425cc6b 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -157,6 +157,8 @@ See also: @subsubsection changelog-latest-changes-shaders Shaders library +- All shaders now have reasonable default values for uniforms in order to + further simplify and remove friction from quick prototyping use cases - @ref Shaders::Flat::bindTexture(), @ref Shaders::Phong::bindAmbientTexture(), @ref Shaders::Phong::bindDiffuseTexture(), @ref Shaders::Phong::bindSpecularTexture() and diff --git a/doc/shaders.dox b/doc/shaders.dox index 2b17427cb..44fa4c134 100644 --- a/doc/shaders.dox +++ b/doc/shaders.dox @@ -63,8 +63,11 @@ usage examples. Example mesh configuration for @ref Shaders::Phong shader: Each shader then has its own set of configuration functions. Some configuration is static, specified commonly as flags in constructor, directly affecting compiled shader code. Other configuration is specified through uniforms and -various binding points, commonly exposed through various setters. Example -configuration and rendering using @link Shaders::Phong @endlink: +various binding points, commonly exposed through various setters. All shader +uniforms have a reasonable defaults so you are able to see at least something +when using the shader directly without any further configuration, but in most +cases you may want to specify at least the transformation/projection matrices. +Example configuration and rendering using @link Shaders::Phong @endlink: @snippet MagnumShaders.cpp shaders-rendering diff --git a/src/Magnum/Shaders/AbstractVector2D.vert b/src/Magnum/Shaders/AbstractVector2D.vert index ad60a1a16..9c38f120f 100644 --- a/src/Magnum/Shaders/AbstractVector2D.vert +++ b/src/Magnum/Shaders/AbstractVector2D.vert @@ -31,7 +31,11 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif -uniform highp mat3 transformationProjectionMatrix; +uniform highp mat3 transformationProjectionMatrix + #ifndef GL_ES + = mat3(1.0) + #endif + ; #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/AbstractVector3D.vert b/src/Magnum/Shaders/AbstractVector3D.vert index 578ac7de2..7e419200c 100644 --- a/src/Magnum/Shaders/AbstractVector3D.vert +++ b/src/Magnum/Shaders/AbstractVector3D.vert @@ -31,7 +31,11 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif -uniform highp mat4 transformationProjectionMatrix; +uniform highp mat4 transformationProjectionMatrix + #ifndef GL_ES + = mat4(1.0) + #endif + ; #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/DistanceFieldVector.cpp b/src/Magnum/Shaders/DistanceFieldVector.cpp index ab8ef990f..e00f20ac2 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.cpp +++ b/src/Magnum/Shaders/DistanceFieldVector.cpp @@ -99,6 +99,8 @@ template DistanceFieldVector::DistanceFieldV /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES + setTransformationProjectionMatrix({}); + setColor(Color4{1.0f}); /* Outline color is zero by default */ setOutlineRange(0.5f, 1.0f); setSmoothness(0.04f); #endif diff --git a/src/Magnum/Shaders/DistanceFieldVector.frag b/src/Magnum/Shaders/DistanceFieldVector.frag index 7d96f1165..e56cfdff9 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.frag +++ b/src/Magnum/Shaders/DistanceFieldVector.frag @@ -32,12 +32,16 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 1) #endif -uniform lowp vec4 color; +uniform lowp vec4 color + #ifndef GL_ES + = vec4(1.0, 1.0, 1.0, 1.0) + #endif + ; #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 2) #endif -uniform lowp vec4 outlineColor; +uniform lowp vec4 outlineColor; /* defaults to zero */ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 3) diff --git a/src/Magnum/Shaders/DistanceFieldVector.h b/src/Magnum/Shaders/DistanceFieldVector.h index 050c0df7a..e24ba98e2 100644 --- a/src/Magnum/Shaders/DistanceFieldVector.h +++ b/src/Magnum/Shaders/DistanceFieldVector.h @@ -41,13 +41,15 @@ namespace Magnum { namespace Shaders { /** @brief Distance field vector shader -Renders vector graphics in form of signed distance field. See +Renders vector graphics in a form of signed distance field. See @ref TextureTools::distanceField() for more information. Note that the final rendered outlook will greatly depend on radius of input distance field and value passed to @ref setSmoothness(). You need to provide @ref Position and @ref TextureCoordinates attributes in your triangle mesh and call at least -@ref setTransformationProjectionMatrix(), @ref setColor() and -@ref bindVectorTexture(). +@ref bindVectorTexture(). By default, the shader renders the distance field +texture with a white color in an identity transformation, use +@ref setTransformationProjectionMatrix(), @ref setColor() and others to +configure the shader. @image html shaders-distancefieldvector.png @@ -102,6 +104,8 @@ template class MAGNUM_SHADERS_EXPORT DistanceFieldVector /** * @brief Set transformation and projection matrix * @return Reference to self (for method chaining) + * + * Initial value is an identity matrix. */ DistanceFieldVector& setTransformationProjectionMatrix(const MatrixTypeFor& matrix) { GL::AbstractShaderProgram::setUniform(_transformationProjectionMatrixUniform, matrix); @@ -112,6 +116,7 @@ template class MAGNUM_SHADERS_EXPORT DistanceFieldVector * @brief Set fill color * @return Reference to self (for method chaining) * + * Initial value is @cpp 0xffffffff_rgbaf @ce. * @see @ref setOutlineColor() */ DistanceFieldVector& setColor(const Color4& color) { @@ -123,6 +128,8 @@ template class MAGNUM_SHADERS_EXPORT DistanceFieldVector * @brief Set outline color * @return Reference to self (for method chaining) * + * Initial value is @cpp 0x00000000_rgbaf @ce and the outline is not + * drawn --- see @ref setOutlineRange() for more information. * @see @ref setOutlineRange(), @ref setColor() */ DistanceFieldVector& setOutlineColor(const Color4& color) { @@ -134,11 +141,11 @@ template class MAGNUM_SHADERS_EXPORT DistanceFieldVector * @brief Set outline range * @return Reference to self (for method chaining) * - * Parameter @p start describes where fill ends and possible outline - * starts. Initial value is @cpp 0.5f @ce, larger values will make the - * vector art look thinner, smaller will make it look thicker. + * The @p start parameter describes where fill ends and possible + * outline starts. Initial value is @cpp 0.5f @ce, larger values will + * make the vector art look thinner, smaller will make it look thicker. * - * Parameter @p end describes where outline ends. If set to value + * The @p end parameter describes where outline ends. If set to value * larger than @p start the outline is not drawn. Initial value is * @cpp 1.0f @ce. * diff --git a/src/Magnum/Shaders/Flat.cpp b/src/Magnum/Shaders/Flat.cpp index ee9d48655..b2aab4063 100644 --- a/src/Magnum/Shaders/Flat.cpp +++ b/src/Magnum/Shaders/Flat.cpp @@ -102,8 +102,8 @@ template Flat::Flat(const Flags flags): _fla /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES - /* Default to fully opaque white so we can see the texture */ - if(flags & Flag::Textured) setColor(Color4(1.0f)); + setTransformationProjectionMatrix({}); + setColor(Color4{1.0f}); if(flags & Flag::AlphaMask) setAlphaMask(0.5f); #endif } diff --git a/src/Magnum/Shaders/Flat.h b/src/Magnum/Shaders/Flat.h index 4dc042b66..eadec22b8 100644 --- a/src/Magnum/Shaders/Flat.h +++ b/src/Magnum/Shaders/Flat.h @@ -50,15 +50,16 @@ namespace Implementation { /** @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 -@ref setTransformationProjectionMatrix() and @ref setColor(). +Draws the whole mesh with given color or texture. For a colored mesh you need +to provide the @ref Position attribute in your triangle mesh. By default, the +shader renders the mesh with a white color in an identity transformation. +Use @ref setTransformationProjectionMatrix(), @ref setColor() and others to +configure the shader. -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 bindTexture(). The texture is -multipled by the color, which is by default set to fully opaque white if -texturing is enabled. +If you want to use a texture, you need to provide also @ref TextureCoordinates +attribute. Pass @ref Flag::Textured to the constructor and then at render time +don't forget to bind also the texture via @ref bindTexture(). The texture is +multipled by the color, which is by default set to @cpp 0xffffffff_rgbaf @ce. For coloring the texture based on intensity you can use the @ref Vector shader. @@ -191,6 +192,8 @@ template class MAGNUM_SHADERS_EXPORT Flat: public GL::Ab /** * @brief Set transformation and projection matrix * @return Reference to self (for method chaining) + * + * Initial value is an identity matrix. */ Flat& setTransformationProjectionMatrix(const MatrixTypeFor& matrix) { setUniform(_transformationProjectionMatrixUniform, matrix); @@ -201,7 +204,7 @@ template class MAGNUM_SHADERS_EXPORT Flat: public GL::Ab * @brief Set color * @return Reference to self (for method chaining) * - * If @ref Flag::Textured is set, default value is + * If @ref Flag::Textured is set, initial value is * @cpp 0xffffffff_rgbaf @ce and the color will be multiplied with * texture. * @see @ref bindTexture() @@ -212,7 +215,7 @@ template class MAGNUM_SHADERS_EXPORT Flat: public GL::Ab } /** - * @brief Bind texture + * @brief Bind a color texture * @return Reference to self (for method chaining) * * Expects that the shader was created with @ref Flag::Textured @@ -227,7 +230,7 @@ template class MAGNUM_SHADERS_EXPORT Flat: public GL::Ab * * Expects that the shader was created with @ref Flag::AlphaMask * enabled. Fragments with alpha values smaller than the mask value - * will be discarded. Default is @cpp 0.5f @ce. See the flag + * will be discarded. Initial value is @cpp 0.5f @ce. See the flag * documentation for further information. */ Flat& setAlphaMask(Float mask); diff --git a/src/Magnum/Shaders/Flat2D.vert b/src/Magnum/Shaders/Flat2D.vert index 4941c2b91..5b64e0541 100644 --- a/src/Magnum/Shaders/Flat2D.vert +++ b/src/Magnum/Shaders/Flat2D.vert @@ -31,7 +31,11 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif -uniform highp mat3 transformationProjectionMatrix; +uniform highp mat3 transformationProjectionMatrix + #ifndef GL_ES + = mat3(1.0) + #endif + ; #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/Flat3D.vert b/src/Magnum/Shaders/Flat3D.vert index b49a6856c..e3d5b01be 100644 --- a/src/Magnum/Shaders/Flat3D.vert +++ b/src/Magnum/Shaders/Flat3D.vert @@ -31,7 +31,11 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif -uniform highp mat4 transformationProjectionMatrix; +uniform highp mat4 transformationProjectionMatrix + #ifndef GL_ES + = mat4(1.0) + #endif + ; #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/Generic.h b/src/Magnum/Shaders/Generic.h index 5d5c4d334..2ab726d8f 100644 --- a/src/Magnum/Shaders/Generic.h +++ b/src/Magnum/Shaders/Generic.h @@ -36,10 +36,9 @@ namespace Magnum { namespace Shaders { /** @brief Generic shader definition -Definitions common for majority of shaders in @ref Shaders namespace, allowing -mesh configured for the generic shader to be used with any of them. See -@ref shaders-generic for more information. - +Definitions common for majority of shaders in the @ref Shaders namespace, +allowing mesh configured for a generic shader to be used with any of them. +See @ref shaders-generic for more information. @see @ref shaders, @ref Generic2D, @ref Generic3D */ #ifndef DOXYGEN_GENERATING_OUTPUT diff --git a/src/Magnum/Shaders/MeshVisualizer.cpp b/src/Magnum/Shaders/MeshVisualizer.cpp index 7f6e3c44f..765e7a450 100644 --- a/src/Magnum/Shaders/MeshVisualizer.cpp +++ b/src/Magnum/Shaders/MeshVisualizer.cpp @@ -141,9 +141,11 @@ MeshVisualizer::MeshVisualizer(const Flags flags): _flags{flags} { /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ #ifdef MAGNUM_TARGET_GLES + setTransformationProjectionMatrix({}); setColor(Color3(1.0f)); if(_flags & Flag::Wireframe) { - setWireframeColor(Color3(0.0f)); + /* Viewport size is zero by default */ + setWireframeColor(Color3{0.0f}); setWireframeWidth(1.0f); setSmoothness(2.0f); } diff --git a/src/Magnum/Shaders/MeshVisualizer.geom b/src/Magnum/Shaders/MeshVisualizer.geom index 9dcf7a1d8..c8c6a199c 100644 --- a/src/Magnum/Shaders/MeshVisualizer.geom +++ b/src/Magnum/Shaders/MeshVisualizer.geom @@ -37,7 +37,7 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 1) #endif -uniform lowp vec2 viewportSize; +uniform lowp vec2 viewportSize; /* defaults to zero */ layout(triangles) in; diff --git a/src/Magnum/Shaders/MeshVisualizer.h b/src/Magnum/Shaders/MeshVisualizer.h index 89df8f440..0af4c6e61 100644 --- a/src/Magnum/Shaders/MeshVisualizer.h +++ b/src/Magnum/Shaders/MeshVisualizer.h @@ -40,8 +40,10 @@ namespace Magnum { namespace Shaders { @brief Mesh visualization shader Uses geometry shader to visualize wireframe of 3D meshes. You need to provide -@ref Position attribute in your triangle mesh and call at least -@ref setTransformationProjectionMatrix() to be able to render. +the @ref Position attribute in your triangle mesh. By default, the shader +renders the mesh with a white color in an identity transformation. Use +@ref setTransformationProjectionMatrix(), @ref setColor() and others to +configure the shader. @image html shaders-meshvisualizer.png @@ -59,8 +61,8 @@ If you have geometry shaders available, you don't need to do anything else. If you don't have geometry shaders, you need to set @ref Flag::NoGeometryShader (it's enabled by default in OpenGL ES 2.0) and use only **non-indexed** triangle -meshes (see @ref MeshTools::duplicate() for possible solution). Additionaly, if -you have OpenGL < 3.1 or OpenGL ES 2.0, you need to provide also +meshes (see @ref MeshTools::duplicate() for a possible solution). Additionaly, +if you have OpenGL < 3.1 or OpenGL ES 2.0, you need to provide also the @ref VertexIndex attribute. @requires_gles30 Extension @gl_extension{OES,standard_derivatives} for @@ -186,6 +188,8 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizer: public GL::AbstractShaderProgram { /** * @brief Set transformation and projection matrix * @return Reference to self (for method chaining) + * + * Initial value is an identity matrix. */ MeshVisualizer& setTransformationProjectionMatrix(const Matrix4& matrix) { setUniform(_transformationProjectionMatrixUniform, matrix); @@ -197,7 +201,7 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizer: public GL::AbstractShaderProgram { * @return Reference to self (for method chaining) * * Has effect only if @ref Flag::Wireframe is enabled and geometry - * shaders are used. + * shaders are used. Initial value is a zero vector. */ MeshVisualizer& setViewportSize(const Vector2& size) { if(_flags & Flag::Wireframe && !(_flags & Flag::NoGeometryShader)) @@ -209,7 +213,7 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizer: public GL::AbstractShaderProgram { * @brief Set base object color * @return Reference to self (for method chaining) * - * Initial value is fully opaque white. + * Initial value is @cpp 0xffffffff_rgbaf @ce. */ MeshVisualizer& setColor(const Color4& color) { setUniform(_colorUniform, color); @@ -220,7 +224,7 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizer: public GL::AbstractShaderProgram { * @brief Set wireframe color * @return Reference to self (for method chaining) * - * Initial value is fully opaque black. Has effect only if + * Initial value is @cpp 0x000000ff_rgbaf @ce. Has effect only if * @ref Flag::Wireframe is enabled. */ MeshVisualizer& setWireframeColor(const Color4& color) { diff --git a/src/Magnum/Shaders/MeshVisualizer.vert b/src/Magnum/Shaders/MeshVisualizer.vert index 1a5b702e9..1c9ed28fd 100644 --- a/src/Magnum/Shaders/MeshVisualizer.vert +++ b/src/Magnum/Shaders/MeshVisualizer.vert @@ -31,7 +31,11 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif -uniform highp mat4 transformationProjectionMatrix; +uniform highp mat4 transformationProjectionMatrix + #ifndef GL_ES + = mat4(1.0) + #endif + ; #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) diff --git a/src/Magnum/Shaders/Phong.cpp b/src/Magnum/Shaders/Phong.cpp index a5a217e24..2a682269c 100644 --- a/src/Magnum/Shaders/Phong.cpp +++ b/src/Magnum/Shaders/Phong.cpp @@ -95,7 +95,7 @@ Phong::Phong(const Flags flags): _flags(flags) { _transformationMatrixUniform = uniformLocation("transformationMatrix"); _projectionMatrixUniform = uniformLocation("projectionMatrix"); _normalMatrixUniform = uniformLocation("normalMatrix"); - _lightUniform = uniformLocation("light"); + _lightPositionUniform = uniformLocation("lightPosition"); _ambientColorUniform = uniformLocation("ambientColor"); _diffuseColorUniform = uniformLocation("diffuseColor"); _specularColorUniform = uniformLocation("specularColor"); @@ -118,13 +118,16 @@ Phong::Phong(const Flags flags): _flags(flags) { /* Default to fully opaque white so we can see the textures */ if(flags & Flag::AmbientTexture) setAmbientColor(Color4{1.0f}); else setAmbientColor(Color4{0.0f, 1.0f}); - - if(flags & Flag::DiffuseTexture) setDiffuseColor(Color4{1.0f}); - + setDiffuseColor(Color4{1.0f}); setSpecularColor(Color4{1.0f}); setLightColor(Color4{1.0f}); setShininess(80.0f); if(flags & Flag::AlphaMask) setAlphaMask(0.5f); + + setTransformationMatrix({}); + setProjectionMatrix({}); + setNormalMatrix({}); + /* Light position is zero by default */ #endif } diff --git a/src/Magnum/Shaders/Phong.frag b/src/Magnum/Shaders/Phong.frag index c4e9e889d..8436b7b89 100644 --- a/src/Magnum/Shaders/Phong.frag +++ b/src/Magnum/Shaders/Phong.frag @@ -64,7 +64,7 @@ uniform lowp sampler2D diffuseTexture; layout(location = 5) #endif uniform lowp vec4 diffuseColor - #if !defined(GL_ES) && defined(DIFFUSE_TEXTURE) + #ifndef GL_ES = vec4(1.0) #endif ; diff --git a/src/Magnum/Shaders/Phong.h b/src/Magnum/Shaders/Phong.h index 7ac0f3138..238349931 100644 --- a/src/Magnum/Shaders/Phong.h +++ b/src/Magnum/Shaders/Phong.h @@ -41,9 +41,11 @@ namespace Magnum { namespace Shaders { @brief Phong shader Uses ambient, diffuse and specular color or texture. For colored mesh you need -to provide @ref Position and @ref Normal attributes in your triangle mesh and -call at least @ref setTransformationMatrix(), @ref setNormalMatrix(), -@ref setProjectionMatrix(), @ref setDiffuseColor() and @ref setLightPosition(). +to provide the @ref Position and @ref Normal attributes in your triangle mesh. +By default, the shader renders the mesh with a white color in an identity +transformation. Use @ref setTransformationMatrix(), @ref setNormalMatrix(), +@ref setProjectionMatrix(), @ref setLightPosition() and others to configure +the shader. If you want to use textures, you need to provide also @ref TextureCoordinates attribute. Pass appropriate @ref Flags to constructor and then at render time @@ -216,7 +218,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { } /** - * @brief Bind ambient texture + * @brief Bind an ambient texture * @return Reference to self (for method chaining) * * Expects that the shader was created with @ref Flag::AmbientTexture @@ -238,9 +240,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { * @brief Set diffuse color * @return Reference to self (for method chaining) * - * If @ref Flag::DiffuseTexture is set, default value is - * @cpp 0xffffffff_rgbaf @ce and the color will be multiplied with - * diffuse texture. + * Initial value is @cpp 0xffffffff_rgbaf @ce. * @see @ref bindDiffuseTexture() */ Phong& setDiffuseColor(const Color4& color) { @@ -249,7 +249,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { } /** - * @brief Bind diffuse texture + * @brief Bind a diffuse texture * @return Reference to self (for method chaining) * * Expects that the shader was created with @ref Flag::DiffuseTexture @@ -271,10 +271,10 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { * @brief Set specular color * @return Reference to self (for method chaining) * - * Default value is @cpp 0xffffffff_rgbaf @ce. Color will be multiplied + * Initial value is @cpp 0xffffffff_rgbaf @ce. Color will be multiplied * with specular texture if @ref Flag::SpecularTexture is set. If you * want to have a fully diffuse material, set specular color to - * @cpp 0x000000_rgbf @ce. + * @cpp 0x000000ff_rgbaf @ce. * @see @ref bindSpecularTexture() */ Phong& setSpecularColor(const Color4& color) { @@ -283,7 +283,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { } /** - * @brief Bind specular texture + * @brief Bind a specular texture * @return Reference to self (for method chaining) * * Expects that the shader was created with @ref Flag::SpecularTexture @@ -306,8 +306,8 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { * @return Reference to self (for method chaining) * * A particular texture has effect only if particular texture flag from - * @ref Phong::Flag "Flag" is set, you can use `nullptr` for the rest. - * Expects that the shader was created with at least one of + * @ref Phong::Flag "Flag" is set, you can use @cpp nullptr @ce for the + * rest. Expects that the shader was created with at least one of * @ref Flag::AmbientTexture, @ref Flag::DiffuseTexture or * @ref Flag::SpecularTexture enabled. More efficient than setting each * texture separately. @@ -330,7 +330,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { * @return Reference to self (for method chaining) * * The larger value, the harder surface (smaller specular highlight). - * If not set, default value is @cpp 80.0f @ce. + * Initial value is @cpp 80.0f @ce. */ Phong& setShininess(Float shininess) { setUniform(_shininessUniform, shininess); @@ -343,7 +343,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { * * Expects that the shader was created with @ref Flag::AlphaMask * enabled. Fragments with alpha values smaller than the mask value - * will be discarded. Default is @cpp 0.5f @ce. See the flag + * will be discarded. Initial value is @cpp 0.5f @ce. See the flag * documentation for further information. */ Phong& setAlphaMask(Float mask); @@ -351,6 +351,9 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { /** * @brief Set transformation matrix * @return Reference to self (for method chaining) + * + * You need to set also @ref setNormalMatrix() with a corresponding + * value. Initial value is an identity matrix. */ Phong& setTransformationMatrix(const Matrix4& matrix) { setUniform(_transformationMatrixUniform, matrix); @@ -362,7 +365,9 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { * @return Reference to self (for method chaining) * * The matrix doesn't need to be normalized, as the renormalization - * must be done in the shader anyway. + * must be done in the shader anyway. You need to set also + * @ref setTransformationMatrix() with a corresponding value. Initial + * value is an identity matrix. */ Phong& setNormalMatrix(const Matrix3x3& matrix) { setUniform(_normalMatrixUniform, matrix); @@ -372,6 +377,10 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { /** * @brief Set projection matrix * @return Reference to self (for method chaining) + * + * Initial value is an identity matrix (i.e., an orthographic + * projection of the default @f$ [ -\boldsymbol{1} ; \boldsymbol{1} ] @f$ + * cube). */ Phong& setProjectionMatrix(const Matrix4& matrix) { setUniform(_projectionMatrixUniform, matrix); @@ -381,9 +390,13 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { /** * @brief Set light position * @return Reference to self (for method chaining) + * + * Initial value is a zero vector --- that will in most cases cause the + * object to be rendered black (or in the ambient color), as the light + * is inside of it. */ Phong& setLightPosition(const Vector3& light) { - setUniform(_lightUniform, light); + setUniform(_lightPositionUniform, light); return *this; } @@ -391,7 +404,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { * @brief Set light color * @return Reference to self (for method chaining) * - * If not set, default value is @cpp 0xffffffff_rgbaf @ce. + * Initial value is @cpp 0xffffffff_rgbaf @ce. */ Phong& setLightColor(const Color4& color) { setUniform(_lightColorUniform, color); @@ -403,7 +416,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public GL::AbstractShaderProgram { Int _transformationMatrixUniform{0}, _projectionMatrixUniform{1}, _normalMatrixUniform{2}, - _lightUniform{3}, + _lightPositionUniform{3}, _ambientColorUniform{4}, _diffuseColorUniform{5}, _specularColorUniform{6}, diff --git a/src/Magnum/Shaders/Phong.vert b/src/Magnum/Shaders/Phong.vert index 2d65437d6..31093f288 100644 --- a/src/Magnum/Shaders/Phong.vert +++ b/src/Magnum/Shaders/Phong.vert @@ -31,22 +31,34 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 0) #endif -uniform highp mat4 transformationMatrix; +uniform highp mat4 transformationMatrix + #ifndef GL_ES + = mat4(1.0) + #endif + ; #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 1) #endif -uniform highp mat4 projectionMatrix; +uniform highp mat4 projectionMatrix + #ifndef GL_ES + = mat4(1.0) + #endif + ; #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 2) #endif -uniform mediump mat3 normalMatrix; +uniform mediump mat3 normalMatrix + #ifndef GL_ES + = mat3(1.0) + #endif + ; #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 3) #endif -uniform highp vec3 light; +uniform highp vec3 lightPosition; /* defaults to zero */ #ifdef EXPLICIT_ATTRIB_LOCATION layout(location = POSITION_ATTRIBUTE_LOCATION) @@ -80,7 +92,7 @@ void main() { transformedNormal = normalMatrix*normal; /* Direction to the light */ - lightDirection = normalize(light - transformedPosition); + lightDirection = normalize(lightPosition - transformedPosition); /* Direction to the camera */ cameraDirection = -transformedPosition; diff --git a/src/Magnum/Shaders/Vector.cpp b/src/Magnum/Shaders/Vector.cpp index 8824dd33b..7f09bc1ec 100644 --- a/src/Magnum/Shaders/Vector.cpp +++ b/src/Magnum/Shaders/Vector.cpp @@ -93,6 +93,12 @@ template Vector::Vector() { { GL::AbstractShaderProgram::setUniform(GL::AbstractShaderProgram::uniformLocation("vectorTexture"), AbstractVector::VectorTextureLayer); } + + /* Set defaults in OpenGL ES (for desktop they are set in shader code itself) */ + #ifdef MAGNUM_TARGET_GLES + setTransformationProjectionMatrix({}); + setColor(Color4{1.0f}); /* Background color is zero by default */ + #endif } template class Vector<2>; diff --git a/src/Magnum/Shaders/Vector.frag b/src/Magnum/Shaders/Vector.frag index e9044bc20..6aa3db1fa 100644 --- a/src/Magnum/Shaders/Vector.frag +++ b/src/Magnum/Shaders/Vector.frag @@ -32,12 +32,16 @@ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 1) #endif -uniform lowp vec4 backgroundColor; +uniform lowp vec4 backgroundColor; /* defaults to zero */ #ifdef EXPLICIT_UNIFORM_LOCATION layout(location = 2) #endif -uniform lowp vec4 color; +uniform lowp vec4 color + #ifndef GL_ES + = vec4(1.0) + #endif + ; #ifdef EXPLICIT_TEXTURE_LAYER layout(binding = 15) diff --git a/src/Magnum/Shaders/Vector.h b/src/Magnum/Shaders/Vector.h index 1ed0194aa..368b71121 100644 --- a/src/Magnum/Shaders/Vector.h +++ b/src/Magnum/Shaders/Vector.h @@ -42,11 +42,13 @@ namespace Magnum { namespace Shaders { @brief Vector shader Renders vector art in plain grayscale form. See also @ref DistanceFieldVector -for more advanced effects. For rendering unchanged texture you can use the -@ref Flat shader. You need to provide @ref Position and @ref TextureCoordinates -attributes in your triangle mesh and call at least -@ref setTransformationProjectionMatrix(), @ref setColor() and -@ref bindVectorTexture(). +for more advanced effects. For rendering an unchanged texture you can use the +@ref Flat shader. You need to provide the @ref Position and +@ref TextureCoordinates attributes in your triangle mesh and call at least +@ref bindVectorTexture(). By default, the shader renders the texture with a +white color in an identity transformation. Use +@ref setTransformationProjectionMatrix(), @ref setColor() and others to +configure the shader. @image html shaders-vector.png @image latex shaders-vector.png @@ -99,6 +101,8 @@ template class MAGNUM_SHADERS_EXPORT Vector: public Abst /** * @brief Set transformation and projection matrix * @return Reference to self (for method chaining) + * + * Default is an identity matrix. */ Vector& setTransformationProjectionMatrix(const MatrixTypeFor& matrix) { GL::AbstractShaderProgram::setUniform(_transformationProjectionMatrixUniform, matrix); @@ -109,7 +113,7 @@ template class MAGNUM_SHADERS_EXPORT Vector: public Abst * @brief Set background color * @return Reference to self (for method chaining) * - * Default is transparent black. + * Default is @cpp 0x00000000_rgbaf @ce. * @see @ref setColor() */ Vector& setBackgroundColor(const Color4& color) { @@ -121,6 +125,7 @@ template class MAGNUM_SHADERS_EXPORT Vector: public Abst * @brief Set fill color * @return Reference to self (for method chaining) * + * Default is @cpp 0xffffffff_rgbaf @ce. * @see @ref setBackgroundColor() */ Vector& setColor(const Color4& color) { diff --git a/src/Magnum/Shaders/VertexColor.h b/src/Magnum/Shaders/VertexColor.h index 395fd0104..89678ec57 100644 --- a/src/Magnum/Shaders/VertexColor.h +++ b/src/Magnum/Shaders/VertexColor.h @@ -43,8 +43,9 @@ namespace Magnum { namespace Shaders { @brief Vertex color shader Draws vertex-colored mesh. You need to provide @ref Position and @ref Color -attributes in your triangle mesh and call at least -@ref setTransformationProjectionMatrix(). +attributes in your triangle mesh. By default, the shader renders the mesh in +an identity transformation. Use @ref setTransformationProjectionMatrix() to +configure the shader. @image html shaders-vertexcolor.png @@ -126,7 +127,7 @@ template class MAGNUM_SHADERS_EXPORT VertexColor: public * @brief Set transformation and projection matrix * @return Reference to self (for method chaining) * - * Default is identity matrix. + * Default is an identity matrix. */ VertexColor& setTransformationProjectionMatrix(const MatrixTypeFor& matrix) { setUniform(_transformationProjectionMatrixUniform, matrix);