//-----------------------------------------
void CPUTPostProcess::CreatePostProcess(
    CPUTRenderTargetColor *pSourceRenderTarget
){
    mpRTSourceRenderTarget    = pSourceRenderTarget;

    GLuint sourceFormat  = mpRTSourceRenderTarget->GetColorFormat();
    UINT sourceWidth          = mpRTSourceRenderTarget->GetWidth();
    UINT sourceHeight         = mpRTSourceRenderTarget->GetHeight();

    mpRTDownSample4x4         = new CPUTRenderTargetColor();
    mpRTDownSample4x4PingPong = new CPUTRenderTargetColor();
    mpRT64x64                 = new CPUTRenderTargetColor();
    mpRT4x4                   = new CPUTRenderTargetColor();
    mpRT1x1                   = new CPUTRenderTargetColor();

    mpRTDownSample4x4->CreateRenderTarget(         _L("PostProcessDownsample4x4"),         sourceWidth/4, sourceHeight/4,          sourceFormat );
    mpRTDownSample4x4PingPong->CreateRenderTarget( _L("PostProcessDownsample4x4PingPong"), sourceWidth/4, sourceHeight/4,          sourceFormat );
    mpRT64x64->CreateRenderTarget(                 _L("PostProcessRT64x64"),                          64,             64, DXGI_FORMAT_R32_FLOAT ); 
    mpRT4x4->CreateRenderTarget(                   _L("PostProcessRT4x4"),                             8,              8, DXGI_FORMAT_R32_FLOAT ); 
    mpRT1x1->CreateRenderTarget(                   _L("PostProcessRT1x1"),                             1,              1, DXGI_FORMAT_R32_FLOAT );

    CPUTAssetLibrary *pLibrary = CPUTAssetLibrary::GetAssetLibrary();
    mpMaterialDownSampleBackBuffer4x4 = pLibrary->GetMaterial(_L("PostProcess/DownSampleBackBuffer4x4"));
    mpMaterialDownSample4x4           = pLibrary->GetMaterial(_L("PostProcess/DownSample4x4"));
    mpMaterialDownSample4x4Alpha      = pLibrary->GetMaterial(_L("PostProcess/DownSample4x4Alpha"));
    mpMaterialDownSampleLogLum        = pLibrary->GetMaterial(_L("PostProcess/DownSampleLogLum"));
    mpMaterialBlurHorizontal          = pLibrary->GetMaterial(_L("PostProcess/BlurHorizontal"));
    mpMaterialBlurVertical            = pLibrary->GetMaterial(_L("PostProcess/BlurVertical"));
    mpMaterialComposite               = pLibrary->GetMaterial(_L("PostProcess/Composite"));
    mpMaterialSpriteNoAlpha           = pLibrary->GetMaterial(_L("PostProcess/Sprite"));

    mpFullScreenSprite = new CPUTSprite();
    mpFullScreenSprite->CreateSprite( -1.0f, -1.0f, 2.0f, 2.0f, _L("Sprite") );
}
// Load and register all the resources needed by the GUI system
//-----------------------------------------------------------------------------
CPUTResult CPUTGuiControllerDX11::Initialize(const std::string& material, const std::string& font)
{
    ID3D11DeviceContext* pImmediateContext = CPUT_DX11::GetContext();
    ID3D11Device *pDevice = CPUT_DX11::GetDevice();
    CPUTAssetLibrary* pAssetLibrary = CPUTAssetLibrary::GetAssetLibrary();
    HRESULT hr = S_OK;
    std::string ErrorMessage;

    CPUTResult result = CPUT_SUCCESS;
    {
        std::string name = "$cbGUIValues";
        CPUTBufferDesc desc;
        desc.cpuAccess = BUFFER_CPU_WRITE;
        desc.memory = BUFFER_DYNAMIC;
        desc.target = BUFFER_UNIFORM;
        desc.pData = NULL;
        desc.sizeBytes = sizeof(GUIConstantBufferVS);
        mpConstantBufferVS = CPUTBuffer::Create(name, &desc);
        pAssetLibrary->AddConstantBuffer(name, "", "", mpConstantBufferVS);
    }

    mpMaterial = pAssetLibrary->GetMaterial(material);
    mpFont = pAssetLibrary->GetFont(font);
    
    CPUTBufferElementInfo pGUIVertex[3] = {
        { "POSITION", 0, 0, CPUT_F32, 3, 3*sizeof(float), 0 },            
        { "TEXCOORD", 0, 1, CPUT_F32, 2, 2*sizeof(float), 3*sizeof(float)},
        { "COLOR",    0, 2, CPUT_F32, 4, 4*sizeof(float), 5*sizeof(float)},
    };
    
    mpUberBuffer = CPUTMeshDX11::Create();
    mpUberBuffer->SetMeshTopology(CPUT_TOPOLOGY_INDEXED_TRIANGLE_LIST);
    mpUberBuffer->CreateNativeResources(NULL, 0, 3, pGUIVertex, mBufferSize, mpMirrorBuffer, NULL, 0, NULL);
    return CPUT_SUCCESS;
}
void SampleUtil_Init()
{
	std::string dir;
	CPUTFileSystem::GetExecutableDirectory(&dir);
	CPUTFileSystem::StripDirectoriesFromPath(&dir, 4);
	CPUTFileSystem::CombinePath(dir, "Media\\MyAssets", &gUtilGlob.myAssetsDirectory);
	CPUTFileSystem::CombinePath(dir, "userdata", &gUtilGlob.userDataDirectory);

	CPUTAssetLibrary *pAssetLibrary = CPUTAssetLibrary::GetAssetLibrary();
	for (int i = 0; i < CODE_TEXTURE_COUNT; i++)
	{
		char textureName[64];
		snprintf(textureName, sizeof(textureName), "$CODETEXTURE%d", i);
		std::string textureNameString = std::string(textureName);
		gUtilGlob.codeTextures[i] = (CPUTTextureDX11*)CPUTTextureDX11::Create(textureNameString, NULL, NULL);
		pAssetLibrary->AddTexture(textureName, "", "", gUtilGlob.codeTextures[i]);
	}
	gUtilGlob.codeMaterial = pAssetLibrary->GetMaterial("MyAssets/codesprite");

	pAssetLibrary->SetMediaDirectoryName(GetMyAssetsDirectory() + "\\");
	gUtilGlob.meshPreviewMaterial = pAssetLibrary->GetMaterial("meshpreview");


	// Load cube and sphere
	pAssetLibrary->SetRootRelativeMediaDirectory("");

	std::string filename;
	CPUTFileSystem::GetMediaDirectory(&filename);
	CPUTFileSystem::CombinePath(filename, "common.scene", &filename);
	gUtilGlob.boxScene = CPUTScene::Create();
	gUtilGlob.boxScene->LoadScene(filename.c_str());
	gUtilGlob.boxModel = pAssetLibrary->FindModel("pCube1", true);

 
	pAssetLibrary->SetMediaDirectoryName(GetMyAssetsDirectory() + "\\");
	CPUTMaterial *quadSpriteMat = pAssetLibrary->GetMaterial("debugQuad");
	gUtilGlob.QuadSprite = CPUTSprite::Create(0.0f, 0.0f, 1.0f, 1.0f, quadSpriteMat);
	SAFE_RELEASE(quadSpriteMat);

}
void MySample::CreateResources()
{
    CPUT_DX11::CreateResources();
    int width, height;
    mpWindow->GetClientDimensions(&width, &height);

    CPUTAssetLibrary *pAssetLibrary = CPUTAssetLibrary::GetAssetLibrary();

    std::string executableDirectory;
    std::string mediaDirectory;

    CPUTFileSystem::GetExecutableDirectory(&executableDirectory);
    CPUTFileSystem::ResolveAbsolutePathAndFilename(executableDirectory + "../../../../Media/", &mediaDirectory);
    
	pAssetLibrary->SetMediaDirectoryName(mediaDirectory + "gui_assets/");
    pAssetLibrary->SetSystemDirectoryName(mediaDirectory + "System/");
    pAssetLibrary->SetFontDirectoryName(mediaDirectory + "gui_assets/Font/");

    CPUTGuiController *pGUI = CPUTGetGuiController();
    pGUI->Initialize("guimaterial_dds_16", "arial_16.fnt");

    pGUI->SetCallback(this);

    pAssetLibrary->SetMediaDirectoryName(mediaDirectory);

    mpShadowRenderTarget = CPUTRenderTargetDepth::Create();
    mpShadowRenderTarget->CreateRenderTarget(std::string("$shadow_depth"), SHADOW_WIDTH_HEIGHT, SHADOW_WIDTH_HEIGHT, DXGI_FORMAT_D32_FLOAT);

    CPUTMaterial* pDebugMaterial = pAssetLibrary->GetMaterial("%sprite");
    mpDebugSprite = CPUTSprite::Create(-1.0f, -1.0f, 0.5f, 0.5f, pDebugMaterial);
    SAFE_RELEASE(pDebugMaterial);

    mpCameraController = CPUTCameraControllerFPS::Create();
    mpShadowCamera = CPUTCamera::Create(CPUT_ORTHOGRAPHIC);
     
    pGUI->Resize(width, height);
	
}
void Menu_FaceMapping::Init()
{
	MenuBase::Init();

	SetDefaultTweaks(&mTweaks);

	CPUTAssetLibrary *pAssetLibrary = CPUTAssetLibrary::GetAssetLibrary();

	mpFaceMapRTColor = CPUTRenderTargetColor::Create();
	mpFaceMapRTColor->CreateRenderTarget("$FaceColor", FACE_DISPLACEMENT_WIDTH_HEIGHT, FACE_DISPLACEMENT_WIDTH_HEIGHT, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB);

	mpFaceMapRTDepth = CPUTRenderTargetColor::Create();
	mpFaceMapRTDepth->CreateRenderTarget("$FaceDisplacement", FACE_DISPLACEMENT_WIDTH_HEIGHT, FACE_DISPLACEMENT_WIDTH_HEIGHT, DXGI_FORMAT_R32_FLOAT);

	mpFaceMapDepthBuffer = CPUTRenderTargetDepth::Create();
	mpFaceMapDepthBuffer->CreateRenderTarget("$FaceDisplacementDepth", FACE_DISPLACEMENT_WIDTH_HEIGHT, FACE_DISPLACEMENT_WIDTH_HEIGHT, DXGI_FORMAT_D32_FLOAT);

	mDisplacementCamera = CPUTCamera::Create(CPUT_ORTHOGRAPHIC);
	mDisplacementCamera->SetPosition(0.0f, 0.0f, -10.0f);
	mDisplacementCamera->LookAt( 0.0f, 0.0f, 0.0f );
	
	mDisplacementCamera->SetFarPlaneDistance(20);
	mDisplacementCamera->SetNearPlaneDistance(1.0f);
	mDisplacementCamera->SetAspectRatio(1.0f);
	mDisplacementCamera->SetWidth(10);
	mDisplacementCamera->SetHeight(10);
	mDisplacementCamera->Update();
	
	pAssetLibrary->SetRootRelativeMediaDirectory("MyAssets");
	CPUTMaterial* mat = pAssetLibrary->GetMaterial("render_map_diffuse");
	mMapSprites[FaceMapTexture_Color] = CPUTSprite::Create(-1.0f, -1.0f, 0.5f, 0.5f, mat);
	SAFE_RELEASE(mat);
	mat = pAssetLibrary->GetMaterial("render_map_depth");
	mMapSprites[FaceMapTexture_Depth] = CPUTSprite::Create(-1.0f, -0.4f, 0.5f, 0.5f, mat);
	SAFE_RELEASE(mat);

	// load the head model
	std::string mediaFilename;
	CPUTFileSystem::GetMediaDirectory(&mediaFilename);
	pAssetLibrary->SetMediaDirectoryName(mediaFilename+"\\");
	CPUTFileSystem::CombinePath(mediaFilename, "basicHead_03.scene", &mediaFilename);
	mScene = CPUTScene::Create();
	mScene->LoadScene(mediaFilename);
	mHeadModel = pAssetLibrary->FindModel("head4", true);

	CPUTAssetSet *hairSetMale = pAssetLibrary->FindAssetByNameNoPath("hairMale_01.set", CPUTAssetLibrary::mpAssetSetList);
	CPUTAssetSet *hairSetMale_Black = pAssetLibrary->FindAssetByNameNoPath("hairMale_01_black.set", CPUTAssetLibrary::mpAssetSetList);
	CPUTAssetSet *hairSetFemale = pAssetLibrary->FindAssetByNameNoPath("hairGeekGirl.set", CPUTAssetLibrary::mpAssetSetList);
	CPUTAssetSet *hairSetFemale_Blonde = pAssetLibrary->FindAssetByNameNoPath("hairGeekGirl_blonde.set", CPUTAssetLibrary::mpAssetSetList);
	CPUTAssetSet *hairSetFemale_Black = pAssetLibrary->FindAssetByNameNoPath("hairGeekGirl_black.set", CPUTAssetLibrary::mpAssetSetList);
	CPUTAssetSet *hairSetCrazy = pAssetLibrary->FindAssetByNameNoPath("hair_01.set", CPUTAssetLibrary::mpAssetSetList);

	HairInfo info;
	info.name = "Hair Short";
	info.set = hairSetMale;
	mHairList.push_back(info);

	info.name = "Hair Short Black";
	info.set = hairSetMale_Black;
	mHairList.push_back(info);

	info.name = "Hair Long";
	info.set = hairSetFemale;
	mHairList.push_back(info);

	info.name = "Hair Long Blonde";
	info.set = hairSetFemale_Blonde;
	mHairList.push_back(info);

	info.name = "Hair Long Black";
	info.set = hairSetFemale_Black;
	mHairList.push_back(info);

	info.name = "Madness";
	info.set = hairSetCrazy;
	mHairList.push_back(info);


	GetCPUTLandmark( std::string( "LANDMARK_EYE_LEFT_CENTER" ), &mHeadLandmark_LeftEyeCenter);
	GetCPUTLandmark( std::string( "LANDMARK_EYE_RIGHT_CENTER" ), &mHeadLandmark_RightEyeCenter);
	GetCPUTLandmark(std::string("LANDMARK_NOSE_TIP"), &mHeadLandmark_NoseTip);

	mCameraController = new CPUTCameraModelViewer();
	mCameraController->SetTarget(float3(0, 0, 0));
	mCameraController->SetDistance( 20, 10, 40);
	mCameraController->SetViewAngles(0, 0);

	mMapProjectionCamera = CPUTCamera::Create(CPUT_ORTHOGRAPHIC);
	float y = 5.0f;
	mMapProjectionCamera->SetPosition(0.0f, y, -10.0f );
	mMapProjectionCamera->LookAt(0.0f, y, 0.0f);

	mMapProjectionCamera->SetFarPlaneDistance(100);
	mMapProjectionCamera->SetNearPlaneDistance(0.1f);
	mMapProjectionCamera->SetAspectRatio(1.0f);
	mMapProjectionCamera->SetWidth(15);
	mMapProjectionCamera->SetHeight(15);
	mMapProjectionCamera->Update();

    mSaveSculptedObj = false;
}