Browse Source

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.
pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
3eb7f6f2b8
  1. 7
      src/Shaders/AbstractVector.h
  2. 1
      src/Shaders/CMakeLists.txt
  3. 1
      src/Shaders/DistanceFieldVector.h
  4. 6
      src/Shaders/Flat.h
  5. 108
      src/Shaders/Generic.h
  6. 8
      src/Shaders/Phong.h
  7. 2
      src/Shaders/Shaders.h
  8. 1
      src/Shaders/Vector.h
  9. 4
      src/Shaders/VertexColor.h

7
src/Shaders/AbstractVector.h

@ -28,8 +28,7 @@
* @brief Class Magnum::Shaders::AbstractVector, typedef Magnum::Shaders::AbstractVector2D, Magnum::Shaders::AbstractVector3D * @brief Class Magnum::Shaders::AbstractVector, typedef Magnum::Shaders::AbstractVector2D, Magnum::Shaders::AbstractVector3D
*/ */
#include "AbstractShaderProgram.h" #include "Shaders/Generic.h"
#include "DimensionTraits.h"
namespace Magnum { namespace Shaders { namespace Magnum { namespace Shaders {
@ -41,10 +40,10 @@ namespace Magnum { namespace Shaders {
template<UnsignedInt dimensions> class AbstractVector: public AbstractShaderProgram { template<UnsignedInt dimensions> class AbstractVector: 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 */
typedef Attribute<1, Vector2> TextureCoordinates; typedef typename Generic<dimensions>::TextureCoordinates TextureCoordinates;
enum: Int { enum: Int {
VectorTextureLayer = 16 /**< Layer for vector texture */ VectorTextureLayer = 16 /**< Layer for vector texture */

1
src/Shaders/CMakeLists.txt

@ -37,6 +37,7 @@ set(MagnumShaders_HEADERS
DistanceFieldVector.h DistanceFieldVector.h
AbstractVector.h AbstractVector.h
Flat.h Flat.h
Generic.h
MeshVisualizer.h MeshVisualizer.h
Phong.h Phong.h
Shaders.h Shaders.h

1
src/Shaders/DistanceFieldVector.h

@ -31,6 +31,7 @@
#include "Math/Matrix3.h" #include "Math/Matrix3.h"
#include "Math/Matrix4.h" #include "Math/Matrix4.h"
#include "Color.h" #include "Color.h"
#include "DimensionTraits.h"
#include "AbstractVector.h" #include "AbstractVector.h"
#include "magnumShadersVisibility.h" #include "magnumShadersVisibility.h"

6
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"
@ -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<1, 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. */

108
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š <mosra@centrum.cz>
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<UnsignedInt> struct Generic;
#else
template<UnsignedInt dimensions> 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

8
src/Shaders/Phong.h

@ -29,8 +29,8 @@
*/ */
#include "Math/Matrix4.h" #include "Math/Matrix4.h"
#include "AbstractShaderProgram.h"
#include "Color.h" #include "Color.h"
#include "Shaders/Generic.h"
#include "magnumShadersVisibility.h" #include "magnumShadersVisibility.h"
@ -58,8 +58,8 @@ myDiffuseTexture.bind(Shaders::Phong::DiffuseTextureLayer);
*/ */
class MAGNUM_SHADERS_EXPORT Phong: public AbstractShaderProgram { class MAGNUM_SHADERS_EXPORT Phong: public AbstractShaderProgram {
public: public:
typedef Attribute<0, Vector3> Position; /**< @brief Vertex position */ typedef Generic3D::Position Position; /**< @brief Vertex position */
typedef Attribute<2, Vector3> Normal; /**< @brief Normal direction */ typedef Generic3D::Normal Normal; /**< @brief Normal direction */
/** /**
* @brief Texture coordinates * @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 * Used only if one of @ref Flag::AmbientTexture, @ref Flag::DiffuseTexture
* or @ref Flag::SpecularTexture is set. * or @ref Flag::SpecularTexture is set.
*/ */
typedef Attribute<1, Vector2> TextureCoordinates; typedef Generic3D::TextureCoordinates TextureCoordinates;
enum: Int { enum: Int {
/** /**

2
src/Shaders/Shaders.h

@ -44,6 +44,8 @@ template<UnsignedInt> class Flat;
typedef Flat<2> Flat2D; typedef Flat<2> Flat2D;
typedef Flat<3> Flat3D; typedef Flat<3> Flat3D;
/* Generic is used only statically */
class MeshVisualizer; class MeshVisualizer;
class Phong; class Phong;

1
src/Shaders/Vector.h

@ -31,6 +31,7 @@
#include "Math/Matrix3.h" #include "Math/Matrix3.h"
#include "Math/Matrix4.h" #include "Math/Matrix4.h"
#include "Color.h" #include "Color.h"
#include "DimensionTraits.h"
#include "Shaders/AbstractVector.h" #include "Shaders/AbstractVector.h"
#include "magnumShadersVisibility.h" #include "magnumShadersVisibility.h"

4
src/Shaders/VertexColor.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"
@ -47,7 +47,7 @@ Draws vertex-colored mesh.
template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VertexColor: public AbstractShaderProgram { template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VertexColor: 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 Vertex color */ /** @brief Vertex color */
typedef Attribute<3, Color3> Color; typedef Attribute<3, Color3> Color;

Loading…
Cancel
Save