[SlimDX]Problem With Index Buffers [New Problem]

Started by
4 comments, last by jdub 15 years, 3 months ago
I am really new to SlimDX and I am learning about indices/index buffers. My code below throws a InvalidCall exception when it runs. I could really use some help with this as I have no clue as to what is wrong:

void RenderCube()
        {
            IndexBuffer indexBuff;
            VertexBuffer vertexBuff;

            vertexBuff = new VertexBuffer ( device,
                                            D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * 8,
                                            Usage.None,
                                            UntransformedVertex.FVFFormat,
                                            Pool.Default );

            indexBuff = new IndexBuffer ( device, sizeof(short) * 36, Usage.WriteOnly, Pool.Default, true );

            DataStream dStream = vertexBuff.Lock ( 0, 0, LockFlags.None );

            UntransformedVertex[] verts = new UntransformedVertex[8];
            
            //front lower left corner
            verts[0] = new UntransformedVertex ( new Vector3 ( 0, 0, 1f ), Color.Green.ToArgb () );

            //front lower right corner
            verts[1] = new UntransformedVertex ( new Vector3 ( 1f, 0f, 1f ), Color.Red.ToArgb () );

            //front upper left corner
            verts[2] = new UntransformedVertex ( new Vector3 ( 0f, 1f, 1f ), Color.Green.ToArgb () );

            //front upper right corner
            verts[3] = new UntransformedVertex ( new Vector3 ( 1f, 1f, 1f ), Color.Red.ToArgb () );

            //back lower left corner
            verts[4] = new UntransformedVertex ( new Vector3 ( 0f, 0f, 2f ), Color.Green.ToArgb () );

            //back lower right corner
            verts[5] = new UntransformedVertex ( new Vector3 ( 1f, 0f, 2f ), Color.Red.ToArgb () );

            //back upper left corner
            verts[6] = new UntransformedVertex ( new Vector3 ( 0f, 1f, 2f ), Color.Green.ToArgb () );

            //back upper right corner
            verts[7] = new UntransformedVertex ( new Vector3 ( 1f, 1f, 2f ), Color.Red.ToArgb () );

            dStream.WriteRange ( verts );

            vertexBuff.Unlock ();

            dStream.Dispose ();

            dStream = indexBuff.Lock ( 0, 0, LockFlags.None );

            short[] indices = new short[36];

            //front face
            indices[0] = 0; indices[1] = 1; indices[2] = 2;
            indices[3] = 3; indices[4] = 2; indices[5] = 1;

            //left face
            indices[6] = 4; indices[7] = 6; indices[8] = 0;
            indices[9] = 7; indices[10] = 2; indices[11] = 0;
            
            //bottom face
            indices[12] = 4; indices[13] = 5; indices[14] = 1;
            indices[15] = 0; indices[16] = 4; indices[17] = 5;

            //right face
            indices[18] = 1; indices[19] = 3; indices[20] = 7;
            indices[21] = 5; indices[22] = 3; indices[23] = 7;

            //top face
            indices[24] = 6; indices[25] = 7; indices[26] = 3;
            indices[27] = 2; indices[28] = 3; indices[29] = 6;

            //back face
            indices[30] = 6; indices[31] = 7; indices[32] = 4;
            indices[33] = 5; indices[34] = 7; indices[35] = 4;

            indexBuff.Unlock ();
            dStream.Dispose ();

            device.Indices = indexBuff;
            device.SetStreamSource ( 0, this.vertBuffer, 0, D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) );
            
            //Exception thrown right here
            device.DrawIndexedPrimitives ( PrimitiveType.TriangleList, 0, 0, 8, 0, 12 );
        }



Thanks, jdub [Edited by - jdub on December 22, 2008 1:19:22 AM]
J.W.
Advertisement
Do you set the vertex format on the device anywhere?
Mike Popoloski | Journal | SlimDX
in the indices lock part, you are not actually writing to the buffer... you are though at the vertex part.

DataStream dStream = vertexBuff.Lock ( 0, 0, LockFlags.None );
....
dStream.WriteRange ( verts );

vertexBuff.Unlock ();

but at the indices:

dStream = indexBuff.Lock ( 0, 0, LockFlags.None );
...
indexBuff.Unlock ();

so my guess is that there's something wrong?
Thanks a ton guys! I feel really stupid because those were both very obvious errors. I fixed the exception and got the cube to render.

I still have a problem though. When I try to rotate the cube around the X axis, it rotates around fine. However, when I try to rotate it around the Y or Z axis, it won't render properly. Based on the amount of rotation, it either shows a thin sliver of the shape or doesn't draw at all.

I turned off culling to see if that might be a problem but still nothing

