void ofxKinectTracking::init(){
	openCL.setupFromOpenGL();
	
#ifndef NORMAL_CAMERA
	kinect = new ofxKinect();
	kinect->init();
	kinect->setVerbose(true);
	kinect->open();
	
	
	
	//The depth image
	clImage[0].initFromTexture(kinect->getDepthTextureReference(), CL_MEM_READ_ONLY, 0);	
#else
	videoGrabber.initGrabber(640, 480, true);
	clImage[0].initWithTexture(640, 480, GL_LUMINANCE, CL_MEM_READ_ONLY);
	pixels		= new unsigned char[640*480];
	
#endif
	
	//The debug image
	clImage[1].initWithTexture(640, 480, GL_RGBA);
	
	//Intialize the ants buffer
	resetBufferData();
	clAntsBuffer.initBuffer(sizeof(Ant)*NUM_ANTS, CL_MEM_READ_WRITE, ants, true);
	
	//Buffer of shared variables
	sharedVariables[0] = 0;	
	clSharedBuffer.initBuffer(sizeof(int)*1, CL_MEM_READ_WRITE, sharedVariables, true);
	
	openCL.loadProgramFromFile("../../../../../addons/ofxKinectTracking/src/KinectTracking.cl");
	
	openCL.loadKernel("preUpdate");
	openCL.loadKernel("update");
	openCL.loadKernel("postUpdate");
}
void ofxKinectTracking::clear(){
	resetBufferData();
//	clAntsBuffer.initBuffer(sizeof(Ant)*NUM_ANTS, CL_MEM_READ_WRITE, ants, true);
	clAntsBuffer.write(ants, 0, sizeof(Ant)*NUM_ANTS, true);
}
void ofxKinectTracking::update(){
	bool newFrame;
	
#ifndef NORMAL_CAMERA
	kinect->update();	
	newFrame = kinect->isFrameNew();
	
#else
	videoGrabber.update();
	newFrame = videoGrabber.isFrameNew();
	if(newFrame){
		
		
		int pixelIndex = 0;
		for(int i=0; i<640; i++) {
			for(int j=0; j<480; j++) {
				int indexRGB	= pixelIndex * 3;
				int indexL	= pixelIndex;
				
				pixels[indexL] = videoGrabber.getPixels()[indexRGB+1  ];
				pixelIndex++;
			}
		}
		
		
		// write the new pixel data into the OpenCL Image (and thus the OpenGL texture)
		clImage[0].write(pixels);
	}
	
#endif
	if(newFrame){
		cl_int2 spawnCoord = {int(ofRandom(0, 640)), int(ofRandom(0, 480))};
		if(ofRandom(0, 1) < 0.500){
			spawnCoord[0] = -1;
			spawnCoord[0] = -1;			
		}
		
		
		MSA::OpenCLKernel *preKernel = openCL.kernel("preUpdate");
		MSA::OpenCLKernel *updateKernel = openCL.kernel("update");
		MSA::OpenCLKernel *postKernel = openCL.kernel("postUpdate");
		
		
		//Prepare timetaking 
		double totalTimeTmp;
		uint64_t mbeg, mend;
		openCL.finish();
		mbeg = mach_absolute_time();
		
		//Run pre update kernel
		preKernel->setArg(0, clImage[0].getCLMem());
		preKernel->setArg(1, clAntsBuffer.getCLMem());	
		preKernel->run2D(640, 480);
		
		//Calculate time
		openCL.finish();		
		mend = mach_absolute_time();
		killingTime.push_back(machcore(mend, mbeg));
		totalTimeTmp = machcore(mend, mbeg);
		mbeg = mach_absolute_time();
		
		//Run update kernel
		size_t shared_size = (1 * 64) * sizeof(int);		
		updateKernel->setArg(0, clAntsBuffer.getCLMem());
		updateKernel->setArg(1, clSharedBuffer.getCLMem());
		updateKernel->setArg(2, spawnCoord);
		clSetKernelArg(updateKernel->getCLKernel(), 3, shared_size, NULL);
		for(int i=0;i<NUM_ITERATIONS;i++){
			updateKernel->run2D(640, 480);
		}
		
		//Calculate time
		openCL.finish();		
		mend = mach_absolute_time();
		spawningTime.push_back(machcore(mend, mbeg));
		totalTimeTmp += machcore(mend, mbeg);
		mbeg = mach_absolute_time();
		
		//Run post update kernel
		postKernel->setArg(0, clImage[0].getCLMem());
		postKernel->setArg(1, clImage[1].getCLMem());
		postKernel->setArg(2, clAntsBuffer.getCLMem());
		postKernel->setArg(3, clSharedBuffer.getCLMem());
		postKernel->setArg(4, spawnCoord);
		postKernel->run2D(640, 480);
		
		//Calculate time
		openCL.finish();		
		mend = mach_absolute_time();
		updateTime.push_back(machcore(mend, mbeg));
		totalTimeTmp += machcore(mend, mbeg);		
		totalTime.push_back(totalTimeTmp);
		if(totalTime.size() > (640*2)/3){
			totalTime.erase(totalTime.begin());
			killingTime.erase(killingTime.begin());
			spawningTime.erase(spawningTime.begin());
			updateTime.erase(updateTime.begin());
		}
		
	}
}
Пример #4
-1
//--------------------------------------------------------------
void testApp::setup(){
	ofBackground(0, 0, 0);
	ofSetLogLevel(OF_LOG_VERBOSE);
	ofSetVerticalSync(false);
	
#ifdef USE_OPENGL_CONTEXT
	opencl.setupFromOpenGL();
#else
	opencl.setup(CL_DEVICE_TYPE_CPU, 2);
#endif
	
	for(int i=0; i<NUM_PARTICLES; i++) {
		Particle &p = particles[i];
		p.vel.set(0, 0);
		p.mass = ofRandom(0.5, 1);
		particlesPos[i].set(ofRandomWidth(), ofRandomHeight());
	}
	
	glGenBuffersARB(1, vbo);
	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[0]);
	glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float2) * NUM_PARTICLES, particlesPos, GL_DYNAMIC_COPY_ARB);
	glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
	
	
	opencl.loadProgramFromFile("MSAOpenCL/Particle.cl");
	kernelUpdate = opencl.loadKernel("updateParticle");
	
	
	clMemParticles.initBuffer(sizeof(Particle) * NUM_PARTICLES, CL_MEM_READ_WRITE, particles);
#ifdef USE_OPENGL_CONTEXT
	clMemPosVBO.initFromGLObject(vbo[0]);
#else
	clMemPosVBO.initBuffer(sizeof(Vec2) * NUM_PARTICLES, CL_MEM_READ_WRITE, particlesPos);
#endif
	
	kernelUpdate->setArg(0, clMemParticles.getCLMem());
	kernelUpdate->setArg(1, clMemPosVBO.getCLMem());
	kernelUpdate->setArg(2, mousePos);
	kernelUpdate->setArg(3, dimensions);
	
	glPointSize(1);
}