Example #1
0
void Map::GeoBuffer()
{
	for (int y = 0; y < Md_.height; y++)
	{
		for (int x = 0; x < Md_.width; x++)
		{
			float ID = (float)Md_.grid[x][y][0] - 1;
			float col = floor(ID / 52.0f);
			float row = ID - (col * 52.0f);
			float sx = 1.0f / 52.0f;
			float sy = 1.0f / 52.0f;
			XMMATRIX trans = XMMatrixTranslation(row, col, 0.0f);
			XMMATRIX scale = XMMatrixScaling(sx, sy, 1.0f);
			XMStoreFloat4x4(&mTexTransform[x + (y * Md_.width)], trans * scale);

			GeometryGenerator::MeshData box;

			GeometryGenerator geoGen;
			//geoGen.CreateBox(1.0f, 1.0f, 1.0f, box);
			//geoGen.CreateFullscreenQuad(box); //Works
			//geoGen.CreateGrid(2.0f, 2.0f, 10, 10, box);
			//geoGen.CreateGeosphere(0.75f, 100, box);
			geoGen.CreateSquare(x, y, box);
			// Cache the vertex offsets to each object in the concatenated vertex buffer.
			mBoxVertexOffset = 0;

			// Cache the index count of each object.
			mBoxIndexCount = box.Indices.size();

			// Cache the starting index for each object in the concatenated index buffer.
			mBoxIndexOffset = 0;

			//UINT totalVertexCount = box.Vertices.size();
			UINT totalVertexCount = box.Vertices.size();

			UINT totalIndexCount = mBoxIndexCount;

			//
			// Extract the vertex elements we are interested in and pack the
			// vertices of all the meshes into one vertex buffer.
			//
			
			std::vector<GeometryGenerator::Vertex> vertices(totalVertexCount);

			UINT k = 0;
			for (size_t i = 0; i < box.Vertices.size(); ++i, ++k)
			{
				vertices[k].Position = box.Vertices[i].Position;
				vertices[k].Normal = box.Vertices[i].Normal;
				vertices[k].TexC = box.Vertices[i].TexC;
			}

			D3D11_BUFFER_DESC vbd;
			vbd.Usage = D3D11_USAGE_IMMUTABLE;
			vbd.ByteWidth = sizeof(GeometryGenerator::Vertex) * totalVertexCount;
			vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
			vbd.CPUAccessFlags = 0;
			vbd.MiscFlags = 0;
			D3D11_SUBRESOURCE_DATA vinitData;
			vinitData.pSysMem = &vertices[0];
			pDev_->CreateBuffer(&vbd, &vinitData, &mBoxVB[x + (y * Md_.width)]);

			//
			// Pack the indices of all the meshes into one index buffer.
			//

			std::vector<UINT> indices;
			indices.insert(indices.end(), box.Indices.begin(), box.Indices.end());

			D3D11_BUFFER_DESC ibd;
			ibd.Usage = D3D11_USAGE_IMMUTABLE;
			ibd.ByteWidth = sizeof(UINT) * totalIndexCount;
			ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
			ibd.CPUAccessFlags = 0;
			ibd.MiscFlags = 0;
			D3D11_SUBRESOURCE_DATA iinitData;
			iinitData.pSysMem = &indices[0];
			pDev_->CreateBuffer(&ibd, &iinitData, &mBoxIB[x + (y * Md_.width)]);
		}
	}
}