TexGen 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_TEXGEN
  14. #define OSG_TEXGEN 1
  15. #include <osg/Plane>
  16. #include <osg/StateAttribute>
  17. #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) || defined(OSG_GLES3_AVAILABLE) || defined(OSG_GL3_AVAILABLE)
  18. #define GL_OBJECT_LINEAR 0x2401
  19. #define GL_EYE_LINEAR 0x2400
  20. #define GL_SPHERE_MAP 0x2402
  21. #define GL_TEXTURE_GEN_S 0x0C60
  22. #define GL_TEXTURE_GEN_T 0x0C61
  23. #define GL_TEXTURE_GEN_R 0x0C62
  24. #define GL_TEXTURE_GEN_Q 0x0C63
  25. #endif
  26. #ifndef GL_NORMAL_MAP_ARB
  27. #define GL_NORMAL_MAP_ARB 0x8511
  28. #endif
  29. #ifndef GL_REFLECTION_MAP_ARB
  30. #define GL_REFLECTION_MAP_ARB 0x8512
  31. #endif
  32. namespace osg {
  33. /** TexGen encapsulates the OpenGL glTexGen (texture coordinate generation)
  34. * state.*/
  35. class OSG_EXPORT TexGen : public StateAttribute
  36. {
  37. public :
  38. TexGen();
  39. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  40. TexGen(const TexGen& texgen,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  41. StateAttribute(texgen,copyop),
  42. _mode(texgen._mode),
  43. _plane_s(texgen._plane_s),
  44. _plane_t(texgen._plane_t),
  45. _plane_r(texgen._plane_r),
  46. _plane_q(texgen._plane_q) {}
  47. META_StateAttribute(osg, TexGen, TEXGEN);
  48. virtual bool isTextureAttribute() const { return true; }
  49. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  50. virtual int compare(const StateAttribute& sa) const
  51. {
  52. // Check for equal types, then create the rhs variable
  53. // used by the COMPARE_StateAttribute_Parameter macros below.
  54. COMPARE_StateAttribute_Types(TexGen,sa)
  55. // Compare each parameter in turn against the rhs.
  56. COMPARE_StateAttribute_Parameter(_mode)
  57. COMPARE_StateAttribute_Parameter(_plane_s)
  58. COMPARE_StateAttribute_Parameter(_plane_t)
  59. COMPARE_StateAttribute_Parameter(_plane_r)
  60. COMPARE_StateAttribute_Parameter(_plane_q)
  61. return 0; // Passed all the above comparison macros, so must be equal.
  62. }
  63. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  64. {
  65. usage.usesTextureMode(GL_TEXTURE_GEN_S);
  66. usage.usesTextureMode(GL_TEXTURE_GEN_T);
  67. // Not happy with turning all tex gen parameters on
  68. // as the OSG currently only supports 2D textures and therefore
  69. // only S and T will be required, R&Q would be redundant...
  70. // So commenting out the following until OSG supports 3D textures.
  71. // I plan to revamp the OpenGL state management later so will
  72. // tidy up then. Robert Osfield. Jan 2001.
  73. // The tidy up is now happening, but will have a think before
  74. // resolving the below parameters.
  75. usage.usesTextureMode(GL_TEXTURE_GEN_R);
  76. usage.usesTextureMode(GL_TEXTURE_GEN_Q);
  77. return true;
  78. }
  79. virtual void apply(State& state) const;
  80. enum Mode {
  81. OBJECT_LINEAR = GL_OBJECT_LINEAR,
  82. EYE_LINEAR = GL_EYE_LINEAR,
  83. SPHERE_MAP = GL_SPHERE_MAP,
  84. NORMAL_MAP = GL_NORMAL_MAP_ARB,
  85. REFLECTION_MAP = GL_REFLECTION_MAP_ARB
  86. };
  87. inline void setMode( Mode mode ) { _mode = mode; }
  88. Mode getMode() const { return _mode; }
  89. enum Coord {
  90. S, T, R, Q
  91. };
  92. void setPlane(Coord which, const Plane& plane);
  93. Plane& getPlane(Coord which);
  94. const Plane& getPlane(Coord which) const;
  95. /** Set the tex gen planes from specified matrix.
  96. * Typical usage would be to pass in a projection
  97. * matrix to set up projective texturing.
  98. */
  99. void setPlanesFromMatrix(const Matrixd& matrix);
  100. protected :
  101. virtual ~TexGen( void );
  102. Mode _mode;
  103. /** Additional texgen coefficients for GL_OBJECT_PLANE or
  104. * GL_EYE_PLANE, */
  105. Plane _plane_s, _plane_t, _plane_r, _plane_q;
  106. };
  107. }
  108. #endif