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


 
#include "ConvolutionFilter.h" 
 
using namespace std; 
 
ConvolutionFilter::ConvolutionFilter(float* convolutionData, int numChannels, CGcontext context) : GenericFilter(context){ 
	_convolutionData = convolutionData; 
 
	int totSize = _width*_height; 
	int hWidth = _width/2; 
	int hHeight = _height/2; 
	char* convProgram = new char[1000]; 
	sprintf(convProgram,  
		"void main(float2 iCoords   : TEX0,\n" 
		"          uniform samplerRECT texture,\n" 
		"          const uniform float kernel[%d],\n" 
		"          out float%d oColor : COLOR) { \n" 
		"   oColor = 0;\n" 
		"   for(int i = 0; i < %d; i++) {\n" 
		"       for(int j = 0; j < %d; j++) {\n" 
		"           oColor += kernel[%d*i+j]*f%dtexRECT(texture, iCoords+float2(i-%d,j-%d)); \n}\n}\n}\n", 
		totSize, 
		numChannels, 
		width, 
		height, 
		height, 
		numChannels, 
		hWidth, 
		hHeight); 
 
//	cout << "convProgram"<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);	 
	cgGLSetParameterArray1f(_kernelParam, 0,_width*_height,_convolutionData); 
 
	cgGLEnableProfile(_fragmentProfile); 
 
	_DrawFull(image1->GetWidth(), image1->GetHeight()); 
 
	cgGLDisableTextureParameter(_textureParam); 
	cgGLDisableProfile(_fragmentProfile); 
	image1->Flip(); 
	if(unBind) { 
		image1->End(); 
	} 
} 
 
void ConvolutionFilter::applyFilter(GPUVision *image1, GPUVision *image2){ 
	cout << "ConvolutionFilter applyFilter 2" << endl; 
	this->GenericFilter::applyFilter(image1,image2); 
}