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

Example for rendering something using the multisample. This provides anti aliasing for rendering things in a framebuffer

,glfFramebufferObject

Author
Ingo Schiller / Bogomil Bartzcak
Date
12/2009
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT 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 ExampleMultisampleBlit.cpp
@relates PMDWarp,glfFramebufferObject
@brief Example for rendering something using the multisample.
This provides anti aliasing for rendering things in a framebuffer
@ingroup g_examples
@ingroup g_openglframework
@author Ingo Schiller / Bogomil Bartzcak
@date 12/2009
*/
#include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
#include <OpenGLFramework/SpecializedBatches/PMDWarp.hh>
#include <OpenGLFramework/Base/glfException.hh>
#include <OpenGLFramework/Base/glfFramebufferObject.hh>
#include <Utils/Param.hh>
#include <Utils/IOUtils.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/Image/Image.hh>
#include <Image/Camera.hh>
#include <Gui/biasgl.h>
#include <GLviewer/Scenes/ScenePointLight.hh>
#include <GLviewer/GLProjectionParametersPerspective.hh>
#ifdef BIAS_HAVE_OPENSCENEGRAPH
#include <GLviewer/Scenes/ScenePlainOpenSceneGraph.hh>
#define HAVE_A_SCENEGRAPH
#endif
#ifdef BIAS_HAVE_OSG
#include <GLviewer/Scenes/SceneOSG.hh>
#define HAVE_A_SCENEGRAPH
#endif
using namespace std;
using namespace BIAS;
int main(int argc, char* argv[])
{
Param params(true);
string *model = NULL;
model = params.AddParamString("Model", "", "",'m',GRP_NOSHOW);
string *projection = params.AddParamString("Projection", "", "",'p',GRP_NOSHOW);
int *numSamples = params.AddParamInt("samples", "number of samples used for multi sample buffers",
16, 0,16,'b',GRP_NOSHOW);
if(!IOUtils::ParseCommandLineEvalHelp(params, argc, argv) )
return 0;
#ifdef HAVE_A_SCENEGRAPH
bool hasmodel = false;
#endif
unsigned int width = 1024;
unsigned int height = 768;
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;
pbuffer.Init(cfg);
pbuffer.MakeCurrent();
glViewport(0,0,width,height);
/**
* Build target fbo with textures
**/
glfTexture2D colorTexture;
colorTexture.Create();
colorTexture.SetMagFilter(GL_NEAREST);
colorTexture.SetMinFilter(GL_NEAREST);
colorTexture.SetWrapS(GL_CLAMP);
colorTexture.SetWrapT(GL_CLAMP);
colorTexture.Allocate(width, height, GL_RGB);
glfTexture2D depthTexture;
depthTexture.Create();
depthTexture.SetMagFilter(GL_NEAREST);
depthTexture.SetMinFilter(GL_NEAREST);
depthTexture.SetWrapS(GL_CLAMP);
depthTexture.SetWrapT(GL_CLAMP);
depthTexture.Allocate(width, height, GL_DEPTH_COMPONENT);
targetFbo.Create();
targetFbo.AttachTexture(colorTexture, GL_COLOR_ATTACHMENT0_EXT);
targetFbo.AttachTexture(depthTexture, GL_DEPTH_ATTACHMENT_EXT);
targetFbo.CheckComplete();
GLF_THROW_ON_OPENGL_ERROR;
glParams.SetSimplePerspectiveCam(60.0, width, height);
glParams.SetZClippingPlanes(10, 7500);
if(*projection != "") {
cout<<"Loading projection:"<<*projection<<endl;
Projection proj;
int ret = proj.XMLRead(*projection);
if(ret != 0) BIASERR("Reading projection failed!");
glParams.SetIntrinsics(proj.GetParameters());
glParams.SetExtrinsics(proj.GetC(),proj.GetQ());
glParams.SetUndistortion(0.0,0.0,0.0,0.0);
}
const BIAS::Vector4<float> cc(0.2, 0.4, 0.1, 1);
glParams.SetClearColor(cc);
#ifdef BIAS_HAVE_OPENSCENEGRAPH
#endif
#ifdef BIAS_HAVE_OSG
BIAS::SceneOSG sceneOSG;
#endif
#ifdef HAVE_A_SCENEGRAPH
if(*model != ""){
sceneOSG.Init();
cout<<"Loading model:"<<*model<<endl;
if(sceneOSG.AppendSubTreeFromFile(*model)>=0)
hasmodel= true;
}
lightScene.SetLightAsHeadLight(true);
lightScene.SetDrawLightPosition(false);
lightScene.SetAmbient(Vector4<double> (0.3, 0.3, 0.3, 1.0));
lightScene.SetDiffuse(Vector4<double> (1.0, 1.0, 1.0, 1.0));
lightScene.SetSpecular(Vector4<double> (0.1, 0.1, 0.1, 1.0));
#endif
glClearColor(0.0, 1.0, 0.0, 0.0);
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLineWidth(1);
#ifdef HAVE_A_SCENEGRAPH
if(hasmodel){
pbuffer.MakeCurrent();
glParams.SetRenderTarget(&targetFbo);
vector<SceneBase*> scenes; scenes.push_back(&lightScene);
scenes.push_back(&sceneOSG);
glParams.Draw(scenes);
cout<<"Drawing target scene!"<<endl;
}
#endif
glDisable(GL_DEPTH_TEST);
glLineWidth(5);
glBegin(GL_LINES);
glColor3f(0.8,0.8,0.0);
glVertex2f(0.25,0.25);
glColor3f(0.0,0.0,1.0);
glVertex2f(-0.25,-0.25);
glEnd();
//write out result to show that we have cleared the target to black, and 0.75 depth width a line
Camera<unsigned char> resultColor;
Camera<float> resultDepth;
colorTexture.CopyToImage(resultColor);
resultColor.Flip();
IOUtils::SaveCamera("colorBeforeBlit.mip",resultColor);
depthTexture.CopyToImage(resultDepth);
resultDepth.Flip();
IOUtils::SaveCamera("depthBeforeBlit.mip", resultDepth);
/**
* Build source fbo with multisample renderbuffers
**/
glfRenderbuffer colorRenderbuffer;
colorRenderbuffer.Create(GL_RGBA, width, height, *numSamples);//uses multisampling!
glfRenderbuffer depthRenderbuffer;
depthRenderbuffer.Create(GL_DEPTH_COMPONENT, width, height, *numSamples);//uses multisampling!
sourceFbo.Create();
sourceFbo.AttachRenderbuffer(colorRenderbuffer, GL_COLOR_ATTACHMENT0_EXT);
sourceFbo.AttachRenderbuffer(depthRenderbuffer, GL_DEPTH_ATTACHMENT_EXT);
sourceFbo.CheckComplete();
glClearColor(1.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLineWidth(1);
#ifdef HAVE_A_SCENEGRAPH
if(hasmodel){
pbuffer.MakeCurrent();
glParams.SetRenderTarget(&sourceFbo);
vector<SceneBase*> scenes; scenes.push_back(&lightScene);
scenes.push_back(&sceneOSG);
glParams.Draw(scenes);//glParams.Draw(scenes);
cout<<"Drawing source scene!"<<endl;
}
#endif
glDisable(GL_DEPTH_TEST);
//using immediate mode here avoiding overhead through batch etc...
glLineWidth(5);
glBegin(GL_LINES);
glColor3f(0.8,0.8,0.0);
glVertex2f(0.25,0.25);
glColor3f(0.0,0.0,1.0);
glVertex2f(-0.25,-0.25);
glEnd();
if(*numSamples == 0) {
//write out renderbuffer contents if multisampling is inactive
colorRenderbuffer.CopyToImage(resultColor);
IOUtils::SaveCamera("colorRenderbuffer.mip",resultColor);
depthRenderbuffer.CopyToImage(resultDepth);
IOUtils::SaveCamera("depthRenderbuffer.mip", resultDepth);
}
glfFramebufferObject::BlitColorBuffer(&sourceFbo, &targetFbo, GL_LINEAR);
glfFramebufferObject::Blit(&sourceFbo, &targetFbo, GL_DEPTH_BUFFER_BIT);
GLF_THROW_ON_OPENGL_ERROR;
//write out result to show that we have blitted the source, i.e color is now blue and depth 0.25
colorTexture.CopyToImage(resultColor);
resultColor.Flip();
IOUtils::SaveCamera("colorAfterBlit.mip",resultColor);
depthTexture.CopyToImage(resultDepth);
resultDepth.Flip();
IOUtils::SaveCamera("depthAfterBlit.mip", resultDepth);
} catch (glfException& e) {
std::cout << "Error: " << e.GetMessageString() << std::endl;
pbuffer.Destroy();
return -1;
}
pbuffer.Destroy();
}