Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public void OnClick(MouseEventArgs e)
        {
            var x = (float)e.X / D3DApp.GD3DApp.Window.ClientSize.Width;
            var y = (float)e.Y / D3DApp.GD3DApp.Window.ClientSize.Height;
            var p = new Vector2(x, y);

            if (Contains(ref p))
            {
                // convert minimap-space to world-space and set the camera target
                var terrainX = _terrain.Width * p.X - _terrain.Width / 2;
                var terrainZ = -_terrain.Depth * p.Y + _terrain.Depth / 2;
                var cam      = _viewCam as LookAtCamera;
                if (cam != null)
                {
                    cam.Target = new Vector3(terrainX, _terrain.Height(terrainX, terrainZ), terrainZ);
                }
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public override bool Init() {
            if ( ! base.Init()) return false;

            Effects.InitAll(Device);
            InputLayouts.InitAll(Device);
            RenderStates.InitAll(Device);

            _sky = new Sky(Device, "Textures/grasscube1024.dds", 5000.0f);
            Patch.InitPatchData(Terrain.CellsPerPatch, Device);
            var tii = new InitInfo {
                HeightMapFilename = "Textures/terrain.raw",
                LayerMapFilename0 = "textures/grass.dds",
                LayerMapFilename1 = "textures/darkdirt.dds",
                LayerMapFilename2 = "textures/stone.dds",
                LayerMapFilename3 = "Textures/lightdirt.dds",
                LayerMapFilename4 = "textures/snow.dds",
                BlendMapFilename = "textures/blend.dds",
                HeightScale = 50.0f,
                HeightMapWidth = 2049,
                HeightMapHeight = 2049,
                CellSpacing = 0.5f
            };
            _terrain = new Terrain();
            _terrain.Init(Device, ImmediateContext, tii);

            _camera.Height = _terrain.Height;
            _txMgr = new TextureManager();
            _txMgr.Init(Device);
            _treeModel = new BasicModel(Device, _txMgr, "Models/tree.x", "Textures");
            _treeInstances = new List<BasicModelInstance>();
            
            
            for (var i = 0; i < NumTrees; i++) {
                var good = false;
                var x = MathF.Rand(0, _terrain.Width);
                var z = MathF.Rand(0, _terrain.Depth);
                while (!good) {

                    
                    if (_terrain.Height(x, z) < 12.0f) {
                        good = true;
                    }
                    x = MathF.Rand(-_terrain.Width/2, _terrain.Width/2);
                    z = MathF.Rand(-_terrain.Depth/2, _terrain.Depth/2);
                }
                var treeInstance = new BasicModelInstance(_treeModel) {
                    
                    World = Matrix.RotationX(MathF.PI / 2) * Matrix.Translation(x, _terrain.Height(x, z), z)
                };
                _treeInstances.Add(treeInstance);
            }
            BuildInstancedBuffer();

            return true;
        }