Sign in to follow this  
andi_bold2

Problem using a non-FVF vertexbuffer

Recommended Posts

andi_bold2    146
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.
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();
}

Hoping for some help or hints in the right direction...

Share this post


Link to post
Share on other sites
andi_bold2    146
Ok, solved it. I told you it probably was a trivial thing, and it was. (Problem is those simple things are the hardest ones to find [smile]). I had to specify the stride in setstreamsource... Well now my triangle is just where it should be and I just hope it was the only problem so I can move on with my real engine.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this