Material.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "TextureResource.h"
  25. #include "Resource.h"
  26. #include "Vec3.h"
  27. #include "Color4.h"
  28. namespace crown
  29. {
  30. //! Max texture layers supported by the material.
  31. //! Note that the maximum number of usable layers
  32. //! depends on the graphic card.
  33. const uint32_t MAX_TEXTURE_LAYERS = 8;
  34. enum FogMode
  35. {
  36. FM_LINEAR = 0,
  37. FM_EXP = 1,
  38. FM_EXP2 = 2,
  39. FM_COUNT
  40. };
  41. enum CompareFunction
  42. {
  43. CF_NEVER = 0,
  44. CF_LESS = 1,
  45. CF_EQUAL = 2,
  46. CF_LEQUAL = 3,
  47. CF_GREATER = 4,
  48. CF_NOTEQUAL = 5,
  49. CF_GEQUAL = 6,
  50. CF_ALWAYS = 7,
  51. CF_COUNT
  52. };
  53. enum ShadingType
  54. {
  55. ST_FLAT = 0,
  56. ST_SMOOTH = 1,
  57. ST_COUNT
  58. };
  59. enum PolygonMode
  60. {
  61. PM_POINT = 0,
  62. PM_LINE = 1,
  63. PM_FILL = 2,
  64. PM_COUNT
  65. };
  66. enum FrontFace
  67. {
  68. FF_CW = 0,
  69. FF_CCW = 1,
  70. FF_COUNT
  71. };
  72. enum BlendEquation
  73. {
  74. BE_FUNC_ADD = 0,
  75. BE_FUNC_SUBTRACT = 1,
  76. BE_FUNC_REVERSE_SUBTRACT = 2,
  77. BE_MIN = 3,
  78. BE_MAX = 4,
  79. BE_COUNT
  80. };
  81. enum BlendFunction
  82. {
  83. BF_ZERO = 0,
  84. BF_ONE = 1,
  85. BF_SRC_COLOR = 2,
  86. BF_ONE_MINUS_SRC_COLOR = 3,
  87. BF_DST_COLOR = 4,
  88. BF_ONE_MINUS_DST_COLOR = 5,
  89. BF_SRC_ALPHA = 6,
  90. BF_ONE_MINUS_SRC_ALPHA = 7,
  91. BF_DST_ALPHA = 8,
  92. BF_ONE_MINUS_DST_ALPHA = 9,
  93. BF_CONSTANT_COLOR = 10,
  94. BF_ONE_MINUS_CONSTANT_COLOR = 11,
  95. BF_CONSTANT_ALPHA = 12,
  96. BF_ONE_MINUS_CONSTANT_ALPHA = 13,
  97. BF_SRC_ALPHA_SATURATE = 14,
  98. BF_COUNT
  99. };
  100. class MaterialResource
  101. {
  102. public:
  103. //! Constructor
  104. MaterialResource();
  105. //! Destructor
  106. ~MaterialResource();
  107. //! Returns the ambient reflectance
  108. const Color4& GetAmbient() const;
  109. //! Sets the ambient reflectance
  110. void SetAmbient(const Color4& ambient);
  111. //! Returns the diffuse reflectance
  112. const Color4& GetDiffuse() const;
  113. //! Sets the diffuse reflectance
  114. void SetDiffuse(const Color4& diffuse);
  115. //! Returns the specular reflectance
  116. const Color4& GetSpecular() const;
  117. //! Sets the specular reflectance
  118. void SetSpecular(const Color4& specular);
  119. //! Returns the emitted light int32_tensity
  120. const Color4& GetEmission() const;
  121. //! Sets the emitted light int32_tensity
  122. void SetEmission(const Color4& emission);
  123. //! Returns the shininess
  124. int32_t GetShininess() const;
  125. //! Sets the shininess
  126. void SetShininess(int32_t shininess);
  127. //! Returns whether lighting is enabled
  128. bool GetLighting() const;
  129. //! Sets whether lighting is enabled
  130. void SetLighting(bool lighting);
  131. //! Returns whether texturing is enabled
  132. bool GetTexturing() const;
  133. //! Sets whether texturing is enabled
  134. void SetTexturing(bool texturing);
  135. //! Returns whether backface culling is enabled
  136. bool GetBackfaceCulling() const;
  137. //! Sets whether backface culling is enabled
  138. void SetBackfaceCulling(bool culling);
  139. //! Returns whether separate specular color is enabled
  140. bool GetSeparateSpecularColor() const;
  141. //! Sets whether separate specular color is enabled
  142. void SetSeparateSpecularColor(bool separate);
  143. //! Returns whether depth test is enabled
  144. bool GetDepthTest() const;
  145. //! Sets whether depth test is enabled
  146. void SetDepthTest(bool test);
  147. //! Returns whether depth writing is enabled
  148. bool GetDepthWrite() const;
  149. //! Returns whether depth writing is enabled
  150. void SetDepthWrite(bool write);
  151. //! Returns whether auto normal rescaling is enabled
  152. bool GetRescaleNormals() const;
  153. //! Sets whether auto normal rescaling is enabled
  154. void SetRescaleNormals(bool rescale);
  155. //! Returns whether blending is enabled
  156. bool GetBlending() const;
  157. //! Sets whether blending is enabled
  158. void SetBlending(bool blending);
  159. //! Returns whether writing int32_to the color buffer is enabled
  160. bool GetColorWrite() const;
  161. //! Sets whether writing int32_to the color buffer is enabled
  162. void SetColorWrite(bool write);
  163. //! Returns whether fog is enabled
  164. bool GetFog() const;
  165. //! Sets whether fog is enabled
  166. void SetFog(bool fog);
  167. //! Returns whether alpha test is enabled
  168. bool GetAlphaTest() const;
  169. //! Sets whether alpha test is enabled
  170. void SetAlphaTest(bool test);
  171. //! Returns whether point32_t sprite is enabled
  172. bool GetPointSprite() const;
  173. //! Sets whether point32_t sprite is enabled
  174. void SetPointSprite(bool sprite);
  175. //! Returns the shading type
  176. ShadingType GetShadingType() const;
  177. //! Sets the shading type
  178. void SetShadingType(ShadingType type);
  179. //! Returns the polygon mode
  180. PolygonMode GetPolygonMode() const;
  181. //! Sets the polygon mode
  182. void SetPolygonMode(PolygonMode mode);
  183. //! Returns the front face
  184. FrontFace GetFrontFace() const;
  185. //! Sets the front face
  186. void SetFrontFace(FrontFace front);
  187. //! Returns the depth function
  188. CompareFunction GetDepthFunc() const;
  189. //! Sets the depth function
  190. void SetDepthFunc(CompareFunction func);
  191. //! Returns the fog mode
  192. FogMode GetFogMode() const;
  193. //! Sets the fog mode
  194. void SetFogMode(FogMode mode);
  195. //! Returns the fog density
  196. float GetFogDensity() const;
  197. //! Sets the fog density
  198. void SetFogDensity(float density);
  199. //! Returns the fog start
  200. float GetFogStart() const;
  201. //! Sets the fog start
  202. void SetFogStart(float start);
  203. //! Returns the fog end
  204. float GetFogEnd() const;
  205. //! Sets the fog end
  206. void SetFogEnd(float end);
  207. //! Returns the fog color
  208. const Color4& GetFogColor() const;
  209. //! Sets the fog color
  210. void SetFogColor(const Color4& color);
  211. //! Returns the alpha function
  212. CompareFunction GetAlphaFunc() const;
  213. //! Sets the alpha function
  214. void SetAlphaFunction(CompareFunction func);
  215. //! Returns the alpha reference
  216. float GetAlphaRef() const;
  217. //! Sets the alpha reference
  218. void SetAlphaRef(float ref);
  219. //! Returns the point size
  220. float GetPointSize() const;
  221. //! Sets the point size
  222. void SetPointSize(float size);
  223. //! Returns the minimum point size
  224. float GetPointSizeMin() const;
  225. //! Sets the minimum point size
  226. void SetPointSizeMin(float min);
  227. //! Returns the maximum point size
  228. float GetPointSizeMax() const;
  229. //! Sets the maximum point' size
  230. void SetPointSizeMax(float max);
  231. //! Returns the blending equation
  232. BlendEquation GetBlendEquation() const;
  233. //! Sets the blending equation
  234. void SetBlendEquation(BlendEquation equation);
  235. //! Returns the source blending function
  236. BlendFunction GetSrcBlendFunc() const;
  237. //! Sets the source blending function
  238. void SetSrcBlendFunc(BlendFunction src);
  239. //! Returns the destination blending function
  240. BlendFunction GetDstBlendFunc() const;
  241. //! Sets the destination blending function
  242. void SetDstBlendFunc(BlendFunction dst);
  243. //! Sets the blending function for both source and destination
  244. void SetBlendFunc(BlendFunction src, BlendFunction dst);
  245. //! Returns the blending color
  246. Color4& GetBlendColor();
  247. //! Sets the blending color
  248. void SetBlendColor(const Color4& color);
  249. //! Sets "texture" to layer "layer"
  250. //! Returns true if success
  251. bool SetTextureLayer(uint32_t layer, ResourceId texture);
  252. //! Returns the texture at layer "layer"
  253. ResourceId GetTextureLayer(uint32_t layer) const;
  254. //! Sets the texture mode for all layers
  255. void SetTextureMode(TextureMode mode);
  256. //! Sets the texture filter for all layers
  257. void SetTextureFilter(TextureFilter filter);
  258. //! Sets the texture wrap mode for all layers
  259. void SetTextureWrap(TextureWrap wrap);
  260. virtual void Load(const char* name);
  261. virtual void Unload(const char* name, bool reload);
  262. //private:
  263. Color4 mAmbient;
  264. Color4 mDiffuse;
  265. Color4 mSpecular;
  266. Color4 mEmission;
  267. int32_t mShininess;
  268. bool mLighting : 1; // Whether lighting is enabled
  269. bool mTexturing : 1; // Whether texturing is enabled
  270. bool mBackfaceCulling : 1; // Whether backface-culling is enabled
  271. bool mSeparateSpecularColor : 1; // Whether separate specular color is enabled
  272. bool mDepthTest : 1; // Whether depth test is enabled
  273. bool mDepthWrite : 1; // Whether depth write is enabled
  274. bool mRescaleNormals : 1; // Whether auto normal rescaling is enabled
  275. bool mBlending : 1; // Whether blending is enabled
  276. bool mColorWrite : 1; // Whether writing int32_to the color buffer is enabled
  277. bool mFog : 1; // Whether fog is enabled
  278. bool mAlphaTest : 1; // Whether alpha test is enabled
  279. bool mPointSprite : 1; // Whether point sprite is enabled
  280. ShadingType mShadingType;
  281. PolygonMode mPolygonMode;
  282. FrontFace mFrontFace;
  283. CompareFunction mDepthFunc;
  284. FogMode mFogMode;
  285. float mFogDensity;
  286. float mFogStart;
  287. float mFogEnd;
  288. Color4 mFogColor;
  289. CompareFunction mAlphaFunc;
  290. float mAlphaRef;
  291. float mPointSize;
  292. float mPointSizeMin;
  293. float mPointSizeMax;
  294. BlendEquation mBlendEquation;
  295. BlendFunction mBlendSrc;
  296. BlendFunction mBlendDst;
  297. Color4 mBlendColor;
  298. // A material can contain up to MAX_TEXTURE_LAYERS texture layers.
  299. // However, the maximum number of texture layers really usable is renderer-dependent.
  300. ResourceId mTextureLayer[MAX_TEXTURE_LAYERS];
  301. };
  302. } // namespace crown