Cafu Engine
Shader.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 /**************/
8 /*** Shader ***/
9 /**************/
10 
11 #ifndef CAFU_MATSYS_SHADER_HPP_INCLUDED
12 #define CAFU_MATSYS_SHADER_HPP_INCLUDED
13 
14 #include <string>
15 
16 
17 template<class T> class ArrayT;
18 namespace MatSys
19 {
20  class MeshT;
21  class RenderMaterialT;
22 }
23 class MaterialT;
24 
25 
26 /// This class represents a shader. A shader works together with a material for rendering a chunk of geometry with that material.
27 /// However, a shader does not handle the rendering itself, it only sets up the appropriate API state.
28 class ShaderT
29 {
30  protected:
31 
32  /// The constructor registers this shader at the global shader repository.
33  /// It is protected so that only derived classes can be instantiated.
34  /// TODO: Make the default ctor private, and e.g. ShaderT(std::string Name_) protected?
35  ShaderT();
36 
37  /// Mark the destructor as being virtual (g++ 4.x raises a warning otherwise).
38  virtual ~ShaderT() { }
39 
40 
41  public:
42 
43  /// Returns the name of this shader.
44  /// IMPLEMENTORS: Shader names must be unique and renderer-independent, e.g. "MyCarPaintMetallicBlue1"
45  /// (such that Materials that specify a specific Shader are also renderer-independent).
46  virtual const std::string& GetName() const=0;
47 
48  /// Returns if and how well this shader can handle the ambient parts of the Material (fully, limited, or not at all).
49  /// Returns 0 if this shader cannot handle the Material at all.
50  /// Returns 255 if this shader is sure that it can fully handle the Material at maxmimum quality.
51  /// Returns a number between 1 and 254 if this shader provides limited handling of the Material.
52  /// The higher the number, the better the provided quality and the less the limitations.
53  /// The advantage over simple true/false statements is that not every shader needs detail knowledge about every other shader in this renderer.
54  virtual char CanHandleAmbient(const MaterialT& Material) const=0;
55 
56  /// Returns if and how well this shader can handle the per-lightsource parts of the Material (fully, limited, or not at all).
57  /// Returns 0 if this shader cannot handle the Material at all.
58  /// Returns 255 if this shader is sure that it can fully handle the Material at maxmimum quality.
59  /// Returns a number between 1 and 254 if this shader provides limited handling of the Material.
60  /// The higher the number, the better the provided quality and the less the limitations.
61  /// The advantage over simple true/false statements is that not every shader needs detail knowledge about every other shader in this renderer.
62  virtual char CanHandleLighting(const MaterialT& Material) const=0;
63 
64  /// Returns if this shader can handle the rendering of stencil shadow volumes.
65  /// There should only be at most one such shader in each renderer.
66  virtual bool CanHandleStencilShadowVolumes() const=0;
67 
68 
69  /// This function activates this shader.
70  virtual void Activate()=0;
71 
72  /// This function deactivates this shader.
73  virtual void Deactivate()=0;
74 
75  /// The caller can use these functions in order to learn what attributes and parameters it has to pass-in
76  /// with the mesh / mesh vertices for the currently bound / activated Material.
77  virtual bool NeedsNormals() const=0;
78  virtual bool NeedsTangentSpace() const=0;
79  virtual bool NeedsXYAttrib() const=0;
80 
81  /// Renders the Mesh, using the renderers currently bound material.
82  virtual void RenderMesh(const MatSys::MeshT& Mesh)=0;
83 
84 
85  // Returns the number of passes that this shader needs to render its effect.
86  // CanHandleAmbient(Material) or CanHandleLighting(Material) should previously have returned true for the Material.
87  // virtual unsigned long GetNrOfPasses(const RenderMaterialT& Material) const;
88 
89  // This function activates this shader in combination with the Material.
90  // CanHandleAmbient(Material) or CanHandleLighting(Material) should previously have returned true for the Material.
91  // virtual void ActivatePass(unsigned long PassNr, const RenderMaterialT& Material) const;
92 
93  // A shader needs a way to determine global rendering parameters: E.g. the currently active lightsource params (pos, color, ...),
94  // Eye Pos, ... This is easiest achieved by providing it with ("circular") access to the renderer, which in turn provides an interface
95  // for this purpose.
96 };
97 
98 
99 /// This returns the global shader repository, where all shaders of a renderer register themselves.
100 ArrayT<ShaderT*>& GetShaderRepository();
101 
102 /// This returns the generic shadow volume shader for rendering stencil shadows.
103 /// NULL may be returned if no such shader exists.
104 ShaderT* GetStencilShadowVolumesShader();
105 
106 #endif
virtual void Activate()=0
This function activates this shader.
virtual void Deactivate()=0
This function deactivates this shader.
virtual void RenderMesh(const MatSys::MeshT &Mesh)=0
Renders the Mesh, using the renderers currently bound material.
virtual ~ShaderT()
Mark the destructor as being virtual (g++ 4.x raises a warning otherwise).
Definition: Shader.hpp:38
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
virtual char CanHandleAmbient(const MaterialT &Material) const =0
Returns if and how well this shader can handle the ambient parts of the Material (fully, limited, or not at all).
virtual char CanHandleLighting(const MaterialT &Material) const =0
Returns if and how well this shader can handle the per-lightsource parts of the Material (fully...
virtual const std::string & GetName() const =0
Returns the name of this shader.
This class represents a shader.
Definition: Shader.hpp:28
virtual bool CanHandleStencilShadowVolumes() const =0
Returns if this shader can handle the rendering of stencil shadow volumes.
ShaderT()
The constructor registers this shader at the global shader repository.
Definition: Shader.cpp:53
virtual bool NeedsNormals() const =0
The caller can use these functions in order to learn what attributes and parameters it has to pass-in...
Definition: Renderer.hpp:16
This class represents a polygonal mesh.
Definition: Mesh.hpp:45