PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Direct3D simple polygone erzeugen


Odal
2004-04-21, 21:53:07
ok moechte in der unteren testapplikation möglichst einfach belibige polygone (konvex/konkav) einbauen (keine mit 3Dstudio erzeugten meshes) wie, wo und was :)
testaplikation stellt bisher nur ein dreieck dar (bei bedarf dank triangleFan auch ein viereck....
die polygoninformation is einfach ein punktearray...ich wollte eigentlich nicht eine extra triangulationsfunktion schreiben um die polygone in dreiecke zu zerlegen


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MyDirect3DProject
{
/// <summary>
/// This is the main class of my Direct3D application
/// </summary>
public class MainClass : Form
{
private Device device ;
private VertexBuffer vertices;
static void Main()
{
MainClass app = new MainClass();
app.InitializeGraphics();
app.Show();

while (app.Created)
{
app.Render();
Application.DoEvents();
}
app.DisposeGraphics();
}

protected bool InitializeGraphics ()
{
PresentParameters pres = new PresentParameters ();
pres.Windowed = true ;
pres.SwapEffect = SwapEffect.Discard ;
pres.EnableAutoDepthStencil = true ;
pres.AutoDepthStencilFormat = DepthFormat.D16;
device = new Device(0,
DeviceType.Hardware , this ,
CreateFlags.SoftwareVertexProcessing , pres);
vertices = CreateVertexBuffer (device);
device.RenderState.Lighting = false ;
return true ;
}

protected void Render()
{
// Clear the back buffer
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0F, 0);

// Ready Direct3D to begin drawing
device.BeginScene();

SetupMatrices();
device.SetStreamSource(0, vertices, 0);
device.RenderState.CullMode = Cull.None;
device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 1);

// Indicate to Direct3D that we’re done drawing
device.EndScene();

// Copy the back buffer to the display
device.Present();
}

private void DisposeGraphics()
{
device . Dispose ();
}

protected void SetupMatrices()
{
float angle = Environment.TickCount / 500.0F;
device.Transform.World = Matrix.RotationY(angle);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0.5F, -3),
new Vector3(0, 0.5F, 0), new Vector3(0, 1, 0));
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4.0F, 1.0F, 1.0F, 10.0F);

}

protected VertexBuffer CreateVertexBuffer (Device device )
{
device.VertexFormat = CustomVertex.PositionColored.Format;
int i = 0;

VertexBuffer buf = new VertexBuffer( typeof( CustomVertex.PositionColored ),
4 , device, 0,
CustomVertex.PositionColored.Format ,
Pool.Default
);

CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[]) buf.Lock(0,0);

verts[i++] = new CustomVertex.PositionColored(0, 1, 0, Color.Blue.ToArgb ());
verts[i++] = new CustomVertex.PositionColored(-0.5F, 0, 0, Color.Yellow.ToArgb ());
verts[i++] = new CustomVertex.PositionColored(0.5F, 0, 0, Color.Red.ToArgb ());
verts[i++] = new CustomVertex.PositionColored(-1, 1, 1, Color.Pink.ToArgb());


//GraphicsStream stm = buf.Lock (0, 0, 0);
//stm.Write ( verts );
buf.Unlock ();
return buf ;
}
}
}