1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
35 \*---------------------------------------------------------------------------*/
36 //---------------------------------------------------------------------------
38 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
49 #include "OSGBaseFunctions.h"
51 #include "OSGSimpleSHLFunctions.h"
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
,
67 iFilterWidth
= Int32(floor(3.0f
* fBlurWidth
) - 1);
69 Int32 size
= iFilterWidth
* 2 + 1;
71 vWeights
.resize(size
);
75 for(Int32 x
= 0; x
< size
; x
++)
77 vWeights
[x
] = gaussian(Real32(x
- iFilterWidth
), fBlurWidth
);
82 for(Int32 x
= 0; x
< size
; x
++)
88 std::string
generate1DConvolutionFilterFPString(Real32 fBlurWidth
,
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];
109 if (i
*2+1 > nsamples
-1)
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
;
121 << "uniform sampler2D inputTex;\n"
123 << "void main(void)\n"
125 << " vec2 texCoord;\n"
126 << " vec4 sum = vec4(0., 0., 0., 0.);\n"
130 for(int i
=0; i
<nsamples2
; i
++) {
131 float x_offset
= 0, y_offset
= 0;
133 y_offset
= (i
*2)-width
+offsets
[i
];
135 x_offset
= (i
*2)-width
+offsets
[i
];
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("
146 << y_offset
<< ");\n";
149 // ost << "TEX H1, R0, texture[0], 2D;\n";
151 // ost << "TEX H1, R0, texture[0], RECT;\n";
156 // ost << "MUL H2, H1, {" << weight << "}.x;\n";
160 // ost << "MAD H2, H1, {" << weight << "}.x, H2;\n";
161 ost
<< " sum += texture2D(inputTex, texCoord) * "
168 << " gl_FragColor = sum;\n"
177 SimpleSHLChunkTransitPtr
generate1DConvolutionFilterFP(Real32 fBlurWidth
,
183 SimpleSHLChunkTransitPtr returnValue
= SimpleSHLChunk::create();
185 std::string szFragProg
= generate1DConvolutionFilterFPString(fBlurWidth
,
192 returnValue
->setFragmentProgram(szFragProg
);
197 SimpleSHLChunkTransitPtr
generate2DShrinkHalfFilterFP(UInt32 uiTexCoord
)
199 std::ostringstream ost
;
201 ost
<< "uniform sampler2D inputTex;"
205 << " gl_FragColor = texture2D(inputTex, gl_TexCoord["
206 << uiTexCoord
<< "].xy);"
209 SimpleSHLChunkTransitPtr returnValue
= SimpleSHLChunk::create();
211 returnValue
->setFragmentProgram(ost
.str());