www.pudn.com > GPUVision_5-13-05-2.zip > ConvolutionFilter2Pack.cpp


 
#include "ConvolutionFilter2Pack.h" 
 
using namespace std; 
 
ConvolutionFilter2Pack::ConvolutionFilter2Pack(float* convolutionData, int width, int height, int numChannels, CGcontext context) : GenericFilter(context){ 
	_width = width; 
	_height = height; 
	_convolutionData = convolutionData; 
 
	int totSize = _width*_height; 
	int hWidth = _width/2; 
	int hHeight = _height/2; 
//	char* convProgram = new char[20000]; 
	_useArray = true; 
/*	sprintf(convProgram,  
		"void main(float2 iCoords   : TEX0,\n" 
		"          uniform samplerRECT texture,\n" 
		"          const uniform float kernel[%d],\n" 
		"          out float4 oColor : COLOR) { \n" 
		"   oColor = 0;\n" 
		"   for(int i = 0; i < %d; i++) {\n" 
		"       for(int j = 0; j < %d; j+=2) {\n" 
		"           float4 val = f4texRECT(texture, iCoords+float2(i-%d,j-%d));\n" 
		"           float2 zw = 0;\n" 
		"           if(j<%d-1)\n" 
		"              zw = kernel[%d*i+j+1]*val.zw;\n" 
		"           oColor += float4(kernel[%d*i+j]*val.xy + zw,0,0); \n" 
		"       }   \n}\n}\n", 
		totSize, 
		width, 
		height, 
		hWidth, 
		hHeight, 
		width, 
		height, 
		height); 
*/ 
//#if defined(_DEBUG) | defined(DEBUG) 
//	cout << "ConvolutionFilter2Pack"<_program); 
//	delete _convolutionData; 
} 
 
 
void ConvolutionFilter2Pack::applyFilter(GPUVision *image1){ 
	bool res = image1->IsReset(); 
	bool unBind = false; 
	if(!image1->IsBound()) { 
		image1->Begin(); 
		unBind = true; 
	} 
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
 
	cgGLBindProgram(_program); 
	cgGLSetTextureParameter(_textureParam, image1->IsReset()?image1->GetTextureID():image1->GetRenderedTextureID()); 
	cgGLEnableTextureParameter(_textureParam);	 
	if(_useArray) { 
		cgGLSetParameterArray1f(_kernelParam, 0,_width*_height,_convolutionData); 
	} 
 
	cgGLEnableProfile(_fragmentProfile); 
 
	_DrawFull(image1->GetWidth()/2, image1->GetHeight()); 
 
	cgGLDisableTextureParameter(_textureParam); 
	cgGLDisableProfile(_fragmentProfile); 
	image1->Flip(); 
	if(unBind) { 
		image1->End(); 
	} 
} 
 
// image2 is what we will use for output size and what we will render to 
void ConvolutionFilter2Pack::applyFilter(GPUVision *image1, GPUVision *image2){ 
	bool res = image2->IsReset(); 
	bool unBind = false; 
	if(!image2->IsBound()) { 
		image2->Begin(); 
		unBind = true; 
	} 
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
 
	cgGLBindProgram(_program); 
	cgGLSetTextureParameter(_textureParam, image1->IsReset()?image1->GetTextureID():image1->GetRenderedTextureID()); 
	cgGLEnableTextureParameter(_textureParam);	 
	if(_useArray) { 
		cgGLSetParameterArray1f(_kernelParam, 0,_width*_height,_convolutionData); 
	} 
 
	cgGLEnableProfile(_fragmentProfile); 
 
	_DrawIntoCoords(image1->GetWidth(), image1->GetHeight(), image2->GetWidth(), image2->GetHeight()); 
 
	cgGLDisableTextureParameter(_textureParam); 
	cgGLDisableProfile(_fragmentProfile); 
	image2->Flip(); 
	if(unBind) { 
		image2->End(); 
	} 
}