void SceneManager::drawDepth(Depth & depth, int x, int y)
{
	depthShader.begin();
	depthShader.setUniform1i("isClipping", 0);
	depth.draw(x, y, depth.getWidth() * 0.6, depth.getHeight() * 0.6);
	depthShader.end();
}
void SceneManager::update(Depth & depth, bool isPaused)
{
	if (player.isLoaded() && isPlayingSequence && !isPaused)
	{
		player.update();

		dancerSilhouette.updateVideoProgress(player.getCurrentFrame(), player.getTotalNumFrames());

		// check to see if the video has completed
		if (player.getCurrentFrame() >= player.getTotalNumFrames() - 2)
		{
			cout << "stopping video" << endl;
			player.stop();
			isPlayingSequence = false;
			isUserVisible = true;
			isDancerVisible = false;

			userSilhouette.startIntroFade();
			
			instructions.isVideoRunning = false;
			
			float ddd;
			ofNotifyEvent(videoCompleteEvent, ddd);	

		}
		
		// blur the video horizontally
		currentVidFbo = 1 - currentVidFbo;
		dancerFbo[currentVidFbo].begin();
		blurShaderH.begin();
		blurShaderH.setUniform1f("blurAmount", dancerBlurAmount);
		blurShaderH.setUniform1i("numSamples", dancerBlurSamples);
		blurShaderH.setUniform2f("resolution", dancerFbo[currentVidFbo].getWidth(), dancerFbo[currentVidFbo].getHeight());
		player.draw(0,0);
		blurShaderH.end();
		dancerFbo[currentVidFbo].end();
		
		// blur the video vertically
		currentVidFbo = 1 - currentVidFbo;
		dancerFbo[currentVidFbo].begin();
		blurShaderV.begin();
		blurShaderV.setUniform1f("blurAmount", dancerBlurAmount);
		blurShaderV.setUniform1i("numSamples", dancerBlurSamples);
		blurShaderV.setUniform2f("resolution", dancerFbo[currentVidFbo].getWidth(), dancerFbo[currentVidFbo].getHeight());
		dancerFbo[1 - currentVidFbo].draw(0,0);
		blurShaderV.end();
		dancerFbo[currentVidFbo].end();
		
		// pass the blurred image to the dancer silhouette and update
		
		dancerFbo[currentVidFbo].readToPixels(dancerPix);
		//dancerImg.setFromPixels(pix);
	}


	if (isLiveClipping)
	{
		depthFbo.begin();
		depthShader.begin();
		depthShader.setUniform1i("isClipping", (isClipping) ? 1 : 0);
		depthShader.setUniform2f("resolution", srcW, srcH);
		depthShader.setUniform1f("nearClip", liveNearClip);
		depthShader.setUniform1f("farClip", liveFarClip);
		depthShader.setUniform1f("nearFloorClip", liveNearFloorClip);
		depthShader.setUniform1f("farFloorClip", liveFarFloorClip);
		depthShader.setUniform1f("nearCeilingClip", liveNearCeilingClip);
		depthShader.setUniform1f("farCeilingClip", liveFarCeilingClip);
		depthShader.setUniform1f("nearLeftClip", liveNearLeftClip);
		depthShader.setUniform1f("farLeftClip", liveFarLeftClip);
		depthShader.setUniform1f("nearRightClip", liveNearRightClip);
		depthShader.setUniform1f("farRightClip", liveFarRightClip);
		depth.draw(0, 0, srcW, srcH);
		depthShader.end();
		depthFbo.end();
	}
	else
	{
		depthFbo.begin();
		depthShader.begin();
		depthShader.setUniform1i("isClipping", (isClipping) ? 1 : 0);
		depthShader.setUniform2f("resolution", srcW, srcH);
		depthShader.setUniform1f("nearClip", recNearClip);
		depthShader.setUniform1f("farClip", recFarClip);
		depthShader.setUniform1f("nearFloorClip", recNearFloorClip);
		depthShader.setUniform1f("farFloorClip", recFarFloorClip);
		depthShader.setUniform1f("nearCeilingClip", recNearCeilingClip);
		depthShader.setUniform1f("farCeilingClip", recFarCeilingClip);
		depthShader.setUniform1f("nearLeftClip", recNearLeftClip);
		depthShader.setUniform1f("farLeftClip", recFarLeftClip);
		depthShader.setUniform1f("nearRightClip", recNearRightClip);
		depthShader.setUniform1f("farRightClip", recFarRightClip);
		depth.draw(0, 0, srcW, srcH);
		depthShader.end();
		depthFbo.end();
	}


	// blur the user horizontally
	currentUserFbo = 1 - currentUserFbo;
	userFbo[currentUserFbo].begin();
	blurShaderH.begin();
	blurShaderH.setUniform1f("blurAmount", userBlurAmount);
	blurShaderH.setUniform1i("numSamples", userBlurSamples);
	blurShaderH.setUniform2f("resolution", userFbo[currentVidFbo].getWidth(), userFbo[currentVidFbo].getHeight());
	depthFbo.draw(0,0);
	blurShaderH.end();
	userFbo[currentUserFbo].end();
	
	// blur the user vertically
	currentUserFbo = 1 - currentUserFbo;
	userFbo[currentUserFbo].begin();
	blurShaderH.begin();
	blurShaderH.setUniform1f("blurAmount", userBlurAmount);
	blurShaderH.setUniform1i("numSamples", userBlurSamples);
	blurShaderH.setUniform2f("resolution", userFbo[currentVidFbo].getWidth(), userFbo[currentVidFbo].getHeight());
	userFbo[1 - currentUserFbo].draw(0,0);
	blurShaderH.end();
	userFbo[currentUserFbo].end();

	// pass blurred user image to user silhouette and update
	userFbo[currentVidFbo].readToPixels(userPix);
	
	if (dancerPix.getWidth() > 0)
	{
		if (isStartingVideo)
			dancerSilhouette.update(userPix);
		else
			dancerSilhouette.update(dancerPix);
	}
	if (userPix.getWidth() > 0)
	{
		if (isStartingVideo)
		{
			isStartingVideo = false;
			dancerSilhouette.startAnimation(userPix);
		}
		//if (player.isPlaying)
		userSilhouette.update(userPix);
	}
	else
	{
		// just in case the user becomes unrecogniseable 1 frame after the pose
		// is recognised. Unlikely but possible
		if (isStartingVideo)
		{
			isStartingVideo = false;
			player.play();
			player.setFrame(player.getTotalNumFrames() - 3);
			dancerSilhouette.isIntro = false;
		}
	}
}