Beispiel #1
0
        public void CreateMesh(Terrain terrain, Rectangle r, Device device) {
            if (_vb != null) {
                Util.ReleaseCom(ref _vb);
                _vb = null;
            }
            
            var halfWidth = 0.5f * terrain.Width;
            var halfDepth = 0.5f * terrain.Depth;

            var patchWidth = terrain.Width / (terrain.Renderer.NumPatchVertCols - 1);
            var patchDepth = terrain.Depth / (terrain.Renderer.NumPatchVertRows - 1);
            var vertWidth = terrain.Info.CellSpacing;
            var vertDepth = terrain.Info.CellSpacing;
            var du = 1.0f / (terrain.Renderer.NumPatchVertCols - 1) / Terrain.CellsPerPatch;
            var dv = 1.0f / (terrain.Renderer.NumPatchVertRows - 1) / Terrain.CellsPerPatch;

            _verts = new List<TerrainCP>();
            var min = new Vector3(float.MaxValue);
            var max = new Vector3(float.MinValue);
            for (int z = r.Top, z0 = 0; z <= r.Bottom; z++, z0++) {
                var zp = halfDepth - r.Top / Terrain.CellsPerPatch * patchDepth - z0 * vertDepth;

                for (int x = r.Left, x0 = 0; x <= r.Right; x++, x0++) {
                    var xp = -halfWidth + r.Left / Terrain.CellsPerPatch * patchWidth + x0 * vertWidth;
                    var pos = new Vector3(xp, terrain.Height(xp, zp), zp);

                    min = Vector3.Minimize(min, pos);
                    max = Vector3.Maximize(max, pos);

                    var uv = new Vector2(r.Left * du + x0 * du, r.Top * dv + z0 * dv);
                    var v = new TerrainCP(pos, uv, new Vector2());
                    _verts.Add(v);
                }
            }

            Bounds = new BoundingBox(min, max);

            var vbd = new BufferDescription(
                TerrainCP.Stride * _verts.Count,
                ResourceUsage.Immutable,
                BindFlags.VertexBuffer,
                CpuAccessFlags.None,
                ResourceOptionFlags.None,
                0
            );
            _vb = new Buffer(device, new DataStream(_verts.ToArray(), false, false), vbd);
        }
Beispiel #2
0
        private void BuildQuadPatchVB(Device device) {
            var patchVerts = new TerrainCP[_numPatchVertices];
            var halfWidth = 0.5f * _terrain.Width;
            var halfDepth = 0.5f * _terrain.Depth;

            var patchWidth = _terrain.Width / (NumPatchVertCols - 1);
            var patchDepth = _terrain.Depth / (NumPatchVertRows - 1);
            var du = 1.0f / (NumPatchVertCols - 1);
            var dv = 1.0f / (NumPatchVertRows - 1);

            for (var i = 0; i < NumPatchVertRows; i++) {
                var z = halfDepth - i * patchDepth;
                for (var j = 0; j < NumPatchVertCols; j++) {
                    var x = -halfWidth + j * patchWidth;
                    var vertId = i * NumPatchVertCols + j;
                    patchVerts[vertId] = new TerrainCP(
                        new Vector3(x, 0, z),
                        new Vector2(j * du, i * dv),
                        new Vector2()
                        );
                }
            }
            for (var i = 0; i < NumPatchVertRows - 1; i++) {
                for (var j = 0; j < NumPatchVertCols - 1; j++) {
                    var patchID = i * (NumPatchVertCols - 1) + j;
                    var vertID = i * NumPatchVertCols + j;
                    patchVerts[vertID].BoundsY = _patchBoundsY[patchID];
                }
            }

            var vbd = new BufferDescription(
                TerrainCP.Stride * patchVerts.Length,
                ResourceUsage.Immutable,
                BindFlags.VertexBuffer,
                CpuAccessFlags.None,
                ResourceOptionFlags.None, 0
                );
            _quadPatchVB = new Buffer(
                device,
                new DataStream(patchVerts, false, false),
                vbd
                );
        }