Exemplo n.º 1
0
        private void frmTest_Load(object sender, System.EventArgs e)
        {
            int i, j;


            PresentParameters pp;

            pp          = new PresentParameters();
            pp.Windowed = true;
            pp.EnableAutoDepthStencil = true;
            pp.AutoDepthStencilFormat = DepthFormat.D16;
            pp.SwapEffect             = SwapEffect.Discard;
            pp.MultiSample            = MultiSampleType.TwoSamples;

            if (this.FullScreen)
            {
                pp.Windowed                  = false;
                pp.BackBufferWidth           = 800;
                pp.BackBufferHeight          = 600;
                pp.FullScreenRefreshRateInHz = 85;
                pp.BackBufferFormat          = Format.X8R8G8B8;
                pp.PresentationInterval      = PresentInterval.Immediate;
            }

            dev = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pp);
            dev.RenderState.CullMode      = Cull.None;
            dev.RenderState.ZBufferEnable = true;
            dev.RenderState.Lighting      = true;

            dev.RenderState.MultiSampleAntiAlias = true;

            //fog
            dev.RenderState.FogEnable     = true;
            dev.RenderState.FogColor      = Color.FromArgb(this.SkyCol.ToArgb());
            dev.RenderState.FogVertexMode = FogMode.Linear;
            dev.RenderState.FogStart      = 14;
            dev.RenderState.FogEnd        = 25;

            dev.SamplerState[0].MinFilter = TextureFilter.None;
            dev.SamplerState[0].MagFilter = TextureFilter.Linear;

            //enable alpha blending
            dev.RenderState.AlphaBlendEnable = true;
            dev.RenderState.SourceBlend      = Blend.SourceAlpha;
            dev.RenderState.DestinationBlend = Blend.InvSourceAlpha;
            dev.RenderState.BlendOperation   = BlendOperation.Add;



            Direct3DAid.Surfaces.Technique t;
            //t = Technique.DefaultRectangledSimple(new SizeF(1.0F,1.0F),HatchingMode.NWSE);
            t = Technique.DefaultRectangledAlternated(new SizeF(1.0F, 1.0F), HatchingMode.NWSE);
            //t = Technique.DefaultTriangledEquilateral(1.0F,true);

            s = new Direct3DAid.Surfaces.SurfaceSplatted(new Size(w, h), new ControlPoint(0.0F, Color.LightGray.ToArgb(), 0), t, 5, dev);

            this.wSMax = (this.w - 1) * s.SurfaceTechnique.TileSize.Width;
            this.hSMax = (this.h - 1) * s.SurfaceTechnique.TileSize.Height;

            s.UpdatingGeometry += new Direct3DAid.Surfaces.SurfaceSplatted.UpdatingGeometryHandler(OnUpdatingGeometry);


            s.SpaceObject = new SpaceObject(s.Device);           //make it's space object


            //load from file
            s.SetHeightsFromHeightMap(this.SurfPath, 0.045F, -0.4F);

            //sets from heights (divisions according to height)
            float[] levs = new float[] { 0.0F, 1.3F, 1.3F, 2.0F };
            s.SetDivisionsAccordingToHeight(levs);


            //make random grass
            Random r = new Random();

            for (i = 0; i <= s.Size.Width - 1; i++)
            {
                for (j = 0; j <= s.Size.Height - 1; j++)
                {
                    if (s.GetControlPoint(i, j).Division == 1)
                    {
                        if (r.NextDouble() > 0.5)
                        {
                            s.SetControlPointDivision(i, j, 2);
                        }
                    }
                }
            }

            //make random colored surf
            for (i = 0; i <= s.Size.Width - 1; i++)
            {
                for (j = 0; j <= s.Size.Height - 1; j++)
                {
                    if (s.GetControlPoint(i, j).Division == 0)
                    {
                        s.SetControlPointColor(i, j, Misc.GetRandomColor(Color.YellowGreen, 10, r).ToArgb());
                    }
                    if ((s.GetControlPoint(i, j).Division == 1) || (s.GetControlPoint(i, j).Division == 2))
                    {
                        s.SetControlPointColor(i, j, Misc.GetRandomColor(Color.LightGreen, 20, r).ToArgb());
                    }
                    if (s.GetControlPoint(i, j).Division == 3)
                    {
                        s.SetControlPointColor(i, j, Misc.GetRandomColor(Color.MediumAquamarine, 15, r).ToArgb());
                    }
                    if (s.GetControlPoint(i, j).Division == 4)
                    {
                        s.SetControlPointColor(i, j, Misc.GetRandomColor(Color.LightGray, 5, r).ToArgb());
                    }
                }
            }


            UVMap uvm, uvm1;

            uvm  = new UVMapPlaneXZ(new PointF(0.0F, 0.0F), new SizeF(1.0F, 1.0F), new SizeF(0.5F, 0.5F));
            uvm1 = new UVMapPlaneXZ(new PointF(0.0F, 0.0F), new SizeF(1.0F, 1.0F), new SizeF(1.2F, 1.2F));

            s.DivisionUVMap[0] = uvm;
            s.DivisionUVMap[1] = uvm1;
            s.DivisionUVMap[2] = uvm1;
            s.DivisionUVMap[3] = uvm;
            s.DivisionUVMap[4] = uvm;


            s.UpdateGeometry(true, 1.2F);


            s.DivisionTexture[0] = TextureLoader.FromFile(dev, "tex\\b0.jpg");
            s.DivisionTexture[1] = TextureLoader.FromFile(dev, "tex\\b1.jpg");
            s.DivisionTexture[2] = TextureLoader.FromFile(dev, "tex\\b1Alt.jpg");
            s.DivisionTexture[3] = TextureLoader.FromFile(dev, "tex\\b2.jpg");
            s.DivisionTexture[4] = TextureLoader.FromFile(dev, "tex\\b3.jpg");


            //this.viewM = Matrix.Multiply(Matrix.Multiply(Matrix.Multiply(Matrix.Translation(-2.5F,-2.5F,0),Matrix.RotationZ(k)),Matrix.Translation(2.5F,2.5F,0)),Matrix.LookAtLH(new Vector3(0,-5,-4.5f),new Vector3(2.5F,2.0F,0),new Vector3(0,0,-1)));
            this.viewM          = Matrix.LookAtLH(new Vector3(1, 2, 1), new Vector3(1, 0, 1), new Vector3(0, 0, 1));
            this.timer1.Enabled = true;


            //CAMERA:

            this.cam            = new Camera(this.dev, (float)(Math.PI / 3), 1.3333F, 0.01F, 100F);
            this.cam.SelfMatrix = viewM;


            this.startT.Start();
        }