fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / State / Shader / SHL / OSGSimpleSHLFunctions.cpp
blob82de2cebec93a9dff1d7af3975d45d5102dc566c
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
8 * *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
11 * License *
12 * *
13 * This library is free software; you can redistribute it and/or modify it *
14 * under the terms of the GNU Library General Public License as published *
15 * by the Free Software Foundation, version 2. *
16 * *
17 * This library is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * Library General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU Library General Public *
23 * License along with this library; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
25 * *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
28 * Changes *
29 * *
30 * *
31 * *
32 * *
33 * *
34 * *
35 \*---------------------------------------------------------------------------*/
36 //---------------------------------------------------------------------------
37 // Includes
38 //---------------------------------------------------------------------------
40 #include <cstdlib>
41 #include <cstdio>
43 #include <sstream>
44 #include <fstream>
46 #include "OSGConfig.h"
47 #include "OSGLog.h"
48 #include "OSGImage.h"
49 #include "OSGBaseFunctions.h"
51 #include "OSGSimpleSHLFunctions.h"
54 OSG_BEGIN_NAMESPACE
56 // 1d Gaussian distribution, s is standard deviation
57 Real32 gaussian(Real32 x, Real32 s)
59 return expf(-x*x/(2.0f*s*s)) / (s*sqrtf(2.0f*Pi));
62 // generate array of weights for Gaussian blur
63 void generateGaussianWeights(Real32 fBlurWidth,
64 std::vector<Real32> &vWeights,
65 Int32 &iFilterWidth)
67 iFilterWidth = Int32(floor(3.0f * fBlurWidth) - 1);
69 Int32 size = iFilterWidth * 2 + 1;
71 vWeights.resize(size);
73 Real32 sum = 0.0;
75 for(Int32 x = 0; x < size; x++)
77 vWeights[x] = gaussian(Real32(x - iFilterWidth), fBlurWidth);
79 sum += vWeights[x];
82 for(Int32 x = 0; x < size; x++)
84 vWeights[x] /= sum;
88 std::string generate1DConvolutionFilterFPString(Real32 fBlurWidth,
89 bool vertical,
90 bool tex2D,
91 Int32 imgWidth,
92 Int32 imgHeight)
94 int width;
95 std::vector<Real32> weights;
97 generateGaussianWeights(fBlurWidth, weights, width);
99 // calculate new set of weights and offsets
100 int nsamples = 2*width+1;
101 int nsamples2 = Int32(ceilf(nsamples/2.0f));
102 float *weights2 = new float [nsamples2];
103 float *offsets = new float [nsamples2];
105 for(int i=0; i<nsamples2; i++)
107 float a = weights[i*2];
108 float b;
109 if (i*2+1 > nsamples-1)
110 b = 0;
111 else
112 b = weights[i*2+1];
113 weights2[i] = a + b;
114 offsets[i] = b / (a + b);
115 // printf("%d: %f %f\n", i, weights2[i], offsets[i]);
117 // printf("nsamples = %d\n", nsamples2);
119 std::ostringstream ost;
120 ost << "\n"
121 << "uniform sampler2D inputTex;\n"
122 << "\n"
123 << "void main(void)\n"
124 << "{\n"
125 << " vec2 texCoord;\n"
126 << " vec4 sum = vec4(0., 0., 0., 0.);\n"
127 << "\n";
130 for(int i=0; i<nsamples2; i++) {
131 float x_offset = 0, y_offset = 0;
132 if (vertical) {
133 y_offset = (i*2)-width+offsets[i];
134 } else {
135 x_offset = (i*2)-width+offsets[i];
137 if (tex2D) {
138 x_offset = x_offset / imgWidth;
139 y_offset = y_offset / imgHeight;
141 float weight = weights2[i];
144 ost << " texCoord = gl_TexCoord[0].xy + vec2("
145 << x_offset << ", "
146 << y_offset << ");\n";
148 // if (tex2D) {
149 // ost << "TEX H1, R0, texture[0], 2D;\n";
150 // } else {
151 // ost << "TEX H1, R0, texture[0], RECT;\n";
152 // }
154 if (i==-width)
156 // ost << "MUL H2, H1, {" << weight << "}.x;\n";
158 else
160 // ost << "MAD H2, H1, {" << weight << "}.x, H2;\n";
161 ost << " sum += texture2D(inputTex, texCoord) * "
162 << weight
163 << ";\n";
167 ost << "\n"
168 << " gl_FragColor = sum;\n"
169 << "}\n";
171 delete [] weights2;
172 delete [] offsets;
174 return ost.str();
177 SimpleSHLChunkTransitPtr generate1DConvolutionFilterFP(Real32 fBlurWidth,
178 bool vertical,
179 bool tex2D,
180 Int32 imgWidth,
181 Int32 imgHeight)
183 SimpleSHLChunkTransitPtr returnValue = SimpleSHLChunk::create();
185 std::string szFragProg = generate1DConvolutionFilterFPString(fBlurWidth,
186 vertical,
187 tex2D,
188 imgWidth,
189 imgHeight );
192 returnValue->setFragmentProgram(szFragProg);
194 return returnValue;
197 SimpleSHLChunkTransitPtr generate2DShrinkHalfFilterFP(UInt32 uiTexCoord)
199 std::ostringstream ost;
201 ost << "uniform sampler2D inputTex;"
202 << ""
203 << "void main(void)"
204 << "{"
205 << " gl_FragColor = texture2D(inputTex, gl_TexCoord["
206 << uiTexCoord << "].xy);"
207 << "}";
209 SimpleSHLChunkTransitPtr returnValue = SimpleSHLChunk::create();
211 returnValue->setFragmentProgram(ost.str());
213 return returnValue;
216 OSG_END_NAMESPACE