コード例 #1
0
        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (counter++ % 2 == 0)
            {
                return;
            }

            //Debug.WriteLine(watch.ElapsedMilliseconds);

            if (sizeChanged)
            {
                pp.BackBufferWidth  = (int)ActualWidth;
                pp.BackBufferHeight = (int)ActualHeight;
                //Device.Reset(pp);
                sizeChanged = false;
            }
            Result result;

            if (d3dimage.IsFrontBufferAvailable)
            {
                result = Device.TestCooperativeLevel();
                if (result.IsFailure)
                {
                    throw new Direct3D9Exception();
                }
                d3dimage.Lock();

                Device.SetRenderState(RenderState.CullMode, Cull.None);
                Device.SetRenderState(RenderState.ZEnable, true);
                Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(1, 1, 1, 1), 1.0f, 0);
                Device.BeginScene();

                var points = animatedDataSource.GetPoints().ToArray();

                var pointList = new VertexPositionColor[points.Length];


                //phase += 0.01;
                //if (phase > 2 * Math.PI)
                //    phase -= 2 * Math.PI;
                //for (int i = 0; i < animatedX.Length; i++)
                //{
                //    animatedX[i] = 2 * Math.PI * i / animatedX.Length;

                //    if (i % 2 == 0)
                //        animatedY[i] = Math.Sin(animatedX[i] + phase);
                //    else
                //        animatedY[i] = -Math.Sin(animatedX[i] + phase);
                //}


                for (int i = 0; i < points.Length; i++)
                {
                    pointList[i] = new VertexPositionColor
                    {
                        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.SetRenderState(RenderState.AntialiasedLineEnable, true);
                device.VertexFormat = VertexFormat.Diffuse | VertexFormat.PositionRhw;
                device.DrawIndexedUserPrimitives <short, VertexPositionColor>(PrimitiveType.LineList, 0, points.Length, points.Length - 1, lineListIndices, Format.Index16, pointList, 20);
                //pointList,
                //0,  // vertex buffer offset to add to each element of the index buffer
                //8,  // number of vertices in pointList
                //lineListIndices,  // the index buffer
                //0,  // first index element to read
                //7   // number of primitives to draw
                //);


                Device.EndScene();
                Device.Present();

                d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, Device.GetBackBuffer(0, 0).ComPointer);
                d3dimage.AddDirtyRect(new Int32Rect(0, 0, d3dimage.PixelWidth, d3dimage.PixelHeight));
                d3dimage.Unlock();
            }
        }
コード例 #2
0
		private void CompositionTarget_Rendering(object sender, EventArgs e)
		{
			if (counter++ % 2 == 0) return;

			//Debug.WriteLine(watch.ElapsedMilliseconds);

			if (sizeChanged)
			{
				pp.BackBufferWidth = (int)ActualWidth;
				pp.BackBufferHeight = (int)ActualHeight;
				//Device.Reset(pp);
				sizeChanged = false;
			}
			Result result;

			if (d3dimage.IsFrontBufferAvailable)
			{
				result = Device.TestCooperativeLevel();
				if (result.IsFailure)
				{
					throw new Direct3D9Exception();
				}
				d3dimage.Lock();

				Device.SetRenderState(RenderState.CullMode, Cull.None);
				Device.SetRenderState(RenderState.ZEnable, true);
				Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(1, 1, 1, 1), 1.0f, 0);
				Device.BeginScene();

				var points = animatedDataSource.GetPoints().ToArray();

				var pointList = new VertexPositionColor[points.Length];


				//phase += 0.01;
				//if (phase > 2 * Math.PI)
				//    phase -= 2 * Math.PI;
				//for (int i = 0; i < animatedX.Length; i++)
				//{
				//    animatedX[i] = 2 * Math.PI * i / animatedX.Length;

				//    if (i % 2 == 0)
				//        animatedY[i] = Math.Sin(animatedX[i] + phase);
				//    else
				//        animatedY[i] = -Math.Sin(animatedX[i] + phase);
				//}


				for (int i = 0; i < points.Length; i++)
				{
					pointList[i] = new VertexPositionColor
					{
						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.SetRenderState(RenderState.PointSize, 10.0f);
				device.SetRenderState(RenderState.AntialiasedLineEnable, true);
				device.SetRenderState(RenderState.Clipping, false);
				device.VertexFormat = VertexFormat.Diffuse | VertexFormat.PositionRhw;
				device.DrawIndexedUserPrimitives<short, VertexPositionColor>(PrimitiveType.PointList, 0, points.Length, points.Length - 1, lineListIndices, Format.Index16, pointList, 20);

				Device.EndScene();
				Device.Present();

				d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, Device.GetBackBuffer(0, 0).ComPointer);
				d3dimage.AddDirtyRect(new Int32Rect(0, 0, d3dimage.PixelWidth, d3dimage.PixelHeight));
				d3dimage.Unlock();
			}

		}