Jump to content
  • Advertisement
Sign in to follow this  
kanato

[MDX] Making a VertexFormat...

This topic is 4236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!