[SlimDX] Vertex format

Started by
3 comments, last by Bargaust 15 years, 3 months ago
I have a vertex format which currently only specifies the vertex Position and the Texture Coordinates. The structure is as follows:
    [StructLayout(LayoutKind.Sequential)]
    struct TestVertex
    {
        public Vector4 Position;
        public Vector2 TextureCoords;

        public static VertexFormat Format
        {
            get
            {
                return VertexFormat.Position | VertexFormat.Texture1;
            }
        }

        public static int SizeInBytes
        {
            get
            {
                return Marshal.SizeOf(typeof(TestVertex));
            }
        }
    }
I need to be able to transform the vertices, so I'm used Position instead of PositionRhw. However, when I try to render a square using this format, I just get a black square with no texture. If I change Position to PositionRhw, I get the texture showing properly, but don't get the transformation applied. Can anyone see what I'm doing wrong? Here is some of the other code if it helps: Initialising the rectangle:

            this.vertices = new VertexBuffer(device, 4 * TestVertex.SizeInBytes, Usage.WriteOnly, TestVertex.Format, Pool.Default);

            DataStream stream = vertices.Lock(0, 0, LockFlags.None);

            TestVertex[] vertexData = new TestVertex[4];

            vertexData[0].Position = new Vector4(0F, 256F, 1F, 1F);
            vertexData[0].TextureCoords = new Vector2(0F, 1F);

            vertexData[1].Position = new Vector4(0F, 0F, 1F, 1F);
            vertexData[1].TextureCoords = new Vector2(0F, 0F);

            vertexData[2].Position = new Vector4(256F, 0F, 1F, 1F);
            vertexData[2].TextureCoords = new Vector2(1F, 0F);

            vertexData[3].Position = new Vector4(256F, 256F, 1F, 1F);
            vertexData[3].TextureCoords = new Vector2(1F, 1F);

            stream.WriteRange(vertexData);

            vertices.Unlock();

Rendering the rectangle:

            this.device.SetTexture(0, texture);
            this.device.SetStreamSource(0, vertices, 0, TestVertex.SizeInBytes);
            this.device.VertexFormat = TestVertex.Format;

            this.device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);

Advertisement
When you use the VertexFormat.Position specifier, Direct3D expects your position data to come in XYZ format, not XYZW. I'd guess that it's looking for your texture coordinates four bytes early and getting the W coordinate of the position instead.
Mike Popoloski | Journal | SlimDX
I've changed my Vertex structure to:

    [StructLayout(LayoutKind.Sequential)]    struct TestVertex    {        public Vector3 Position;        public Vector2 TextureCoords;        public static VertexFormat Format        {            get            {                return VertexFormat.Position |  VertexFormat.Texture1;            }        }        public static int SizeInBytes        {            get            {                return Marshal.SizeOf(typeof(TestVertex));            }        }     }    


And... I'm still getting the same. :(


Thanks though, I'm sure that was an issue too.
Have you disabled lighting (i.e. called device.SetRenderState( RenderState.Lighting, false ); )? In Direct3D lighting is enabled by default and, as far as I remebmer is skipped if you're using transformed coordinates.
A-ha! Thanks a lot! It works perfectly now.

Thank you both. :)

This topic is closed to new replies.

Advertisement