示例#1
0
	void Engine::CreateTestData()
	{
		D3D11_INPUT_ELEMENT_DESC layout[] =
		{
			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 
				D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "NORMAL", 0, DXGI_FORMAT_R16G16_UNORM, 0, 
				D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "TEXCOORD", 0, DXGI_FORMAT_R16G16_UNORM, 0, 
				D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 
				D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		};

		Vector<StringByte> shaderSource;
	
		// g-buffer write
		FileSystem::Get()->Read("data/shaders/default.vsh", shaderSource);
		vertexShader.Load(shaderSource.data(), layout, util::SizeOfArray(layout));

		FileSystem::Get()->Read("data/shaders/default.psh", shaderSource);
		pixelShader.Load(shaderSource.data());

		FileSystem::Get()->Read("data/shaders/grid.psh", shaderSource);
		gridPixelShader.Load(shaderSource.data());		

		// deferred composite
		FileSystem::Get()->Read("data/shaders/identity.vsh", shaderSource);
		identityVertexShader.Load(shaderSource.data(), layout, util::SizeOfArray(layout));

		FileSystem::Get()->Read("data/shaders/composite.psh", shaderSource);
		compositePixelShader.Load(shaderSource.data());

		FileSystem::Get()->Read("data/shaders/texture.psh", shaderSource);
		texturePixelShader.Load(shaderSource.data());

		const int GridWidth = 21;
		const int HalfGridWidth = GridWidth / 2;
		F32 heightMap[GridWidth][GridWidth];
		Vertex floorVertices[GridWidth * GridWidth * 6];
		
		for (U32 yIndex = 0; yIndex < GridWidth; yIndex++)
		{
			for (U32 xIndex = 0; xIndex < GridWidth; xIndex++)
			{
				float x = xIndex + 0.5f - GridWidth/2.0f;
				float y = yIndex + 0.5f - GridWidth/2.0f;

				auto height = glm::simplex(glm::vec2(x, y) / 8.0f);
				height += glm::simplex(glm::vec2(x, y) / 3.75f) * 0.6f;

				heightMap[yIndex][xIndex] = height;
			}
		}

		auto index = 0u;		
		for (U32 yIndex = 0; yIndex < GridWidth; yIndex++)
		{
			for (U32 xIndex = 0; xIndex < GridWidth; xIndex++)
			{
				float x = xIndex + 0.5f - GridWidth/2.0f;
				float y = yIndex + 0.5f - GridWidth/2.0f;

				auto p = Vec3(x, 0.0f, y);

				auto p1 = p + Vec3(0.5f, 0.0f, -0.5f);
				auto p2 = p + Vec3(-0.5f, 0.0f, -0.5f);
				auto p3 = p + Vec3(-0.5f, 0.0f, 0.5f);
				auto p4 = p + Vec3(0.5f, 0.0f, 0.5f);

				auto PushBack = [&](Vec3 pf)
				{
					auto h = [&](float x, float z)
					{
						return heightMap
							[math::Clamp(static_cast<S32>(floor(z)), -HalfGridWidth, HalfGridWidth) + HalfGridWidth]
							[math::Clamp(static_cast<S32>(floor(x)), -HalfGridWidth, HalfGridWidth) + HalfGridWidth];
					};

					pf.y = h(pf.x, pf.z);

					float deltaX = h(pf.x + 1, pf.z) - h(pf.x - 1, pf.z);
					float deltaZ = h(pf.x, pf.z + 1) - h(pf.x, pf.z - 1);
					Vec3 normal = glm::normalize(Vec3(-deltaX, 2, -deltaZ));

					auto col = Vec4(
						(pf.x + HalfGridWidth) / static_cast<float>(GridWidth), 
						1.0f, 
						(pf.z + HalfGridWidth) / static_cast<float>(GridWidth),
						1.0f);

					Vertex vertex;
					vertex.position = pf;
					vertex.SetNormal(normal);
					vertex.colour = col;

					floorVertices[index] = vertex;
					index++;
				};

				PushBack(p1);
				PushBack(p2);
				PushBack(p3);

				PushBack(p4);
				PushBack(p1);
				PushBack(p3);
			}
		}

		floorMesh.Create(floorVertices);

		Vertex gizmoVertices[] =
		{
			{Vec3(0.0f, 0.0f, 0.0f), Vec3(1.0f, 0.0f, 0.0f), Colour::Red},
			{Vec3(1.0f, 0.0f, 0.0f), Vec3(1.0f, 0.0f, 0.0f), Colour::Red},

			{Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f), Colour::Green},
			{Vec3(0.0f, 1.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f), Colour::Green},

			{Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 0.0f, 1.0f), Colour::Blue},
			{Vec3(0.0f, 0.0f, 1.0f), Vec3(0.0f, 0.0f, 1.0f), Colour::Blue},
		};

		gizmoMesh.Create(gizmoVertices, D3D11_PRIMITIVE_TOPOLOGY_LINELIST);

		Vertex screenVertices[] =
		{
			{Vec3(1, -1, 0), Vec3(0, 0, -1), Vec2(1, 1)},
			{Vec3(-1, -1, 0), Vec3(0, 0, -1), Vec2(0, 1)},
			{Vec3(-1, 1, 0), Vec3(0, 0, -1), Vec2(0, 0)},
			
			{Vec3(1, 1, 0), Vec3(0, 0, -1), Vec2(1, 0)},
			{Vec3(1, -1, 0), Vec3(0, 0, -1), Vec2(1, 1)},
			{Vec3(-1, 1, 0), Vec3(0, 0, -1), Vec2(0, 0)},
		};

		screenMesh.Create(screenVertices);
	}