PlaneMesh::PlaneMesh(ASTRING fileName, AREAL scalex, AREAL scaley)
{
	//Initialize buffers
	m_pVertices = new VertexBuffer();
	m_pTexCoords = new VertexBuffer();
	m_pNormals = new VertexBuffer();

	//m_pShaders = new ShaderBunch();

	//Buffer resource
	BufferParams params;
	params.FillVertexBufferParams(sizeof(float3), 36, true, false, false, false);

	//resource data
	SubresourceData data;

	#pragma region Positions
	/*float3 pos [] = 
	{
		float3(-0.5f*scalex, 0.5f*scaley, 0.0f),
		float3(0.5f*scalex, 0.5f*scaley, 0.0f),
		float3(-0.5f*scalex, -0.5f*scaley, 0.0f),

		float3(-0.5f*scalex, -0.5f*scaley, 0.0f),
		float3(0.5f*scalex, 0.5f*scaley, 0.0f),
		float3(0.5f*scalex, -0.5f*scaley, 0.0f),
	}; */
	float3 pos [] =
	{
		float3(-0.5f*scalex, 0.0f, 0.5f*scaley),
		float3(0.5f*scalex, 0.0f, 0.5f*scaley),
		float3(-0.5f*scalex, 0.0f, -0.5f*scaley),

		float3(-0.5f*scalex, 0.0f, -0.5f*scaley),
		float3(0.5f*scalex, 0.0f, 0.5f*scaley),
		float3(0.5f*scalex, 0.0f, -0.5f*scaley),
	};

	params.SetSize(sizeof(float3) * 6);
	data.SetData(pos);
	if (!m_pVertices->Create(&params, &data, 6, sizeof(float3)))
	{
		assert(0 && "Error creating vertex buffer in Plane Mesh initialization!");
	}
#pragma endregion

	#pragma region Texture Coordinates
	float2 texCoords[] =
	{
		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),
		float2(0.0f, 1.0f),

		float2(0.0f, 1.0f),
		float2(1.0f, 0.0f),
		float2(1.0f, 1.0f),
	};

	params.SetSize(sizeof(float2) * 6);
	data.SetData(texCoords);
	if (!m_pTexCoords->Create(&params, &data, 6, sizeof(float2)))
	{
		assert(0 && "Error creating vertex buffer with texture coordinates in Plane Mesh initialization!");
	}

#pragma endregion

	#pragma region Normal Data
	struct NormalData
	{
		float3 normal;
		float3 tangent;
		float3 binormal;
	};

	NormalData normalData[6];

	//generate normals data for each face
	for (int i = 0; i < 2; i++)
	{
		float3 normal;
		float3 tangent;
		float3 binormal;

		CreateNormalTangentBinormal(&pos[i*3], &texCoords[i*3], normal, tangent, binormal);

		//fill the data for vertex buffer
		normalData[i*3+2].normal = normal;
		normalData[i*3+2].tangent = tangent;
		normalData[i*3+2].binormal = binormal;

		normalData[i*3+1].normal = normal;
		normalData[i*3+1].tangent = tangent;
		normalData[i*3+1].binormal = binormal;

		normalData[i*3].normal = normal;
		normalData[i*3].tangent = tangent;
		normalData[i*3].binormal = binormal;
	}

	params.SetSize(sizeof(NormalData) * 6);
	data.SetData(normalData);
	if (!m_pNormals->Create(&params, &data, 6, sizeof(NormalData)))
	{
		assert(0 && "Error creating vertex buffer with Normal data in Plane Mesh initialization!");
	}

