root/OpenSceneGraph/trunk/src/osgUtil/CubeMapGenerator.cpp @ 13041

Revision 13041, 2.4 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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#include <osgUtil/CubeMapGenerator>
14#include <stdlib.h>
15
16#include <osg/Matrix>
17
18using namespace osgUtil;
19
20CubeMapGenerator::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
33CubeMapGenerator::CubeMapGenerator(const CubeMapGenerator &copy, const osg::CopyOp &copyop)
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
44void 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}
Note: See TracBrowser for help on using the browser.