Sign in to follow this  
martinperry

SlimDX - VertexDeclaration problem

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 useless

public 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

Share this post


Link to post
Share on other sites
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; }
}

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