//Class Constructor 2
        public SurfaceSplatted(Size size, ControlPoint initValue, Technique surfaceTechnique, int numDivisions, Device device) :
            base(size, initValue, surfaceTechnique)
        {
            this._SpaceObject = null;             //no space object initially attached

            this.Device        = device;
            this._NumDivisions = numDivisions;
            this._Geometry     = new GeometryIndexedSplatted(this.Device, typeof(FVF_PosNorColTex), FVF_PosNorColTex.Format, PrimitiveType.TriangleList, this.NumDivisions);     //an empty geometry
        }
        //(Re)Generates the VB and IB that hold out the surface by recreating the geometry property. The new surface appearence can be customizable in this class' properties, like surfaceTechnique property;
        public void UpdateGeometry(bool normalize, float normalizeScale)
        {
            //Alert - UpdatingGeometry
            if (this.UpdatingGeometry != null)
            {
                this.UpdatingGeometry(new UpdatingGeometryInfo(UpdatingGeometryStage.Starting, new Point(-1, -1), GeometryIndexed.NormalizingInfo.Empty, GeometryIndexedSplatted.UpdatingDivisionedGeometryInfo.Empty));
            }

            VertexBuffer vb;
            IndexBuffer  ib;

            vb = new VertexBuffer(typeof(FVF_PosNorColTex), this.Size.Width * this.Size.Height, this.Device, Usage.SoftwareProcessing, FVF_PosNorColTex.Format, Pool.Default);
            ib = new IndexBuffer(typeof(short), (this.Size.Width - 1) * (this.Size.Height - 1) * 2 * 3, this.Device, Usage.SoftwareProcessing, Pool.Default);

            uint[] divs;             //distribution of divisions made out to index values from the geometry
            divs = new uint[this.Size.Width * this.Size.Height];


            //create geometry
            FVF_PosNorColTex[] verts;             //retrieve the vertices in an array
            verts = (FVF_PosNorColTex[])vb.Lock(0, 0);

            short[] indices;             //open the geometry information for use
            indices = (short[])ib.Lock(0, 0);


            int k, l;            //keep track of the current vertex in vb, and the current index in ib

            k = 0; l = 0;

            //Alert - UpdatingGeometry
            if (this.UpdatingGeometry != null)
            {
                this.UpdatingGeometry(new UpdatingGeometryInfo(UpdatingGeometryStage.ProcessingPoint, new Point(-1, -1), GeometryIndexed.NormalizingInfo.Empty, GeometryIndexedSplatted.UpdatingDivisionedGeometryInfo.Empty));
            }

            int i, j;

            for (j = 0; j <= this.Size.Height - 1; j++)
            {
                for (i = 0; i <= this.Size.Width - 1; i++)
                {
                    //Alert - UpdatingGeometry
                    if ((this.UpdatingGeometry != null) && (this.FrequentAlerts))
                    {
                        this.UpdatingGeometry(new UpdatingGeometryInfo(UpdatingGeometryStage.ProcessingPoint, new Point(i, j), GeometryIndexed.NormalizingInfo.Empty, GeometryIndexedSplatted.UpdatingDivisionedGeometryInfo.Empty));
                    }

                    //set vertex properties for point (i,j) -> [k]
                    Vector3 p = this.GetPointRealCoordinatesXYHeight(i, j, true);
                    verts[k].Pos = new Vector3(p.X, p.Z, p.Y);
                    verts[k].Col = this.GetControlPoint(i, j).Color;

                    divs[k] = this.GetControlPoint(i, j).Division;

                    k++;

                    //create triangles for tile (i,j)
                    if ((j < this.Size.Height - 1) && (i < this.Size.Width - 1))
                    {
                        short i00, i01, i10, i11;                      //hold indices of points from this current tile
                        i00 = (short)Misc.GetIndexFromCoordinates(this.Size.Width, new Point(i, j));
                        i01 = (short)Misc.GetIndexFromCoordinates(this.Size.Width, new Point(i, j + 1));
                        i10 = (short)Misc.GetIndexFromCoordinates(this.Size.Width, new Point(i + 1, j));
                        i11 = (short)Misc.GetIndexFromCoordinates(this.Size.Width, new Point(i + 1, j + 1));

                        if (this.GetTileHatching(i, j) == HatchingMode.NESW)
                        {
                            indices[l + 0] = i00; indices[l + 1] = i01; indices[l + 2] = i11;
                            indices[l + 3] = i11; indices[l + 4] = i10; indices[l + 5] = i00;
                        }
                        else
                        {
                            indices[l + 0] = i01; indices[l + 1] = i11; indices[l + 2] = i10;
                            indices[l + 3] = i10; indices[l + 4] = i00; indices[l + 5] = i01;
                        }

                        l += 6;
                    }
                }
            }

            vb.Unlock();
            ib.Unlock();
            //

            //usually an empty geometry was created at constructor, all it needs now is to fill information for it (the VB and the IB)
            this.Geometry.DivisionDistribution = divs;
            this.Geometry.SetVBAndIB(vb, ib);

            //and update it
            if (this.UpdatingGeometry != null)
            {
                if (normalize)
                {
                    //Adding event handler to a method described below, in order to catch the events raised by the Normalize method of this surface's geometry
                    GeometryIndexed.NormalizingHandler hn = new Direct3DAid.Geometries.GeometryIndexed.NormalizingHandler(OnNormalizingGeometry);
                    this._Geometry.Normalizing += hn;
                    this._Geometry.Normalize(normalizeScale);
                    this._Geometry.Normalizing -= hn;
                }
                //Adding event handler to a method described below, in order to catch the events raised by the UpdateDivisionedGeometry method of this surface's geometry
                GeometryIndexedSplatted.UpdatingDivisionedGeometryHandler hu = new Direct3DAid.Geometries.GeometryIndexedSplatted.UpdatingDivisionedGeometryHandler(OnUpdatingDivisionedGeometry);
                this._Geometry.UpdatingDivisionedGeometry += hu;
                this._Geometry.UpdateDivisionedGeometry();
                this._Geometry.UpdatingDivisionedGeometry -= hu;
            }
            else
            {
                //just call the methods
                if (normalize)
                {
                    this._Geometry.Normalize(normalizeScale);
                }
                this._Geometry.UpdateDivisionedGeometry();
            }


            //set the geometry to the space object (if case) (for render)
            if (this.SpaceObject != null)
            {
                this.SpaceObject.Geometry = this.Geometry;
            }
        }
