Sign in to follow this  

[C#] GraphicsStream woes

This topic is 4074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently bought Intro to 3D Game Programming: Shader.., and I'm trying to convert the terrain chapter to C#. I got the brute force approach just fine, but when I try to split the terrain into subgrids I start having problems. Here's the code:
private void buildSubGridMesh(Rectangle R, VertexPosNormTex[] gridVerts)
{
    VertexElement[] elems = VertexPosNormTex.mDecleration.GetDeclaration();
    Mesh subMesh = new Mesh(SubGrid.NUM_TRIS, SubGrid.NUM_VERTS, MeshFlags.Managed, elems, mGDevice);

    VertexPosNormTex[] v = new VertexPosNormTex[subMesh.NumberVertices];
    GraphicsStream vertStream = subMesh.LockVertexBuffer(LockFlags.None);
    
    int k = 0;
    for (int i = R.Top; i <= R.Bottom; i++)
    {
        for (int j = R.Left; j <= R.Right; j++)
        {
            v[k] = gridVerts[i * mHeightMap.NumCellsPerCol + j];
            k++;
        }
    }

    BoundingBox bndBox;
    Geometry.ComputeBoundingBox(vertStream, subMesh.NumberVertices, subMesh.VertexFormat, out bndBox.min, out bndBox.max);
    vertStream.Write(v);
    subMesh.UnlockVertexBuffer();

    Vector3[] tempVerts;
    int[] tempIndices;

    DXUtil.GenTriGrid(SubGrid.NUM_ROWS, SubGrid.NUM_COLS, mHeightMap.CellSpacing, mHeightMap.CellSpacing,
                        new Vector3(), out tempVerts, out tempIndices);

    ushort[] indices = new ushort[tempIndices.Length];
    ushort[] attributes = new ushort[SubGrid.NUM_TRIS];

    GraphicsStream indexStream = subMesh.LockIndexBuffer(LockFlags.None);
    GraphicsStream attribStream = subMesh.LockAttributeBuffer(LockFlags.None);
    for(int i = 0; i < SubGrid.NUM_TRIS; ++i)
    {
        indices[i*3+0] = tempIndices[i*3+0];
        indices[i*3+1] = tempIndices[i*3+1];
        indices[i*3+2] = tempIndices[i*3+2];

        attributes[i] = 0; // All in subset 0.
    }
    indexStream.Write(indices);
    attribStream.Write(attributes); /* Here I get an InvalidOperationException */

    subMesh.UnlockIndexBuffer();
    subMesh.UnlockAttributeBuffer();

    int[] adjacency = new int[subMesh.NumberFaces * 3];
    subMesh.GenerateAdjacency(.001f, adjacency);
    subMesh.OptimizeInPlace(MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeAttributeSort, adjacency);

    mSubMeshes.Add(subMesh);
    mMeshBndBoxes.Add(bndBox);
}


R is the rectangle the global grid verts used for the submesh gridVerts is the vertices for the entire terrain grid I keep getting an InvalidOperationExecution at "attribStream.Write(attributes)." I never had this problem when I didn't break up the mesh into submeshes. I'm using C# Express and the Feb06 DirectX. [Edited by - glaeken on October 20, 2006 5:44:04 PM]

Share this post


Link to post
Share on other sites
Unless you specified 32bit indices at mesh creation, the index buffer will have 16bit values. You'd want to write ushort values, not int.

If you are using 32bit values, you'd still want to use unsigned values.

Hope this helps.

Share this post


Link to post
Share on other sites
Thanks sirob, this fixed the exception I was getting at "indexStream.Write(indices)" however, now I am getting the InvalidOperationExecution exception at attribStream.Write(attributes). What am I doing wrong?

Share this post


Link to post
Share on other sites

This topic is 4074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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