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
00048 #include <OSGTextureGrabBackground.h>
00049 #include <OSGPassiveBackground.h>
00050 #include <OSGSolidBackground.h>
00051 #include <OSGSimpleGeometry.h>
00052 #include <OSGMaterialChunk.h>
00053 #include <OSGImage.h>
00054 #include <OSGMatrixCamera.h>
00055 #include <OSGRenderActionBase.h>
00056 #include <OSGDrawAction.h>
00057 #include <OSGMatrixUtility.h>
00058
00059 #include "OSGDisplayFilterForeground.h"
00060 #include "OSGDisplayFilter.h"
00061
00062 OSG_BEGIN_NAMESPACE
00063
00064
00065
00066
00067
00072
00073
00074
00075
00076
00077
00078
00079
00080 void DisplayFilterForeground::initMethod (void)
00081 {
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 DisplayFilterForeground::DisplayFilterForeground(void) :
00096 Inherited()
00097 {
00098 }
00099
00100 DisplayFilterForeground::DisplayFilterForeground(const DisplayFilterForeground &source) :
00101 Inherited(source)
00102 {
00103 }
00104
00105 DisplayFilterForeground::~DisplayFilterForeground(void)
00106 {
00107 }
00108
00109
00110
00111 void DisplayFilterForeground::changed(BitVector whichField, UInt32 origin)
00112 {
00113 if(whichField & FilterFieldMask) {
00114 clearGroups();
00115 }
00116 Inherited::changed(whichField, origin);
00117 }
00118
00119 void DisplayFilterForeground::dump( UInt32 ,
00120 const BitVector ) const
00121 {
00122 SLOG << "Dump DisplayFilterForeground NI" << std::endl;
00123 }
00124
00127 void DisplayFilterForeground::draw(DrawActionBase *action, Viewport *port)
00128 {
00129 if (!getActive())
00130 return;
00131
00132 UInt32 p,f;
00133 WindowPtr window = port->getParent();
00134 MFDisplayFilterPtr::iterator fI;
00135
00136 RenderActionBase *ract = dynamic_cast<RenderActionBase*>(action);
00137 DrawAction *dact = dynamic_cast<DrawAction*>(action);
00138
00139 glPushAttrib(GL_ALL_ATTRIB_BITS);
00140 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00141
00142
00143 if(_changedState.size() != getFilter().size())
00144 {
00145 clearGroups();
00146 }
00147 else
00148 {
00149 for(f = 0 ; f < getFilter().size() ; ++f)
00150 {
00151 if(_changedState[f] != getFilter()[f]->getChanged())
00152 {
00153 clearGroups();
00154 break;
00155 }
00156 }
00157 }
00158
00159
00160 if(_group.empty())
00161 {
00162
00163
00164
00165
00166
00167 _changedState.resize(getFilter().size());
00168 for(f = 0 ; f < getFilter().size() ; ++f)
00169 {
00170 _changedState[f] = getFilter()[f]->getChanged();
00171 if(getFilter()[f]->getEnabled())
00172 getFilter()[f]->createFilter(this,port);
00173 }
00174 }
00175
00176 for(p = 0 ; p < _port.size() ; ++p)
00177 {
00178 _port[p]->setLeft(port->getLeft());
00179 _port[p]->setBottom(port->getBottom());
00180 _port[p]->setRight(port->getRight());
00181 _port[p]->setTop(port->getTop());
00182 }
00183
00184 _hasNonPowTwoTex = true;
00185
00186
00187 if(!_hasNonPowTwoTex)
00188 {
00189 std::map<std::string,DisplayFilterGroup*>::iterator cI;
00190 for(cI = _group.begin() ;
00191 cI != _group.end() ;
00192 ++cI)
00193 {
00194 Matrix m;
00195 UInt32 tw = osgnextpower2(port->getPixelWidth());
00196 UInt32 th = osgnextpower2(port->getPixelHeight());
00197 Real32 sx = (Real32)port->getPixelWidth() / tw;
00198 Real32 sy = (Real32)port->getPixelHeight() / th;
00199 m.setScale(sx,sy,1);
00200 if(m != cI->second->getTextureTransform()->getMatrix())
00201 {
00202 beginEditCP(cI->second->getTextureTransform(),TextureTransformChunk::MatrixFieldMask);
00203 cI->second->getTextureTransform()->setMatrix(m);
00204 endEditCP(cI->second->getTextureTransform(),TextureTransformChunk::MatrixFieldMask);
00205 }
00206 }
00207 }
00208
00209
00210 for(fI = getFilter().begin() ;
00211 fI != getFilter().end() ;
00212 ++fI) {
00213 if((*fI)->getEnabled())
00214 (*fI)->updateFilter(port);
00215 }
00216
00217 for(p = 0 ; p < _port.size() ; ++p)
00218 {
00219
00220
00221
00222
00223 _port[p]->setParent(window);
00224 if(ract)
00225 {
00226 _port[p]->render(ract);
00227 }
00228 else
00229 if(dact)
00230 _port[p]->draw(dact);
00231
00232 _port[p]->setParent(NullFC);
00233 }
00234
00235 glPopAttrib();
00236 }
00237
00238 DisplayFilterForeground::DisplayFilterGroup *DisplayFilterForeground::findReadbackGroup(const std::string &name)
00239 {
00240 DisplayFilterGroup *group;
00241 std::map<std::string,DisplayFilterGroup*>::iterator cI;
00242
00243 std::string id = name + "_overlay";
00244 cI = _group.find(name);
00245 if(cI != _group.end())
00246 return cI->second;
00247
00248 group = new DisplayFilterGroup(true,_port);
00249 _group[name] = group;
00250 return group;
00251 }
00252
00253 DisplayFilterForeground::DisplayFilterGroup *DisplayFilterForeground::findOverlayGroup(const std::string &name)
00254 {
00255 DisplayFilterGroup *group;
00256 std::map<std::string,DisplayFilterGroup*>::iterator cI;
00257
00258 std::string id = name + "_readback";
00259 cI = _group.find(id);
00260 if(cI != _group.end())
00261 return cI->second;
00262
00263 group = new DisplayFilterGroup(false,_port);
00264 _group[id] = group;
00265 return group;
00266 }
00267
00268 void DisplayFilterForeground::clearGroups()
00269 {
00270 std::map<std::string,DisplayFilterGroup*>::iterator cI;
00271 for(cI = _group.begin() ;
00272 cI != _group.end() ;
00273 ++cI)
00274 delete cI->second;
00275 _group.clear();
00276 _port.clear();
00277 }
00278
00279
00280
00281
00282
00292 DisplayFilterForeground::DisplayFilterGroup::DisplayFilterGroup(
00293 bool readback,
00294 std::vector<ViewportPtr> &ports)
00295 {
00296
00297 _vp = Viewport::create();
00298 _geometry = Geometry::create();
00299 _transform = ComponentTransform::create();
00300 _material = ChunkMaterial::create();
00301 _root = Node::create();
00302 NodePtr node = Node::create();
00303 MatrixCameraPtr cam = MatrixCamera::create();
00304 beginEditCP(node);
00305 beginEditCP(_vp);
00306 beginEditCP(_root);
00307 beginEditCP(_geometry);
00308 beginEditCP(_material);
00309 beginEditCP(_transform);
00310
00311 createGrid(2,2);
00312
00313 addRefCP(_vp);
00314 addRefCP(_root);
00315
00316 ports.push_back(_vp);
00317
00318
00319 cam->setNear(-1);
00320 cam->setFar ( 1);
00321 cam->setModelviewMatrix(Matrix::identity());
00322 cam->setProjectionMatrix(Matrix::identity());
00323
00324 Matrix proj;
00325 proj.setIdentity();
00326 MatrixOrthogonal(proj,0,1,0,1,-1,1);
00327 cam->setProjectionMatrix(proj);
00328
00329
00330 node->setCore(_geometry);
00331 _root->setCore(_transform);
00332 _root->addChild(node);
00333 _geometry->setMaterial(_material);
00334
00335 _vp->setRoot(_root);
00336 _vp->setCamera(cam);
00337
00338 if(!readback)
00339 {
00340
00341 PassiveBackgroundPtr back = PassiveBackground::create();
00342 _vp->setBackground(back);
00343 }
00344 else
00345 {
00346
00347 ImagePtr img = Image::create();
00348 beginEditCP(img);
00349 img->set(GL_RGB,1,1);
00350 endEditCP(img);
00351
00352 _texture = TextureChunk::create();
00353 beginEditCP(_texture);
00354 _texture->setImage(img);
00355 _texture->setWrapS(GL_CLAMP_TO_EDGE);
00356 _texture->setWrapT(GL_CLAMP_TO_EDGE);
00357 _texture->setMinFilter(GL_LINEAR);
00358 _texture->setMagFilter(GL_LINEAR);
00359 _texture->setScale(false);
00360 _texture->setEnvMode(GL_REPLACE);
00361 _texture->setNPOTMatrixScale(true);
00362 _texture->setScale(false);
00363 endEditCP(_texture);
00364
00365 TextureGrabBackgroundPtr back = TextureGrabBackground::create();
00366 beginEditCP(back);
00367 back->setTexture(_texture);
00368 back->setColor(Color3f(0,0,0));
00369 endEditCP(back);
00370 _vp->setBackground(back);
00371
00372 _material->addChunk(_texture);
00373 }
00374
00375
00376 _textureTransform = TextureTransformChunk::create();
00377 _material->addChunk(_textureTransform);
00378
00379 endEditCP(_vp);
00380 endEditCP(node);
00381 endEditCP(_root);
00382 endEditCP(_geometry);
00383 endEditCP(_material);
00384 endEditCP(_transform);
00385 }
00386
00387 DisplayFilterForeground::DisplayFilterGroup::~DisplayFilterGroup()
00388 {
00389 subRefCP(_vp);
00390 subRefCP(_root);
00391 }
00392
00393 TextureChunkPtr DisplayFilterForeground::DisplayFilterGroup::getTexture(void)
00394 {
00395 return _texture;
00396 }
00397
00398 GeometryPtr DisplayFilterForeground::DisplayFilterGroup::getGeometry(void)
00399 {
00400 return _geometry;
00401 }
00402
00403 ChunkMaterialPtr DisplayFilterForeground::DisplayFilterGroup::getMaterial(void)
00404 {
00405 return _material;
00406 }
00407
00408 ComponentTransformPtr DisplayFilterForeground::DisplayFilterGroup::getTransform(void)
00409 {
00410 return _transform;
00411 }
00412
00413 TextureTransformChunkPtr DisplayFilterForeground::DisplayFilterGroup::getTextureTransform(void)
00414 {
00415 return _textureTransform;
00416 }
00417
00418 void DisplayFilterForeground::DisplayFilterGroup::createGrid(UInt32 width,UInt32 height)
00419 {
00420 GeoPTypesUI8Ptr types = GeoPTypesUI8::create();
00421 GeoPLengthsPtr lens = GeoPLengthsUI32::create();
00422 GeoIndicesUI32Ptr index = GeoIndicesUI32::create();
00423 GeoPositions3fPtr pos = GeoPositions3f::create();
00424 GeoTexCoords2fPtr texcoords = GeoTexCoords2f::create();
00425
00426 if(width<2)
00427 width = 2;
00428 if(height<2)
00429 height = 2;
00430
00431 beginEditCP(pos);
00432 beginEditCP(texcoords);
00433 beginEditCP(lens);
00434 beginEditCP(index);
00435
00436 UInt16 x,y;
00437 Real32 xstep,ystep;
00438 xstep=1.0 / (width-1);
00439 ystep=1.0 / (height-1);
00440
00441 for(y = 0; y < height; y++)
00442 {
00443 for(x = 0; x < width; x++)
00444 {
00445 pos->push_back(Pnt3f(x*xstep,y*ystep,0));
00446 texcoords->push_back(Vec2f(x*xstep, y*ystep));
00447 }
00448 }
00449
00450 for(y = 0; y < (height-1); y++)
00451 {
00452 types->push_back(GL_TRIANGLE_STRIP);
00453 lens->push_back(2 * width);
00454
00455 for(x = 0; x < width; x++)
00456 {
00457 index->push_back((y + 1) * width + x);
00458 index->push_back( y * width + x);
00459 }
00460 }
00461
00462 beginEditCP(_geometry);
00463 _geometry->setPositions(pos);
00464 _geometry->setTypes(types);
00465 _geometry->setTexCoords(texcoords);
00466 _geometry->setLengths(lens);
00467 _geometry->setIndices(index);
00468 endEditCP(_geometry);
00469
00470 endEditCP(pos);
00471 endEditCP(texcoords);
00472 endEditCP(lens);
00473 endEditCP(index);
00474 }
00475
00476
00477
00478
00479 #ifdef OSG_SGI_CC
00480 #pragma set woff 1174
00481 #endif
00482
00483 #ifdef OSG_LINUX_ICC
00484 #pragma warning( disable : 177 )
00485 #endif
00486
00487 namespace
00488 {
00489 static Char8 cvsid_cpp [] = "@(#)$Id: FCTemplate_cpp.h,v 1.19 2003/05/05 10:05:28 dirk Exp $";
00490 static Char8 cvsid_hpp [] = OSGDISPLAYFILTERFOREGROUNDBASE_HEADER_CVSID;
00491 static Char8 cvsid_inl [] = OSGDISPLAYFILTERFOREGROUNDBASE_INLINE_CVSID;
00492
00493 static Char8 cvsid_fields_hpp[] = OSGDISPLAYFILTERFOREGROUNDFIELDS_HEADER_CVSID;
00494 }
00495
00496 #ifdef __sgi
00497 #pragma reset woff 1174
00498 #endif
00499
00500 OSG_END_NAMESPACE
00501