Esempio n. 3
0
        private void frmTest_Load(object sender, System.EventArgs e)
        {
            PresentParameters pp;

            pp                        = new PresentParameters();
            pp.Windowed               = true;               // We don't want to run fullscreen
            pp.SwapEffect             = SwapEffect.Discard; // Discard the frames
            pp.EnableAutoDepthStencil = true;               // Turn on a Depth stencil
            pp.AutoDepthStencilFormat = DepthFormat.D16;    // And the stencil format

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

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



            vb = new VertexBuffer(typeof(FVF_PosColTex), 25, dev, Usage.SoftwareProcessing, FVF_PosColTex.Format, Pool.Default);
            ib = new IndexBuffer(typeof(short), 28 * 3, dev, Usage.SoftwareProcessing, Pool.Default);


            //defining vertices
            FVF_PosColTex[] verts;
            verts = (FVF_PosColTex[])vb.Lock(0, 0);

            /*
             * verts[0].Pos = new Vector3(0,1,0);
             * verts[1].Pos = new Vector3(1,1,0);
             * verts[2].Pos = new Vector3(0,0,0);
             * verts[3].Pos = new Vector3(1,0,0);
             *
             * verts[0].Col = Color.Red.ToArgb();
             * verts[1].Col = Color.White.ToArgb();
             * verts[2].Col = Color.Red.ToArgb();
             * verts[3].Col = Color.Blue.ToArgb();
             */

            verts[0].Pos  = new Vector3(4, 0, 0);
            verts[1].Pos  = new Vector3(0, 0, 0);
            verts[2].Pos  = new Vector3(1, 0, 0);
            verts[3].Pos  = new Vector3(1, 1, 0);
            verts[4].Pos  = new Vector3(1, 2, 0);
            verts[5].Pos  = new Vector3(2, 0, 0);
            verts[6].Pos  = new Vector3(3, 1, 0);
            verts[7].Pos  = new Vector3(2, 2, 0);
            verts[8].Pos  = new Vector3(2, 3, 0);
            verts[9].Pos  = new Vector3(0, 3, 0);
            verts[10].Pos = new Vector3(1, 4, 0);
            verts[11].Pos = new Vector3(1, 5, 0);
            verts[12].Pos = new Vector3(2, 4, 0);
            verts[13].Pos = new Vector3(3, 5, 0);
            verts[14].Pos = new Vector3(4, 5, 0);
            verts[15].Pos = new Vector3(5, 0, 0);
            verts[16].Pos = new Vector3(5, 1, 0);
            verts[17].Pos = new Vector3(4, 1, 0);
            verts[18].Pos = new Vector3(3, 3, 0);
            verts[19].Pos = new Vector3(4, 4, 0);
            verts[20].Pos = new Vector3(5, 4, 0);
            verts[21].Pos = new Vector3(4, 3, 0);
            verts[22].Pos = new Vector3(5, 3, 0);
            verts[23].Pos = new Vector3(4, 2, 0);
            verts[24].Pos = new Vector3(5, 2, 0);

            for (int i = 0; i <= 24; i++)
            {
                verts[i].Col = Color.FromArgb(255, 255, 255).ToArgb();
            }


            vb.Unlock();


            //defining indices
            short[] indices;
            indices = (short[])ib.Lock(0, 0);

            /*
             * indices[0]=0;indices[1]=1;indices[2]=3;
             * indices[3]=0;indices[3]=3;indices[4]=2;
             */

            indices[0]  = 1; indices[1] = 3; indices[2] = 2;
            indices[3]  = 2; indices[4] = 3; indices[5] = 5;
            indices[6]  = 3; indices[7] = 7; indices[8] = 5;
            indices[9]  = 5; indices[10] = 7; indices[11] = 6;
            indices[12] = 7; indices[13] = 23; indices[14] = 6;
            indices[15] = 23; indices[16] = 17; indices[17] = 6;
            indices[18] = 23; indices[19] = 24; indices[20] = 17;
            indices[21] = 17; indices[22] = 24; indices[23] = 16;
            indices[24] = 17; indices[25] = 16; indices[26] = 0;
            indices[27] = 0; indices[28] = 16; indices[29] = 15;
            indices[30] = 9; indices[31] = 11; indices[32] = 10;
            indices[33] = 9; indices[34] = 10; indices[35] = 8;
            indices[36] = 9; indices[37] = 8; indices[38] = 4;
            indices[39] = 11; indices[40] = 12; indices[41] = 10;
            indices[42] = 10; indices[43] = 12; indices[44] = 18;
            indices[45] = 10; indices[46] = 18; indices[47] = 8;
            indices[48] = 8; indices[49] = 7; indices[50] = 4;
            indices[51] = 8; indices[52] = 18; indices[53] = 7;
            indices[54] = 23; indices[55] = 7; indices[56] = 18;
            indices[57] = 18; indices[58] = 21; indices[59] = 23;
            indices[60] = 21; indices[61] = 24; indices[62] = 23;
            indices[63] = 21; indices[64] = 22; indices[65] = 24;
            indices[66] = 11; indices[67] = 13; indices[68] = 12;
            indices[69] = 12; indices[70] = 13; indices[71] = 18;
            indices[72] = 13; indices[73] = 14; indices[74] = 18;
            indices[75] = 14; indices[76] = 20; indices[77] = 19;
            indices[78] = 20; indices[79] = 21; indices[80] = 19;
            indices[81] = 22; indices[82] = 21; indices[83] = 20;


            ib.Unlock();

            tex = TextureLoader.FromFile(dev, "c:\\test.jpg");

            //////////////////////////////////////////////////////////////

            //div distrib:
            uint[] dd;
            dd     = new uint[25];
            dd[0]  = 2;
            dd[1]  = 2;
            dd[2]  = 2;
            dd[3]  = 2;
            dd[4]  = 0;
            dd[5]  = 2;
            dd[6]  = 1;
            dd[7]  = 1;
            dd[8]  = 0;
            dd[9]  = 0;
            dd[10] = 1;
            dd[11] = 0;
            dd[12] = 0;
            dd[13] = 1;
            dd[14] = 0;
            dd[15] = 2;
            dd[16] = 2;
            dd[17] = 2;
            dd[18] = 1;
            dd[19] = 0;
            dd[20] = 0;
            dd[21] = 0;
            dd[22] = 2;
            dd[23] = 1;
            dd[24] = 2;

            g = new GeometryIndexedSplatted(this.dev, this.vb, this.ib, typeof(FVF_PosColTex), FVF_PosColTex.Format, PrimitiveType.TriangleList, 3, dd);
            g.DivisionTexture[0] = TextureLoader.FromFile(dev, "c:\\t0.jpg");
            g.DivisionTexture[1] = TextureLoader.FromFile(dev, "c:\\t1.jpg");
            g.DivisionTexture[2] = TextureLoader.FromFile(dev, "c:\\t2.jpg");

            g.UpdateDivisionedGeometry();



            this.timer1.Enabled = true;
        }