]> git.mxchange.org Git - simgear.git/blob - simgear/scene/util/StateAttributeFactory.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / util / StateAttributeFactory.cxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2007 Tim Moore
4  *
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.
9  *
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.
14  *
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,
18  * MA 02110-1301, USA.
19  *
20  */
21 #include "StateAttributeFactory.hxx"
22
23 #include <osg/AlphaFunc>
24 #include <osg/Array>
25 #include <osg/BlendFunc>
26 #include <osg/CullFace>
27 #include <osg/Depth>
28 #include <osg/ShadeModel>
29 #include <osg/Texture2D>
30 #include <osg/Texture3D>
31 #include <osg/TexEnv>
32
33 #include <osg/Image>
34
35 #include <simgear/debug/logstream.hxx>
36
37 #include "Noise.hxx"
38
39 using namespace osg;
40
41 namespace simgear
42 {
43 StateAttributeFactory::StateAttributeFactory()
44 {
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,
63                               GL_UNSIGNED_BYTE);
64     unsigned char* imageBytes = dummyImage->data(0, 0);
65     imageBytes[0] = 255;
66     imageBytes[1] = 255;
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);
76     imageBytes[0] = 255;
77     imageBytes[1] = 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);
92 }
93
94 osg::Image* make3DNoiseImage(int texSize)
95 {
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);
101
102     const int startFrequency = 4;
103     const int numOctaves = 4;
104
105     int f, i, j, k, inc;
106     double ni[3];
107     double inci, incj, inck;
108     int frequency = startFrequency;
109     GLubyte *ptr;
110     double amp = 0.5;
111
112     SG_LOG(SG_TERRAIN, SG_BULK, "creating 3D noise texture... ");
113
114     for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
115     {
116         SetNoiseFrequency(frequency);
117         ptr = image->data();
118         ni[0] = ni[1] = ni[2] = 0;
119
120         inci = 1.0 / (texSize / frequency);
121         for (i = 0; i < texSize; ++i, ni[0] += inci)
122         {
123             incj = 1.0 / (texSize / frequency);
124             for (j = 0; j < texSize; ++j, ni[1] += incj)
125             {
126                 inck = 1.0 / (texSize / frequency);
127                 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
128                 {
129                     *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
130                 }
131             }
132         }
133     }
134
135     SG_LOG(SG_TERRAIN, SG_BULK, "DONE");
136
137     return image;
138 }
139
140 osg::Texture3D* StateAttributeFactory::getNoiseTexture(int size)
141 {
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));
153     return noiseTexture;
154 }
155
156 // anchor the destructor into this file, to avoid ref_ptr warnings
157 StateAttributeFactory::~StateAttributeFactory()
158 {
159   
160 }
161 }