VertexDeclaration and FVF

Started by
6 comments, last by vidalsasoon 18 years, 7 months ago
CODE THAT WORKS WITH FVF:


Render()...

dev.SetRenderTarget(0,_backBuffer); 

dev.SetTexture(0,_renderTexture);
dev.SetStreamSource(0, vertBuffer, 0);
dev.VertexFormat = CustomVertex.TransformedTextured.Format;
dev.Indices = ib;		    dev.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);


CODE THAT I WOULD LIKE TO WORK WITH VERTEXDECLARATION!:


Constructor()...

VertexElement[] elements = new VertexElement[]
{	
	new VertexElement(0, 0, DeclarationType.Float4,
	DeclarationMethod.Default,
	DeclarationUsage.Position, 0),
			                            
	new VertexElement(0, 16, DeclarationType.Float2,
	DeclarationMethod.Default,
	DeclarationUsage.TextureCoordinate, 0),		                    
            
	VertexElement.VertexDeclarationEnd 
};
decl = new VertexDeclaration(dev, elements);

Render()...

dev.SetRenderTarget(0,_backBuffer); 
dev.SetTexture(0,_renderTexture);
dev.SetStreamSource(0, vertBuffer, 0);
//dev.VertexFormat = CustomVertex.TransformedTextured.Format;
dev.VertexDeclaration = decl;
dev.Indices = ib;		    dev.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);



Vertex Buffer

vertBuffer = new VertexBuffer(typeof(PositionTexVertex), 4, dev, Usage.None, PositionTexVertex.FVF, Pool.Managed);

			PositionTexVertex[] vbData = (PositionTexVertex[]) vertBuffer.Lock(0, typeof(PositionTexVertex), LockFlags.None, 4);

			vbData[0].Position = new Vector4(0, dev.Viewport.Height, 0.0f, 1);  
			vbData[0].UV = new Vector2(0,1);

			vbData[1].Position = new Vector4(0, 0, 0.0f, 1);  
			vbData[1].UV = new Vector2(0,0);

			vbData[2].Position = new Vector4(dev.Viewport.Width, 0, 0.0f, 1);  
			vbData[2].UV = new Vector2(1,0);

			vbData[3].Position = new Vector4(dev.Viewport.Width, dev.Viewport.Height, 0.0f, 1);  
			vbData[3].UV = new Vector2(1,1);


			//Unlock the vb before you can use it elsewhere
			vertBuffer.Unlock();


PositionTexVertex struct

	public struct PositionTexVertex
	{
		public Vector4 Position;
		public Vector2 UV;
		//public static readonly VertexFormats FVF = VertexFormats.Position | VertexFormats.Texture0;
		public static readonly VertexFormats FVF = CustomVertex.TransformedTextured.Format;
	}


Basically, CustomVertex.TransformedTextured.Format works but when trying to switch over to a vertexdeclaration, information is no longer synchronized? Should both VertexDeclaration and FVF be used at the same time? Thanks, V.
Advertisement
Hi there VidalSasoon,
How are you doing?

The Problem
Your own custom vertex format... not working like it should.

The Solution
Buddy, this tutorial by DrunkenHyena is fantastic.

1) You have to give direct3d a format to work with. It has to know how your
vertex buffer is formatted to be able to render from it
2) The vertex format would be
e.g.
public static readonly D3D.VertexFormats Format =
Microsoft.DirectX.Direct3D.VertexFormats.Position | Microsoft.DirectX.Direct3D.VertexFormats.Diffuse;

I hope this helps buddy.
Take care.
Quote:Original post by vidalsasoon
Should both VertexDeclaration and FVF be used at the same time?

You shouldn't need both of them.

Is the debug runtime telling you anything? I find that it's pretty good at complaining about FVF/VertDecl mis-matches [smile]

I can't see anything else that's obviously wrong with your code... Make sure you run with a software device and the debug runtimes cranked up!

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Typically you would only change the vertex declaration if you are using a programmable shader. Is this what you meant to do? If you change it, then you must set your FVF so that the pipeline knows which elements you are using, so yes you must uncomment your dev.VertexFormat = CustomVertex.TransformedTextured.Format; line. I think this is correct anyway...haven't messed with decls in a while.
Chris ByersMicrosoft DirectX MVP - 2005

thanks for replies but i'm still confused.


is the following equivalent?

CustomVertex.TransformedTexture.Format == VertexFormats.Transformed | VertexFormats.Texture1

I think they are so i'll assume yes.

Setting my vertex declaration always crashes app when trying at DrawPrimitive()..


dev.VertexDeclaration = decl;

Now from what I can tell TransformedTexture is a Vector4 followed by a Verctor2?

I don't know what's wrong :(

...

VertexElement[] elements = new VertexElement[]
{
new VertexElement(0, 0, DeclarationType.Float4,
DeclarationMethod.Default,
DeclarationUsage.Position, 0),

new VertexElement(0, 16, DeclarationType.Float2,
DeclarationMethod.Default,
DeclarationUsage.TextureCoordinate, 0),

VertexElement.VertexDeclarationEnd
};

decl = new VertexDeclaration(dev, elements);
Hi there. Your problem is with your declaration.

VertexElement[] elements = new VertexElement[]
{
new VertexElement(0, 0, DeclarationType.Float4,
DeclarationMethod.Default,
DeclarationUsage.PositionTransformed, 0),

new VertexElement(0, 16, DeclarationType.Float2,
DeclarationMethod.Default,
DeclarationUsage.TextureCoordinate, 0),

VertexElement.VertexDeclarationEnd
};


DeclarationUsage.Position should be changed to DeclarationUsage.PositionTransformed
Anyway, you do not need to set your VertexFormat if you are using VertexDeclaration.
EUREKA!

btw, I will need the vertex declaration for some shader stuff in the near future.

Thanks,

V.

This topic is closed to new replies.

Advertisement