Material 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_MATERIAL
  14. #define OSG_MATERIAL 1
  15. #include <osg/Vec4>
  16. #include <osg/StateAttribute>
  17. #ifndef OSG_GL_FIXED_FUNCTION_AVAILABLE
  18. #define GL_AMBIENT 0x1200
  19. #define GL_DIFFUSE 0x1201
  20. #define GL_SPECULAR 0x1202
  21. #define GL_EMISSION 0x1600
  22. #define GL_AMBIENT_AND_DIFFUSE 0x1602
  23. #define GL_COLOR_MATERIAL 0x0B57
  24. #endif
  25. namespace osg {
  26. /** Material - encapsulates OpenGL glMaterial state.*/
  27. class OSG_EXPORT Material : public StateAttribute
  28. {
  29. public :
  30. Material();
  31. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  32. Material(const Material& mat,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  33. StateAttribute(mat,copyop),
  34. _colorMode(mat._colorMode),
  35. _ambientFrontAndBack(mat._ambientFrontAndBack),
  36. _ambientFront(mat._ambientFront),
  37. _ambientBack(mat._ambientBack),
  38. _diffuseFrontAndBack(mat._diffuseFrontAndBack),
  39. _diffuseFront(mat._diffuseFront),
  40. _diffuseBack(mat._diffuseBack),
  41. _specularFrontAndBack(mat._specularFrontAndBack),
  42. _specularFront(mat._specularFront),
  43. _specularBack(mat._specularBack),
  44. _emissionFrontAndBack(mat._emissionFrontAndBack),
  45. _emissionFront(mat._emissionFront),
  46. _emissionBack(mat._emissionBack),
  47. _shininessFrontAndBack(mat._shininessFrontAndBack),
  48. _shininessFront(mat._shininessFront),
  49. _shininessBack(mat._shininessBack) {}
  50. META_StateAttribute(osg, Material, MATERIAL);
  51. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  52. virtual int compare(const StateAttribute& sa) const
  53. {
  54. // check the types are equal and then create the rhs variable
  55. // used by the COMPARE_StateAttribute_Parameter macros below.
  56. COMPARE_StateAttribute_Types(Material,sa)
  57. // compare each parameter in turn against the rhs.
  58. COMPARE_StateAttribute_Parameter(_colorMode)
  59. COMPARE_StateAttribute_Parameter(_ambientFrontAndBack)
  60. COMPARE_StateAttribute_Parameter(_ambientFront)
  61. COMPARE_StateAttribute_Parameter(_ambientBack)
  62. COMPARE_StateAttribute_Parameter(_diffuseFrontAndBack)
  63. COMPARE_StateAttribute_Parameter(_diffuseFront)
  64. COMPARE_StateAttribute_Parameter(_diffuseBack)
  65. COMPARE_StateAttribute_Parameter(_specularFrontAndBack)
  66. COMPARE_StateAttribute_Parameter(_specularFront)
  67. COMPARE_StateAttribute_Parameter(_specularBack)
  68. COMPARE_StateAttribute_Parameter(_emissionFrontAndBack)
  69. COMPARE_StateAttribute_Parameter(_emissionFront)
  70. COMPARE_StateAttribute_Parameter(_emissionBack)
  71. COMPARE_StateAttribute_Parameter(_shininessFrontAndBack)
  72. COMPARE_StateAttribute_Parameter(_shininessFront)
  73. COMPARE_StateAttribute_Parameter(_shininessBack)
  74. return 0; // passed all the above comparison macros, must be equal.
  75. }
  76. Material& operator = (const Material& rhs);
  77. virtual bool getModeUsage(StateAttribute::ModeUsage& /*usage*/) const
  78. {
  79. // note, since Material does it's own glEnable/glDisable of GL_COLOR_MATERIAL
  80. // we shouldn't declare usage of that mode, so commenting out the below usage.
  81. // usage.usesMode(GL_COLOR_MATERIAL);
  82. return true;
  83. }
  84. virtual void apply(State& state) const;
  85. enum Face {
  86. FRONT = GL_FRONT,
  87. BACK = GL_BACK,
  88. FRONT_AND_BACK = GL_FRONT_AND_BACK
  89. };
  90. enum ColorMode {
  91. AMBIENT = GL_AMBIENT,
  92. DIFFUSE = GL_DIFFUSE,
  93. SPECULAR = GL_SPECULAR,
  94. EMISSION = GL_EMISSION,
  95. AMBIENT_AND_DIFFUSE = GL_AMBIENT_AND_DIFFUSE,
  96. OFF
  97. };
  98. inline void setColorMode(ColorMode mode) { _colorMode = mode; }
  99. inline ColorMode getColorMode() const { return _colorMode; }
  100. void setAmbient( Face face, const Vec4& ambient );
  101. const Vec4& getAmbient(Face face) const;
  102. inline bool getAmbientFrontAndBack() const { return _ambientFrontAndBack; }
  103. void setDiffuse( Face face, const Vec4& diffuse );
  104. const Vec4& getDiffuse(Face face) const;
  105. inline bool getDiffuseFrontAndBack() const { return _diffuseFrontAndBack; }
  106. /** Set specular value of specified face(s) of the material,
  107. * valid specular[0..3] range is 0.0 to 1.0.
  108. */
  109. void setSpecular( Face face, const Vec4& specular );
  110. /** Get the specular value for specified face. */
  111. const Vec4& getSpecular(Face face) const;
  112. /** Return whether specular values are equal for front and back faces
  113. * or not.
  114. */
  115. inline bool getSpecularFrontAndBack() const { return _specularFrontAndBack; }
  116. /** Set emission value of specified face(s) of the material,
  117. * valid emission[0..3] range is 0.0 to 1.0.
  118. */
  119. void setEmission( Face face, const Vec4& emission );
  120. /** Get the emission value for specified face. */
  121. const Vec4& getEmission(Face face) const;
  122. /** Return whether emission values are equal for front and back faces
  123. * or not.
  124. */
  125. inline bool getEmissionFrontAndBack() const { return _emissionFrontAndBack; }
  126. /** Set shininess of specified face(s) of the material.
  127. * valid shininess range is 0.0 to 128.0.
  128. */
  129. void setShininess(Face face, float shininess );
  130. /** Get the shininess value for specified face. */
  131. float getShininess(Face face) const;
  132. /** Return whether shininess values are equal for front and back faces
  133. * or not.
  134. */
  135. inline bool getShininessFrontAndBack() const { return _shininessFrontAndBack; }
  136. /** Set the alpha value of ambient, diffuse, specular and emission
  137. * colors of specified face, to 1-transparency.
  138. * Valid transparency range is 0.0 to 1.0.
  139. */
  140. void setTransparency(Face face,float trans);
  141. /** Set the alpha value of ambient, diffuse, specular and emission
  142. * colors. Valid transparency range is 0.0 to 1.0.
  143. */
  144. void setAlpha(Face face,float alpha);
  145. protected :
  146. virtual ~Material();
  147. ColorMode _colorMode;
  148. bool _ambientFrontAndBack;
  149. Vec4 _ambientFront; // r, g, b, w
  150. Vec4 _ambientBack; // r, g, b, w
  151. bool _diffuseFrontAndBack;
  152. Vec4 _diffuseFront; // r, g, b, w
  153. Vec4 _diffuseBack; // r, g, b, w
  154. bool _specularFrontAndBack;
  155. Vec4 _specularFront; // r, g, b, w
  156. Vec4 _specularBack; // r, g, b, w
  157. bool _emissionFrontAndBack;
  158. Vec4 _emissionFront; // r, g, b, w
  159. Vec4 _emissionBack; // r, g, b, w
  160. bool _shininessFrontAndBack;
  161. float _shininessFront; // values 0 - 128.0
  162. float _shininessBack; // values 0 - 128.0
  163. };
  164. }
  165. #endif