I went through the indices and vertices and checked to make sure that I had the right values and I could find nothing wrong.

It would be great if I could get some more help with this.

updated source:
void RenderCube()        {            IndexBuffer indexBuff;            VertexBuffer vertexBuff;            vertexBuff = new VertexBuffer ( device,                                            D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * 8,                                            Usage.WriteOnly,                                            UntransformedVertex.FVFFormat,                                            Pool.Default );            indexBuff = new IndexBuffer ( device, sizeof(short) * 36, Usage.WriteOnly, Pool.Default, true );            DataStream dStream = vertexBuff.Lock ( 0, 0, LockFlags.None );            UntransformedVertex[] verts = new UntransformedVertex[8];            camera.LookAt ( Vector3.Zero );            camera.Position = new Vector3 ( 5, 5, 5 );            //front lower right corner            verts[0] = new UntransformedVertex ( new Vector3 ( 0.5f, 0f, 0f ), Color.Red.ToArgb () );            //front lower left corner            verts[1] = new UntransformedVertex ( new Vector3 ( 0f, 0f, 0f ), Color.Green.ToArgb () );            //front upper left corner            verts[2] = new UntransformedVertex ( new Vector3 ( 0f, 0.5f, 0f ), Color.Green.ToArgb () );            //front upper right corner            verts[3] = new UntransformedVertex ( new Vector3 ( 0.5f, 0.5f, 0f ), Color.Red.ToArgb () );            //back lower left corner            verts[4] = new UntransformedVertex ( new Vector3 ( 0f, 0f, 1f ), Color.Green.ToArgb () );            //back lower right corner            verts[5] = new UntransformedVertex ( new Vector3 ( 0.5f, 0f, 1f ), Color.Red.ToArgb () );            //back upper left corner            verts[6] = new UntransformedVertex ( new Vector3 ( 0f, 0.5f, 1f ), Color.Green.ToArgb () );            //back upper right corner            verts[7] = new UntransformedVertex ( new Vector3 ( 0.5f, 0.5f, 1f ), Color.Red.ToArgb () );            dStream.WriteRange ( verts );            vertexBuff.Unlock ();            dStream.Dispose ();            dStream = indexBuff.Lock ( 0, 0, LockFlags.None );            short[] indices = new short[36];            //front face            indices[0] = 1; indices[1] = 0; indices[2] = 2;            indices[3] = 3; indices[4] = 2; indices[5] = 0;            //left face             indices[6] = 4; indices[7] = 6; indices[8] = 1;            indices[9] = 6; indices[10] = 2; indices[11] = 1;                        //bottom face            indices[12] = 4; indices[13] = 5; indices[14] = 0;            indices[15] = 1; indices[16] = 4; indices[17] = 5;            //right face            indices[18] = 0; indices[19] = 3; indices[20] = 7;            indices[21] = 5; indices[22] = 0; indices[23] = 7;            //top face            indices[24] = 6; indices[25] = 7; indices[26] = 3;            indices[27] = 2; indices[28] = 3; indices[29] = 6;            //back face            indices[30] = 6; indices[31] = 7; indices[32] = 4;            indices[33] = 5; indices[34] = 7; indices[35] = 4;            dStream.WriteRange ( indices );            indexBuff.Unlock ();            dStream.Dispose ();            device.Indices = indexBuff;            device.SetStreamSource ( 0, vertexBuff, 0, D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) );            device.VertexFormat = UntransformedVertex.FVFFormat;            device.SetRenderState ( RenderState.Lighting, false );            device.SetRenderState ( RenderState.CullMode, Cull.None );            Matrix mat = device.GetTransform ( TransformState.World );            mat *= Matrix.RotationY ( MathHelper.ToRadians ( 45 ) );            device.SetTransform ( TransformState.World, mat );            device.DrawIndexedPrimitives ( PrimitiveType.TriangleList, 0, 0, 8, 0, 12 );        }


Thanks again, jdub.
J.W.
Your method of using GetTransform each frame is rather unorthodox, and I'm not sure that it's guaranteed to work properly. The usual method would be to have a variable somewhere that holds the rotation, and then update it each frame. After it's updated, build a fresh rotation matrix from that angle.
Mike Popoloski | Journal | SlimDX
So I changed these lines:

Matrix mat = device.GetTransform ( TransformState.World );

mat *= Matrix.RotationY ( MathHelper.ToRadians ( 45 ) );
to:


Matrix mat;
mat = Matrix.RotationY ( MathHelper.ToRadians ( 45 ) );

device.SetTransform ( TransformState.World, mat );

It still produces the same results.
J.W.

This topic is closed to new replies.

Advertisement