00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include <OSGConfig.h>
00047 #include <OSGViewport.h>
00048 #include <OSGImage.h>
00049 #include <OSGTextureChunk.h>
00050 #include <OSGComponentTransform.h>
00051 #include <OSGGeometry.h>
00052 #include <OSGWindow.h>
00053 #include <OSGChunkMaterial.h>
00054 #include <OSGRegisterCombinersChunk.h>
00055 #include <OSGRenderAction.h>
00056 #include <OSGWindow.h>
00057 #include <OSGSHLChunk.h>
00058
00059 #include "OSGColorDisplayFilter.h"
00060
00061 OSG_BEGIN_NAMESPACE
00062
00063
00064
00065
00066
00071
00072
00073
00074
00075
00076
00077
00078
00079 void ColorDisplayFilter::initMethod (void)
00080 {
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 ColorDisplayFilter::ColorDisplayFilter(void) :
00095 Inherited()
00096 {
00097 }
00098
00099 ColorDisplayFilter::ColorDisplayFilter(const ColorDisplayFilter &source) :
00100 Inherited(source)
00101 {
00102 }
00103
00104 ColorDisplayFilter::~ColorDisplayFilter(void)
00105 {
00106 }
00107
00108
00109
00110 void ColorDisplayFilter::updateFilterValues()
00111 {
00112 UInt32 c;
00113 std::vector<UChar8> imageData;
00114
00115 if(_shadingImage == NullFC || _shlChunk == NullFC)
00116 return;
00117
00118
00119 UInt32 size = getWidth() * getHeight() * getDepth();
00120 UInt32 width = getWidth();
00121 UInt32 height = getHeight();
00122 UInt32 depth = getDepth();
00123 if(size != getMFTable()->size())
00124 {
00125
00126 FWARNING(("Wrong shanding table size\n"));
00127 width = height = 1;
00128 depth = 2;
00129 size = width * height * depth;
00130 imageData.push_back(0);
00131 imageData.push_back(0);
00132 imageData.push_back(0);
00133 imageData.push_back(255);
00134 imageData.push_back(255);
00135 imageData.push_back(255);
00136 }
00137 else
00138 {
00139 imageData.resize(size*3);
00140 for(c=0 ; c<size ; ++c)
00141 {
00142 imageData[c*3 + 0] = UChar8(getTable(c)[0]*255);
00143 imageData[c*3 + 1] = UChar8(getTable(c)[1]*255);
00144 imageData[c*3 + 2] = UChar8(getTable(c)[2]*255);
00145 }
00146 }
00147 _shadingImage->set(Image::OSG_RGB_PF,width,height,depth,1,1,0,&imageData[0]);
00148
00149 beginEditCP(_shlChunk);
00150 _shlChunk->setUniformParameter("colorMatrix", getMatrix());
00151 _shlChunk->setUniformParameter("gamma", getGamma());
00152 _shlChunk->setUniformParameter("shadingWidth", Int32(width));
00153 _shlChunk->setUniformParameter("shadingHeight", Int32(height));
00154 _shlChunk->setUniformParameter("shadingDepth", Int32(depth));
00155 endEditCP(_shlChunk);
00156 }
00157
00158 void ColorDisplayFilter::changed(BitVector whichField, UInt32 origin)
00159 {
00160
00161 Inherited::changed(whichField, origin);
00162 }
00163
00164 void ColorDisplayFilter::dump(UInt32, const BitVector ) const
00165 {
00166 SLOG << "Dump ColorDisplayFilter NI" << std::endl;
00167 }
00168
00169 void ColorDisplayFilter::createFilter(DisplayFilterForeground *fg,
00170 Viewport *port)
00171 {
00172 DisplayFilterForeground::DisplayFilterGroup *group =
00173 fg->findReadbackGroup("ColorDisplayFilter");
00174 WindowPtr window = port->getParent();
00175
00176 std::string vp_program =
00177 "varying vec2 position;\n"
00178 "varying mat4 shadingTexMat;\n"
00179 "\n"
00180 "void main(void)\n"
00181 "{\n"
00182 " gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
00183 " shadingTexMat = gl_TextureMatrix[1];\n"
00184 " gl_Position = ftransform();\n"
00185 " position = gl_Vertex.xy;\n"
00186 "}\n";
00187
00188 std::string fp_program =
00189 "varying vec2 position;\n"
00190 "varying mat4 shadingTexMat;\n"
00191 "\n"
00192 "uniform sampler2D grabTexture;\n"
00193 "uniform sampler3D shadingTexture;\n"
00194 "\n"
00195 "uniform mat4 colorMatrix;\n"
00196 "uniform float gamma;\n"
00197 "uniform int shadingWidth;\n"
00198 "uniform int shadingHeight;\n"
00199 "uniform int shadingDepth;\n"
00200 "\n"
00201 "void main(void)\n"
00202 "{\n"
00203 " // read color from grab texture\n"
00204 " vec4 color=texture2D(grabTexture,gl_TexCoord[0].xy);\n"
00205 " vec2 pos;\n"
00206 "\n"
00207 " // clamp to 0-1\n"
00208 " color.rgb = clamp(color.rgb,0.0,1.0);\n"
00209 "\n"
00210 " // make linear\n"
00211 " color.r = pow(color.r,gamma);\n"
00212 " color.g = pow(color.g,gamma);\n"
00213 " color.b = pow(color.b,gamma);\n"
00214 "\n"
00215 " // color matrix transformation\n"
00216 " color *= colorMatrix;\n"
00217 "\n"
00218 " // Scale color from the center of the first texel to the center of\n"
00219 " // the last texel\n"
00220 " float shadingScale = (float(shadingDepth)-1.0)/float(shadingDepth);\n"
00221 " float shadingOffset = (1.0 - shadingScale) / 2.0;\n"
00222 " color.rgb *= shadingScale;\n"
00223 " color.rgb += vec3(shadingOffset);\n"
00224 "\n"
00225 " shadingScale = (float(shadingWidth)-1.0)/float(shadingWidth);\n"
00226 " shadingOffset = (1.0 - shadingScale) / 2.0;\n"
00227 " pos.x = position.x * shadingScale;\n"
00228 " pos.x += shadingOffset;\n"
00229 "\n"
00230 " shadingScale = (float(shadingHeight)-1.0)/float(shadingHeight);\n"
00231 " shadingOffset = (1.0 - shadingScale) / 2.0;\n"
00232 " pos.y = position.y * shadingScale;\n"
00233 " pos.y += shadingOffset;\n"
00234 "\n"
00235 " vec4 lutCoordR = vec4(pos,color.r,1.0);\n"
00236 " lutCoordR = shadingTexMat * lutCoordR;\n"
00237 " vec4 lutCoordG = vec4(pos,color.g,1.0);\n"
00238 " lutCoordG = shadingTexMat * lutCoordG;\n"
00239 " vec4 lutCoordB = vec4(pos,color.b,1.0);\n"
00240 " lutCoordB = shadingTexMat * lutCoordB;\n"
00241 "\n"
00242 " // shading\n"
00243 " color.r = texture3D(shadingTexture,lutCoordR.rgb).r;\n"
00244 " color.g = texture3D(shadingTexture,lutCoordG.rgb).g;\n"
00245 " color.b = texture3D(shadingTexture,lutCoordB.rgb).b;\n"
00246 "\n"
00247 " gl_FragColor = color;\n"
00248 "}\n";
00249
00250 _shlChunk = SHLChunk::create();
00251 beginEditCP(_shlChunk);
00252 _shlChunk->setVertexProgram(vp_program);
00253 _shlChunk->setFragmentProgram(fp_program);
00254 _shlChunk->setUniformParameter("grabTexture", 0);
00255 _shlChunk->setUniformParameter("shadingTexture", 1);
00256 endEditCP(_shlChunk);
00257
00258 TextureChunkPtr shadingTextureChunk = TextureChunk::create();
00259 _shadingImage = Image::create();
00260 beginEditCP(shadingTextureChunk);
00261 shadingTextureChunk->setImage(_shadingImage);
00262 shadingTextureChunk->setMinFilter(GL_LINEAR);
00263 shadingTextureChunk->setMagFilter(GL_LINEAR);
00264 shadingTextureChunk->setWrapS(GL_CLAMP_TO_EDGE);
00265 shadingTextureChunk->setWrapT(GL_CLAMP_TO_EDGE);
00266 shadingTextureChunk->setWrapR(GL_CLAMP_TO_EDGE);
00267 shadingTextureChunk->setNPOTMatrixScale(true);
00268 shadingTextureChunk->setScale(false);
00269
00270 endEditCP(shadingTextureChunk);
00271
00272 beginEditCP(group->getMaterial());
00273 group->getMaterial()->addChunk(shadingTextureChunk);
00274 group->getMaterial()->addChunk(_shlChunk);
00275 endEditCP(group->getMaterial());
00276
00277 updateFilterValues();
00278 }
00279
00280
00281
00282
00283 #ifdef OSG_SGI_CC
00284 #pragma set woff 1174
00285 #endif
00286
00287 #ifdef OSG_LINUX_ICC
00288 #pragma warning( disable : 177 )
00289 #endif
00290
00291 namespace
00292 {
00293 static Char8 cvsid_cpp [] = "@(#)$Id: FCTemplate_cpp.h,v 1.19 2003/05/05 10:05:28 dirk Exp $";
00294 static Char8 cvsid_hpp [] = OSGCOLORDISPLAYFILTERBASE_HEADER_CVSID;
00295 static Char8 cvsid_inl [] = OSGCOLORDISPLAYFILTERBASE_INLINE_CVSID;
00296
00297 static Char8 cvsid_fields_hpp[] = OSGCOLORDISPLAYFILTERFIELDS_HEADER_CVSID;
00298 }
00299
00300 #ifdef __sgi
00301 #pragma reset woff 1174
00302 #endif
00303
00304 OSG_END_NAMESPACE
00305