BumpMapping 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. //osgFX - Copyright (C) 2003 Marco Jez
  14. #ifndef OSGFX_BUMPMAPPING_
  15. #define OSGFX_BUMPMAPPING_
  16. #include <osgFX/Export>
  17. #include <osgFX/Effect>
  18. #include <osg/ref_ptr>
  19. #include <osg/Texture2D>
  20. namespace osgFX
  21. {
  22. /**
  23. This effect makes surfaces appear bumpy. Children nodes must use two textures,
  24. one for diffuse color and one for the normal map (which can be created
  25. from a height map with tools like nVIDIA's normal map generator). Furthermore,
  26. tangent-space basis vectors must be created and assigned to each Geometry; this
  27. can be done quickly by calling BumpMapping::prepareChildren(). Note that both
  28. diffuse and normal map textures must have corresponding UV maps defined in
  29. Geometry objects.
  30. This effect defines a preferred technique which uses ARB vertex & fragment
  31. programs, and a fallback technique which doesn't use fragment programs. The
  32. latter is more limited though since it can't handle ambient and specular
  33. components.
  34. */
  35. class OSGFX_EXPORT BumpMapping: public Effect {
  36. public:
  37. BumpMapping();
  38. BumpMapping(const BumpMapping& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  39. META_Effect(osgFX, BumpMapping,
  40. "Bump Mapping",
  41. "This effect makes surfaces appear bumpy. Children nodes must use two textures, "
  42. "one for diffuse color and one for the normal map (which can be created "
  43. "from a height map with tools like nVIDIA's normal map generator). Furthermore, "
  44. "tangent-space basis vectors must be created and assigned to each Geometry; this "
  45. "can be done quickly by calling BumpMapping::prepareChildren(). Note that both "
  46. "diffuse and normal map textures must have corresponding UV maps defined in "
  47. "Geometry objects.\n"
  48. "This effect defines a preferred technique which uses ARB vertex & fragment "
  49. "programs, and a fallback technique which doesn't use fragment programs. The "
  50. "latter is more limited though since it can't handle ambient and specular "
  51. "components.",
  52. "Marco Jez");
  53. /** get the OpenGL light number */
  54. inline int getLightNumber() const;
  55. /** set the OpenGL light number that will be used in lighting computations */
  56. inline void setLightNumber(int n);
  57. /** get the texture unit that contains diffuse color texture. Default is 1 */
  58. inline int getDiffuseTextureUnit() const;
  59. /** set the texture unit that contains diffuse color texture. Default is 1 */
  60. inline void setDiffuseTextureUnit(int n);
  61. /** get the texture unit that contains normal map texture. Default is 0 */
  62. inline int getNormalMapTextureUnit() const;
  63. /** set the texture unit that contains normal map texture. Default is 0 */
  64. inline void setNormalMapTextureUnit(int n);
  65. /** get the diffuse color texture that overrides children's texture */
  66. inline osg::Texture2D* getOverrideDiffuseTexture();
  67. /** get the const diffuse color texture that overrides children's texture */
  68. inline const osg::Texture2D* getOverrideDiffuseTexture() const;
  69. /** set the diffuse color texture that overrides children's texture */
  70. inline void setOverrideDiffuseTexture(osg::Texture2D* texture);
  71. /** get the normal map texture that overrides children's texture */
  72. inline osg::Texture2D* getOverrideNormalMapTexture();
  73. /** get the const normal map texture that overrides children's texture */
  74. inline const osg::Texture2D* getOverrideNormalMapTexture() const;
  75. /** set the normal map texture that overrides children's texture */
  76. inline void setOverrideNormalMapTexture(osg::Texture2D* texture);
  77. /**
  78. prepare a Geometry for bump lighting. Tangent-space basis vectors are
  79. generated and attached to the geometry as vertex attribute arrays.
  80. */
  81. void prepareGeometry(osg::Geometry* geo);
  82. /** prepare a Node for bump lighting, calling prepareGeometry() for each Geometry */
  83. void prepareNode(osg::Node* node);
  84. /** prepare children for bump lighting. Actually calls prepareNode() for each child */
  85. void prepareChildren();
  86. /** set up a demo environment with predefined diffuse and normal maps, as well as texture coordinates */
  87. void setUpDemo();
  88. protected:
  89. virtual ~BumpMapping() {}
  90. BumpMapping &operator=(const BumpMapping &) { return *this; }
  91. bool define_techniques();
  92. private:
  93. int _lightnum;
  94. int _diffuse_unit;
  95. int _normal_unit;
  96. osg::ref_ptr<osg::Texture2D> _diffuse_tex;
  97. osg::ref_ptr<osg::Texture2D> _normal_tex;
  98. };
  99. // INLINE METHODS
  100. inline int BumpMapping::getLightNumber() const
  101. {
  102. return _lightnum;
  103. }
  104. inline void BumpMapping::setLightNumber(int n)
  105. {
  106. _lightnum = n;
  107. dirtyTechniques();
  108. }
  109. inline int BumpMapping::getDiffuseTextureUnit() const
  110. {
  111. return _diffuse_unit;
  112. }
  113. inline void BumpMapping::setDiffuseTextureUnit(int n)
  114. {
  115. _diffuse_unit = n;
  116. dirtyTechniques();
  117. }
  118. inline int BumpMapping::getNormalMapTextureUnit() const
  119. {
  120. return _normal_unit;
  121. }
  122. inline void BumpMapping::setNormalMapTextureUnit(int n)
  123. {
  124. _normal_unit = n;
  125. dirtyTechniques();
  126. }
  127. inline osg::Texture2D* BumpMapping::getOverrideDiffuseTexture()
  128. {
  129. return _diffuse_tex.get();
  130. }
  131. inline const osg::Texture2D* BumpMapping::getOverrideDiffuseTexture() const
  132. {
  133. return _diffuse_tex.get();
  134. }
  135. inline void BumpMapping::setOverrideDiffuseTexture(osg::Texture2D* texture)
  136. {
  137. _diffuse_tex = texture;
  138. dirtyTechniques();
  139. }
  140. inline osg::Texture2D* BumpMapping::getOverrideNormalMapTexture()
  141. {
  142. return _normal_tex.get();
  143. }
  144. inline const osg::Texture2D* BumpMapping::getOverrideNormalMapTexture() const
  145. {
  146. return _normal_tex.get();
  147. }
  148. inline void BumpMapping::setOverrideNormalMapTexture(osg::Texture2D* texture)
  149. {
  150. _normal_tex = texture;
  151. dirtyTechniques();
  152. }
  153. }
  154. #endif