Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42334

[VB.NET] Help with DirectX9 scaling and translating

$
0
0
Hi all,
I posted this in vb.net a few weeks ago, and although it is vb.net code, I don't think it was the appropriate forum, so I am reposting here. If this is not the right way to bump a thread to a different forum, my apologies. Mod feel free to move it if needed. The original thread is here


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 am not trying to scale the raw data. Instead I what to transform the vertices after they are in the buffer.

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....


VB Code:
  1. Private Sub _Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         Initialize()
  3.     End Sub
  4.  
  5.     Private Sub Initialize()
  6.         Dim params As New PresentParameters
  7.  
  8.         params.Windowed = True
  9.         params.SwapEffect = SwapEffect.Discard
  10.  
  11.         device = New Device(0, DeviceType.Hardware, Me, CreateFlags.HardwareVertexProcessing, params)
  12.         Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
  13.  
  14.     End Sub
  15.  
  16.     Private Sub SetupCamera()
  17.         With device
  18.             .Transform.Projection = Matrix.PerspectiveFovRH(Math.PI / 4, CSng(Me.Size.Width / Me.Size.Height), 1, 100)
  19.             .Transform.View = Matrix.LookAtRH(New Vector3(0.0F, 0.0F, 18.0F), New Vector3(), New Vector3(0, 1, 0))
  20.             .RenderState.Lighting = False
  21.         End With
  22.     End Sub
  23.  
  24.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  25.  
  26.         Try
  27.             'create the new buffer from the points array
  28.             Dim buffer As New VertexBuffer(GetType(CustomVertex.PositionColored), vList.Count, device, Usage.Dynamic And Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default)
  29.             buffer.SetData(vList.ToArray, 0, LockFlags.None)
  30.             With device
  31.  
  32.                 .Clear(ClearFlags.Target, Color.Black, 1, 0)
  33.                 SetupCamera()
  34.                 .BeginScene()
  35.  
  36.                 .VertexFormat = CustomVertex.PositionColored.Format
  37.  
  38.                 .SetStreamSource(0, buffer, 0)
  39.  
  40.                 Dim m As Matrix = Matrix.Scaling(xScale, yScale, 0) ' xScale,yScale, xTrans and yTrans are pulic properies of the control
  41.                 Dim mT As Matrix = Matrix.Translation(xTrans, yTrans, 0)
  42.                 .Transform.World = m * mT
  43.  
  44.                 .DrawPrimitives(PrimitiveType.LineStrip, 0, vList.Count - 1) ' vList is a public array of CustomVertex.PositionColored that holds the data
  45.  
  46.                 .EndScene()
  47.                 .Present()
  48.  
  49.             End With
  50.         Catch ex As Exception
  51.  
  52.         End Try
  53.  
  54.     End Sub
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

Viewing all articles
Browse latest Browse all 42334

Trending Articles