Sign in to follow this  
simotix

[MDX] Create a vertex buffer

Recommended Posts

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;

    };

}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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[i].VertexBuffer, 0);

BasicTextureEffect.CommitChanges();

planeList[i].Render(ptrDevice);

.... Render
pDevice.Indices = IndiceBuffer;
pDevice.DrawIndexedPrimitives(PrimitiveType.TriangleFan, 0, 0, VertCount, 0, TriCount);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this