[SharpDX] [Direct3D9] vertex array in VertexBuffer via DataStream kopieren?

  • C#
  • .NET (FX) 4.5–4.8

    [SharpDX] [Direct3D9] vertex array in VertexBuffer via DataStream kopieren?

    Hallo liebe Community!

    EDIT: Erledigt... DataStream.WriteRange existert...

    Hat jemand von euch Erfahrung mit SharpDX? Ich versuche im Moment die Direct3D9 CDraw::Circle Funktion welche ich hier gefunden habe nach C# zu porten.
    Spoiler anzeigen

    Hier der C++ Code von der Webseite

    C-Quellcode

    1. struct vertex
    2. {
    3. FLOAT x, y, z, rhw;
    4. DWORD color;
    5. };
    6. void CDraw::Circle(float x, float y, float radius, int rotate, int type, bool smoothing, int resolution, DWORD color)
    7. {
    8. std::vector<vertex> circle(resolution + 2);
    9. float angle = rotate*D3DX_PI/180;
    10. float pi;
    11. if(type == full) pi = D3DX_PI; // Full circle
    12. if(type == half) pi = D3DX_PI/2; // 1/2 circle
    13. if(type == quarter) pi = D3DX_PI/4; // 1/4 circle
    14. for(int i = 0; i < resolution + 2; i++)
    15. {
    16. circle[i].x = (float)(x - radius*cos(i*(2*pi/resolution)));
    17. circle[i].y = (float)(y - radius*sin(i*(2*pi/resolution)));
    18. circle[i].z = 0;
    19. circle[i].rhw = 1;
    20. circle[i].color = color;
    21. }
    22. // Rotate matrix
    23. int _res = resolution + 2;
    24. for(int i = 0; i < _res; i++)
    25. {
    26. circle[i].x = x + cos(angle)*(circle[i].x - x) - sin(angle)*(circle[i].y - y);
    27. circle[i].y = y + sin(angle)*(circle[i].x - x) + cos(angle)*(circle[i].y - y);
    28. }
    29. pDevice->CreateVertexBuffer((resolution + 2) * sizeof(vertex), D3DUSAGE_WRITEONLY, D3DFVF_XYZRHW|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &g_pVB, NULL);
    30. VOID* pVertices;
    31. g_pVB->Lock(0, (resolution + 2) * sizeof(vertex), (void**)&pVertices, 0);
    32. memcpy(pVertices, &circle[0], (resolution + 2) * sizeof(vertex));
    33. g_pVB->Unlock();
    34. pDevice->SetTexture(0, NULL);
    35. pDevice->SetPixelShader(NULL);
    36. if (smoothing)
    37. {
    38. pDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
    39. pDevice->SetRenderState(D3DRS_ANTIALIASEDLINEENABLE, TRUE);
    40. }
    41. pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
    42. pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    43. pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    44. pDevice->SetStreamSource(0, g_pVB, 0, sizeof(vertex));
    45. pDevice->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);
    46. pDevice->DrawPrimitive(D3DPT_LINESTRIP, 0, resolution);
    47. if(g_pVB != NULL) g_pVB->Release();
    48. }



    Nur habe ich ein Problem: Ich weiß nicht wie ich das vertex array in den SharpDX.Direct3D9.VertexBuffer kopieren kann mit C# ?(

    Hier mal der C# Code:

    C#-Quellcode

    1. public struct vertex {
    2. public float x, y, z, rhw;
    3. public int color;
    4. };
    5. public static void DrawCircle(IntPtr device, float x, float y, float radius, int rotate, int type, bool smoothing, int resolution, Color color)
    6. {
    7. vertex[] circle = new vertex[resolution + 2];
    8. circle.Initialize();
    9. float angle = rotate * (float)Math.PI / 180f;
    10. float pi = 0f;
    11. if (type == 0) pi = (float)Math.PI;
    12. if (type == 1) pi = (float)Math.PI / 2;
    13. if (type == 2) pi = (float)Math.PI / 4;
    14. for (int i = 0; i < circle.Length; i++) {
    15. circle[i].x = (float)(x - radius * Math.Cos(i * (2 * Math.PI / resolution)));
    16. circle[i].y = (float)(y - radius * Math.Sin(i * (2 * Math.PI / resolution)));
    17. circle[i].z = 0f;
    18. circle[i].rhw = 1f;
    19. circle[i].color = color.ToArgb();
    20. }
    21. // Rotate matrix
    22. int _res = resolution + 2;
    23. for (int i = 0; i < _res; i++) {
    24. circle[i].x = (float)(x + Math.Cos(angle) * (circle[i].x - x) - Math.Sin(angle) * (circle[i].y - y));
    25. circle[i].y = (float)(y + Math.Sin(angle) * (circle[i].x - x) + Math.Cos(angle) * (circle[i].y - y));
    26. }
    27. Device d = (Device)device;
    28. // VertexBuffer Source: https://github.com/sharpdx/SharpDX/blob/master/Source/SharpDX.Direct3D9/VertexBuffer.cs
    29. VertexBuffer g_pVB = new VertexBuffer(d, (resolution + 2) * Marshal.SizeOf(typeof(vertex)), Usage.WriteOnly, VertexFormat.PositionRhw | VertexFormat.Diffuse, Pool.Managed);
    30. // DataStream Source: https://github.com/sharpdx/SharpDX/blob/master/Source/SharpDX/DataStream.cs
    31. DataStream ds = g_pVB.Lock(0, (resolution + 2) * Marshal.SizeOf(typeof(vertex)), LockFlags.None);
    32. // g_pVB <- circle ?
    33. g_pVB.Unlock();
    34. d.SetStreamSource(0, g_pVB, 0, Marshal.SizeOf(typeof(vertex)));
    35. d.VertexFormat = VertexFormat.PositionRhw | VertexFormat.Diffuse;
    36. d.DrawPrimitives(PrimitiveType.LineStrip, 0, resolution);
    37. if (g_pVB != null) g_pVB.Dispose();
    38. }


    Habe leider auch kein Erfolg beim googeln gehabt für SharpDX.Direct3D9...
    Ich würde mich sehr über Hilfe von euch freuen! (Ich bin übrigens auf Direct3D9 angewiesen und kann leider nicht upgraden)
    Wenn ich dir auf irgendeiner Art und Weise helfen konnte, drück doch bitte den "Hilfreich" Button :thumbup:

    Für VB.NET Entwickler: Option Strict On nicht vergessen!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ClonkAndre“ () aus folgendem Grund: Erledigt... DataStream.WriteRange existert...