From 3eb7f6f2b8aa2b842b481e6bfc86d12e71b00e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 20 Nov 2013 01:31:25 +0100 Subject: [PATCH] Shaders: added generic shader definition. Defines common attributes which are shared by majority of the shaders, allowing mesh to be configured for the generic shader to be used with any of them. --- src/Shaders/AbstractVector.h | 7 +- src/Shaders/CMakeLists.txt | 1 + src/Shaders/DistanceFieldVector.h | 1 + src/Shaders/Flat.h | 6 +- src/Shaders/Generic.h | 108 ++++++++++++++++++++++++++++++ src/Shaders/Phong.h | 8 +-- src/Shaders/Shaders.h | 2 + src/Shaders/Vector.h | 1 + src/Shaders/VertexColor.h | 4 +- 9 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 src/Shaders/Generic.h diff --git a/src/Shaders/AbstractVector.h b/src/Shaders/AbstractVector.h index 89115edd3..ec4bedcb9 100644 --- a/src/Shaders/AbstractVector.h +++ b/src/Shaders/AbstractVector.h @@ -28,8 +28,7 @@ * @brief Class Magnum::Shaders::AbstractVector, typedef Magnum::Shaders::AbstractVector2D, Magnum::Shaders::AbstractVector3D */ -#include "AbstractShaderProgram.h" -#include "DimensionTraits.h" +#include "Shaders/Generic.h" namespace Magnum { namespace Shaders { @@ -41,10 +40,10 @@ namespace Magnum { namespace Shaders { template class AbstractVector: public AbstractShaderProgram { public: /** @brief Vertex position */ - typedef Attribute<0, typename DimensionTraits::VectorType> Position; + typedef typename Generic::Position Position; /** @brief Texture coordinates */ - typedef Attribute<1, Vector2> TextureCoordinates; + typedef typename Generic::TextureCoordinates TextureCoordinates; enum: Int { VectorTextureLayer = 16 /**< Layer for vector texture */ diff --git a/src/Shaders/CMakeLists.txt b/src/Shaders/CMakeLists.txt index 41b921b8f..cd66e8b93 100644 --- a/src/Shaders/CMakeLists.txt +++ b/src/Shaders/CMakeLists.txt @@ -37,6 +37,7 @@ set(MagnumShaders_HEADERS DistanceFieldVector.h AbstractVector.h Flat.h + Generic.h MeshVisualizer.h Phong.h Shaders.h diff --git a/src/Shaders/DistanceFieldVector.h b/src/Shaders/DistanceFieldVector.h index dc80aa958..9c61647de 100644 --- a/src/Shaders/DistanceFieldVector.h +++ b/src/Shaders/DistanceFieldVector.h @@ -31,6 +31,7 @@ #include "Math/Matrix3.h" #include "Math/Matrix4.h" #include "Color.h" +#include "DimensionTraits.h" #include "AbstractVector.h" #include "magnumShadersVisibility.h" diff --git a/src/Shaders/Flat.h b/src/Shaders/Flat.h index 862023537..8e2b69627 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" @@ -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<1, Vector2> TextureCoordinates; + typedef typename Generic::TextureCoordinates TextureCoordinates; enum: Int { /** Layer for color texture. Used only if @ref Flag::Textured is set. */ diff --git a/src/Shaders/Generic.h b/src/Shaders/Generic.h new file mode 100644 index 000000000..5676cbd0c --- /dev/null +++ b/src/Shaders/Generic.h @@ -0,0 +1,108 @@ +#ifndef Magnum_Shaders_GenericShader_h +#define Magnum_Shaders_GenericShader_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +/** @file + * @brief Struct @ref Magnum::Shaders::Generic, typedef @ref Magnum::Shaders::Generic2D, @ref Magnum::Shaders::Generic3D + */ + +#include "AbstractShaderProgram.h" + +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. + +Example usage (configuring the mesh for generic shader, then using it with +@ref Shaders::Phong): +@code +Mesh mesh; +Buffer vertexBuffer; + +// ... + +mesh.addVertexBuffer(vertexBuffer, 0, + Shaders::Generic3D::Position(), + Shaders::Generic3D::Normal(), + Shaders::Generic3D::TextureCoordinates()); + +Shaders::Phong phong; +// ... +phong.use(); +mesh.draw(); +@endcode + +@see @ref Generic2D, @ref Generic3D +*/ +#ifndef DOXYGEN_GENERATING_OUTPUT +template struct Generic; +#else +template struct Generic { + /** + * @brief Vertex position + * + * Defined as @ref Vector2 in 2D and @ref Vector3 in 3D. + */ + typedef AbstractShaderProgram::Attribute<0, T> Position; + + /** @brief 2D texture coordinates */ + typedef AbstractShaderProgram::Attribute<1, Vector2> TextureCoordinates; + + /** + * @brief Vertex normal + * + * Defined only in 3D. + */ + typedef AbstractShaderProgram::Attribute<2, Vector3> Normal; +}; +#endif + +/** @brief Generic 2D shader definition */ +typedef Generic<2> Generic2D; + +/** @brief Generic 3D shader definition */ +typedef Generic<3> Generic3D; + +#ifndef DOXYGEN_GENERATING_OUTPUT +struct BaseGeneric { + typedef AbstractShaderProgram::Attribute<1, Vector2> TextureCoordinates; +}; + +template<> struct Generic<2>: BaseGeneric { + typedef AbstractShaderProgram::Attribute<0, Vector2> Position; +}; + +template<> struct Generic<3>: BaseGeneric { + typedef AbstractShaderProgram::Attribute<0, Vector3> Position; + typedef AbstractShaderProgram::Attribute<2, Vector3> Normal; +}; +#endif + +}} + +#endif diff --git a/src/Shaders/Phong.h b/src/Shaders/Phong.h index f2a4c1234..2d380a7d7 100644 --- a/src/Shaders/Phong.h +++ b/src/Shaders/Phong.h @@ -29,8 +29,8 @@ */ #include "Math/Matrix4.h" -#include "AbstractShaderProgram.h" #include "Color.h" +#include "Shaders/Generic.h" #include "magnumShadersVisibility.h" @@ -58,8 +58,8 @@ myDiffuseTexture.bind(Shaders::Phong::DiffuseTextureLayer); */ class MAGNUM_SHADERS_EXPORT Phong: public AbstractShaderProgram { public: - typedef Attribute<0, Vector3> Position; /**< @brief Vertex position */ - typedef Attribute<2, Vector3> Normal; /**< @brief Normal direction */ + typedef Generic3D::Position Position; /**< @brief Vertex position */ + typedef Generic3D::Normal Normal; /**< @brief Normal direction */ /** * @brief Texture coordinates @@ -67,7 +67,7 @@ class MAGNUM_SHADERS_EXPORT Phong: public AbstractShaderProgram { * Used only if one of @ref Flag::AmbientTexture, @ref Flag::DiffuseTexture * or @ref Flag::SpecularTexture is set. */ - typedef Attribute<1, Vector2> TextureCoordinates; + typedef Generic3D::TextureCoordinates TextureCoordinates; enum: Int { /** diff --git a/src/Shaders/Shaders.h b/src/Shaders/Shaders.h index 96ee60972..95d259f1d 100644 --- a/src/Shaders/Shaders.h +++ b/src/Shaders/Shaders.h @@ -44,6 +44,8 @@ template class Flat; typedef Flat<2> Flat2D; typedef Flat<3> Flat3D; +/* Generic is used only statically */ + class MeshVisualizer; class Phong; diff --git a/src/Shaders/Vector.h b/src/Shaders/Vector.h index 33dcd3bbf..45c800bab 100644 --- a/src/Shaders/Vector.h +++ b/src/Shaders/Vector.h @@ -31,6 +31,7 @@ #include "Math/Matrix3.h" #include "Math/Matrix4.h" #include "Color.h" +#include "DimensionTraits.h" #include "Shaders/AbstractVector.h" #include "magnumShadersVisibility.h" diff --git a/src/Shaders/VertexColor.h b/src/Shaders/VertexColor.h index 8bb656004..067e859f9 100644 --- a/src/Shaders/VertexColor.h +++ b/src/Shaders/VertexColor.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" @@ -47,7 +47,7 @@ Draws vertex-colored mesh. template class MAGNUM_SHADERS_EXPORT VertexColor: public AbstractShaderProgram { public: /** @brief Vertex position */ - typedef Attribute<0, typename DimensionTraits::VectorType> Position; + typedef typename Generic::Position Position; /** @brief Vertex color */ typedef Attribute<3, Color3> Color;