SlimDX - VertexDeclaration problem

Started by
7 comments, last by martinperry 14 years, 2 months ago
I tried to create VertexDeclaration with color If I use this:

Color4 color;

...
new VertexElement(0, 36, DeclarationType.Float4,
DelarationMethod.Default,
DeclarationUsage.Color, 0)
...
It worked for shader, but without shader my app crash: Direct3D9: Decl Validator: X254: (Element Error) (Decl Element [2]) Declaration can't map to fixed function FVF because color0/color1 cannot have type other than D3DDECLTYPE_D3DCOLOR But If I use this:

int color;

...
new VertexElement(0, 36, DeclarationType.Color,
DelarationMethod.Default,
DeclarationUsage.Color, 0)
...
It didnt crash, but colors are incorect... What is wrong ?
Advertisement
DeclarationType.Color is 4 bytes - one byte per component, which is mapped to the 0-1 range in the shader. DeclarationType.Float4 is 16 bytes - one float per component.

I'd suggest using DeclarationType.Color unless you need colours outside the 0-1 range.
I tried this

Color4 color;...new VertexElement(0, 36, DeclarationType.Color,DelarationMethod.Default,DeclarationUsage.Color, 0)...


It didnt crash neither, but I got still same red color (or better .. colors are incorrect.. i have black / red / weird blue-to-white color... no matter what i set up as color)

If I use DeclarationType.Float4, than colors are correct, but only if it goes thru my shader, otherwise crash, as i posted before.


[Edited by - martinperry on February 11, 2010 6:01:39 AM]
In order to make DeclarationType.Color work, you'll need to modify your vertex data as well as the declaration.
For VertexDeclaration.Color I have data set-up this way:

private Color4 color;public Vector3 Position;public Vector2 texture;public Vector4 additionalData; //special data needed in shader, without shader its uselesspublic static int SizeInBytes{ get { return Vector3.SizeInBytes + Vector2.SizeInBytes + Vector4.SizeInBytes + Vector4.SizeInBytes; }}public static VertexElement[] Declaration{get   {				   VertexElement[] elements = new VertexElement[]   {      new VertexElement(0, 0, DeclarationType.Float3,	DeclarationMethod.Default,	DeclarationUsage.Position, 0),				        	new VertexElement(0, 12, DeclarationType.Float2,	DeclarationMethod.Default,	DeclarationUsage.TextureCoordinate, 0),	new VertexElement(0, 20, DeclarationType.Float4,	DeclarationMethod.Default,	DeclarationUsage.TextureCoordinate, 1),	new VertexElement(0, 36, DeclarationType.Color, //with Float4 ok colors in shader, but crash without shader	DeclarationMethod.Default,	DeclarationUsage.Color, 0),	VertexElement.VertexDeclarationEnd};return elements; }}public int Color{get { return this.color.ToArgb(); }set { this.color = new Color4(System.Drawing.Color.FromArgb(value)); }}


Shader work, without shader crash
You need to store your color in the vertex structure as a packed int, not a Color4 for it to work with the fixed function pipeline (ie. without a shader).

Change the following:
private Color4 color;


To:
private int color;


And:
public int Color{get { return this.color.ToArgb(); }set { this.color = new Color4(System.Drawing.Color.FromArgb(value)); }}


To:
public int Color{get { return this.color; }set { this.color = value; }}
Ah.. I can see now... I thought, that I can do it in one piece. Shader and non-Shader version. I will made 2 VertexDeclarations than.

thanks :)
The same declaration should work when using a shader and without one.
Of course... you are right... I forgot change Vertex Size from Vector4.SizeInBytes to sizeof(int), so i had bad size for vertex.

This topic is closed to new replies.

Advertisement