CubeMapGenerator 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 OSGUTIL_CUBEMAPGENERATOR_
  14. #define OSGUTIL_CUBEMAPGENERATOR_
  15. #include <osgUtil/Export>
  16. #include <osg/Vec3>
  17. #include <osg/Vec4>
  18. #include <osg/CopyOp>
  19. #include <osg/Referenced>
  20. #include <osg/TextureCubeMap>
  21. #include <osg/Image>
  22. #include <osg/Notify>
  23. #include <vector>
  24. namespace osgUtil
  25. {
  26. /** This is the base class for cube map generators.
  27. It exposes the necessary interface to access the six generated images;
  28. descendants should only override the compute_color() method.
  29. */
  30. class OSGUTIL_EXPORT CubeMapGenerator: public osg::Referenced {
  31. public:
  32. explicit CubeMapGenerator(int texture_size = 64);
  33. CubeMapGenerator(const CubeMapGenerator &copy, const osg::CopyOp &copyop = osg::CopyOp::SHALLOW_COPY);
  34. inline osg::Image *getImage(osg::TextureCubeMap::Face face);
  35. inline const osg::Image *getImage(osg::TextureCubeMap::Face face) const;
  36. /** Generate the six cube images.
  37. If use_osg_system is true, then the OSG's coordinate system is used instead
  38. of the default OpenGL one.
  39. */
  40. void generateMap(bool use_osg_system = true);
  41. protected:
  42. virtual ~CubeMapGenerator() {}
  43. CubeMapGenerator &operator=(const CubeMapGenerator &) { return *this; }
  44. inline void set_pixel(int index, int c, int r, const osg::Vec4 &color);
  45. inline static osg::Vec4 vector_to_color(const osg::Vec3 &vec);
  46. /** Override this method to define how colors are computed.
  47. The parameter R is the reflection vector, pointing from the center of the cube.
  48. The return value should be the RGBA color associated with that reflection ray.
  49. */
  50. virtual osg::Vec4 compute_color(const osg::Vec3 &R) const = 0;
  51. private:
  52. int texture_size_;
  53. typedef std::vector<osg::ref_ptr<osg::Image> > Image_list;
  54. Image_list images_;
  55. };
  56. // INLINE METHODS
  57. inline osg::Image *CubeMapGenerator::getImage(osg::TextureCubeMap::Face face)
  58. {
  59. switch (face) {
  60. case osg::TextureCubeMap::POSITIVE_X: return images_[0].get();
  61. case osg::TextureCubeMap::NEGATIVE_X: return images_[1].get();
  62. case osg::TextureCubeMap::POSITIVE_Y: return images_[2].get();
  63. case osg::TextureCubeMap::NEGATIVE_Y: return images_[3].get();
  64. case osg::TextureCubeMap::POSITIVE_Z: return images_[4].get();
  65. case osg::TextureCubeMap::NEGATIVE_Z: return images_[5].get();
  66. default: return 0;
  67. }
  68. }
  69. inline const osg::Image *CubeMapGenerator::getImage(osg::TextureCubeMap::Face face) const
  70. {
  71. switch (face) {
  72. case osg::TextureCubeMap::POSITIVE_X: return images_[0].get();
  73. case osg::TextureCubeMap::NEGATIVE_X: return images_[1].get();
  74. case osg::TextureCubeMap::POSITIVE_Y: return images_[2].get();
  75. case osg::TextureCubeMap::NEGATIVE_Y: return images_[3].get();
  76. case osg::TextureCubeMap::POSITIVE_Z: return images_[4].get();
  77. case osg::TextureCubeMap::NEGATIVE_Z: return images_[5].get();
  78. default: return 0;
  79. }
  80. }
  81. inline void CubeMapGenerator::set_pixel(int index, int c, int r, const osg::Vec4 &color)
  82. {
  83. osg::Image *i = images_[index].get();
  84. if (i) {
  85. *(i->data(c, r)+0) = static_cast<unsigned char>(osg::clampBetween(color.x(),0.0f,1.0f) * 255);
  86. *(i->data(c, r)+1) = static_cast<unsigned char>(osg::clampBetween(color.y(),0.0f,1.0f) * 255);
  87. *(i->data(c, r)+2) = static_cast<unsigned char>(osg::clampBetween(color.z(),0.0f,1.0f) * 255);
  88. *(i->data(c, r)+3) = static_cast<unsigned char>(osg::clampBetween(color.w(),0.0f,1.0f) * 255);
  89. } else {
  90. osg::notify(osg::WARN) << "Warning: CubeMapGenerator::set_pixel(): invalid image index\n";
  91. }
  92. }
  93. inline osg::Vec4 CubeMapGenerator::vector_to_color(const osg::Vec3 &vec)
  94. {
  95. return osg::Vec4(
  96. vec.x() / vec.length() / 2 + 0.5f,
  97. vec.y() / vec.length() / 2 + 0.5f,
  98. vec.z() / vec.length() / 2 + 0.5f,
  99. 1);
  100. }
  101. }
  102. #endif