[SlimDX]What is wrong with this code?

Started by
1 comment, last by Tape_Worm 15 years, 7 months ago
When I try to render these 3 vertices I get an INVALID_CALL error. What could be wrong?

using System.Windows.Forms;
using SlimDX.Direct3D9;
using SlimDX;
using System.Drawing;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace D3DExperiment
{

    [StructLayout(LayoutKind.Sequential)]
    public struct TransformedColoredVertex
    {
        public Vector4 Position;
        public SlimDX.Color4 color;
    }

    class BZWindow : Form
    {
        
        Device device;
        Direct3D d3d;
        TransformedColoredVertex[] vertices = new TransformedColoredVertex[3];

        private void CreateDevice()
        {
            
            d3d = new Direct3D ();

            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Flip;
            presentParams.BackBufferFormat = Format.A8R8G8B8;
            presentParams.BackBufferHeight = 600;
            presentParams.BackBufferWidth = 800;
            presentParams.DeviceWindowHandle = this.Handle;


            vertices[0].color = new Color4(Color.Tan.ToArgb());
            vertices[0].Position = new Vector4(new Vector3(20 , 20 , 20 ) , 1);

            vertices[1].color = new Color4(Color.Tan.ToArgb());
            vertices[1].Position = new Vector4(new Vector3(25 , 25 , 25 ) , 1);

            vertices[2].color = new Color4(Color.Tan.ToArgb());
            vertices[2].Position = new Vector4(new Vector3(345 , 200 , 200 ) , 1);

            device = new Device ( d3d , 0, DeviceType.Reference, this.Handle , CreateFlags.SoftwareVertexProcessing ,presentParams );
            
        }

        public BZWindow()
        {
        }
        

        protected override void OnPaint(PaintEventArgs e)
        {
            device.BeginScene ();
                                                                    //zdepth and stencil
            device.Clear ( ClearFlags.Target, new Color4(Color.Red) , 1 , 0 );
            device.DrawUserPrimitives<TransformedColoredVertex> ( PrimitiveType.TriangleList, 1, 3, this.vertices );

            device.EndScene ();

            
            device.Present ();
        }

        static void Main()
        {
            using (BZWindow window = new BZWindow())
            {
                window.CreateDevice ();
                Application.Run ( window );
            }
        }
    }
}


[Edited by - jdub on September 17, 2008 10:25:03 PM]
J.W.
Advertisement
It doesn't look like you've set a VertexDeclaration or VertexFormat anywhere.

Also, on a side note, your application is rather malformed. It will only draw when you get paint messages, which can come whenever Windows feels like sending them. Take a look at the MiniTri sample that comes with SlimDX to see the preferred method of rendering a simple triangle.
Mike Popoloski | Journal | SlimDX
Also, you should use the D3D debug runtimes (they come with the SDK). In this case specifically, the debug runtime would have told you that you were missing a vertex declaration/format. The error messages provided by Direct3D aren't exactly brimming with information about the cause of errors, and this is where the debug runtime can help you immensely.

This topic is closed to new replies.

Advertisement