private void buildGeometryHeirarchy() { //VertexElement[] elems = WaterDMapVertex.Decleration.GetDeclaration(); /*Mesh systemMesh = new Mesh( (int)mNumTris, (int)mNumVertices, MeshFlags.SystemMemory | MeshFlags.Use32Bit, * elems, mGDevice );*/ Vector3[] vertices; int[] indices; Utils.GenTriGrid((int)mInfo.vertRows, (int)mInfo.vertCols, mInfo.dx, mInfo.dz, new Vector3(0, 0, 0), out vertices, out indices); #region get data for the vertices WaterDMapVertex[] verts = new WaterDMapVertex[mNumVertices]; for (int i = 0; i < mInfo.vertRows; ++i) { for (int j = 0; j < mInfo.vertCols; ++j) { int index = i * mInfo.vertCols + j; verts[index].pos = vertices[index]; verts[index].scaledTexC = new Vector2((float)j / mInfo.vertCols, (float)i / mInfo.vertRows) * mInfo.texScale; verts[index].normalizedTexC = new Vector2((float)j / mInfo.vertCols, (float)i / mInfo.vertRows); } } #endregion /*#region mesh index data, compute normals, optimize * //write the vertex data * systemMesh.SetVertexBufferData( verts, LockFlags.None ); * * //write the index data * systemMesh.SetIndexBufferData( indices, LockFlags.None ); * * int[] adjacency = new int[ systemMesh.NumberFaces * 3 ]; * systemMesh.GenerateAdjacency( .001f, adjacency ); * systemMesh.OptimizeInPlace( MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeAttributeSort, adjacency ); #endregion*/ /*mVertexBuffer = new VertexBuffer( mGDevice, * mNumVertices * CustomVertex.PositionNormalTextured.StrideSize * 4, * Usage.WriteOnly, * CustomVertex.PositionNormalTextured.Format, * Pool.Managed );*/ mVertexBuffer = new VertexBuffer(mGDevice, typeof(VertexPositionNormalTexture), mNumVertices, BufferUsage.WriteOnly); mVertexBuffer.SetData(verts); Rectangle Rec = new Rectangle( 0, 0, (mInfo.vertCols - 1), (mInfo.vertRows - 1)); mRootSubGrid = new SubGrid(mInfo.vertRows, mInfo.vertCols); recursiveTerrainBuild(mRootSubGrid, Rec, verts); /*systemMesh.Dispose(); * systemMesh = null;*/ }
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; }
public Water(IXNAGame _game, WaterInfoTest waterInfo, TextureCube envMap) { numLeaves = 0; game = _game; mGDevice = game.GraphicsDevice; mInfo = waterInfo; mWidth = (waterInfo.vertCols - 1) * waterInfo.dx; mHeight = (waterInfo.vertRows - 1) * waterInfo.dz; mWaveNMapOffset0 = new Vector2(0, 0); mWaveNMapOffset1 = new Vector2(0, 0); mWaveDMapOffset0 = new Vector2(0, 0); mWaveDMapOffset1 = new Vector2(0, 0); mNumTris = (waterInfo.vertRows - 1) * (waterInfo.vertCols - 1) * 2; mNumVertices = waterInfo.vertRows * waterInfo.vertCols; WorldMatrix = waterInfo.toWorld; #region Build the Mesh VertexElement[] decleration = new VertexElement[] { new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0), new VertexElement(0, 12, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0), new VertexElement(0, 20, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1) }; VertexElement[] elems = WaterDMapVertex.CreateVertexElements(); // Generate the water grid and write to water mesh //mMesh = new Mesh( mNumTris, mNumVertices, MeshFlags.Managed | MeshFlags.Use32Bit, elems, mGDevice ); mMesh = new Mesh(game, mNumTris, mNumVertices, WaterDMapVertex.StrideSize , Graphics.AttributeSystem.CreateVertexDeclaration(game.GraphicsDevice, typeof(WaterDMapVertex))); //Mesh temp = mMesh.Clone(MeshFlags.Managed, decleration, mGDevice); //mesh.Dispose(); Vector3[] verts; int[] indices; Utils.GenTriGridOld(mInfo.vertRows, mInfo.vertCols, mInfo.dx, mInfo.dz, new Vector3(0, 0, 0), out verts, out indices); WaterDMapVertex[] v = new WaterDMapVertex[mNumVertices]; for (int i = 0; i < mInfo.vertRows; ++i) { for (int j = 0; j < mInfo.vertCols; ++j) { int index = i * mInfo.vertCols + j; v[index].pos = verts[index]; v[index].scaledTexC = new Vector2((float)j / mInfo.vertCols, (float)i / mInfo.vertRows) * mInfo.texScale; v[index].normalizedTexC = new Vector2((float)j / mInfo.vertCols, (float)i / mInfo.vertRows); } } //set the vertexbuffer data mMesh.SetVertexBufferData(v); //set the index data mMesh.SetIndexBufferData(indices); int[] adjac = new int[mMesh.NumberFaces * 3]; mMesh.GenerateAdjacency(0.001f, adjac); //mMesh.OptimizeInPlace( MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeAttributeSort, adjac ); mMesh.OptimizeInPlace(0, adjac); adjac = null; #endregion /*-------------------------------------------------------------------- * Create the Textures */ int m = mInfo.vertRows; int n = mInfo.vertCols; mWaveMap0 = Texture2D.FromFile(mGDevice, mInfo.waveMapFileName0); mWaveMap1 = Texture2D.FromFile(mGDevice, mInfo.waveMapFileName1); //mReflectMap = new RenderToTexture( mGDevice, 1024, 1024, 0, Format.A8R8G8B8, true, DepthFormat.D24X8, true ); //mRefractMap = new RenderToTexture( mGDevice, 1024, 1024, 0, Format.A8R8G8B8, true, DepthFormat.D24X8, true ); mReflectMap = new RenderTarget2D(game.GraphicsDevice, 1024, 1024, 0, SurfaceFormat.Color); mRefractMap = new RenderTarget2D(game.GraphicsDevice, 1024, 1024, 0, SurfaceFormat.Color); DepthBuffer = new DepthStencilBuffer(game.GraphicsDevice, 1024, 1024, game.GraphicsDevice.DepthStencilBuffer.Format); mVisibleSubgrids = new List <SubGrid>(); buildGeometryHeirarchy(); buildEffect(game, envMap); }