Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleStencilBuffer.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /**
26  @example ExampleStencilBuffer.cpp
27  @relates glfPBuffer, GLProjectionParametersPerspective,SceneOpenSceneGraph
28  @brief Example for usage Stencil buffer usage
29  @ingroup g_examples
30  @ingroup g_glviewer
31  @author MIP
32 */
33 
34 #include <iostream>
35 #include <Geometry/ProjectionParametersPerspective.hh>
36 #include <GLviewer/GLProjectionParametersPerspective.hh>
37 #include <GLviewer/Scenes/SceneOpenSceneGraph.hh>
38 #include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
39 #include <Base/Image/ImageIO.hh>
40 
41 using namespace std;
42 using namespace BIAS;
43 
44 int main(int argc, char *argv[]) {
45 
46 
47  glfPBuffer pbuffer;
48 
49  try {
50 
52  cfg.width = 5;
53  cfg.height = 5;
54  cfg.doubleBuffer = 0;
55  cfg.redSize = 8;
56  cfg.greenSize = 8;
57  cfg.blueSize = 8;
58  cfg.alphaSize = 0;
59  cfg.depthSize = 24;
60  cfg.stencilSize = 0;
61 
62  // initialize context
63  pbuffer.Init(cfg);
64 
65  } catch (glfException& e) {
66  std::cout << "Error: " << e.GetMessageString() << std::endl;
67  pbuffer.Destroy();
68  return -1;
69  }
70 
71 
72  //create synth projection parameters.
74  ppp.SetC(Vector3<double>(0,0,0));
75  ppp.SetR(RMatrix(Vector3<double>(1,0,0),0));
76  ppp.SetSimplePerspective(M_PI/2,320,240);
77  //some realistic distortion parameters
78 
79  ppp.SetUndistortion(-0.1614995748,0.1328560412,-6.499856681e-005,0.0005424174597);
81  glppp.SwitchToOffscreenRendering(true);
82  glppp.IgnoreDistortion(false);
83  glppp.SetSimplePerspectiveCam(90);
84  //glppp.UseStencilBuffer(true);
85  glppp.SetClearColor(Vector4<float>(1,0,1,0));
86  vector<SceneBase*> scenes;
88  sosg.Init();
89 //---- create a white quad
90  osg::Group* group = new osg::Group;
91  osg::Geometry* geom = new osg::Geometry;
92  osg::Vec3Array* vertices = new osg::Vec3Array(4);
93  osg::StateSet *stateset = group->getOrCreateStateSet();
94 
95  stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
96  (*vertices)[0] = osg::Vec3(-10, -10,10 );
97  (*vertices)[1] = osg::Vec3(10,-10 ,10 );
98  (*vertices)[2] = osg::Vec3(10,10 ,10 );
99  (*vertices)[3] = osg::Vec3(-10,10 ,10 );
100  geom->setVertexArray(vertices);
101  osg::Vec4Array* colors = new osg::Vec4Array(1);
102  (*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f);
103  geom->setColorArray(colors);
104  geom->setColorBinding(osg::Geometry::BIND_OVERALL);
105  geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
106  osg::Geode* geom_geode = new osg::Geode;
107  geom_geode->addDrawable(geom);
108  group->addChild(geom_geode);
109  osg::ref_ptr<osg::Node> node = group;
110 //----
111  sosg.AppendSubTree(node);
112  scenes.push_back(&sosg);
113 
114  glppp.SetAutoReshapeBehaviour(GLProjectionParametersBase::AdaptToCroppedImage);
115  glppp.SetIntrinsics(&ppp); //force initialization of distortion shader
116  glppp.SetGLMatrices(); //seems a good idea!
117  glppp.SetZClippingPlanes(1.0,20.0);
118  int res=glppp.Draw(scenes); //let's render!
119  res=glppp.Draw(scenes); //let's render!
120  if(0!=res)
121  {
122  cerr<<"draw returned "<<res<<endl;
123  return -1;
124  }
125  Image<float> depth;
126  Image<unsigned char> color;
127  glppp.GetImage(color);
128  cout << "get depth" << endl;
129  glppp.GetDepthMap(depth);
130  if(depth.IsEmpty())
131  {
132  cerr<<"draw image empty. "<<endl;
133  return -1;
134  }
135  ImageIO::Save("distortest.mip",depth);
136  ImageIO::Save("distortest-c.mip",color);
137 
138  //if we have at the center of the depth image a depth close to 10
139  float** pDat = depth.GetImageDataArray();
140  float d = pDat[depth.GetHeight()/2][depth.GetWidth()/2];
141  if((d<9.8f)||(d>14.0f))
142  {
143  cerr << "unexpected depth value - distortion failed." <<endl;
144  return -1;
145  }
146  return 0;
147 }
virtual void SetR(const BIAS::RMatrix &R)
Set orientation from rotation matrix R.
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void AppendSubTree(osg::ref_ptr< osg::Node > &childNode)
Append an OSG subtree to the constructed root tree.
Exception class used for run-time errors in the OpenGLFramework.
Definition: glfException.hh:79
unsigned int GetWidth() const
Definition: ImageBase.hh:312
virtual void Destroy()
Uninitializes the rendering context.
const std::string & GetMessageString() const
Returns the description of the error including the file name and line number where the error occured...
3D rotation matrix
Definition: RMatrix.hh:49
class Vec3f Vec3
unsigned int GetHeight() const
Definition: ImageBase.hh:319
GLX pbuffer rendering context.
Implements narrow access to OpenSceneGraph.
int Init()
Initializes OpenSG tree for scene appendance.
virtual void Init(const glfRenderingContextConfig &config)
Initializes the rendering context with the given configuration.
void SetSimplePerspective(const double FoV=M_PI/2, const unsigned int width=512, const unsigned int height=512)
Sets the parameters for a simple perspective camera with imagesize 512x512, focal length 512 (fov = 9...
Configuration for a rendering context.
virtual void SetC(const BIAS::Vector3< double > &C)
Set projection center.
class for rendering with projection parameters of ProjectionParametersPerspective ...
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase
Definition: Image.hh:153