OpenWalnut  1.4.0
WGEPostprocessorCelShading.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <osg/Camera>
00026 
00027 #include "../shaders/WGEPropertyUniform.h"
00028 
00029 #include "WGEPostprocessorCelShading.h"
00030 
00031 WGEPostprocessorCelShading::WGEPostprocessorCelShading():
00032     WGEPostprocessor( "Cel Shading",
00033                       "This postprocessor reduces color by binning colors." )
00034 {
00035 }
00036 
00037 WGEPostprocessorCelShading::WGEPostprocessorCelShading( osg::ref_ptr< WGEOffscreenRenderNode > offscreen,
00038                                                         const WGEPostprocessor::PostprocessorInput& gbuffer ):
00039     WGEPostprocessor( "Cel Shading",
00040                       "This postprocessor reduces color by binning colors." )
00041 {
00042     // we also provide a property
00043     WPropInt bins = m_properties->addProperty( "Number of Bins", "The number of color bins used for cel shading.", 10 );
00044     bins->setMin( 1 );
00045     bins->setMax( 100 );
00046 
00047     // hide m_effectOnly since this is not useful for Cel Shading
00048     m_effectOnly->setHidden( true );
00049 
00050     // construct pipeline
00051 
00052     // Use the standard postprocessor uber-shader
00053     WGEShader::RefPtr s = new WGEShader( "WGEPostprocessor" );
00054     s->setDefine( "WGE_POSTPROCESSOR_CEL" );
00055 
00056     // also add the m_effectOnly property as shader preprocessor
00057     s->addPreprocessor( m_effectOnlyPreprocessor );
00058 
00059     // create the rendering pass
00060     osg::ref_ptr< WGEOffscreenTexturePass > pass = offscreen->addTextureProcessingPass( s, "Cel Shading" );
00061     pass->getOrCreateStateSet()->addUniform( new WGEPropertyUniform< WPropInt >( "u_celShadingBins", bins ) );
00062 
00063     // attach color0 output
00064     m_resultTextures.push_back( pass->attach( WGECamera::COLOR_BUFFER0, GL_RGBA ) );
00065 
00066     // provide the Gbuffer input
00067     gbuffer.bind( pass );
00068 }
00069 
00070 WGEPostprocessorCelShading::~WGEPostprocessorCelShading()
00071 {
00072     // cleanup
00073 }
00074 
00075 WGEPostprocessor::SPtr WGEPostprocessorCelShading::create( osg::ref_ptr< WGEOffscreenRenderNode > offscreen,
00076     const WGEPostprocessor::PostprocessorInput& gbuffer ) const
00077 {
00078     return WGEPostprocessor::SPtr( new WGEPostprocessorCelShading( offscreen, gbuffer ) );
00079 }