Material.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "Types.h"
  24. #include "Texture.h"
  25. #include "Vec3.h"
  26. namespace Crown
  27. {
  28. //! Max texture layers supported by the material.
  29. //! Note that the maximum number of usable layers
  30. //! depends on the graphic card.
  31. const uint MAX_TEXTURE_LAYERS = 8;
  32. enum FogMode
  33. {
  34. FM_LINEAR = 0,
  35. FM_EXP = 1,
  36. FM_EXP2 = 2,
  37. FM_COUNT
  38. };
  39. enum CompareFunction
  40. {
  41. CF_NEVER = 0,
  42. CF_LESS = 1,
  43. CF_EQUAL = 2,
  44. CF_LEQUAL = 3,
  45. CF_GREATER = 4,
  46. CF_NOTEQUAL = 5,
  47. CF_GEQUAL = 6,
  48. CF_ALWAYS = 7,
  49. CF_COUNT
  50. };
  51. enum ShadingType
  52. {
  53. ST_FLAT = 0,
  54. ST_SMOOTH = 1,
  55. ST_COUNT
  56. };
  57. enum PolygonMode
  58. {
  59. PM_POINT = 0,
  60. PM_LINE = 1,
  61. PM_FILL = 2,
  62. PM_COUNT
  63. };
  64. enum FrontFace
  65. {
  66. FF_CW = 0,
  67. FF_CCW = 1,
  68. FF_COUNT
  69. };
  70. enum BlendEquation
  71. {
  72. BE_FUNC_ADD = 0,
  73. BE_FUNC_SUBTRACT = 1,
  74. BE_FUNC_REVERSE_SUBTRACT = 2,
  75. BE_MIN = 3,
  76. BE_MAX = 4,
  77. BE_COUNT
  78. };
  79. enum BlendFunction
  80. {
  81. BF_ZERO = 0,
  82. BF_ONE = 1,
  83. BF_SRC_COLOR = 2,
  84. BF_ONE_MINUS_SRC_COLOR = 3,
  85. BF_DST_COLOR = 4,
  86. BF_ONE_MINUS_DST_COLOR = 5,
  87. BF_SRC_ALPHA = 6,
  88. BF_ONE_MINUS_SRC_ALPHA = 7,
  89. BF_DST_ALPHA = 8,
  90. BF_ONE_MINUS_DST_ALPHA = 9,
  91. BF_CONSTANT_COLOR = 10,
  92. BF_ONE_MINUS_CONSTANT_COLOR = 11,
  93. BF_CONSTANT_ALPHA = 12,
  94. BF_ONE_MINUS_CONSTANT_ALPHA = 13,
  95. BF_SRC_ALPHA_SATURATE = 14,
  96. BF_COUNT
  97. };
  98. class Material : public Resource
  99. {
  100. public:
  101. //! Constructor
  102. Material();
  103. //! Destructor
  104. ~Material();
  105. //! Returns the ambient reflectance
  106. const Color4& GetAmbient() const;
  107. //! Sets the ambient reflectance
  108. void SetAmbient(const Color4& ambient);
  109. //! Returns the diffuse reflectance
  110. const Color4& GetDiffuse() const;
  111. //! Sets the diffuse reflectance
  112. void SetDiffuse(const Color4& diffuse);
  113. //! Returns the specular reflectance
  114. const Color4& GetSpecular() const;
  115. //! Sets the specular reflectance
  116. void SetSpecular(const Color4& specular);
  117. //! Returns the emitted light intensity
  118. const Color4& GetEmission() const;
  119. //! Sets the emitted light intensity
  120. void SetEmission(const Color4& emission);
  121. //! Returns the shininess
  122. int GetShininess() const;
  123. //! Sets the shininess
  124. void SetShininess(int shininess);
  125. //! Returns whether lighting is enabled
  126. bool GetLighting() const;
  127. //! Sets whether lighting is enabled
  128. void SetLighting(bool lighting);
  129. //! Returns whether texturing is enabled
  130. bool GetTexturing() const;
  131. //! Sets whether texturing is enabled
  132. void SetTexturing(bool texturing);
  133. //! Returns whether backface culling is enabled
  134. bool GetBackfaceCulling() const;
  135. //! Sets whether backface culling is enabled
  136. void SetBackfaceCulling(bool culling);
  137. //! Returns whether separate specular color is enabled
  138. bool GetSeparateSpecularColor() const;
  139. //! Sets whether separate specular color is enabled
  140. void SetSeparateSpecularColor(bool separate);
  141. //! Returns whether depth test is enabled
  142. bool GetDepthTest() const;
  143. //! Sets whether depth test is enabled
  144. void SetDepthTest(bool test);
  145. //! Returns whether depth writing is enabled
  146. bool GetDepthWrite() const;
  147. //! Returns whether depth writing is enabled
  148. void SetDepthWrite(bool write);
  149. //! Returns whether auto normal rescaling is enabled
  150. bool GetRescaleNormals() const;
  151. //! Sets whether auto normal rescaling is enabled
  152. void SetRescaleNormals(bool rescale);
  153. //! Returns whether blending is enabled
  154. bool GetBlending() const;
  155. //! Sets whether blending is enabled
  156. void SetBlending(bool blending);
  157. //! Returns whether writing into the color buffer is enabled
  158. bool GetColorWrite() const;
  159. //! Sets whether writing into the color buffer is enabled
  160. void SetColorWrite(bool write);
  161. //! Returns whether fog is enabled
  162. bool GetFog() const;
  163. //! Sets whether fog is enabled
  164. void SetFog(bool fog);
  165. //! Returns whether alpha test is enabled
  166. bool GetAlphaTest() const;
  167. //! Sets whether alpha test is enabled
  168. void SetAlphaTest(bool test);
  169. //! Returns whether point sprite is enabled
  170. bool GetPointSprite() const;
  171. //! Sets whether point sprite is enabled
  172. void SetPointSprite(bool sprite);
  173. //! Returns the shading type
  174. ShadingType GetShadingType() const;
  175. //! Sets the shading type
  176. void SetShadingType(ShadingType type);
  177. //! Returns the polygon mode
  178. PolygonMode GetPolygonMode() const;
  179. //! Sets the polygon mode
  180. void SetPolygonMode(PolygonMode mode);
  181. //! Returns the front face
  182. FrontFace GetFrontFace() const;
  183. //! Sets the front face
  184. void SetFrontFace(FrontFace front);
  185. //! Returns the depth function
  186. CompareFunction GetDepthFunc() const;
  187. //! Sets the depth function
  188. void SetDepthFunc(CompareFunction func);
  189. //! Returns the fog mode
  190. FogMode GetFogMode() const;
  191. //! Sets the fog mode
  192. void SetFogMode(FogMode mode);
  193. //! Returns the fog density
  194. float GetFogDensity() const;
  195. //! Sets the fog density
  196. void SetFogDensity(float density);
  197. //! Returns the fog start
  198. float GetFogStart() const;
  199. //! Sets the fog start
  200. void SetFogStart(float start);
  201. //! Returns the fog end
  202. float GetFogEnd() const;
  203. //! Sets the fog end
  204. void SetFogEnd(float end);
  205. //! Returns the fog color
  206. const Color4& GetFogColor() const;
  207. //! Sets the fog color
  208. void SetFogColor(const Color4& color);
  209. //! Returns the alpha function
  210. CompareFunction GetAlphaFunc() const;
  211. //! Sets the alpha function
  212. void SetAlphaFunction(CompareFunction func);
  213. //! Returns the alpha reference
  214. float GetAlphaRef() const;
  215. //! Sets the alpha reference
  216. void SetAlphaRef(float ref);
  217. //! Returns the points' size
  218. float GetPointSize() const;
  219. //! Sets the points' size
  220. void SetPointSize(float size);
  221. //! Returns the minimum points' size
  222. float GetPointSizeMin() const;
  223. //! Sets the minimum points' size
  224. void SetPointSizeMin(float min);
  225. //! Returns the maximum points' size
  226. float GetPointSizeMax() const;
  227. //! Sets the maximum points' size
  228. void SetPointSizeMax(float max);
  229. //! Returns the blending equation
  230. BlendEquation GetBlendEquation() const;
  231. //! Sets the blending equation
  232. void SetBlendEquation(BlendEquation equation);
  233. //! Returns the source blending function
  234. BlendFunction GetSrcBlendFunc() const;
  235. //! Sets the source blending function
  236. void SetSrcBlendFunc(BlendFunction src);
  237. //! Returns the destination blending function
  238. BlendFunction GetDstBlendFunc() const;
  239. //! Sets the destination blending function
  240. void SetDstBlendFunc(BlendFunction dst);
  241. //! Sets the blending function for both source and destination
  242. void SetBlendFunc(BlendFunction src, BlendFunction dst);
  243. //! Returns the blending color
  244. Color4& GetBlendColor();
  245. //! Sets the blending color
  246. void SetBlendColor(const Color4& color);
  247. //! Sets "texture" to layer "layer"
  248. //! Returns true if success
  249. bool SetTextureLayer(uint layer, Texture* texture);
  250. //! Returns the texture at layer "layer"
  251. Texture* GetTextureLayer(uint layer) const;
  252. //! Sets the texture mode for all layers
  253. void SetTextureMode(TextureMode mode);
  254. //! Sets the texture filter for all layers
  255. void SetTextureFilter(TextureFilter filter);
  256. //! Sets the texture wrap mode for all layers
  257. void SetTextureWrap(TextureWrap wrap);
  258. virtual void Load(const char* name);
  259. virtual void Unload(const char* name, bool reload);
  260. //private:
  261. Color4 mAmbient;
  262. Color4 mDiffuse;
  263. Color4 mSpecular;
  264. Color4 mEmission;
  265. int mShininess;
  266. bool mLighting : 1; // Whether lighting is enabled
  267. bool mTexturing : 1; // Whether texturing is enabled
  268. bool mBackfaceCulling : 1; // Whether backface-culling is enabled
  269. bool mSeparateSpecularColor : 1; // Whether separate specular color is enabled
  270. bool mDepthTest : 1; // Whether depth test is enabled
  271. bool mDepthWrite : 1; // Whether depth write is enabled
  272. bool mRescaleNormals : 1; // Whether auto normal rescaling is enabled
  273. bool mBlending : 1; // Whether blending is enabled
  274. bool mColorWrite : 1; // Whether writing into the color buffer is enabled
  275. bool mFog : 1; // Whether fog is enabled
  276. bool mAlphaTest : 1; // Whether alpha test is enabled
  277. bool mPointSprite : 1; // Whether point sprite is enabled
  278. ShadingType mShadingType;
  279. PolygonMode mPolygonMode;
  280. FrontFace mFrontFace;
  281. CompareFunction mDepthFunc;
  282. FogMode mFogMode;
  283. float mFogDensity;
  284. float mFogStart;
  285. float mFogEnd;
  286. Color4 mFogColor;
  287. CompareFunction mAlphaFunc;
  288. float mAlphaRef;
  289. float mPointSize;
  290. float mPointSizeMin;
  291. float mPointSizeMax;
  292. BlendEquation mBlendEquation;
  293. BlendFunction mBlendSrc;
  294. BlendFunction mBlendDst;
  295. Color4 mBlendColor;
  296. //! A material can contain up to MAX_TEXTURE_LAYERS texture layers.
  297. //! However, the maximum number of texture layers really usable is renderer-dependent.
  298. Texture* mTextureLayer[MAX_TEXTURE_LAYERS];
  299. };
  300. } // namespace Crown