3D-s kocka rajzolása DirectX segítségével

Eza kis program az itt bemutatott áramkör által küldött adatokat vizualizálja egy 3D-s téglatest segítségével. A rajzoláshoz DirectX-et választottam, segítségével nagyon egyszerűen megoldható az objektum forgatása.

A projekthez szükséges a directx SDK telepítése, melyet a microsoft honlapjáról lehet letölteni. A téglatestet „UserControl”-ba készítettem el, így könnyen felhasználható más projekteknél is.

Directx Inicializálás

A konstruktorba került:

private Device device;        //Directx device object
private CustomVertex.PositionNormalColored[] verts;       //Coordinátákat tároló tömb

public DXGyro()
{
      InitializeComponent(); 
      verts = CreateCube();                              //Coordinátákat megadó függvény
      PresentParameters pp = new PresentParameters();    //Directx beállítások
      pp.PresentFlag = PresentFlag.LockableBackBuffer;
      pp.Windowed = true;
      pp.SwapEffect = SwapEffect.Discard;
      pp.BackBufferWidth = this.Width;
      pp.BackBufferHeight = this.Height;
      device = new Device(0, DeviceType.Hardware, this, 
             CreateFlags.HardwareVertexProcessing, pp);  //eszköz inicializálása
 }

A koordináták megadása

Itt csak egy négyzetet mutatok be a terjedelem miatt. Minden ábrát háromszögekből (primitívekből) lehet kirajzolni. Így egy négyzethez két háromszög koordinátáit kell megadnunk:

protected CustomVertex.PositionNormalColored[] CreateCube()
{
      float X2 = 0.5f;
      float Y2 = 0.2f;
      float Z2 = 1.0f;

      CustomVertex.PositionNormalColored[] verts =
      new CustomVertex.PositionNormalColored[36];
      verts[0] = new CustomVertex.PositionNormalColored(new Vector3(-X2, Y2, Z2),
       new Vector3(0, 0, 1), _cubecolor.ToArgb());
      verts[1] = new CustomVertex.PositionNormalColored(new Vector3(-X2, -Y2, Z2),
       new Vector3(0, 0, 1), _cubecolor.ToArgb());
      verts[2] = new CustomVertex.PositionNormalColored(new Vector3(X2, Y2, Z2),
       new Vector3(0, 0, 1), _cubecolor.ToArgb());
      verts[3] = new CustomVertex.PositionNormalColored(new Vector3(-X2, -Y2, Z2),
       new Vector3(0, 0, 1), _cubecolor.ToArgb());
      verts[4] = new CustomVertex.PositionNormalColored(new Vector3(X2, -Y2, Z2),
       new Vector3(0, 0, 1), _cubecolor.ToArgb());
      verts[5] = new CustomVertex.PositionNormalColored(new Vector3(X2, Y2, Z2),
       new Vector3(0, 0, 1), _cubecolor.ToArgb());
      return verts;
}

Kamera nézőszög és megvilágítás beállítása

protected void SetupCamera()
{
     //Perspektíva és orientáció
     device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, 
        (float)this.Width / (float)this.Height, 1.0f, 100.0f);
     device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0F), new Vector3(),
         new Vector3(0, 1, 0));

     device.RenderState.Lighting = true;

     //Egyszerű pont megvilágítás
     device.Lights[0].Type = LightType.Point;
     device.Lights[0].Position = new Vector3(0, 0, 10);
     device.Lights[0].Ambient = System.Drawing.Color.White;
     device.Lights[0].Attenuation0 = 0.8F;
     device.Lights[0].Attenuation1 = 0.02F;
     device.Lights[0].Range = 1000.0F;
     device.Lights[0].Enabled = true;
}

A rajzolás

protected void DoDraw()
{
       SetupCamera();
       //Először mindíg törölni kell a képernyőt
       device.Clear(ClearFlags.Target, Color.Black, 1, 0);       
       device.BeginScene();
       device.VertexFormat = CustomVertex.PositionNormalColored.Format;
       //Forgatás
       device.Transform.World = Matrix.RotationYawPitchRoll(0, (float)_Pitch *
              (float)Math.PI / 180.0f, -(float)_Roll * (float)Math.PI / 180.0f);
       //A koordináták átadása, mivel háromszöget rajzol a koordináták harmadát kell megadni
       device.DrawUserPrimitives(PrimitiveType.TriangleList, 12, verts);
       device.EndScene();
       device.Present();
}

A komplett forráskód githubon elérhető.

Facebook Comments