[C#] DirectX Vertex-Manipulation

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von razorblade, 3. Juni 2010 .

Schlagworte:
Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 3. Juni 2010
    DirectX Vertex-Manipulation

    Hi Leute,

    ich muss für ein Projekt eine Wellensimulation programmieren.

    Diese soll grafisch dargestellt werden.

    Mein erster Ansatz war mit einem SolidBrush "Pixel" zu malen und diese nach jedem rechenschritt neu einzufärben. Das funktioniert im Prinzip auch ist nur so langsam, dass es verboten gehört.

    Nun hab ich einen neuen Ansatz das ganze mit DirectX umzusetzen... habe ein Tut gefunden in dem ein rotierendes Dreieck erstellt wird... wie die Farbe gesetzt wird weiß ich prinzipiell auch. Nur wie ändere ich die Farbe während das ganze angezeigt wird.

    Hier der bisherige Code:

    Spoiler
    Code:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    
    public class Form1 : Form
    {
     [STAThread]
     static void Main() { Application.Run(new Form1()); }
     static Device device = null;
     static float fAngle;
     VertexBuffer vertexBuffer;
     int rd = 0;
     int gre = 200;
     static int blu = 50;
     static CustomVertex.PositionColored[] v = new CustomVertex.PositionColored[3];
     Timer myTimer = new Timer();
     public Form1()
     {
     Text = "D3DTriangleAnimation";
     //fill coordinates and colors into an array "v"
     v[0].X = -1f; v[0].Y = -1f; v[0].Z = 0f;
     v[1].X = 1f; v[1].Y = -1f; v[1].Z = 0f;
     v[2].X = 0f; v[2].Y = 1f; v[2].Z = 0f;
     //v[3].X = 1f; v[3].Y = 1f; v[3].Z = 0f;
     v[0].Color = System.Drawing.Color.FromArgb(100,rd,gre,blu).ToArgb();
     v[1].Color = System.Drawing.Color.Magenta.ToArgb();
     v[2].Color = System.Drawing.Color.Yellow.ToArgb();
     //v[3].Color = System.Drawing.Color.Green.ToArgb();
     myTimer.Tick += new EventHandler(OnTimer);
     myTimer.Interval = 1;
     ClientSize = new Size(400, 300); //Calls OnResize( ... )
     }
    
     protected override void OnResize(System.EventArgs e)
     //Whenever the window changes we have to initialize Direct3D from scratch
     {
     myTimer.Stop();// stop the timer during initialization
     try
     { //get information from the operating system about its current graphics properties
     PresentParameters presentParams = new PresentParameters();
     //we have to set two extra flags
     presentParams.Windowed = true; //no full screen display
     presentParams.SwapEffect = SwapEffect.Discard; //no swap buffer
     //create a new D3D-device that serves as canvas
     if (device != null) device.Dispose(); //free the old canvas if any
     device = new Device(0, DeviceType.Hardware, this,
     CreateFlags.SoftwareVertexProcessing, presentParams);
     //set up the transformation of world coordinates into camera or view space
     device.Transform.View = Matrix.LookAtLH(
     new Vector3(0f, 0f, -4f), //eye point 4.0 in front of the canvas
     new Vector3(0f, 0f, 0f), //camera looks at point 0,0,0
     new Vector3(0f, 1f, 0f)); //world's up direction is the y-axis
     //set up the projection transformation using 4 parameters:
     //1.: field of view = 45 degrees; 2.: aspect ratio = width / height = 1 = square window;
     //3.: near clipping distance = 0; 4.: far clipping distance = 10;
     device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1f, 0f, 10f);
     //Turn off culling, so the user sees the front and back of the triangle
     device.RenderState.CullMode = Cull.None;
     //Turn off lighting, since the triangle provides its own colors
     device.RenderState.Lighting = false;
     //set up the property that fills the triangle with colors
     device.VertexFormat = CustomVertex.PositionColored.Format;
    
     if (vertexBuffer != null) vertexBuffer.Dispose();//Free the old vertexBuffer if any.
     //Create a new vertex buffer on the graphics card and connect it to the device.
     vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3,
     device, Usage.WriteOnly, CustomVertex.PositionColored.Format,
     Pool.Default);
     vertexBuffer.SetData(v, 0, LockFlags.None);//Copy the vertices from main memory to graphics card memory.
     device.SetStreamSource(0, vertexBuffer, 0);//Tell the device to use the vertexBuffer on the graphics card.
     myTimer.Start();//start the timer again
     }
     catch (DirectXException) { MessageBox.Show("Could not initialize Direct3D."); return; }
     }
    
     protected static void OnTimer(Object myObject, EventArgs myEventArgs)
     {
     if (device == null) return;
     //throw the old image away
     device.Clear(ClearFlags.Target, Color.LightSteelBlue, 1f, 0);
     //rotate with an angular velocity = 0.1
     fAngle += 0.1f;
     blu += 1;
     // device.Transform.World = Matrix.RotationX(fAngle); 
     device.Transform.World = Matrix.RotationY(fAngle);
     //draw on the canvas
     device.BeginScene();
     device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
     device.EndScene();
     device.Present(); // show the canvas
     }
    }
    

    Greetz und Danke im Voraus
    razorblade
     
  2. 4. Juni 2010
    AW: DirectX Vertex-Manipulation

    Du kannst die Farbe der Vertices ändern wann immer du willst. Du musst nur daran denken, die neuen Daten auch in den VertexBuffer zu schreiben (-> vertexBuffer.SetData(v, 0, LockFlags.None); ).

    Aber so kannst du auch nicht komfortabel auf Pixelebene malen. Dazu nimmt man für gewöhnlich ein Fullscreen-Quad, dessen Textur man fortlaufend ändert.

    Warum überhaupt Managed DirectX? Das geht viel einfacher mit XNA...

    mfg r90
     
  3. 4. Juni 2010
    AW: DirectX Vertex-Manipulation

    :angry: so was dachte ich mir schon *kopf@tisch*
    das mit dem fullscreen quad stell ich mir schwer vor, da ich ja keine textur hab sondern die farbe der pixel anhand der amplitude in der berechnungsmatrix ändern muss.

    Managed DirectX weil ich bisher noch nie was von XNA gehört hab

    BW haste thx
     
  4. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.