Original Post
Hi, I want to use a non-FVF (FVF=0) vertexbuffer. I tried this code, modified from a book example and it works just fine when creating an FVF buffer (the commented VB creation). But as soon as I try to change it to a non-FVF buffer it just renders "nothing without errors". In the beginning I was using a shader effect but I simplified the code to use fixed function and it still doesn't work. I know it should work in principle because I know that others have used non-FVF buffers both with the fixed function and the programmable pipeline. The problem is just that I can't get it to work. Am I missing something trivial? As I understand the vertexdeclaration substitutes the setvertexformat (SetFVF in c++) call. Here's the code. It's in C# and uses DX9. Hoping for some help or hints in the right direction...
private VertexBuffer vb = null;
private VertexDeclaration decl = null;
private Device device = null;
// Our matrices
private Matrix worldMatrix;
private Matrix viewMatrix;
private Matrix projMatrix;
private float angle = 0.0f;
public void InitializeGraphics()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
// Create our vertex data
/*vb = new VertexBuffer(typeof(CustomVertex.PositionOnly), 3, device,
Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionOnly.Format,
Pool.Default);*/
vb = new VertexBuffer(device, 3*12, Usage.Dynamic | Usage.WriteOnly, 0, Pool.Default);
vb.Created += new EventHandler(this.OnVertexBufferCreate);
OnVertexBufferCreate(vb, null);
projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4,
this.Width / this.Height, 1.0f, 100.0f);
viewMatrix = Matrix.LookAtLH(new Vector3(0,0, 5.0f), new Vector3(),
new Vector3(0,1,0));
VertexElement[] elements = new VertexElement[]
{
new VertexElement(0, 0, DeclarationType.Float3,
DeclarationMethod.Default,
DeclarationUsage.Position, 0),
VertexElement.VertexDeclarationEnd
};
decl = new VertexDeclaration(device, elements);
}
private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionOnly[] verts = new CustomVertex.PositionOnly[3];
verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
buffer.SetData(verts, 0, LockFlags.None);
}
private void UpdateWorld()
{
worldMatrix = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f),
angle / ((float)Math.PI * 4.0f), angle / ((float)Math.PI * 6.0f)),
angle / (float)Math.PI);
angle += 0.1f;
device.Transform.View = viewMatrix;
device.Transform.Projection = projMatrix;
device.Transform.World = worldMatrix;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);
UpdateWorld();
device.BeginScene();
device.SetStreamSource(0, vb, 0);
device.VertexDeclaration = decl;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
device.EndScene();
device.Present();
this.Invalidate();
}