Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleStencilBuffer.cpp

Example for usage Stencil buffer usage

, GLProjectionParametersPerspective,SceneOpenSceneGraph

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003, 2004 (see file CONTACTS for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleStencilBuffer.cpp
@relates glfPBuffer, GLProjectionParametersPerspective,SceneOpenSceneGraph
@brief Example for usage Stencil buffer usage
@ingroup g_examples
@ingroup g_glviewer
@author MIP
*/
#include <iostream>
#include <Geometry/ProjectionParametersPerspective.hh>
#include <GLviewer/GLProjectionParametersPerspective.hh>
#include <GLviewer/Scenes/SceneOpenSceneGraph.hh>
#include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
#include <Base/Image/ImageIO.hh>
using namespace std;
using namespace BIAS;
int main(int argc, char *argv[]) {
glfPBuffer pbuffer;
try {
cfg.width = 5;
cfg.height = 5;
cfg.doubleBuffer = 0;
cfg.redSize = 8;
cfg.greenSize = 8;
cfg.blueSize = 8;
cfg.alphaSize = 0;
cfg.depthSize = 24;
cfg.stencilSize = 0;
// initialize context
pbuffer.Init(cfg);
} catch (glfException& e) {
std::cout << "Error: " << e.GetMessageString() << std::endl;
pbuffer.Destroy();
return -1;
}
//create synth projection parameters.
ppp.SetC(Vector3<double>(0,0,0));
ppp.SetR(RMatrix(Vector3<double>(1,0,0),0));
ppp.SetSimplePerspective(M_PI/2,320,240);
//some realistic distortion parameters
ppp.SetUndistortion(-0.1614995748,0.1328560412,-6.499856681e-005,0.0005424174597);
glppp.IgnoreDistortion(false);
//glppp.UseStencilBuffer(true);
glppp.SetClearColor(Vector4<float>(1,0,1,0));
vector<SceneBase*> scenes;
sosg.Init();
//---- create a white quad
osg::Group* group = new osg::Group;
osg::Geometry* geom = new osg::Geometry;
osg::Vec3Array* vertices = new osg::Vec3Array(4);
osg::StateSet *stateset = group->getOrCreateStateSet();
stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
(*vertices)[0] = osg::Vec3(-10, -10,10 );
(*vertices)[1] = osg::Vec3(10,-10 ,10 );
(*vertices)[2] = osg::Vec3(10,10 ,10 );
(*vertices)[3] = osg::Vec3(-10,10 ,10 );
geom->setVertexArray(vertices);
osg::Vec4Array* colors = new osg::Vec4Array(1);
(*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f);
geom->setColorArray(colors);
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
osg::Geode* geom_geode = new osg::Geode;
geom_geode->addDrawable(geom);
group->addChild(geom_geode);
//----
sosg.AppendSubTree(node);
scenes.push_back(&sosg);
glppp.SetAutoReshapeBehaviour(GLProjectionParametersBase::AdaptToCroppedImage);
glppp.SetIntrinsics(&ppp); //force initialization of distortion shader
glppp.SetGLMatrices(); //seems a good idea!
glppp.SetZClippingPlanes(1.0,20.0);
int res=glppp.Draw(scenes); //let's render!
res=glppp.Draw(scenes); //let's render!
if(0!=res)
{
cerr<<"draw returned "<<res<<endl;
return -1;
}
Image<float> depth;
glppp.GetImage(color);
cout << "get depth" << endl;
glppp.GetDepthMap(depth);
if(depth.IsEmpty())
{
cerr<<"draw image empty. "<<endl;
return -1;
}
ImageIO::Save("distortest.mip",depth);
ImageIO::Save("distortest-c.mip",color);
//if we have at the center of the depth image a depth close to 10
float** pDat = depth.GetImageDataArray();
float d = pDat[depth.GetHeight()/2][depth.GetWidth()/2];
if((d<9.8f)||(d>14.0f))
{
cerr << "unexpected depth value - distortion failed." <<endl;
return -1;
}
return 0;
}