#pragma endregion

	//initialize input layout 
	/*INPUT_LAYOUT layout[] =
	{
		{ "POSITION", 0, TEX_R32G32B32_FLOAT, 0, 0,  IA_PER_VERTEX_DATA, 0},
		{ "TEXCOORDS", 0, TEX_R32G32_FLOAT,	  1, 0,  IA_PER_VERTEX_DATA, 0},
		{ "NORMAL",   0, TEX_R32G32B32_FLOAT, 2, 0,  IA_PER_VERTEX_DATA, 0},
		{ "TANGENT", 0, TEX_R32G32B32_FLOAT, 2, 12, IA_PER_VERTEX_DATA, 0},
		{ "BINORMAL",  0, TEX_R32G32B32_FLOAT, 2, 24, IA_PER_VERTEX_DATA, 0},
	};

	//Initialize shaders
	m_pShaders->VSetVertexShader(L"GBufferShader.hlsl", "VS", layout, 5, TOPOLOGY_TRIANGLELIST);
	m_pShaders->VSetPixelShader(L"GBufferShader.hlsl", "PS"); */
}
Beispiel #2
0
GrassPack::GrassPack(	const Vec & center, const AREAL r32HalfWidth, const AREAL r32HalfHeight,
						const AINT32 & num)
{
	const AREAL size_x = 0.5f;
	const AREAL size_y = 0.7f;

	m_worldTransform = Mat4x4::Identity().CreateTranslation(center);
	m_pVertices = new VertexBuffer();
	m_pTexCoords = new VertexBuffer();
	m_pNormals = new VertexBuffer();

	m_pMaterial = new Material();
	m_pShaders = new ShaderBunch();

	//Initialize vertex buffers
	BufferParams params;
	params.FillVertexBufferParams(sizeof(float3), 18 * num, true, false, false, false);

	SubresourceData data;

#pragma region Vertex buffers
	float3* pos = new float3[18 * num];
	float2* texCoords = new float2[18 * num];
	float3* normals = new float3[18 * num];
	
	//calculate grass quad offsets
	Mat4x4 rotCW = Mat4x4::Identity();
	Mat4x4 rotCCW = Mat4x4::Identity();
	rotCW = rotCW.CreateRotationY(Pi / 4.0);
	rotCCW = rotCCW.CreateRotationY(-Pi / 4.0);

	Vec leftTopVec		= Vector(-size_x, size_y, 0.0f, 0.0f);
	Vec rightTopVec		= Vector(size_x, size_y, 0.0f, 0.0f);
	Vec leftBottomVec	= Vector(-size_x, -size_y, 0.0f, 0.0f);
	Vec rightBottomVec	= Vector(size_x, -size_y, 0.0f, 0.0f);
	float3 leftTop1		= float3(leftTopVec);
	float3 rightTop1	= float3(rightTopVec);
	float3 leftBottom1	= float3(leftBottomVec);
	float3 rightBottom1	= float3(rightBottomVec);
	float3 leftTop2		= float3(leftTopVec * rotCW);
	float3 rightTop2	= float3(rightTopVec * rotCW);
	float3 leftBottom2	= float3(leftBottomVec * rotCW);
	float3 rightBottom2 = float3(rightBottomVec * rotCW);
	float3 leftTop3		= float3(leftTopVec * rotCCW);
	float3 rightTop3	= float3(rightTopVec * rotCCW);
	float3 leftBottom3	= float3(leftBottomVec * rotCCW);
	float3 rightBottom3 = float3(rightBottomVec * rotCCW);

	float3 normal = leftTop1 - leftBottom1;

	AINT32 numWidth = num / (AREAL)(r32HalfHeight + r32HalfHeight);
	AREAL stepWidth  =  (r32HalfWidth * 2) / (AREAL)numWidth;

	AINT32 numHeight = num / numWidth;
	AREAL stepHeight = (r32HalfHeight * 2) / (AREAL)numHeight;

	for (AINT32 i = 0; i < num; i++)
	{
		///////////////////////////////////////////////////
		//Calculate block center position
		//AINT32 x = Random(-r32HalfWidth, r32HalfWidth);
		//AINT32 y = Random(-r32HalfHeight, r32HalfHeight);
		AREAL x = (i % numWidth) * stepWidth;
		AREAL z = (i / numWidth) * stepHeight;

		float3 blockCenter = float3(getx(center) + x, gety(center), getz(center) + z);

		///////////////////////////////////////////////////
		//Calculate invidual vertex data for each block

		//first block
		pos[i*18]		= blockCenter + leftTop1;
		pos[i*18+1]		= blockCenter + rightTop1;
		pos[i*18+2]		= blockCenter + leftBottom1;
		pos[i*18+3]		= blockCenter + leftBottom1;
		pos[i*18+4]		= blockCenter + rightTop1;
		pos[i*18+5]		= blockCenter + rightBottom1;

		texCoords[i*18]	  = float2(0.0f, 0.0f);
		texCoords[i*18+1] = float2(1.0f, 0.0f);
		texCoords[i*18+2] = float2(0.0f, 1.0f);
		texCoords[i*18+3] = float2(0.0f, 1.0f);
		texCoords[i*18+4] = float2(1.0f, 0.0f);
		texCoords[i*18+5] = float2(1.0f, 1.0f);

		normals[i*18]	= normal;
		normals[i*18+1] = normal;
		normals[i*18+2] = normal;
		normals[i*18+3] = normal;
		normals[i*18+4] = normal;
		normals[i*18+5] = normal;

		//second block
		pos[i*18+6]		= blockCenter + leftTop2;
		pos[i*18+7]		= blockCenter + rightTop2;
		pos[i*18+8]		= blockCenter + leftBottom2;
		pos[i*18+9]		= blockCenter + leftBottom2;
		pos[i*18+10]	= blockCenter + rightTop2;
		pos[i*18+11]	= blockCenter + rightBottom2;

		texCoords[i*18+6]	  = float2(0.0f, 0.0f);
		texCoords[i*18+7] = float2(1.0f, 0.0f);
		texCoords[i*18+8] = float2(0.0f, 1.0f);
		texCoords[i*18+9] = float2(0.0f, 1.0f);
		texCoords[i*18+10] = float2(1.0f, 0.0f);
		texCoords[i*18+11] = float2(1.0f, 1.0f);

		normals[i*18+6]	= normal;
		normals[i*18+7] = normal;
		normals[i*18+8] = normal;
		normals[i*18+9] = normal;
		normals[i*18+10] = normal;
		normals[i*18+11] = normal;

		//third block
		pos[i*18+12]	= blockCenter + leftTop3;
		pos[i*18+13]	= blockCenter + rightTop3;
		pos[i*18+14]	= blockCenter + leftBottom3;
		pos[i*18+15]	= blockCenter + leftBottom3;
		pos[i*18+16]	= blockCenter + rightTop3;
		pos[i*18+17]	= blockCenter + rightBottom3;

		texCoords[i*18+12] = float2(0.0f, 0.0f);
		texCoords[i*18+13] = float2(1.0f, 0.0f);
		texCoords[i*18+14] = float2(0.0f, 1.0f);
		texCoords[i*18+15] = float2(0.0f, 1.0f);
		texCoords[i*18+16] = float2(1.0f, 0.0f);
		texCoords[i*18+17] = float2(1.0f, 1.0f);

		normals[i*18+12]	= normal;
		normals[i*18+13] = normal;
		normals[i*18+14] = normal;
		normals[i*18+15] = normal;
		normals[i*18+16] = normal;
		normals[i*18+17] = normal;
	}

	params.SetSize(sizeof(float3)*18*num);
	data.SetData(pos);
	if (!m_pVertices->Create(&params, &data, 18*num, sizeof(float3)))
	{
		assert(0 && "Error creating vertex buffer in Grass initialization!");
	}

	params.SetSize(sizeof(float2)*18*num);
	data.SetData(texCoords);
	if (!m_pTexCoords->Create(&params, &data, 18*num, sizeof(float2)))
	{
		assert(0 && "Error creating vertex buffer in Grass initialization!");
	}

	params.SetSize(sizeof(float3)*18*num);
	data.SetData(normals);
	if (!m_pNormals->Create(&params, &data, 18*num, sizeof(float3)))
	{
		assert(0 && "Error creating vertex buffer in Grass Initialization!");
	}
#pragma endregion

	//Initilize input layout
	INPUT_LAYOUT layout[] =
	{
		{ "POSITION", 0, TEX_R32G32B32_FLOAT, 0, 0, IA_PER_VERTEX_DATA, 0},
		{ "TEXCOORD", 0, TEX_R32G32_FLOAT,	  1, 0, IA_PER_VERTEX_DATA, 0},
		{ "NORMAL",   0, TEX_R32G32B32_FLOAT, 2, 0, IA_PER_VERTEX_DATA, 0},
	};

	m_pShaders->VSetVertexShader(L"Grass_Vertex.hlsl", "Grass_VS", layout, 3, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pShaders->VSetPixelShader(L"Grass_Pixel.hlsl", "Grass_PS", "ps_5_0");
}
bool MeshLoader::VLoadResource(char* pRawBuffer, unsigned int uRawSize, shared_ptr<ResHandle> pHandle)
{
    auto pExtra = make_shared<MeshResourceExtraData>();
    pHandle->SetExtra(pExtra);

    pExtra->m_pMesh = make_shared<IndexedMesh>();

    FILE *file;
    fopen_s(&file, pRawBuffer, "rb");
    if (!file)
        return false;

    // check idString
    char idString[5];
    memset(idString, 0, 5);
    fread(idString, sizeof(char), 4, file);
    if (strcmp(idString, "DMSH") != 0)
    {
        fclose(file);
        return false;
    }

    // check version
    int version;
    fread(&version, sizeof(int), 1, file);
    if (version != 1)
    {
        fclose(file);
        return false;
    }

    // get number of vertices
    int numVertices;
    fread(&numVertices, sizeof(int), 1, file);
    if (numVertices < 3)
    {
        fclose(file);
        return false;
    }

    struct GEOMETRY_VERTEX
    {
        float3 position;
        float2 texCoords;
        float3 normal;
        Vec tangent;
    };
    // load vertices
    GEOMETRY_VERTEX *vertices = new GEOMETRY_VERTEX[numVertices];
    if (!vertices)
    {
        fclose(file);
        return false;
    }
    fread(vertices, sizeof(GEOMETRY_VERTEX), numVertices, file);

    //create vertex buffer
    float3* pos = new float3[numVertices];
    float2* texCoords = new float2[numVertices];
    float3* normals = new float3[numVertices];
    Vec* tangents = new Vec[numVertices];

    for (auto i = 0; i < numVertices; ++i)
    {
        pos[i] = vertices[i].position;
        texCoords[i] = vertices[i].texCoords;
        normals[i] = vertices[i].normal;
        tangents[i] = vertices[i].tangent;
    }

    BufferParams params;
    params.FillVertexBufferParams(sizeof(float3), numVertices, true, false, false, false);

    SubresourceData data;
    data.SetData(pos);

    pExtra->m_pMesh->m_pVertices = make_shared<VertexBuffer>();
    if (!pExtra->m_pMesh->m_pVertices->Create(params, &data, numVertices, sizeof(float3)))
    {
        assert(0 && "Error creating vertex buffer!");
    }

    params.FillVertexBufferParams(sizeof(float2), numVertices, true, false, false, false);
    data.SetData(texCoords);

    pExtra->m_pMesh->m_pTexCoords = make_shared<VertexBuffer>();
    if (!pExtra->m_pMesh->m_pTexCoords->Create(params, &data, numVertices, sizeof(float2)))
    {
        assert(0 && "Error creating vertex buffer!");
    }

    params.FillVertexBufferParams(sizeof(float3), numVertices, true, false, false, false);
    data.SetData(normals);

    pExtra->m_pMesh->m_pNormals = make_shared<VertexBuffer>();
    if (!pExtra->m_pMesh->m_pNormals->Create(params, &data, numVertices, sizeof(float3)))
    {
        assert(0 && "Error creating vertex buffer!");
    }

    params.FillVertexBufferParams(sizeof(Vec), numVertices, true, false, false, false);
    data.SetData(tangents);

    pExtra->m_pMesh->m_pTangents = make_shared<VertexBuffer>();
    if (!pExtra->m_pMesh->m_pTangents->Create(params, &data, numVertices, sizeof(Vec)))
    {
        assert(0 && "Error creating vertex buffer!");
    }

    SAFE_DELETE_ARRAY(tangents);
    SAFE_DELETE_ARRAY(normals);
    SAFE_DELETE_ARRAY(texCoords);
    SAFE_DELETE_ARRAY(pos);
    SAFE_DELETE_ARRAY(vertices);

    // get number of indices
    int numIndices;
    fread(&numIndices, sizeof(int), 1, file);
    if (numIndices<3)
    {
        fclose(file);
        return false;
    }

    // load indices
    int *indices = new int[numIndices];
    if (!indices)
    {
        fclose(file);
        return false;
    }
    fread(indices, sizeof(int), numIndices, file);

    params.FillIndexBufferParams(sizeof(int), numIndices, true, false, false, false);
    data.SetData(indices);

    pExtra->m_pMesh->m_pIndexBuffer = make_shared<IndexBuffer>();
    pExtra->m_pMesh->m_pIndexBuffer->Create(params, &data, numIndices, sizeof(int));

    SAFE_DELETE_ARRAY(indices);

    // get number of sub-meshes
    int numSubMeshes;
    fread(&numSubMeshes, sizeof(int), 1, file);
    if (numSubMeshes<1)
    {
        fclose(file);
        return false;
    }

    //load and create submeshes
    pExtra->m_pMesh->m_subMeshes.reserve(numSubMeshes);
    pExtra->m_uNumMeshes = numSubMeshes;
    for (int i = 0; i < numSubMeshes; ++i)
    {
        auto pMesh = make_shared<SubMesh>();
        char materialName[256];
        fread(materialName, sizeof(char), 256, file);
        pMesh->SetMaterial(materialName);

        fread(&pMesh->m_firstIndex, sizeof(int), 1, file);
        fread(&pMesh->m_numIndices, sizeof(int), 1, file);

        pExtra->m_pMesh->AddSubMesh(pMesh);
    }

    return true;
}
ABOOL DeferredRenderer::VInitialize(HWND hWnd, AUINT32 width, AUINT32 height)
{
	if (!Renderer::VInitialize(hWnd, width, height))
	{
		assert(0 && "Some problem initializing Renderer occured");
	}

	//initialize gbuffer
	m_gbuffer.VInitialize();

	//initialize data for light pass
	BufferParams matBufferParams;
	matBufferParams.FillConstantBufferParams(sizeof(Mat4x4), true, false, false);
	m_pMatrixBuffer->Create(&matBufferParams, NULL);

	//initialize data for light rendering
	BufferParams params;
	params.FillVertexBufferParams(sizeof(float3), 6, true, false, false, false);

	//position data
	SubresourceData data;

	AREAL32 left = (AREAL32)(SCREEN_WIDTH / 2 * -1);
	AREAL32 right = left + SCREEN_WIDTH;
	AREAL32 top = (AREAL32)(SCREEN_HEIGHT / 2);
	AREAL32 bottom = top - SCREEN_HEIGHT;

	float3 pos [] = 
	{
		float3(left,	top,	0.0f), //left top
		float3(right,	top,	0.0f), //right top
		float3(left,	bottom, 0.0f), //left bottom
		float3(left,	bottom, 0.0f), //left bottom
		float3(right,	top,	0.0f), //right top
		float3(right,	bottom, 0.0f), //right bottom
	};

	data.SetData(pos);
	if (!m_pVertices->Create(&params, &data, 6, sizeof(float3)))
	{
		assert(0 && "Error creating vertex buffer in Deferred Renderer initialization!");
	}

	//texture coordinates
	params.SetSize(sizeof(float2) * 6);

	float2 texCoords [] =
	{
		float2(0.0f, 0.0f), //left top
		float2(1.0f, 0.0f), //right top
		float2(0.0f, 1.0f), //left bottom
		float2(0.0f, 1.0f), //left bottom
		float2(1.0f, 0.0f), //right top
		float2(1.0f, 1.0f), //right bottom
	};

	data.SetData(texCoords);
	if (!m_pTexCoords->Create(&params, &data, 6, sizeof(float2)))
	{
		assert(0 && "Error creating vertex buffer with texture coordinates in Deferred Renderer initialization!");
	}

	//initialize input layout 
	m_pDefaultLayout = new INPUT_LAYOUT[5];

	m_pDefaultLayout[0].SemanticName			= "POSITION";			m_pDefaultLayout[1].SemanticName			= "TEXCOORDS";
	m_pDefaultLayout[0].SemanticIndex			= 0;					m_pDefaultLayout[1].SemanticIndex			= 0;
	m_pDefaultLayout[0].Format					= TEX_R32G32B32_FLOAT;	m_pDefaultLayout[1].Format					= TEX_R32G32_FLOAT;
	m_pDefaultLayout[0].InputSlot				= 0;					m_pDefaultLayout[1].InputSlot				= 1;
	m_pDefaultLayout[0].AlignedByteOffset		= 0;					m_pDefaultLayout[1].AlignedByteOffset		= 0;
	m_pDefaultLayout[0].InputSlotClass			= IA_PER_VERTEX_DATA;	m_pDefaultLayout[1].InputSlotClass			= IA_PER_VERTEX_DATA;
	m_pDefaultLayout[0].InstanceDataStepRate	= 0;					m_pDefaultLayout[1].InstanceDataStepRate	= 0;

	m_pDefaultLayout[2].SemanticName			= "NORMAL";				m_pDefaultLayout[3].SemanticName			= "TANGENT";
	m_pDefaultLayout[2].SemanticIndex			= 0;					m_pDefaultLayout[3].SemanticIndex			= 0;
	m_pDefaultLayout[2].Format					= TEX_R32G32B32_FLOAT;	m_pDefaultLayout[3].Format					= TEX_R32G32B32_FLOAT;
	m_pDefaultLayout[2].InputSlot				= 2;					m_pDefaultLayout[3].InputSlot				= 2;
	m_pDefaultLayout[2].AlignedByteOffset		= 0;					m_pDefaultLayout[3].AlignedByteOffset		= 12;
	m_pDefaultLayout[2].InputSlotClass			= IA_PER_VERTEX_DATA;	m_pDefaultLayout[3].InputSlotClass			= IA_PER_VERTEX_DATA;
	m_pDefaultLayout[2].InstanceDataStepRate	= 0;					m_pDefaultLayout[3].InstanceDataStepRate	= 0;

	m_pDefaultLayout[4].SemanticName			= "BINORMAL";			
	m_pDefaultLayout[4].SemanticIndex			= 0;					
	m_pDefaultLayout[4].Format					= TEX_R32G32B32_FLOAT;	
	m_pDefaultLayout[4].InputSlot				= 2;					
	m_pDefaultLayout[4].AlignedByteOffset		= 24;					
	m_pDefaultLayout[4].InputSlotClass			= IA_PER_VERTEX_DATA;	
	m_pDefaultLayout[4].InstanceDataStepRate	= 0;					

	//Initialize shaders
	m_pDefaultGBufferShaders->VSetVertexShader(L"GBufferShader.hlsl", "VS", m_pDefaultLayout, 5, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pDefaultGBufferShaders->VSetPixelShader(L"GBufferShader.hlsl", "PS", "ps_5_0");

	//input layout
	m_pLayout = new INPUT_LAYOUT[2];
	m_pLayout[0].SemanticName			= "POSITION";			m_pLayout[1].SemanticName			= "TEXCOORDS";
	m_pLayout[0].SemanticIndex			= 0;					m_pLayout[1].SemanticIndex			= 0;
	m_pLayout[0].Format					= TEX_R32G32B32_FLOAT;	m_pLayout[1].Format					= TEX_R32G32_FLOAT;
	m_pLayout[0].InputSlot				= 0;					m_pLayout[1].InputSlot				= 1;
	m_pLayout[0].AlignedByteOffset		= 0;					m_pLayout[1].AlignedByteOffset		= 0;
	m_pLayout[0].InputSlotClass			= IA_PER_VERTEX_DATA;	m_pLayout[1].InputSlotClass			= IA_PER_VERTEX_DATA;
	m_pLayout[0].InstanceDataStepRate	= 0;					m_pLayout[1].InstanceDataStepRate	= 0;

	////////////////////////////////////////////////////////
	//Shadow mapping
	m_pShadowsLayout = new INPUT_LAYOUT();
	m_pShadowsLayout->SemanticName	=	"POSITION";
	m_pShadowsLayout->SemanticIndex	=	0;
	m_pShadowsLayout->Format		=	TEX_R32G32B32_FLOAT;
	m_pShadowsLayout->InputSlot		=	0;
	m_pShadowsLayout->AlignedByteOffset =	0;
	m_pShadowsLayout->InputSlotClass	=	IA_PER_VERTEX_DATA;
	m_pShadowsLayout->InstanceDataStepRate =	0;

	m_pShadowsShaders->VSetVertexShader(L"Resources\\Shaders\\Shadows\\ShadowMap_VS.hlsl", "Shadows_VS", m_pShadowsLayout, 1, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pShadowsShaders->VSetPixelShader(L"Resources\\Shaders\\Shadows\\ShadowMap_PS.hlsl", "Shadows_PS", "ps_5_0");

	m_pShadowCubeShaders->VSetVertexShader(L"Shaders\\CubeShadowMap_VS.hlsl", "CubeShadowMap_VS", m_pShadowsLayout, 1, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pShadowCubeShaders->VSetGeometryShader(L"Shaders\\CubeShadowMap_GS.hlsl", "CubeShadowMap_GS", "gs_5_0");
	m_pShadowCubeShaders->VSetPixelShader(L"Shaders\\CubeShadowMap_PS.hlsl", "CubeShadowMap_PS", "gs_5_0");
	
	m_pVarianceShadowsShaders->VSetVertexShader(L"Resources\\Shaders\\Shadows\\VarianceShadows_DepthVS.hlsl", "VarianceShadows_DepthVS", m_pShadowsLayout, 1, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pVarianceShadowsShaders->VSetPixelShader(L"Resources\\Shaders\\Shadows\\VarianceShadows_DepthPS.hlsl", "VarianceShadows_DepthPS", "ps_5_0");

	////////////////////////////////////////////////////////
	//Motion Blur
	m_pMotionBlurLayout = new INPUT_LAYOUT[2];
	m_pMotionBlurLayout[0].SemanticName = "POSITION";				m_pMotionBlurLayout[1].SemanticName = "TEXCOORD";
	m_pMotionBlurLayout[0].SemanticIndex = 0;						m_pMotionBlurLayout[1].SemanticIndex = 0;
	m_pMotionBlurLayout[0].Format = TEX_R32G32B32_FLOAT;			m_pMotionBlurLayout[1].Format = TEX_R32G32_FLOAT;
	m_pMotionBlurLayout[0].InputSlot = 0;							m_pMotionBlurLayout[1].InputSlot = 1;
	m_pMotionBlurLayout[0].AlignedByteOffset = 0;					m_pMotionBlurLayout[1].AlignedByteOffset = 0;
	m_pMotionBlurLayout[0].InputSlotClass = IA_PER_VERTEX_DATA;		m_pMotionBlurLayout[1].InputSlotClass = IA_PER_VERTEX_DATA;
	m_pMotionBlurLayout[0].InstanceDataStepRate = 0;				m_pMotionBlurLayout[1].InstanceDataStepRate = 0;

	m_pVelocityMapShaders->VSetVertexShader(L"Resources\\Shaders\\PostProcessing\\VelocityMap_VS.hlsl", "GenerateVelocityMap_VS", m_pMotionBlurLayout, 2, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pVelocityMapShaders->VSetPixelShader(L"Resources\\Shaders\\PostProcessing\\VelocityMap_PS.hlsl", "GenerateVelocityMap_PS", "ps_5_0");

	m_pMotionBlurShaders->VSetVertexShader(L"Resources\\Shaders\\PostProcessing\\MotionBlur_VS.hlsl", "MotionBlur_VS", m_pMotionBlurLayout, 2, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pMotionBlurShaders->VSetPixelShader(L"Resources\\Shaders\\PostProcessing\\MotionBlur_PS.hlsl", "MotionBlur_PS", "ps_5_0");

	////////////////////////////////////////////////////////
	//resourece copying
	m_pSRVtoRTVLayout = new INPUT_LAYOUT();
	m_pSRVtoRTVLayout->SemanticName		=	"POSITION";
	m_pSRVtoRTVLayout->SemanticIndex	=	0;
	m_pSRVtoRTVLayout->Format			=	TEX_R32G32B32_FLOAT;
	m_pSRVtoRTVLayout->InputSlot		=	0;
	m_pSRVtoRTVLayout->AlignedByteOffset =	0;
	m_pSRVtoRTVLayout->InputSlotClass	=	IA_PER_VERTEX_DATA;
	m_pSRVtoRTVLayout->InstanceDataStepRate =	0;

	m_pSRVtoRTVShaders->VSetVertexShader(L"Resources\\Shaders\\Other\\FromSRVtoRTV_VS.hlsl", "SRVtoRTV_VS", m_pSRVtoRTVLayout, 1, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pSRVtoRTVShaders->VSetPixelShader(L"Resources\\Shaders\\Other\\FromSRVtoRTV_PS.hlsl", "SRVtoRTV_PS", "ps_5_0");

	////////////////////////////////////////////////////////
	//Ambient occlusion
	m_pSSAOLayout = new INPUT_LAYOUT();
	m_pSSAOLayout->SemanticName		=	"POSITION";
	m_pSSAOLayout->SemanticIndex	=	0;
	m_pSSAOLayout->Format			=	TEX_R32G32B32_FLOAT;
	m_pSSAOLayout->InputSlot		=	0;
	m_pSSAOLayout->AlignedByteOffset =	0;
	m_pSSAOLayout->InputSlotClass	=	IA_PER_VERTEX_DATA;
	m_pSSAOLayout->InstanceDataStepRate =	0;

	m_pSSAOShaders->VSetVertexShader(L"SSAO_Vertex.hlsl", "SSAO_VS", m_pSSAOLayout, 1, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pSSAOShaders->VSetPixelShader(L"SSAO_Pixel.hlsl", "SSAO_PS", "ps_5_0");

	Texture2DParams tex2DParams;
	tex2DParams.Init(SCREEN_WIDTH, SCREEN_HEIGHT, 1, TEX_R32G32B32A32_FLOAT, true, true, true, false, 1, 0,
		1, true, false, false);
	m_pSSAOTexture->Create(&tex2DParams);

	//create shader resource and render target view for the texture
	ShaderResourceViewParams srvSSAOParams;
	srvSSAOParams.InitForTexture2D(tex2DParams.Format, 0, 0, false);
	m_pSSAOTexture->CreateShaderResourceView(&m_pSSAOSRV->m_pView, &srvSSAOParams);

	UnorderedAccessViewParams uavSSAOParams;
	uavSSAOParams.InitForTexture2D(tex2DParams.Format, 0);
	m_pSSAOTexture->CreateUnorderedAccessView(&m_pSSAOUAV->m_pView, &uavSSAOParams);

	RenderTargetViewParams rtvSSAOParams;
	rtvSSAOParams.InitForTexture2D(tex2DParams.Format, 0, false);
	m_pSSAOTexture->CreateRenderTargetView(&m_pSSAORTV->m_pView, &rtvSSAOParams);

	///////////////////////////////////////////////
	//Initialize sky resources
	///////////////////////////////////////////////
	tex2DParams.InitCubeTexture(1024, 1024, 1, TEX_R8G8B8A8_UNORM, true, false, false, false, 1, 0,
		1, true, false, false);
	//m_pSkyTexture->CreateFromFile(L"church_cubemap.dds");
	m_pSkyTexture->CreateFromFile(L"space_cubemap.dds");

	ShaderResourceViewParams srvSkyParams;
	srvSkyParams.InitForCubeTexture(TEX_R8G8B8A8_UNORM, 1, 0);
	m_pSkyTexture->CreateShaderResourceView(&m_pSkySRV->m_pView, &srvSkyParams);

	m_pSkyLayout = new INPUT_LAYOUT();
	//m_pSkyLayout[0].SemanticName = "POSITION";					//m_pSkyLayout[1].SemanticName = "TEXCOORD";
	//m_pSkyLayout[0].SemanticIndex = 0;							//m_pSkyLayout[1].SemanticIndex = 0;
	//m_pSkyLayout[0].Format = TEX_R32G32B32_FLOAT;				//m_pSkyLayout[1].Format = TEX_R32G32_FLOAT;
	//m_pSkyLayout[0].InputSlot = 0;								//m_pSkyLayout[1].InputSlot = 1;
	//m_pSkyLayout[0].AlignedByteOffset = 0;						//m_pSkyLayout[1].AlignedByteOffset = 0;
	//m_pSkyLayout[0].InputSlotClass = IA_PER_VERTEX_DATA;		//m_pSkyLayout[1].InputSlotClass = IA_PER_VERTEX_DATA;
	//m_pSkyLayout[0].InstanceDataStepRate = 0;					//m_pSkyLayout[1].InstanceDataStepRate = 0;
	m_pSkyLayout->SemanticName		=	"POSITION";
	m_pSkyLayout->SemanticIndex	=	0;
	m_pSkyLayout->Format			=	TEX_R32G32B32_FLOAT;
	m_pSkyLayout->InputSlot		=	0;
	m_pSkyLayout->AlignedByteOffset =	0;
	m_pSkyLayout->InputSlotClass	=	IA_PER_VERTEX_DATA;
	m_pSkyLayout->InstanceDataStepRate =	0;

	m_pSkyShaders->VSetVertexShader(L"Source\\Shaders\\SkyBox.hlsl", "SkyBox_VS", m_pSkyLayout, 1, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	m_pSkyShaders->VSetPixelShader(L"Source\\Shaders\\SkyBox.hlsl", "SkyBox_PS", "ps_5_0");

	//m_pAtmoShaders->VSetVertexShader(L"Shaders\\Atmosphere\\SkyFromAtmosphere_vs.hlsl", "SkyFromAtmosphere_vs", m_pSkyLayout, 1, TOPOLOGY_TRIANGLELIST, "vs_5_0");
	//m_pAtmoShaders->VSetPixelShader(L"Shaders\\Atmosphere\\SkyFromAtmosphere_ps.hlsl", "SkyFromAtmosphere_ps", "ps_5_0");

	Resource meshResource("sphere.obj");
	shared_ptr<ResHandle> pMeshes = Anubis::SafeGetHandle(&meshResource);
	std::shared_ptr<MeshResourceExtraData> pData = static_pointer_cast<MeshResourceExtraData>(pMeshes->GetExtra());

	m_pSphereMesh = pData->m_pMeshes[0];

	///////////////////////////////////////////////
	//Antialiasing
	///////////////////////////////////////////////
	m_pNullLayout = nullptr;

	struct EmptyVertex {};
	EmptyVertex verts [] =
	{
		EmptyVertex(),
		EmptyVertex(),
		EmptyVertex(),
	};
	params.FillVertexBufferParams(0, 3, true, false, false, false);
	SubresourceData emptydata;
	data.SetData(verts);
//	m_pFullscreenEmpty->Create(&params, &emptydata, 3, 0);

	//m_pFXAAShaders->VSetVertexShader(L"Shaders\\FXAA.hlsl", "FXAA_VS", m_pLayout, 0, TOPOLOGY_TRIANGLELIST);
	//m_pFXAAShaders->VSetPixelShader(L"Shaders\\FXAA.hlsl", "FXAA_PS");
	m_pFXAAShaders->VSetVertexShader(L"Shaders\\FXAA_orig.hlsl", "FxaaVS", m_pLayout, 0, TOPOLOGY_TRIANGLESTRIP, "vs_5_0");
	m_pFXAAShaders->VSetPixelShader(L"Shaders\\FXAA_orig.hlsl", "FxaaPS", "ps_5_0");

	///////////////////////////////////////////////
	//Initialize data for blurring
	///////////////////////////////////////////////
	tex2DParams.Init(SCREEN_WIDTH, SCREEN_HEIGHT, 1, TEX_R32G32B32A32_FLOAT, true, true, false, false, 1, 0,
		1, true, false, false);
	m_pSSAOBlurredTexture->Create(&tex2DParams);

	srvSSAOParams.InitForTexture2D(tex2DParams.Format, 0, 0, false);
	m_pSSAOBlurredTexture->CreateShaderResourceView(&m_pSSAOBlurredSRV->m_pView, &srvSSAOParams);
	
	uavSSAOParams.InitForTexture2D(tex2DParams.Format, 0);
	m_pSSAOBlurredTexture->CreateUnorderedAccessView(&m_pSSAOBlurredUAV->m_pView, &uavSSAOParams);

	return true;
}