#include "PowerPlantScene.h"
#include "DynOctTree.h"

static const int g_iPPSections = 21;
static const bool g_bLoadSection[] = {0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1};
static CMesh* g_pPPSection[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static CMesh* g_pGroundMesh = NULL;

PowerPlantScene::PowerPlantScene(ShadowApp* const pApp) : ShadowScene(pApp)
{
	m_pSPT = new DynOctTree(32, 0.2f);

	for (int i=0; i < g_iPPSections; i++)
	{
		if (g_bLoadSection[i])
		{
			g_pPPSection[i] = new CMesh(m_pApp->GetDevice(), XString("section") + XString(i + 1) + XString(".x"), XString("PowerPlant\\"));
			g_pPPSection[i]->BuildDrawCalls();
			for(unsigned j= 0; j<g_pPPSection[i]->GetMaterialCount(); j++)
				g_pPPSection[i]->GetMaterials()[j].SetAmbient(D3DXCOLOR(0.2f, 0.2f, 0.2f, 1));

			SceneObject* obj = new MeshObject(this, g_pPPSection[i], DXMatrix::IDENTITY());

			m_Objects.AddTail(obj, 0);

			m_pSPT->AddObject(obj);
		}
	}

	g_pGroundMesh = new CMesh(m_pApp->GetDevice(), XString("ground2.x"), XString("PowerPlant\\"));
	g_pGroundMesh->BuildDrawCalls();
	for(unsigned j= 0; j<g_pGroundMesh->GetMaterialCount(); j++)
		g_pGroundMesh->GetMaterials()[j].SetAmbient(D3DXCOLOR(0.2f, 0.2f, 0.2f, 1));
	SceneObject* ground = new MeshObject(this, g_pGroundMesh, DXMatrix::TranslationMatrix(0, 0, 8.8f));
	m_Objects.AddTail(ground, 0);
	m_pSPT->AddObject(ground);

	ResetCam();

	InitCamPositions();

	m_pSkyTexture = m_pApp->GetDevice()->GetTextureManager()->CreateCubeTexture(2048, 1, 0, FMT_A8R8G8B8, POOL_MANAGED);
	LPDIRECT3DSURFACE9 s;

	m_pSkyTexture->GetCubeTexture()->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_X, 0, &s);
	D3DXLoadSurfaceFromFile(s, NULL, NULL, "SkyBox\\SkyBox0001.png", NULL, D3DX_DEFAULT, 0, NULL);
	s->Release();

	m_pSkyTexture->GetCubeTexture()->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_X, 0, &s);
	D3DXLoadSurfaceFromFile(s, NULL, NULL, "SkyBox\\SkyBox0002.png", NULL, D3DX_DEFAULT, 0, NULL);
	s->Release();

	m_pSkyTexture->GetCubeTexture()->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Y, 0, &s);
	D3DXLoadSurfaceFromFile(s, NULL, NULL, "SkyBox\\SkyBox0003.png", NULL, D3DX_DEFAULT, 0, NULL);
	s->Release();

	m_pSkyTexture->GetCubeTexture()->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_Y, 0, &s);
	D3DXLoadSurfaceFromFile(s, NULL, NULL, "SkyBox\\SkyBox0004.png", NULL, D3DX_DEFAULT, 0, NULL);
	s->Release();

	m_pSkyTexture->GetCubeTexture()->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Z, 0, &s);
	D3DXLoadSurfaceFromFile(s, NULL, NULL, "SkyBox\\SkyBox0005.png", NULL, D3DX_DEFAULT, 0, NULL);
	s->Release();

	m_pSkyTexture->GetCubeTexture()->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_Z, 0, &s);
	D3DXLoadSurfaceFromFile(s, NULL, NULL, "SkyBox\\SkyBox0006.png", NULL, D3DX_DEFAULT, 0, NULL);
	s->Release();
}

PowerPlantScene::~PowerPlantScene(void)
{
	delete g_pGroundMesh;
	g_pGroundMesh = NULL;

	for (int i = 0; i < g_iPPSections; i++)
	{
		delete g_pPPSection[i];
		g_pPPSection[i] = NULL;
	}
}

