3 * Copyright (C) 2007 Tim Moore
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 #include "StateAttributeFactory.hxx"
23 #include <osg/AlphaFunc>
25 #include <osg/BlendFunc>
26 #include <osg/CullFace>
28 #include <osg/ShadeModel>
29 #include <osg/Texture2D>
30 #include <osg/Texture3D>
35 #include <simgear/debug/logstream.hxx>
43 StateAttributeFactory::StateAttributeFactory()
45 _standardAlphaFunc = new AlphaFunc;
46 _standardAlphaFunc->setFunction(osg::AlphaFunc::GREATER);
47 _standardAlphaFunc->setReferenceValue(0.01f);
48 _standardAlphaFunc->setDataVariance(Object::STATIC);
49 _smooth = new ShadeModel;
50 _smooth->setMode(ShadeModel::SMOOTH);
51 _smooth->setDataVariance(Object::STATIC);
52 _flat = new ShadeModel(ShadeModel::FLAT);
53 _flat->setDataVariance(Object::STATIC);
54 _standardBlendFunc = new BlendFunc;
55 _standardBlendFunc->setSource(BlendFunc::SRC_ALPHA);
56 _standardBlendFunc->setDestination(BlendFunc::ONE_MINUS_SRC_ALPHA);
57 _standardBlendFunc->setDataVariance(Object::STATIC);
58 _standardTexEnv = new TexEnv;
59 _standardTexEnv->setMode(TexEnv::MODULATE);
60 _standardTexEnv->setDataVariance(Object::STATIC);
61 osg::Image *dummyImage = new osg::Image;
62 dummyImage->allocateImage(1, 1, 1, GL_LUMINANCE_ALPHA,
64 unsigned char* imageBytes = dummyImage->data(0, 0);
67 _whiteTexture = new osg::Texture2D;
68 _whiteTexture->setImage(dummyImage);
69 _whiteTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
70 _whiteTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
71 _whiteTexture->setDataVariance(osg::Object::STATIC);
72 // And now the transparent texture
73 dummyImage = new osg::Image;
74 dummyImage->allocateImage(1, 1, 1, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE);
75 imageBytes = dummyImage->data(0, 0);
78 _transparentTexture = new osg::Texture2D;
79 _transparentTexture->setImage(dummyImage);
80 _transparentTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
81 _transparentTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
82 _transparentTexture->setDataVariance(osg::Object::STATIC);
83 _white = new Vec4Array(1);
84 (*_white)[0].set(1.0f, 1.0f, 1.0f, 1.0f);
85 _white->setDataVariance(Object::STATIC);
86 _cullFaceFront = new CullFace(CullFace::FRONT);
87 _cullFaceFront->setDataVariance(Object::STATIC);
88 _cullFaceBack = new CullFace(CullFace::BACK);
89 _cullFaceBack->setDataVariance(Object::STATIC);
90 _depthWritesDisabled = new Depth(Depth::LESS, 0.0, 1.0, false);
91 _depthWritesDisabled->setDataVariance(Object::STATIC);
94 osg::Image* make3DNoiseImage(int texSize)
96 osg::Image* image = new osg::Image;
97 image->setImage(texSize, texSize, texSize,
98 4, GL_RGBA, GL_UNSIGNED_BYTE,
99 new unsigned char[4 * texSize * texSize * texSize],
100 osg::Image::USE_NEW_DELETE);
102 const int startFrequency = 4;
103 const int numOctaves = 4;
107 double inci, incj, inck;
108 int frequency = startFrequency;
112 SG_LOG(SG_TERRAIN, SG_BULK, "creating 3D noise texture... ");
114 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
116 SetNoiseFrequency(frequency);
118 ni[0] = ni[1] = ni[2] = 0;
120 inci = 1.0 / (texSize / frequency);
121 for (i = 0; i < texSize; ++i, ni[0] += inci)
123 incj = 1.0 / (texSize / frequency);
124 for (j = 0; j < texSize; ++j, ni[1] += incj)
126 inck = 1.0 / (texSize / frequency);
127 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
129 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
135 SG_LOG(SG_TERRAIN, SG_BULK, "DONE");
140 osg::Texture3D* StateAttributeFactory::getNoiseTexture(int size)
142 NoiseMap::iterator itr = _noises.find(size);
143 if (itr != _noises.end())
144 return itr->second.get();
145 Texture3D* noiseTexture = new osg::Texture3D;
146 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
147 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
148 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
149 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
150 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
151 noiseTexture->setImage( make3DNoiseImage(size) );
152 _noises.insert(std::make_pair(size, noiseTexture));
156 // anchor the destructor into this file, to avoid ref_ptr warnings
157 StateAttributeFactory::~StateAttributeFactory()