public SubGrid(int numRows, int numCols) { children = new SubGrid[4]; mesh = null; box = null; mNumRows = numRows; mNumCols = numCols; mNumTris = (mNumRows - 1) * (mNumCols - 1) * 2; mNumVerts = mNumRows * mNumCols; totalIndices = mNumVerts; }
private void buildSubGridMesh(Rectangle R, WaterDMapVertex[] gridVerts, bool buildMesh, SubGrid subGrid) { VertexElement[] elems = WaterDMapVertex.CreateVertexElements(); //Mesh subMesh = new Mesh( subGrid.NumTris, subGrid.NumVerts, MeshFlags.Managed | MeshFlags.Use32Bit, elems, mGDevice ); Mesh subMesh = new Mesh(game, subGrid.NumTris, subGrid.NumVerts, WaterDMapVertex.StrideSize , Graphics.AttributeSystem.CreateVertexDeclaration(game.GraphicsDevice, typeof(WaterDMapVertex))); #region set subgrid verts & generate the bounding box WaterDMapVertex[] v = new WaterDMapVertex[subMesh.NumberVertices]; Vector3[] positions = new Vector3[subMesh.NumberVertices]; //use a stream so we can pass it to computeBoundingBox //GraphicsStream vertexStream = 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 * mInfo.vertCols + j]; positions[k] = gridVerts[i * mInfo.vertCols + j].pos; k++; } } //vertexStream.Write( v ); Utils.BoundingBox bndBox = new Utils.BoundingBox(); subMesh.SetVertexBufferData(v); BoundingBox bb = BoundingBox.CreateFromPoints(positions); bndBox.min = bb.Min; bndBox.max = bb.Max; //Geometry.ComputeBoundingBox( vertexStream, subMesh.NumberVertices, subMesh.VertexFormat, out bndBox.min, out bndBox.max ); //subMesh.UnlockVertexBuffer(); subGrid.box = bndBox; if (buildMesh == false) { subMesh.Dispose(); subMesh = null; return; } #endregion #region generate Indices for the subgrid and Optimize Vector3[] tempVerts; int[] tempIndices; Utils.GenTriGrid(subGrid.NumRows, subGrid.NumCols, mInfo.dx, mInfo.dz, new Vector3(0, 0, 0), out tempVerts, out tempIndices); /*int[] attributes = subMesh.LockAttributeBufferArray( LockFlags.None ); * for ( int i = 0; i < subGrid.NumTris; i++ ) * { * attributes[ i ] = 0; // All in subset 0. * } * * subMesh.UnlockAttributeBuffer();*/ subMesh.SetIndexBufferData(tempIndices); int[] adjacency = new int[subMesh.NumberFaces * 3]; subMesh.GenerateAdjacency(.001f, adjacency); //subMesh.OptimizeInPlace( MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeAttributeSort, adjacency ); subMesh.OptimizeInPlace(0, adjacency); #endregion subGrid.mesh = subMesh; }