Re: Vertex Buffer not Rendering

From: dxrox (anonymous_at_discussions.microsoft.com)
Date: 05/13/04


Date: Thu, 13 May 2004 05:31:14 -0700

Still the same problem, here is the sample code:

public class MDXMain : System.Windows.Forms.Form
{
        ...
        MDXSurface skySurface = null;

        private void InitializeGraphics()
        {
                ...
                skySurface = new MDXSurface(device, @"..\..\skybox_front.jpg", true);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
                ...
                device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
                device.BeginScene();

                skySurface.DrawSurfaceP(device);

                ...
                device.EndScene();
                device.Present();
                this.Invalidate();
        }
}

public class MDXSurface : IDisposable
{
        private VertexBuffer vertices;
        private Texture texture;
        ...

        public MDXSurface(Device device, string fileName, bool position)
        {
                if(position)
                {
                        texture = CreateOverlayTextureP(device, fileName);
                        vertices = CreateVertexBufferP(device);
                }
                else
                {
                        texture = CreateOverlayTextureT(device, fileName);
                        vertices = CreateVertexBufferT(device);
                }
        }

        protected VertexBuffer CreateVertexBufferP(Device device)
        {
                VertexBuffer buf = new VertexBuffer(typeof (CustomVertex.PositionNormalTextured), 4, device, 0, CustomVertex.PositionNormalTextured.Format, Pool.Default);
                PopulateVertexBufferP(buf);
                return buf;
        }

        protected Texture CreateOverlayTextureP(Device device, string fileName)
        {
                Texture t = TextureLoader.FromFile(device, fileName);
                return t;
        }

        protected void PopulateVertexBufferP(VertexBuffer vertices)
        {
                CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[]) vertices.Lock(0, 0);

                int i = 0;
                verts[i++] = new CustomVertex.PositionNormalTextured(-5.0f, 1.0f, 30.0f, 0, 0, -1, 0, 1);
                verts[i++] = new CustomVertex.PositionNormalTextured(-5.0f, 10.0f, 30.0f, 0, 0, -1, 0, 0);
                verts[i++] = new CustomVertex.PositionNormalTextured(5.0f, 1.0f, 30.0f, 0, 0, -1, 1, 1);
                verts[i++] = new CustomVertex.PositionNormalTextured(5.0f, 10.0f, 30.0f, 0, 0, -1, 1, 0);

                vertices.Unlock();
        }

        public void DrawSurfaceP(Device device)
        {
                device.SetTexture(0, texture);
                device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
                device.SetStreamSource(0, vertices, 0);
                device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
        }
        ...
}