Skip to main content
GameDev.net gamedev.net
🔒 Locked

[MDX] Create a vertex buffer

Started by simotix Jul 6, 2008 at 12:11 AM 6 replies 1.9k views
Original Post
simotix
simotix
I am creating a vertex buffer with a custom format and I am not sure how to set the vertex format. Does anyone know how? Right now I am just setting " CustomVertex.PositionTextured.Format" as a temporary one. The reason I ask is because I get this error when I try to call new VertexBuffer on my VertexBuffer. An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.DirectX.dll Additional information: Operation is not valid due to the current state of the object. Vertex Buffer Creation

            D3D.VertexElement[] dvVertexElements = new D3D.VertexElement[]
            {
                new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
                new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),

                D3D.VertexElement.VertexDeclarationEnd

            };

            VertexDeclaration = new VertexDeclaration(pDevice, dvVertexElements);

            // indexed square data
            tShapeVertex[] shapeVerticies = new tShapeVertex[8];

            // Front
            shapeVerticies[0] = new tShapeVertex(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -fUOffset, -fVOffset, 0.0f, 0.0f, 0.0f, 0.0f);	// Front Bottom Left
            shapeVerticies[1] = new tShapeVertex(0.0f, fHeight, 0.0f, 0.0f, 0.0f, -1.0f, -fUOffset, fV, 0.0f, 0.0f, 0.0f, 0.0f);	// Front Upper Left
            shapeVerticies[2] = new tShapeVertex(fWidth, fHeight, 0.0f, 0.0f, 0.0f, -1.0f, fU, fV, 0.0f, 0.0f, 0.0f, 0.0f);	// Front Upper Right
            shapeVerticies[3] = new tShapeVertex(fWidth, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, fU, -fVOffset, 0.0f, 0.0f, 0.0f, 0.0f);	// Front Bottom Right

            if (bTwoSided == true)
            {
                // Back
                shapeVerticies[4] = new tShapeVertex(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -fUOffset, -fVOffset, 0.0f, 0.0f, 0.0f, 0.0f);	// Bottom Left
                shapeVerticies[5] = new tShapeVertex(fWidth, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -fU, -fVOffset, 0.0f, 0.0f, 0.0f, 0.0f);	// Bottom Right
                shapeVerticies[6] = new tShapeVertex(fWidth, fHeight, 0.0f, 0.0f, 0.0f, 1.0f, -fU, -fV, 0.0f, 0.0f, 0.0f, 0.0f);	// Upper Right
                shapeVerticies[7] = new tShapeVertex(0.0f, fHeight, 0.0f, 0.0f, 0.0f, 1.0f, -fUOffset, -fV, 0.0f, 0.0f, 0.0f, 0.0f);	// Upper Left
                VertCount = 8;
            }

            int[] indices = new int[12];
            int nIndicies = 6;

            // Front
            indices[0] = 0; indices[1] = 1; indices[2] = 2;
            indices[3] = 0; indices[4] = 2; indices[5] = 3;
            TriCount = 2;
            nIndicies = 6;

            if (bTwoSided == true)
            {
                // Back
                indices[6] = 4; indices[7] = 5; indices[8] = 6;
                indices[9] = 4; indices[10] = 6; indices[11] = 7;
                TriCount = 4;
                nIndicies = 12;
            }

            // create vertex buffer
            VertexBuffer = new VertexBuffer(typeof(tShapeVertex), 8, pDevice, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
            VertexBuffer.SetData(shapeVerticies, 0, LockFlags.None);

            // create index buffer
            IndiceBuffer = new IndexBuffer(typeof(int), nIndicies, pDevice, Usage.WriteOnly, Pool.Default);
            IndiceBuffer.SetData(indices, 0, LockFlags.None);

Custom Vertex Structure

using System;
using System.Collections.Generic;
using System.Text;

namespace WorldEditor.Engine.Shapes
{
    class tShapeVertex
    {
       // tShapeVertex() { }
        public tShapeVertex(float x, float y, float z,
            float nx, float ny, float nz, float u, float v, float t1, float t2, float t3, float t4)
        {
            _x = x; _y = y; _z = z;
            _nx = nx; _ny = ny; _nz = nz;
            _u = u; _v = v; _t1 = t1;
            _t2 = t2; _t3 = t3; _t4 = t4;
        }

        float _x, _y, _z, _nx, _ny, _nz, _u, _v, _t1, _t2, _t3, _t4;

    };

}


Mike.Popoloski
Mike.Popoloski
I had a hunch about what the problem might be, and a quick look around with Reflector confirmed it. You cannot create a vertex buffer using reference type vertices (ie classes). You need value type vertices. It's not a very descriptive error message actually.
Mike Popoloski | Journal | SlimDX
simotix
simotix
You were correct, I changed my custom vertex type to a struct it and does compile. However, what about for what I put as the VertexFormat parameter? What should really be there since I am using a custom vertex format?
Mike.Popoloski
Mike.Popoloski
Quote:
Original post by simotix
You were correct, I changed my custom vertex type to a struct it and does compile. However, what about for what I put as the VertexFormat parameter? What should really be there since I am using a custom vertex format?


You should put null since you are using vertex declarations. A vertex format is only necessary when doing FFP (fixed function pipeline) work.
Mike Popoloski | Journal | SlimDX
simotix
simotix
I am doing programmable pipeline work, however, I do not get any textures to show on my object. I know they are all loading and everything because I use to creat everything as a mesh and recently converted to vertex buffers (since it is just a smarter thing to do). I updated a few things, one of them being the vertex decleration since I noticed I was not putting in everything. Also, I could not just set VertexFormat to null, I had to set it to VertexFormat.None. Do you notice anything wrong with the current code? The shape does render, however, there is just no texture on it.

D3D.VertexElement[] dvVertexElements = new D3D.VertexElement[]            {                new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),                new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),                new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),                new VertexElement(0, 32, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Tangent, 0),                D3D.VertexElement.VertexDeclarationEnd            };            VertexDeclaration = new VertexDeclaration(pDevice, dvVertexElements);            // indexed square data            tShapeVertex[] shapeVerticies = new tShapeVertex[8];            // Front            shapeVerticies[0] = new tShapeVertex(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, -fUOffset, -fVOffset, 0.0f, 0.0f, 0.0f, 0.0f);	// Front Bottom Left            shapeVerticies[1] = new tShapeVertex(0.0f, fHeight, 0.0f, 0.0f, 0.0f, -1.0f, -fUOffset, fV, 0.0f, 0.0f, 0.0f, 0.0f);	// Front Upper Left            shapeVerticies[2] = new tShapeVertex(fWidth, fHeight, 0.0f, 0.0f, 0.0f, -1.0f, fU, fV, 0.0f, 0.0f, 0.0f, 0.0f);	// Front Upper Right            shapeVerticies[3] = new tShapeVertex(fWidth, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, fU, -fVOffset, 0.0f, 0.0f, 0.0f, 0.0f);	// Front Bottom Right            // Back            shapeVerticies[4] = new tShapeVertex(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -fUOffset, -fVOffset, 0.0f, 0.0f, 0.0f, 0.0f);	// Bottom Left            shapeVerticies[5] = new tShapeVertex(fWidth, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -fU, -fVOffset, 0.0f, 0.0f, 0.0f, 0.0f);	// Bottom Right            shapeVerticies[6] = new tShapeVertex(fWidth, fHeight, 0.0f, 0.0f, 0.0f, 1.0f, -fU, -fV, 0.0f, 0.0f, 0.0f, 0.0f);	// Upper Right            shapeVerticies[7] = new tShapeVertex(0.0f, fHeight, 0.0f, 0.0f, 0.0f, 1.0f, -fUOffset, -fV, 0.0f, 0.0f, 0.0f, 0.0f);	// Upper Left            VertCount = 8;            int[] indices = new int[12];            int nIndicies = 6;            // Front            indices[0] = 0; indices[1] = 1; indices[2] = 2;            indices[3] = 0; indices[4] = 2; indices[5] = 3;            TriCount = 2;            nIndicies = 6;            // Back            indices[6] = 4; indices[7] = 5; indices[8] = 6;            indices[9] = 4; indices[10] = 6; indices[11] = 7;            TriCount = 4;            nIndicies = 12;            // create vertex buffer            VertexBuffer = new VertexBuffer(typeof(tShapeVertex), 8, pDevice, Usage.WriteOnly, VertexFormats.None, Pool.Default);            VertexBuffer.SetData(shapeVerticies, 0, LockFlags.None);            // create index buffer            IndiceBuffer = new IndexBuffer(typeof(int), nIndicies, pDevice, Usage.WriteOnly, Pool.Default);            IndiceBuffer.SetData(indices, 0, LockFlags.None);


My actual render code looks like this, the only thing I thought I needed to set was the vertex buffer.
                ptrDevice.VertexFormat = VertexFormats.None;                ptrDevice.SetStreamSource(0, planeList.VertexBuffer, 0);                BasicTextureEffect.CommitChanges();                planeList.Render(ptrDevice);   .... Render            pDevice.Indices = IndiceBuffer;            pDevice.DrawIndexedPrimitives(PrimitiveType.TriangleFan, 0, 0, VertCount, 0, TriCount);
Mike.Popoloski
Mike.Popoloski
You don't appear to be setting the vertex declaration anywhere. If you are and just haven't shown it, then I would guess that there is something wrong with your vertex shader and/or pixel shader.
Mike Popoloski | Journal | SlimDX
Promit
Promit
Quick sidenote -- in building your vertex declaration, you should probably use Marshal.OffsetOf to ensure that the offsets you pass to D3D are correct. Probably not related to your problem, though.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
simotix
simotix
Mike.Popoloski, you were correct. For some reason I forgot to do that. Thank you for the help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.