AlphaFunc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_ALPHAFUNC
  14. #define OSG_ALPHAFUNC 1
  15. #include <osg/StateAttribute>
  16. namespace osg {
  17. /** Encapsulates OpenGL glAlphaFunc.
  18. */
  19. class OSG_EXPORT AlphaFunc : public StateAttribute
  20. {
  21. public :
  22. enum ComparisonFunction {
  23. NEVER = GL_NEVER,
  24. LESS = GL_LESS,
  25. EQUAL = GL_EQUAL,
  26. LEQUAL = GL_LEQUAL,
  27. GREATER = GL_GREATER,
  28. NOTEQUAL = GL_NOTEQUAL,
  29. GEQUAL = GL_GEQUAL,
  30. ALWAYS = GL_ALWAYS
  31. };
  32. AlphaFunc();
  33. AlphaFunc(ComparisonFunction func,float ref):
  34. _comparisonFunc(func),
  35. _referenceValue(ref) {}
  36. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  37. AlphaFunc(const AlphaFunc& af,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  38. StateAttribute(af,copyop),
  39. _comparisonFunc(af._comparisonFunc),
  40. _referenceValue(af._referenceValue) {}
  41. META_StateAttribute(osg, AlphaFunc,ALPHAFUNC);
  42. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  43. virtual int compare(const StateAttribute& sa) const
  44. {
  45. // Check for equal types, then create the rhs variable
  46. // used by the COMPARE_StateAttribute_Parameter macros below.
  47. COMPARE_StateAttribute_Types(AlphaFunc,sa)
  48. // Compare each parameter in turn against the rhs.
  49. COMPARE_StateAttribute_Parameter(_comparisonFunc)
  50. COMPARE_StateAttribute_Parameter(_referenceValue)
  51. return 0; // Passed all the above comparison macros, so must be equal.
  52. }
  53. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  54. {
  55. usage.usesMode(GL_ALPHA_TEST);
  56. return true;
  57. }
  58. inline void setFunction(ComparisonFunction func,float ref)
  59. {
  60. _comparisonFunc = func;
  61. _referenceValue = ref;
  62. }
  63. inline void setFunction(ComparisonFunction func) { _comparisonFunc=func; }
  64. inline ComparisonFunction getFunction() const { return _comparisonFunc; }
  65. inline void setReferenceValue(float value) { _referenceValue=value; }
  66. inline float getReferenceValue() const { return _referenceValue; }
  67. virtual void apply(State& state) const;
  68. protected:
  69. virtual ~AlphaFunc();
  70. ComparisonFunction _comparisonFunc;
  71. float _referenceValue;
  72. };
  73. }
  74. #endif