Jump to content
  • Advertisement
Sign in to follow this  

[C#] GraphicsStream woes

This topic is 4294 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];

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

    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 = 0; // All in subset 0.
    attribStream.Write(attributes); /* Here I get an InvalidOperationException */


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


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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!