Original Post
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 Custom Vertex Structure
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);
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;
};
}