#include "externals.hh"
#include "combine.hh"
#include "bloom.hh"
#include "profiling.hh"

namespace {
	static const std::string Name_( "Combine" );
}

#define PSTART() T_Profiler::Profiler.start( Name_ )
#define PEND() do { glFinish( ); T_Profiler::Profiler.end( Name_ ); } while ( 0 )


T_CombinePass::T_CombinePass(
		__rw__ T_FilesWatcher& watcher ,
		__rw__ T_Texture& image ,
		__rw__ T_Texture& bloom )
	: txImage_( image ) , txBloom_( bloom ) ,
		program_( GL_FRAGMENT_SHADER , watcher ) ,
		bloomStrength_( 0.45 ) ,
		bloomAttenuation_( 0.3 )
{
	program_.addFile( "combine.glsl" );
	program_.load( );
}

void T_CombinePass::render( )
{
	PSTART( );
	T_Rendertarget::MainOutput( );
	glClearColor( 1 , 0 , 1 , 1 );
	glClear( GL_COLOR_BUFFER_BIT );
	if ( program_.activate( ) ) {
		T_TextureBinding main( 0 ) , blur( 1 );
		main.set( 0 , txImage_ );
		main.bind( );
		blur.set( 1 , txBloom_ );
		blur.bind( );
		glUniform2f( 2 , txImage_.width( ) , txImage_.height( ) );
		glUniform2f( 3 , bloomStrength_ , bloomAttenuation_ );
		glRectf( -1, -1 , 1 , 1 );
		glBindTexture( GL_TEXTURE_2D , 0 );
	}
	PEND( );
}

void T_CombinePass::makeUI( )
{
	if ( !ImGui::CollapsingHeader( "Combine" ) ) {
		return;
	}

	ImGui::DragFloat( "Bloom strength" , &bloomStrength_ , .001f , 0. , 10 );
	ImGui::DragFloat( "Bloom attenuation" , &bloomAttenuation_ , .001f , 0. , 1 );
}