Hi all,
I am building a graphing control using directX. The back story is that I will eventually have an app that will show up to a dozen or so charts in real time and each will plot 1000's of points. GDI+ simply does not work work fast enough to achieve this even when I use only 2-3 charts. I have looked at using heuristics to limit the number of points, but I would rather show all of the data if I can do it using the GPU.
I am able to use directX to draw lines and can scale and translate ok, but I can't figure out the math behind it so don't know how much to scale nor how far to translate each data set. The first data point in each data set to be graphed will be (0,0) and it will be located in the lower left corner of the control, and I know the range of the X and Y values.
I think the amount to scale and translate depend on the view and projection that I have established, but have yet to figure it out.
Here is the code that I have pieced together so far....
If anyone knows how to do this, or has a better method, please let me know.
I am also not using the z-axis yet. I may in the future though.
thanks
KEvin
I am building a graphing control using directX. The back story is that I will eventually have an app that will show up to a dozen or so charts in real time and each will plot 1000's of points. GDI+ simply does not work work fast enough to achieve this even when I use only 2-3 charts. I have looked at using heuristics to limit the number of points, but I would rather show all of the data if I can do it using the GPU.
I am able to use directX to draw lines and can scale and translate ok, but I can't figure out the math behind it so don't know how much to scale nor how far to translate each data set. The first data point in each data set to be graphed will be (0,0) and it will be located in the lower left corner of the control, and I know the range of the X and Y values.
I think the amount to scale and translate depend on the view and projection that I have established, but have yet to figure it out.
Here is the code that I have pieced together so far....
Code:
Private Sub _Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Initialize()
End Sub
Private Sub Initialize()
Dim params As New PresentParameters
params.Windowed = True
params.SwapEffect = SwapEffect.Discard
device = New Device(0, DeviceType.Hardware, Me, CreateFlags.HardwareVertexProcessing, params)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
End Sub
Private Sub SetupCamera()
With device
.Transform.Projection = Matrix.PerspectiveFovRH(Math.PI / 4, CSng(Me.Size.Width / Me.Size.Height), 1, 100)
.Transform.View = Matrix.LookAtRH(New Vector3(0.0F, 0.0F, 18.0F), New Vector3(), New Vector3(0, 1, 0))
.RenderState.Lighting = False
End With
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Try
'create the new buffer from the points array
Dim buffer As New VertexBuffer(GetType(CustomVertex.PositionColored), vList.Count, device, Usage.Dynamic And Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default)
buffer.SetData(vList.ToArray, 0, LockFlags.None)
With device
.Clear(ClearFlags.Target, Color.Black, 1, 0)
SetupCamera()
.BeginScene()
.VertexFormat = CustomVertex.PositionColored.Format
.SetStreamSource(0, buffer, 0)
Dim m As Matrix = Matrix.Scaling(xScale, yScale, 0) ' xScale,yScale, xTrans and yTrans are pulic properies of the control
Dim mT As Matrix = Matrix.Translation(xTrans, yTrans, 0)
.Transform.World = m * mT
.DrawPrimitives(PrimitiveType.LineStrip, 0, vList.Count - 1) ' vList is a public array of CustomVertex.PositionColored that holds the data
.EndScene()
.Present()
End With
Catch ex As Exception
End Try
End Sub
I am also not using the z-axis yet. I may in the future though.
thanks
KEvin