XNA Creators Club Online
Page 1 of 1 (15 items)
Sort Posts: Previous Next

VertexPositionNormalTexture and Color

Last post 05-15-2008 10:02 AM by David Hunt. 14 replies.
  • 04-22-2007 5:22 AM

    VertexPositionNormalTexture and Color

    Hi,

    Is there any way to use a position, normal, texture coordinate and a color in the vertex structure.

    The closest structure there is, is VertexPositionNormalTexture, but it is missing the color.

    Do I need to create a custom one?

    Thanks a lot,
    Grant
  • 04-22-2007 7:16 AM In reply to

    Re: VertexPositionNormalTexture and Color

    Answer

    Yup, I'm afraid you'll need a custom one:

        public struct VertexPositionNormalTextureColor
    {
    public Vector3 position;
    public Vector3 normal;
    public Vector2 texcoord;
    public Color color;

    public VertexPositionNormalTextureColor(Vector3 position, Vector3 normal, Vector2 texcoord, Color color)
    {
    this.position = position;
    this.normal = normal;
    this.texcoord = texcoord;
    this.color = color;
    }

    public static VertexElement[] VertexElements =
    {
    new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0),
    new VertexElement(0, 3*sizeof(float), VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0),
    new VertexElement(0, 6*sizeof(float), VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0),
    new VertexElement(0, 8*sizeof(float), VertexElementFormat.Color, VertexElementMethod.Default, VertexElementUsage.Color, 0)
    };

    public static int SizeInBytes = 9 * sizeof(float);
    }
    Cheers,

        Dave J

     

    http://www.smudgedcat.com
  • 04-23-2007 2:17 AM In reply to

    Re: VertexPositionNormalTexture and Color

    Awesome! Thanks a lot.
  • 05-13-2008 1:03 AM In reply to

    Re: VertexPositionNormalTexture and Color

    Currently, there are four predefined vertex formats, but I'm curious why VertexPositionNormalColor was left out of the framework, while VertexPositionColorTexture was included.

    I would have expected predefined formats for:
    1. A colored vertex, with and without a normal
    2. A textured vertex, with and without a normal

    That would give us flexible formats for solid-colored or textured primatives with lighting (normals) or without lighting (no normals).  Three of the four are indeed there, but then there's that VertexPositionColorTexture which has a color AND texture, but no normals.  What's the reasoning behind that?

    I think the VertexPositionColorTexture format should have been ditched for the VertexPositionNormalColor so we would have these four by default:

    VertexPositionColor
    VertexPositionNormalColor
    VertexPositionTexture
    VertexPositionNormalTexture
  • 05-13-2008 2:03 AM In reply to

    Re: VertexPositionNormalTexture and Color

    David Johnston:
    Yup, I'm afraid you'll need a custom one: [Code Snippet Removed] Cheers, Dave J


    Your SizeInBytes is incorrect. Color is not stored as a float internally. It's actually stored as a packed unsigned integer (uint). (Well technically then I suppose a float is generally 32 bytes as is an unsigned integer but since this isn't a set rule you might want to make that 8 * sizeof(float) + sizeof(uint)).

    What you should instead use is a Vector4 Color and then make your SizeInBytes 12 * sizeof(float);. You'll also then have to update the VertexElements, but then it gives you a much more analogous type to what your HLSL code is going to have.
  • 05-13-2008 11:57 AM In reply to

    Re: VertexPositionNormalTexture and Color

    JSpiel:
    Three of the four are indeed there, but then there's that VertexPositionColorTexture which has a color AND texture, but no normals.  What's the reasoning behind that?


    Once upon a time (pre v1 release) we had all the possible permutations of vertex data that might or might not be there, and it was just silly. Dozens of similar classes confusing everyone, cluttering up Intellisense, and bloating the framework assembly.

    We decided that trying to be comprehensive was causing more trouble than it was worth, and we should trim this down to just a handful of the most commonly used types. We made a subjective judgement as to what those most commonly used types were (knowing that this wouldn't satisfy everyone: the goal was to hit the 90% case, and leave the remaining 10% to fend for themselves and make custom vertex types) and that's what we kept.
    XNA Framework Developer - blog - homepage
  • 05-13-2008 12:15 PM In reply to

    Re: VertexPositionNormalTexture and Color

    Shawn told me a great trick for making the rest/other ones... reflector one that is close and cut & paste the whole class. Make the very small number of changes you need and compile. I think it took me les than 5 minutes.


    The ZBuffer News and information for XNA

    Please read the forum FAQs - Bug reporting
  • 05-13-2008 12:48 PM In reply to

    Re: VertexPositionNormalTexture and Color

    The ZMan:
    reflector one that is close

    "Reflector" is a verb now? ;-)

  • 05-13-2008 2:15 PM In reply to

    Re: VertexPositionNormalTexture and Color

    Besides what has been mentioned, consider adding a color variable to your shader class instead.  It of course depends upon what your doing, but if you are uniformly coloring an item, this would be the better way to go. 
  • 05-14-2008 10:12 PM In reply to

    Re: VertexPositionNormalTexture and Color

    Shawn Hargreaves:
    Once upon a time (pre v1 release) we had all the possible permutations of vertex data that might or might not be there, and it was just silly. [...] We decided that trying to be comprehensive was causing more trouble than it was worth, and we should trim this down to just a handful of the most commonly used types.

    Nice to know.  There are indeed hundreds of possibible formats.

    Yeah, "reflector" is now a verb :)  I copy the reflector source code and modify it.  How should one implement the "SizeInBytes" field?  .NET Reflector shows that it's some kind of constant property.
    public static int modopt(IsConst) SizeInBytes
    {
    get
    {
    return sizeof(VertexPositionColor);
    }
    }
    When I try to implement that, the compiler won't let me use the "sizeof" operator in such a property (if I recall correctly, it's been a while since I tried it).  Was it actually implemented as a static readonly property, or as a constant perhaps?  I try to be consistent with the framework's implementation (hoping it's optimal), but I really don't know how this propery was done.  I do like the idea of having a property instead of a constant, since constants get baked into external assemblies that reference them, requiring recompilation if the constant changes in the source assembly.
  • 05-14-2008 11:07 PM In reply to

    Re: VertexPositionNormalTexture and Color

    sizeof works with value types, but not with reference types. As long as your custom vertex is a struct, it will be just fine.

     

  • 05-15-2008 12:45 AM In reply to

    Re: VertexPositionNormalTexture and Color

    Making it a struct is not enough.  The error is "....VertexFormats.VertexPositionColorNormal' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)".

    The structure is tagged with [Serializable, StructLayout(LayoutKind.Sequential)].  At first I thought specifying the size would help (which would seem to defeat the purpose of the sizeof operator), but changing the tag to [Serializable, StructLayout(LayoutKind.Sequential, Size=28)] does not help, nor does removing the tag altogether.  I tried changing the field to non-static, and that doesn't help either.  The same error always arises.

    This is the code that gives the compilation error:
      [Serializable, StructLayout(LayoutKind.Sequential)]
      public struct VertexPositionColorNormal
      {
        public Vector3    Position;
        public Color     Color;
        public Vector3    Normal;

        public static readonly <