Example #1
0
		protected override void OnDirectXRender()
		{
			var device = Device;

			var points = animatedDataSource.GetPoints().ToArray();
			var pointList = new VertexPosition4Color[points.Length];

			for (int i = 0; i < points.Length; i++)
			{
				pointList[i] = new VertexPosition4Color
				{
					Position = new Vector4(100 + 500 * (float)points[i].X, 500 + 500 * (float)points[i].Y, 0.5f,1 ),
					Color = Color.Orange.ToArgb()
				};
			}

			var lineListIndices = new short[(points.Length * 2) - 2];

			// Populate the array with references to indices in the vertex buffer
			for (int i = 0; i < points.Length - 1; i++)
			{
				lineListIndices[i * 2] = (short)(i);
				lineListIndices[(i * 2) + 1] = (short)(i + 1);
			}

			Device.SetTransform(TransformState.World, Matrix.Translation(100, 0, 0));
			Device.SetTransform(TransformState.View, camera.ViewMatrix);
			Device.SetTransform(TransformState.Projection, camera.ProjectionMatrix);

			device.SetRenderState(SlimDX.Direct3D9.RenderState.AntialiasedLineEnable, true);
			device.VertexFormat = VertexFormat.Diffuse | VertexFormat.PositionRhw;
			device.DrawIndexedUserPrimitives<short, VertexPosition4Color>(PrimitiveType.LineList, 0, points.Length, points.Length - 1, lineListIndices, Format.Index16, pointList, VertexPosition4Color.SizeInBytes);
		}
		private void FillVertexBuffer()
		{
			if (DxHost == null) return;
			if (DataSource == null) return;
			if (Palette == null) return;
			if (Device == null) return;

			var dataSource = DataSource;
			var palette = Palette;
			var minMax = dataSource.GetMinMax();
			transform = Plotter.Transform;

			vertexCount = DataSource.Width * DataSource.Height;

			verticesArray = new VertexPosition4Color[vertexCount];
			for (int i = 0; i < verticesArray.Length; i++)
			{
				int ix = i % DataSource.Width;
				int iy = i / DataSource.Width;
				Point point = dataSource.Grid[ix, iy];
				double data = dataSource.Data[ix, iy];

				double interpolatedData = (data - minMax.Min) / minMax.GetLength();
				var color = palette.GetColor(interpolatedData);

				var pointInScreen = point;//.DataToScreen(transform);
				var position = new Vector4((float)pointInScreen.X, (float)pointInScreen.Y, 0.5f, 1);
				verticesArray[i] = new VertexPosition4Color
				{
					Position = position,
					Color =
						//System.Windows.Media.Colors.Blue.ToArgb()
						color.ToArgb()
				};
			}

			vertexBuffer = new VertexBuffer(Device, vertexCount * VertexPosition4Color.SizeInBytes, Usage.WriteOnly, VertexFormat.Position | VertexFormat.Diffuse, Pool.Default);
			using (var stream = vertexBuffer.Lock(0, vertexCount * VertexPosition4Color.SizeInBytes, LockFlags.None))
			{
				stream.WriteRange<VertexPosition4Color>(verticesArray);
			}
			vertexBuffer.Unlock();

			indicesCount = (dataSource.Width - 1) * (dataSource.Height - 1) * 2 * 3;

			indicesArray = new int[indicesCount];
			int index = 0;
			int width = dataSource.Width;
			for (int iy = 0; iy < dataSource.Height - 1; iy++)
			{
				for (int ix = 0; ix < dataSource.Width - 1; ix++)
				{
					indicesArray[index + 0] = ix + 0 + iy * width;
					indicesArray[index + 1] = ix + 1 + iy * width;
					indicesArray[index + 2] = ix + (iy + 1) * width;

					indicesArray[index + 3] = ix + 1 + iy * width;
					indicesArray[index + 4] = ix + (iy + 1) * width;
					indicesArray[index + 5] = ix + 1 + (iy + 1) * width;

					index += 6;
				}
			}

			indexBuffer = new IndexBuffer(Device, indicesCount * sizeof(int), Usage.WriteOnly, Pool.Default, false);
			using (var stream = indexBuffer.Lock(0, indicesCount * sizeof(int), LockFlags.None))
			{
				stream.WriteRange<int>(indicesArray);
			}
			indexBuffer.Unlock();
		}