| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osgUtil/CubeMapGenerator> |
|---|
| 14 | #include <stdlib.h> |
|---|
| 15 | |
|---|
| 16 | #include <osg/Matrix> |
|---|
| 17 | |
|---|
| 18 | using namespace osgUtil; |
|---|
| 19 | |
|---|
| 20 | CubeMapGenerator::CubeMapGenerator(int texture_size) |
|---|
| 21 | : osg::Referenced(), |
|---|
| 22 | texture_size_(texture_size) |
|---|
| 23 | { |
|---|
| 24 | for (int i=0; i<6; ++i) |
|---|
| 25 | { |
|---|
| 26 | osg::ref_ptr<osg::Image> image = new osg::Image; |
|---|
| 27 | unsigned char* data = new unsigned char [texture_size*texture_size*4]; |
|---|
| 28 | image->setImage(texture_size, texture_size, 1, 4, GL_RGBA, GL_UNSIGNED_BYTE, data, osg::Image::USE_NEW_DELETE); |
|---|
| 29 | images_.push_back(image); |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | CubeMapGenerator::CubeMapGenerator(const CubeMapGenerator ©, const osg::CopyOp ©op) |
|---|
| 34 | : osg::Referenced(copy), |
|---|
| 35 | texture_size_(copy.texture_size_) |
|---|
| 36 | { |
|---|
| 37 | Image_list::const_iterator i; |
|---|
| 38 | for (i=copy.images_.begin(); i!=copy.images_.end(); ++i) |
|---|
| 39 | { |
|---|
| 40 | images_.push_back(static_cast<osg::Image *>(copyop(i->get()))); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void CubeMapGenerator::generateMap(bool use_osg_system) |
|---|
| 45 | { |
|---|
| 46 | osg::Matrix M; |
|---|
| 47 | |
|---|
| 48 | if (use_osg_system) { |
|---|
| 49 | M = osg::Matrix::rotate(osg::PI_2, osg::Vec3(1, 0, 0)); |
|---|
| 50 | } else { |
|---|
| 51 | M = osg::Matrix::identity(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | const float dst = 2.0f/(texture_size_-1); |
|---|
| 55 | |
|---|
| 56 | float t = -1; |
|---|
| 57 | for (int i=0; i<texture_size_; ++i) { |
|---|
| 58 | float s = -1; |
|---|
| 59 | for (int j=0; j<texture_size_; ++j) { |
|---|
| 60 | set_pixel(0, j, i, compute_color(osg::Vec3(1, -t, -s) * M)); |
|---|
| 61 | set_pixel(1, j, i, compute_color(osg::Vec3(-1, -t, s) * M)); |
|---|
| 62 | set_pixel(2, j, i, compute_color(osg::Vec3(s, 1, t) * M)); |
|---|
| 63 | set_pixel(3, j, i, compute_color(osg::Vec3(s, -1, -t) * M)); |
|---|
| 64 | set_pixel(4, j, i, compute_color(osg::Vec3(s, -t, 1) * M)); |
|---|
| 65 | set_pixel(5, j, i, compute_color(osg::Vec3(-s, -t, -1) * M)); |
|---|
| 66 | s += dst; |
|---|
| 67 | } |
|---|
| 68 | t += dst; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|