[MDX] Making a VertexFormat...

Started by
0 comments, last by remigius 17 years, 5 months ago
I'm trying to use a vertex format which has Position, Color, Normal, and Texture coordinates. Unfortunately, the CustomVertex namespace doesn't have anything with all of those. So I tried this:

[StructLayout(LayoutKind.Sequential)]
    public struct PositionColorNormalTexture
    {
        public float X, Y, Z;
        public int Color;
        public float Tu, Tv;
        public float nx, ny, nz;

        // whoops!  Position wasn't specified.
        public static VertexFormats Format =
            VertexFormats.Normal | VertexFormats.Diffuse | VertexFormats.Texture1 | VertexFormats.Normal;

        public PositionColorNormalTexture(float x, float y, float z, int color, float tu, float tv,
            float nx, float ny, float nz)
        {
            X = x;
            Y = y;
            Z = z;
            Color = color;
            Tu = tu;
            Tv = tv;
            this.nx = nx;
            this.ny = ny;
            this.nz = nz;
        }
    }

In the above, the VertexFormats flags didn't have VertexFormats.Position set. Using this rendered fine, but the lighting didn't work right. Vertices would go from suddenly being lit to being completely dark as the light moved slowly towards them. (Yes, the light's range parameter is very large, so it's not a moving in/out of range problem.) So then I went to fix the lacking Position attribute, with this format:

[StructLayout(LayoutKind.Sequential)]
    public struct PositionColorNormalTexture
    {
        public float X, Y, Z;
        public int Color;
        public float Tu, Tv;
        public float nx, ny, nz;

        public static VertexFormats Format =
            VertexFormats.Position | VertexFormats.Diffuse | VertexFormats.Texture1 | VertexFormats.Normal;

        public PositionColorNormalTexture(float x, float y, float z, int color, float tu, float tv,
            float nx, float ny, float nz)
        {
            X = x;
            Y = y;
            Z = z;
            Color = color;
            Tu = tu;
            Tv = tv;
            this.nx = nx;
            this.ny = ny;
            this.nz = nz;
        }
    }

I replaced the extra "Normal" value with VertexFormats.Position. Now nothing renders. (I alsu used VertexFormats.PositionNormal, and that did the same thing). So.. any ideas on what is wrong?
Advertisement
Flexible vertex formats are a bit outdated. By using a VertexDeclaration, you can specify exactly which data your vertex contains and how it's layed out in the struct. For more information, check this how-to on MSDN. For you vertex, the declaration should look like this (untested!):

VertexElement[] elements = new VertexElement[]{    new VertexElement(0, 0, DeclarationType.Float3,                            DeclarationMethod.Default,                            DeclarationUsage.Position, 0),    new VertexElement(0, 12, DeclarationType.Color,                            DeclarationMethod.Default,                            DeclarationUsage.Color, 0),                                new VertexElement(0, 16, DeclarationType.Float2,                             DeclarationMethod.Default,                             DeclarationUsage.TextureCoordinate, 0),                                                                                        new VertexElement(0, 28, DeclarationType.Float3,                             DeclarationMethod.Default,                             DeclarationUsage.Normal, 0),    VertexElement.VertexDeclarationEnd };VertexDeclaration decl = new VertexDeclaration(device, elements);


The vertex declaration is an alternative to the VertexFormat. You no longer need to set the device.VertexFormat property if you're setting a declaration with device.VertexDeclaration. One thing to note is that the declaration is device dependent, meaning you need to call Dispose() on it when the device is lost and recreate it when the device is reset.

Hope this helps :)
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!

This topic is closed to new replies.

Advertisement