Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMultisampleBlit.cpp
1 
2 /*
3  This file is part of the BIAS library (Basic ImageAlgorithmS).
4 
5  Copyright (C) 2003-2009 (see file CONTACT for details)
6  Multimediale Systeme der Informationsverarbeitung
7  Institut fuer Informatik
8  Christian-Albrechts-Universitaet Kiel
9 
10 
11  BIAS is free software; you can redistribute it and/or modify
12  it under the terms of the GNU Lesser General Public License as published by
13  the Free Software Foundation; either version 2.1 of the License, or
14  (at your option) any later version.
15 
16  BIAS is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public License
22  along with BIAS; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */
25 
26 
27 /**
28  @example ExampleMultisampleBlit.cpp
29  @relates PMDWarp,glfFramebufferObject
30  @brief Example for rendering something using the multisample.
31  This provides anti aliasing for rendering things in a framebuffer
32  @ingroup g_examples
33  @ingroup g_openglframework
34  @author Ingo Schiller / Bogomil Bartzcak
35  @date 12/2009
36 */
37 
38 
39 #include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
40 #include <OpenGLFramework/SpecializedBatches/PMDWarp.hh>
41 #include <OpenGLFramework/Base/glfException.hh>
42 #include <OpenGLFramework/Base/glfFramebufferObject.hh>
43 
44 
45 #include <Utils/Param.hh>
46 #include <Utils/IOUtils.hh>
47 #include <Base/Image/ImageIO.hh>
48 #include <Base/Image/Image.hh>
49 #include <Image/Camera.hh>
50 
51 #include <Gui/biasgl.h>
52 #include <GLviewer/Scenes/ScenePointLight.hh>
53 #include <GLviewer/GLProjectionParametersPerspective.hh>
54 #ifdef BIAS_HAVE_OPENSCENEGRAPH
55 #include <GLviewer/Scenes/ScenePlainOpenSceneGraph.hh>
56 #define HAVE_A_SCENEGRAPH
57 #endif
58 #ifdef BIAS_HAVE_OSG
59 #include <GLviewer/Scenes/SceneOSG.hh>
60 #define HAVE_A_SCENEGRAPH
61 #endif
62 
63 using namespace std;
64 using namespace BIAS;
65 
66 
67 
68 int main(int argc, char* argv[])
69 {
70  Param params(true);
71 
72  string *model = NULL;
73  model = params.AddParamString("Model", "", "",'m',GRP_NOSHOW);
74  string *projection = params.AddParamString("Projection", "", "",'p',GRP_NOSHOW);
75  int *numSamples = params.AddParamInt("samples", "number of samples used for multi sample buffers",
76  16, 0,16,'b',GRP_NOSHOW);
77 
78  if(!IOUtils::ParseCommandLineEvalHelp(params, argc, argv) )
79  return 0;
80 
81 #ifdef HAVE_A_SCENEGRAPH
82  bool hasmodel = false;
83 #endif
84  unsigned int width = 1024;
85  unsigned int height = 768;
86 
87 
88  glfPBuffer pbuffer;
89  try {
90 
92  cfg.width = 5;
93  cfg.height = 5;
94  cfg.doubleBuffer = 0;
95  cfg.redSize = 8;
96  cfg.greenSize = 8;
97  cfg.blueSize = 8;
98  cfg.alphaSize = 0;
99  cfg.depthSize = 24;
100  cfg.stencilSize = 0;
101 
102  pbuffer.Init(cfg);
103  pbuffer.MakeCurrent();
104  glViewport(0,0,width,height);
105 
106  /**
107  * Build target fbo with textures
108  **/
109  glfTexture2D colorTexture;
110  colorTexture.Create();
111  colorTexture.SetMagFilter(GL_NEAREST);
112  colorTexture.SetMinFilter(GL_NEAREST);
113  colorTexture.SetWrapS(GL_CLAMP);
114  colorTexture.SetWrapT(GL_CLAMP);
115  colorTexture.Allocate(width, height, GL_RGB);
116 
117  glfTexture2D depthTexture;
118  depthTexture.Create();
119  depthTexture.SetMagFilter(GL_NEAREST);
120  depthTexture.SetMinFilter(GL_NEAREST);
121  depthTexture.SetWrapS(GL_CLAMP);
122  depthTexture.SetWrapT(GL_CLAMP);
123  depthTexture.Allocate(width, height, GL_DEPTH_COMPONENT);
124 
125  glfFramebufferObject targetFbo;
126  targetFbo.Create();
127  targetFbo.AttachTexture(colorTexture, GL_COLOR_ATTACHMENT0_EXT);
128  targetFbo.AttachTexture(depthTexture, GL_DEPTH_ATTACHMENT_EXT);
129  targetFbo.CheckComplete();
130 
131  GLF_THROW_ON_OPENGL_ERROR;
132 
134  glParams.SetSimplePerspectiveCam(60.0, width, height);
135  glParams.SetZClippingPlanes(10, 7500);
136  if(*projection != "") {
137  cout<<"Loading projection:"<<*projection<<endl;
138  Projection proj;
139  int ret = proj.XMLRead(*projection);
140  if(ret != 0) BIASERR("Reading projection failed!");
141  glParams.SetIntrinsics(proj.GetParameters());
142  glParams.SetExtrinsics(proj.GetC(),proj.GetQ());
143  glParams.SetUndistortion(0.0,0.0,0.0,0.0);
144  }
145  const BIAS::Vector4<float> cc(0.2, 0.4, 0.1, 1);
146  glParams.SetClearColor(cc);
147 #ifdef BIAS_HAVE_OPENSCENEGRAPH
149 #endif
150 #ifdef BIAS_HAVE_OSG
151  BIAS::SceneOSG sceneOSG;
152 #endif
153 
154 #ifdef HAVE_A_SCENEGRAPH
155 
156  if(*model != ""){
157  sceneOSG.Init();
158  cout<<"Loading model:"<<*model<<endl;
159  if(sceneOSG.AppendSubTreeFromFile(*model)>=0)
160  hasmodel= true;
161  }
162 
163  BIAS::ScenePointLight lightScene;
164  lightScene.SetLightAsHeadLight(true);
165  lightScene.SetDrawLightPosition(false);
166  lightScene.SetAmbient(Vector4<double> (0.3, 0.3, 0.3, 1.0));
167  lightScene.SetDiffuse(Vector4<double> (1.0, 1.0, 1.0, 1.0));
168  lightScene.SetSpecular(Vector4<double> (0.1, 0.1, 0.1, 1.0));
169 #endif
170  glClearColor(0.0, 1.0, 0.0, 0.0);
171  glEnable(GL_MULTISAMPLE);
172  glEnable(GL_DEPTH_TEST);
173  glClearColor(0.0, 0.0, 0.0, 0.0);
174  glClearDepth(1.0);
175  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
176  glLineWidth(1);
177 #ifdef HAVE_A_SCENEGRAPH
178  if(hasmodel){
179  pbuffer.MakeCurrent();
180  glParams.SetRenderTarget(&targetFbo);
181  vector<SceneBase*> scenes; scenes.push_back(&lightScene);
182  scenes.push_back(&sceneOSG);
183  glParams.Draw(scenes);
184  cout<<"Drawing target scene!"<<endl;
185  }
186 #endif
187  glDisable(GL_DEPTH_TEST);
188  glLineWidth(5);
189  glBegin(GL_LINES);
190  glColor3f(0.8,0.8,0.0);
191  glVertex2f(0.25,0.25);
192  glColor3f(0.0,0.0,1.0);
193  glVertex2f(-0.25,-0.25);
194  glEnd();
195 
196  //write out result to show that we have cleared the target to black, and 0.75 depth width a line
197  Camera<unsigned char> resultColor;
198  Camera<float> resultDepth;
199 
200  colorTexture.CopyToImage(resultColor);
201  resultColor.Flip();
202  IOUtils::SaveCamera("colorBeforeBlit.mip",resultColor);
203 
204  depthTexture.CopyToImage(resultDepth);
205  resultDepth.Flip();
206  IOUtils::SaveCamera("depthBeforeBlit.mip", resultDepth);
207 
208  /**
209  * Build source fbo with multisample renderbuffers
210  **/
211  glfRenderbuffer colorRenderbuffer;
212  colorRenderbuffer.Create(GL_RGBA, width, height, *numSamples);//uses multisampling!
213  glfRenderbuffer depthRenderbuffer;
214  depthRenderbuffer.Create(GL_DEPTH_COMPONENT, width, height, *numSamples);//uses multisampling!
215 
216  glfFramebufferObject sourceFbo;
217  sourceFbo.Create();
218  sourceFbo.AttachRenderbuffer(colorRenderbuffer, GL_COLOR_ATTACHMENT0_EXT);
219  sourceFbo.AttachRenderbuffer(depthRenderbuffer, GL_DEPTH_ATTACHMENT_EXT);
220  sourceFbo.CheckComplete();
221 
222  glClearColor(1.0, 0.0, 0.0, 0.0);
223  glClearDepth(1.0);
224  glEnable(GL_DEPTH_TEST);
225  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
226  glLineWidth(1);
227 
228 #ifdef HAVE_A_SCENEGRAPH
229  if(hasmodel){
230  pbuffer.MakeCurrent();
231  glParams.SetRenderTarget(&sourceFbo);
232  vector<SceneBase*> scenes; scenes.push_back(&lightScene);
233  scenes.push_back(&sceneOSG);
234  glParams.Draw(scenes);//glParams.Draw(scenes);
235  cout<<"Drawing source scene!"<<endl;
236  }
237 #endif
238  glDisable(GL_DEPTH_TEST);
239  //using immediate mode here avoiding overhead through batch etc...
240  glLineWidth(5);
241  glBegin(GL_LINES);
242  glColor3f(0.8,0.8,0.0);
243  glVertex2f(0.25,0.25);
244  glColor3f(0.0,0.0,1.0);
245  glVertex2f(-0.25,-0.25);
246  glEnd();
247 
248  if(*numSamples == 0) {
249  //write out renderbuffer contents if multisampling is inactive
250  colorRenderbuffer.CopyToImage(resultColor);
251  IOUtils::SaveCamera("colorRenderbuffer.mip",resultColor);
252 
253  depthRenderbuffer.CopyToImage(resultDepth);
254  IOUtils::SaveCamera("depthRenderbuffer.mip", resultDepth);
255  }
256 
257 
258  glfFramebufferObject::BlitColorBuffer(&sourceFbo, &targetFbo, GL_LINEAR);
259  glfFramebufferObject::Blit(&sourceFbo, &targetFbo, GL_DEPTH_BUFFER_BIT);
260 
261  GLF_THROW_ON_OPENGL_ERROR;
262 
263  //write out result to show that we have blitted the source, i.e color is now blue and depth 0.25
264  colorTexture.CopyToImage(resultColor);
265  resultColor.Flip();
266  IOUtils::SaveCamera("colorAfterBlit.mip",resultColor);
267 
268  depthTexture.CopyToImage(resultDepth);
269  resultDepth.Flip();
270  IOUtils::SaveCamera("depthAfterBlit.mip", resultDepth);
271 
272 
273  } catch (glfException& e) {
274  std::cout << "Error: " << e.GetMessageString() << std::endl;
275  pbuffer.Destroy();
276  return -1;
277  }
278 
279 
280 
281  pbuffer.Destroy();
282 }
283 
int SetSimplePerspectiveCam(double fovyDEG, int imageWidth=-1, int imageHeight=-1)
void Allocate(int width, int height, GLenum internalFormat, int mipmap=0)
Creates a texture with undefined content.
void AttachTexture(const glfTexture &texture, GLenum attachmentPoint, int attachedMipMapLevel=0, int zSlice=0, GLenum cubeMapSide=0)
Attaches a texture to an attachment point which can be.
int XMLRead(const std::string &Filename)
derived classes must implement the function XMLIn which is called by this function XMLRead to read ev...
Definition: XMLBase.cpp:78
A 2D texture.
Definition: glfTexture2D.hh:40
virtual void SetClearColor(const BIAS::Vector4< float > &cc)
setclearcolor is pure virtual.
void Create()
Creates the texture but does not upload any data yet.
Definition: glfTexture.cpp:80
void SetLightAsHeadLight(bool asHeadLight)
virtual void SetRenderTarget(BIAS::glfRenderTarget *RT)
void SetWrapS(GLenum wrapS)
Sets the wrapping mode for the 1st texture coordinate.
Definition: glfTexture.cpp:105
void Create(GLenum internalFormat, int width, int height, GLsizei samples=0)
Creates the renderbuffer with the given format and size.
void CopyToImage(ImageBase &image, int mipmap=0)
Copies the pixels of a mipmap of the texture to a BIAS::ImageBase.
virtual void MakeCurrent()
Makes this context the current target for OpenGL calls.
Exception class used for run-time errors in the OpenGLFramework.
Definition: glfException.hh:79
void SetSpecular(BIAS::Vector4< double > sp)
virtual int Draw(std::vector< BIAS::SceneBase * > &scenes, BIAS::SceneBGImage *backgroundImageScene=NULL, BIAS::glfFramebufferObject *theTarget=NULL)
Renders scenes passed as argument beginning with the smallest index.
virtual void Destroy()
Uninitializes the rendering context.
BIAS::Quaternion< double > GetQ(unsigned int cam=0) const
return rotation quaternion of camera with index cam if cam&gt;0 this is a relative pose in the coordinat...
Definition: Projection.cpp:524
const std::string & GetMessageString() const
Returns the description of the error including the file name and line number where the error occured...
void AttachRenderbuffer(const glfRenderbuffer &renderbuffer, GLenum attachmentPoint)
Attaches a renderbuffer to the framebuffer object.
void SetMinFilter(GLenum minFilter)
Sets the minifying function.
Definition: glfTexture.cpp:89
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
void CheckComplete() const
Checks whether the framebuffer object is supported in its current state.
void CopyToImage(ImageBase &image)
void SetDiffuse(BIAS::Vector4< double > di)
void SetWrapT(GLenum wrapT)
Sets the wrapping mode for the 2nd texture coordinate.
Definition: glfTexture.cpp:113
GLX pbuffer rendering context.
void SetIntrinsics(BIAS::ProjectionParametersBase *p)
void SetZClippingPlanes(float n, float f)
Setting the near and far z-Clipping planes.
virtual void Init(const glfRenderingContextConfig &config)
Initializes the rendering context with the given configuration.
This class Param provides generic support for parameters.
Definition: Param.hh:231
Implements slim openscenegraph scene without extras.
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
int Flip()
flips the image vertically (row order is inverted) In place function return 0 in case of success...
Definition: ImageBase.cpp:834
Vector3< double > GetC(unsigned int cam=0) const
return Center of camera with index cam.
Definition: Projection.hh:236
int Init(unsigned int width=1200, unsigned int height=900)
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:327
Configuration for a rendering context.
The renderbuffer.
void Create()
Creates the framebuffer object.
void SetMagFilter(GLenum magFilter)
Sets the magnification function.
Definition: glfTexture.cpp:97
void SetAmbient(BIAS::Vector4< double > am)
void SetDrawLightPosition(bool drawLight)
class for rendering with projection parameters of ProjectionParametersPerspective ...
virtual int SetExtrinsics(const BIAS::HomgPoint3D &C, const BIAS::RMatrix &R)