void PowerPlantScene::ResetCam(void)
{
	m_Cam.SetPosition(DXVector3(0, 0, 100));
	m_Cam.SetPerspectiveProj(m_pApp->GetCurrentMode().Width, m_pApp->GetCurrentMode().Height, 0.5, 800);
	m_Cam.SetLookAt(DXVector3(0, 1, 99), DXVector3(0, 0, 1));

	m_DebugCam = m_Cam;
	m_DebugCam.SetPerspectiveProj(m_pApp->GetCurrentMode().Width, m_pApp->GetCurrentMode().Height, 1, 2000);
}

void PowerPlantScene::InitCamPositions(void)
{
	m_CamPos[0] = DXVector3(-125.913216f, 158.185776f, 82.968369f);
	m_CamDir[0] = DXVector3(0.664014f, -0.651659f, -0.366653f);
	m_CamUp[0] = DXVector3(0.261666f, -0.256836f, 0.930362f);
	m_CamLightAngX[0] = 0.598000f; m_CamLightAngZ[0] = 0.323000f;

	m_CamPos[1] = DXVector3(-11.994381f, 110.215805f, 65.577911f);
	m_CamDir[1] = DXVector3(-0.020165f, -0.997019f, 0.074522f);
	m_CamUp[1] = DXVector3(0.001481f, 0.074507f, 0.997223f);
	m_CamLightAngX[1] = 0.410000f; m_CamLightAngZ[1] = 0.865999f;

	m_CamPos[2] = DXVector3(-2.127440f, 85.283630f, 33.467052f);
	m_CamDir[2] = DXVector3(0.457727f, -0.431565f, -0.777332f);
	m_CamUp[2] = DXVector3(0.565555f, -0.533284f, 0.629097f);
	m_CamLightAngX[2] = 0.588000f; m_CamLightAngZ[2] = 1.327999f;

	m_CamPos[3] = DXVector3(50.321091f, 8.824189f, 29.489088f);
	m_CamDir[3] = DXVector3(-0.694617f, -0.091199f, 0.713577f);
	m_CamUp[3] = DXVector3(0.707503f, 0.092929f, 0.700578f);
	m_CamLightAngX[3] = 0.546000f; m_CamLightAngZ[3] = 1.121000f;

	m_CamPos[4] = DXVector3(10.671138f, 14.887597f, 16.024084f);
	m_CamDir[4] = DXVector3(-0.462709f, 0.885807f, -0.035406f);
	m_CamUp[4] = DXVector3(-0.016362f, 0.031390f, 0.999377f);
	m_CamLightAngX[4] = 0.464001f; m_CamLightAngZ[4] = 1.510999f;

	m_CamPos[5] = DXVector3(-162.198318f, -95.662674f, 14.745945f);
	m_CamDir[5] = DXVector3(0.848104f, 0.465725f, -0.252657f);
	m_CamUp[5] = DXVector3(0.221478f, 0.121567f, 0.967564f);
	m_CamLightAngX[5] = 0.681000f; m_CamLightAngZ[5] = 2.115998f;

	m_CamPos[6] = DXVector3(-43.840759f, 24.927395f, 65.786461f);
	m_CamDir[6] = DXVector3(-0.825419f, 0.408348f, -0.389812f);
	m_CamUp[6] = DXVector3(-0.349374f, 0.172868f, 0.920905f);
	m_CamLightAngX[6] = 0.698000f; m_CamLightAngZ[6] = 2.191998f;

	m_CamPos[7] = DXVector3(-47.118015f, 51.397289f, 53.419601f);
	m_CamDir[7] = DXVector3(0.130003f, 0.839681f, -0.527310f);
	m_CamUp[7] = DXVector3(0.080720f, 0.521081f, 0.849686f);
	m_CamLightAngX[7] = 0.305001f; m_CamLightAngZ[7] = 2.151998f;

	m_CamPos[8] = DXVector3(164.806290f, 33.427910f, 25.841290f);
	m_CamDir[8] = DXVector3(0.774204f, -0.515923f, -0.366689f);
	m_CamUp[8] = DXVector3(0.305104f, -0.203392f, 0.930359f);
	m_CamLightAngX[8] = 0.311001f; m_CamLightAngZ[8] = 1.979998f;

	m_CamPos[9] = DXVector3(2.558059f, -118.421860f, 84.924431f);
	m_CamDir[9] = DXVector3(0.000381f, 0.935753f, -0.352699f);
	m_CamUp[9] = DXVector3(0.000241f, 0.352677f, 0.935753f);
	m_CamLightAngX[9] = 0.331001f; m_CamLightAngZ[9] = 2.350997f;
}