示例#1
0
        public override void Init()
        {
            Content.ContentPath = "";
            Content.ContentCachePath = "Cache/";
            Meshes.Load(Device);
            billboard = Meshes.PositionTexcoord(Device, Meshes.IndexedPlane(-8.1640625f, -62f / 20f, 38, 62 / 2f));
            plane = Meshes.PositionTexcoord(Device, Meshes.IndexedGrid(new Vector3(-0.5f, -0.5f, 0), new Vector2(1, 1), 128, 128, Vector2.Zero, new Vector2(1, 1)));
            tree = SlimDX.Direct3D9.Mesh.FromFile(Device, "spruce.x", MeshFlags.Managed);
            unit = SlimDX.Direct3D9.Mesh.FromFile(Device, "unitcylinder.x", MeshFlags.Managed);
            Reloadable.Load((efn) =>
            {
                Effect e = EffectUtil.LoadWithDebugger(Device, efn);
                if(e == null) return;
                if (ef != null) ef.Dispose();
                ef = e;
            }, "geomorph.fx");
            text = Texture.FromFile(Device, "ground.jpg", Usage.None, Pool.Managed);
            text.GenerateMipSublevels();
            treeMap = Texture.FromFile(Device, "spruce.tga", Usage.None, Pool.Managed);
            treeMap.GenerateMipSublevels();
            treeBillboard = Texture.FromFile(Device, "spruceBillboard.tga", Usage.None, Pool.Managed);
            treeBillboard.GenerateMipSublevels();
            String hm = "heightmap-landscape.tga";

            HeightmapProcessor heightmapProcessor = new HeightmapProcessor();
            Reloadable.Load((h) =>
            {
                if (heightMap != null) heightMap.Dispose();
                heightMap = heightmapProcessor.Load(Device, h);
            }, hm);
            textureSize = heightMap.GetLevelDescription(0).Width;

            terrain = new GeoclipmappedTerrain(Device, resolution);

            DataRectangle r = heightMap.LockRectangle(0, LockFlags.ReadOnly);
            data = TextureUtil.ReadTexture<ClientCommon.TextureColor.R32G32B32A32F>(r, textureSize);
            heightMap.UnlockRectangle(0);

            DataRectangle r2 = heightMap.LockRectangle(5, LockFlags.ReadOnly);
            dataLod2 = TextureUtil.ReadTexture<ClientCommon.TextureColor.R32G32B32A32F>(r2, textureSize/(int)Math.Pow(2, 5));
            heightMap.UnlockRectangle(5);

            float prev = 20;
            cam = new WalkaroundCamera(this, (float x, float y) =>
            {
                float z = HeightAt(data, x, y);
                float re = z * 0.05f + prev * 0.95f;
                prev = re;
                return re + 1.8f;
            });
            cam.ZFar *= 100;
            cam.Speed = 2;
            values = new Values();
            values.Show();

            instanceVD = new VertexDeclaration(Device, instanceVES);
            instancingVB = new VertexBuffer(Device, ntrees * Vector3.SizeInBytes, Usage.None, VertexFormat.None, Pool.Managed);

            Random ra = new Random(0);
            trees = new Vector3[ntrees];
            treeRidgeDiff = new float[ntrees];
            for (int i = 0; i < ntrees/40; i++)
            {
                float x = ((float)ra.NextDouble() - 0.5f) * heightmapScale;
                float y = ((float)ra.NextDouble() - 0.5f) * heightmapScale * 0.5f;
                for (int k = 0; k < 40; k++)
                {
                    float x1 = x + ((float)ra.NextDouble() - 0.5f) * 100;
                    float y1 = y + ((float)ra.NextDouble() - 0.5f) * 100;
                    float z = HeightAt(data, x1, y1);
                    trees[i*40 + k] = new Vector3(x1, y1, z);
                    treeRidgeDiff[i*40 + k] = HeightAt(dataLod2, x1, y1) - z;
                }
            }

            //Fullscreen = false;
        }