#ifndef __MATERIAL_H #define __MATERIAL_H #include "glInterface.h" #include "common.h" #include "ShaderProgram.h" namespace CHCDemoEngine { class Texture; class RenderState; class GPUProgramParameters; class ShaderProgram; struct RenderQueueBucket; /** Class representing an rgba color vector. */ class RgbaColor { public: RgbaColor(): r(1), g(1), b(1), a(1) {} RgbaColor(float _r, float _g, float _b, float _a): r(_r), g(_g), b(_b), a(_a) {} friend RgbaColor RandomColor(float a = 0.0f, float b = 1.0f); ///////////////////// float r, g, b, a; }; // Forward declarations RgbaColor RandomColor(float a, float b); /** This class represents a certain rendering technique of a shape. A material consists of one or more techniques */ class Technique { friend class ResourceManager; friend class RenderQueue; friend class Material; public: /** Sets default material (ambient intensity 0.2f, diffuse intensity 1.0f) */ Technique(); /** Sets ambient and diffuse color to color */ Technique(const RgbaColor &color); /** Renders this technique. */ void Render(RenderState *state, SceneEntity *parent = NULL); /** Initialize this technique material with default values */ void Init(); inline Texture *GetTexture() const { return mTexture; } inline RgbaColor GetAmbient() const { return mAmbientColor; } inline RgbaColor GetDiffuse() const { return mDiffuseColor; } inline RgbaColor GetSpecular() const { return mSpecularColor; } inline RgbaColor GetEmmisive() const { return mEmmisiveColor; } inline void SetTexture(Texture *texture) { mTexture = texture; } inline void SetAmbient(const RgbaColor &color) { mAmbientColor = color; } inline void SetDiffuse(const RgbaColor &color) { mDiffuseColor = color; } inline void SetSpecular(const RgbaColor &color) { mSpecularColor = color; } inline void SetEmmisive(const RgbaColor &color) { mEmmisiveColor = color; } inline void SetAlphaTestEnabled(bool alpha) { mAlphaTestEnabled = alpha; } inline void SetCullFaceEnabled(bool cull) { mCullFaceEnabled = cull; } inline bool IsAlphaTestEnabled() const { return mAlphaTestEnabled; } inline bool IsCullFaceEnabled() const { return mCullFaceEnabled; } inline void SetLightingEnabled(bool l) { mLightingEnabled = l; } inline void SetColorWriteEnabled(bool c) { mColorWriteEnabled = c; } inline void SetDepthWriteEnabled(bool d) { mDepthWriteEnabled = d; } inline bool IsLightingEnabled() const { return mLightingEnabled; } inline bool IsColorWriteEnabled() const { return mColorWriteEnabled; } inline bool IsDepthWriteEnabled() const { return mDepthWriteEnabled; } void SetFragmentProgram(ShaderProgram *p); void SetVertexProgram(ShaderProgram *p); inline ShaderProgram *GetFragmentProgram() { return mFragmentProgram; } inline ShaderProgram *GetVertexProgram() { return mVertexProgram; } /** Get the set of fragment parameters of this technique */ GPUProgramParameters * const GetFragmentProgramParameters() { return &mFragmentProgramParameters; } /** Get the set of vertex parameters of this technique */ GPUProgramParameters * const GetVertexProgramParameters() { return &mVertexProgramParameters; } protected: /////////// RgbaColor mDiffuseColor; RgbaColor mSpecularColor; RgbaColor mAmbientColor; RgbaColor mEmmisiveColor; bool mAlphaTestEnabled; bool mCullFaceEnabled; /// the associated texture Texture *mTexture; GPUProgramParameters mVertexProgramParameters; GPUProgramParameters mFragmentProgramParameters; ShaderProgram *mFragmentProgram; ShaderProgram *mVertexProgram; /// if this material can write colors bool mColorWriteEnabled; /// if lighting is used bool mLightingEnabled; /// if depth write is enabled bool mDepthWriteEnabled; /// pointer to the renderqueue bucket this entity belongs to RenderQueueBucket *mRenderQueueBucket; }; /** A material consists of one or more techniques. */ class Material { public: /** Default constructor creating one default technique. */ Material(); ~Material(); /** Renders this material. */ void Render(RenderState *state, SceneEntity *parent); /** Adds a new technique to the material */ void AddTechnique(Technique *t); /** Returns the default technique (usually the first) */ Technique *GetDefaultTechnique() const; Technique *GetTechnique(int i) const; /** Returns the number of techniques associated with this material */ int GetNumTechniques() const; protected: TechniqueContainer mTechniques; }; } #endif