Light 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_LIGHT
  14. #define OSG_LIGHT 1
  15. #include <osg/StateAttribute>
  16. #include <osg/Vec3>
  17. #include <osg/Vec4>
  18. #ifndef GL_LIGHT0
  19. #define GL_LIGHT0 0x4000
  20. #define GL_LIGHT1 0x4001
  21. #define GL_LIGHT2 0x4002
  22. #define GL_LIGHT3 0x4003
  23. #define GL_LIGHT4 0x4004
  24. #define GL_LIGHT5 0x4005
  25. #define GL_LIGHT6 0x4006
  26. #define GL_LIGHT7 0x4007
  27. #endif
  28. #ifndef GL_LIGHTING
  29. #define GL_LIGHTING 0x0B50
  30. #endif
  31. namespace osg {
  32. /** Light state class which encapsulates OpenGL glLight() functionality. */
  33. class OSG_EXPORT Light : public StateAttribute
  34. {
  35. public :
  36. Light();
  37. Light(unsigned int lightnum);
  38. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  39. Light(const Light& light,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  40. StateAttribute(light,copyop),
  41. _lightnum(light._lightnum),
  42. _ambient(light._ambient),
  43. _diffuse(light._diffuse),
  44. _specular(light._specular),
  45. _position(light._position),
  46. _direction(light._direction),
  47. _constant_attenuation(light._constant_attenuation),
  48. _linear_attenuation(light._linear_attenuation),
  49. _quadratic_attenuation(light._quadratic_attenuation),
  50. _spot_exponent(light._spot_exponent),
  51. _spot_cutoff(light._spot_cutoff) {}
  52. virtual osg::Object* cloneType() const { return new Light(_lightnum); }
  53. virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new Light(*this,copyop); }
  54. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Light *>(obj)!=NULL; }
  55. virtual const char* libraryName() const { return "osg"; }
  56. virtual const char* className() const { return "Light"; }
  57. virtual Type getType() const { return LIGHT; }
  58. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  59. virtual int compare(const StateAttribute& sa) const
  60. {
  61. // check the types are equal and then create the rhs variable
  62. // used by the COMPARE_StateAttribute_Parameter macros below.
  63. COMPARE_StateAttribute_Types(Light,sa)
  64. // compare each parameter in turn against the rhs.
  65. COMPARE_StateAttribute_Parameter(_lightnum)
  66. COMPARE_StateAttribute_Parameter(_ambient)
  67. COMPARE_StateAttribute_Parameter(_diffuse)
  68. COMPARE_StateAttribute_Parameter(_specular)
  69. COMPARE_StateAttribute_Parameter(_position)
  70. COMPARE_StateAttribute_Parameter(_direction)
  71. COMPARE_StateAttribute_Parameter(_constant_attenuation)
  72. COMPARE_StateAttribute_Parameter(_linear_attenuation)
  73. COMPARE_StateAttribute_Parameter(_quadratic_attenuation)
  74. COMPARE_StateAttribute_Parameter(_spot_exponent)
  75. COMPARE_StateAttribute_Parameter(_spot_cutoff)
  76. return 0; // passed all the above comparison macros, must be equal.
  77. }
  78. virtual unsigned int getMember() const { return _lightnum; }
  79. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  80. {
  81. usage.usesMode(GL_LIGHT0+_lightnum);
  82. return true;
  83. }
  84. /** Set which OpenGL light to operate on. */
  85. void setLightNum(int num);
  86. /** Get which OpenGL light this osg::Light operates on. */
  87. int getLightNum() const { return _lightnum; }
  88. /** Set the ambient component of the light. */
  89. inline void setAmbient( const Vec4& ambient ) { _ambient = ambient; }
  90. /** Get the ambient component of the light. */
  91. inline const Vec4& getAmbient() const { return _ambient; }
  92. /** Set the diffuse component of the light. */
  93. inline void setDiffuse( const Vec4& diffuse ) { _diffuse = diffuse; }
  94. /** Get the diffuse component of the light. */
  95. inline const Vec4& getDiffuse() const { return _diffuse; }
  96. /** Set the specular component of the light. */
  97. inline void setSpecular( const Vec4& specular ) { _specular = specular; }
  98. /** Get the specular component of the light. */
  99. inline const Vec4& getSpecular() const { return _specular; }
  100. /** Set the position of the light. */
  101. inline void setPosition( const Vec4& position ) { _position = position; }
  102. /** Get the position of the light. */
  103. inline const Vec4& getPosition() const { return _position; }
  104. /** Set the direction of the light. */
  105. inline void setDirection( const Vec3& direction ) { _direction = direction; }
  106. /** Get the direction of the light. */
  107. inline const Vec3& getDirection() const { return _direction; }
  108. /** Set the constant attenuation of the light. */
  109. inline void setConstantAttenuation( float constant_attenuation ) { _constant_attenuation = constant_attenuation; }
  110. /** Get the constant attenuation of the light. */
  111. inline float getConstantAttenuation() const { return _constant_attenuation; }
  112. /** Set the linear attenuation of the light. */
  113. inline void setLinearAttenuation ( float linear_attenuation ) { _linear_attenuation = linear_attenuation; }
  114. /** Get the linear attenuation of the light. */
  115. inline float getLinearAttenuation () const { return _linear_attenuation; }
  116. /** Set the quadratic attenuation of the light. */
  117. inline void setQuadraticAttenuation ( float quadratic_attenuation ) { _quadratic_attenuation = quadratic_attenuation; }
  118. /** Get the quadratic attenuation of the light. */
  119. inline float getQuadraticAttenuation() const { return _quadratic_attenuation; }
  120. /** Set the spot exponent of the light. */
  121. inline void setSpotExponent( float spot_exponent ) { _spot_exponent = spot_exponent; }
  122. /** Get the spot exponent of the light. */
  123. inline float getSpotExponent() const { return _spot_exponent; }
  124. /** Set the spot cutoff of the light. */
  125. inline void setSpotCutoff( float spot_cutoff ) { _spot_cutoff = spot_cutoff; }
  126. /** Get the spot cutoff of the light. */
  127. inline float getSpotCutoff() const { return _spot_cutoff; }
  128. /** Capture the lighting settings of the current OpenGL state
  129. * and store them in this object.
  130. */
  131. void captureLightState();
  132. /** Apply the light's state to the OpenGL state machine. */
  133. virtual void apply(State& state) const;
  134. protected :
  135. virtual ~Light();
  136. /** Initialize the light's settings with some decent defaults. */
  137. void init();
  138. int _lightnum; // OpenGL light number
  139. Vec4 _ambient; // r, g, b, w
  140. Vec4 _diffuse; // r, g, b, w
  141. Vec4 _specular; // r, g, b, w
  142. Vec4 _position; // x, y, z, w
  143. Vec3 _direction; // x, y, z
  144. float _constant_attenuation; // constant
  145. float _linear_attenuation; // linear
  146. float _quadratic_attenuation; // quadratic
  147. float _spot_exponent; // exponent
  148. float _spot_cutoff; // spread
  149. };
  150. }
  151. #endif