Material.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "GraphicsDefs.h"
  25. #include "HashMap.h"
  26. #include "Resource.h"
  27. #include "Vector4.h"
  28. class Pass;
  29. class Technique;
  30. class Texture;
  31. class Texture2D;
  32. class TextureCube;
  33. /// Material's technique list entry
  34. struct TechniqueEntry
  35. {
  36. /// Construct with defaults
  37. TechniqueEntry();
  38. /// Construct with parameters
  39. TechniqueEntry(Technique* technique, unsigned qualityLevel, float lodDistance);
  40. /// Destruct
  41. ~TechniqueEntry();
  42. /// Technique
  43. SharedPtr<Technique> technique_;
  44. /// Quality level
  45. int qualityLevel_;
  46. /// LOD distance
  47. float lodDistance_;
  48. };
  49. /// Describes how to render 3D geometries
  50. class Material : public Resource
  51. {
  52. OBJECT(Material);
  53. public:
  54. /// Construct
  55. Material(Context* context);
  56. /// Destruct
  57. ~Material();
  58. /// Register object factory
  59. static void RegisterObject(Context* context);
  60. /// Load resource. Return true if successful
  61. virtual bool Load(Deserializer& source);
  62. /// Save resource. Return true if successful
  63. virtual bool Save(Serializer& dest);
  64. /// Set number of techniques
  65. void SetNumTechniques(unsigned num);
  66. /// Set technique
  67. void SetTechnique(unsigned index, Technique* technique, unsigned qualityLevel = 0, float lodDistance = 0.0f);
  68. /// Set shader parameter
  69. void SetShaderParameter(ShaderParameter param, const Vector4& value);
  70. /// Set texture
  71. void SetTexture(TextureUnit unit, Texture* texture);
  72. /// Set texture coordinate transform
  73. void SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat);
  74. /// Set texture coordinate transform
  75. void SetUVTransform(const Vector2& offset, float rotation, float repeat);
  76. /// Set culling mode
  77. void SetCullMode(CullMode mode);
  78. /// Set culling mode for shadows
  79. void SetShadowCullMode(CullMode mode);
  80. /// Remove a shader parameter
  81. void RemoveShaderParameter(ShaderParameter param);
  82. /// Reset all shader pointers
  83. void ReleaseShaders();
  84. /// Clone material
  85. SharedPtr<Material> Clone(const String& cloneName = String()) const;
  86. /// Mark material for auxiliary view rendering
  87. void MarkForAuxView(unsigned frameNumber);
  88. /// Return number of techniques
  89. unsigned GetNumTechniques() const { return techniques_.Size(); }
  90. /// Return all techniques
  91. const Vector<TechniqueEntry>& GetTechniques() const { return techniques_; }
  92. /// Return technique entry by index
  93. const TechniqueEntry& GetTechniqueEntry(unsigned index) const;
  94. /// Return technique by index
  95. Technique* GetTechnique(unsigned index) const;
  96. /// Return pass by technique index and pass type
  97. Pass* GetPass(unsigned index, PassType pass) const;
  98. /// Return all textures
  99. const Vector<SharedPtr<Texture> >& GetTextures() const { return textures_; }
  100. /// Return texture by unit
  101. Texture* GetTexture(TextureUnit unit) const;
  102. /// Return all shader parameters
  103. const HashMap<ShaderParameter, Vector4>& GetShaderParameters() const { return shaderParameters_; }
  104. /// Return normal culling mode
  105. CullMode GetCullMode() const { return cullMode_; }
  106. /// Return culling mode for shadows
  107. CullMode GetShadowCullMode() const { return shadowCullMode_; }
  108. /// Return last auxiliary view rendered frame number
  109. unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
  110. /// Return whether should render occlusion
  111. bool GetOcclusion() const { return occlusion_; }
  112. /// Return name for texture unit
  113. static const String& GetTextureUnitName(TextureUnit unit);
  114. private:
  115. /// Re-evaluate occlusion rendering
  116. void Update();
  117. /// Techniques
  118. Vector<TechniqueEntry> techniques_;
  119. /// Textures
  120. Vector<SharedPtr<Texture> > textures_;
  121. /// Shader parameters
  122. HashMap<ShaderParameter, Vector4> shaderParameters_;
  123. /// Normal culling mode
  124. CullMode cullMode_;
  125. /// Culling mode for shadow rendering
  126. CullMode shadowCullMode_;
  127. /// Last auxiliary view rendered frame number
  128. unsigned auxViewFrameNumber_;
  129. /// Render occlusion flag
  130. bool occlusion_;
  131. };