예제 #1
0
        //Gets a division IB, generated by an array of vertices
        //if exclusive is on then only the geometry generated exclusively with the vertices is returned (only primitves that are made up entirely by vertices from vertices parameter)
        //vertices must hold short objects that hold vertices indices
        public IndexBuffer GetGeneratedDivisionIB(System.Collections.ArrayList vertices, bool exclusive)
        {
            //Alert - GettingGeneratedDivisionIB
            if (this.GettingGeneratedDivisionIB != null)
            {
                this.GettingGeneratedDivisionIB(new GettingGeneratedDivisionIBInfo(GettingGeneratedDivisionIBStage.Starting, -1));
            }

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

            //can only use TriangleList or LineList for now, if other is used then announce the user
            if (step == 0)
            {
                throw new ExceptionGeometry("Cannot handle other primitive type than TriangleList or LineList for now. CANNOT GENERATE DIVISION! (change the primitve type to one of these)");
            }

            //holds all the primitves found
            System.Collections.ArrayList primitives;
            primitives = new System.Collections.ArrayList();


            int i, k;

            //Alert - GettingGeneratedDivisionIB
            if (this.GettingGeneratedDivisionIB != null)
            {
                this.GettingGeneratedDivisionIB(new GettingGeneratedDivisionIBInfo(GettingGeneratedDivisionIBStage.CheckingPrimitive, -1));
            }

            //for each primitve
            for (i = 0; i <= this.NumIndices - 1; i += step)
            {
                //Alert - GettingGeneratedDivisionIB
                if ((this.GettingGeneratedDivisionIB != null) && (this.FrequentAlerts))
                {
                    this.GettingGeneratedDivisionIB(new GettingGeneratedDivisionIBInfo(GettingGeneratedDivisionIBStage.CheckingPrimitive, i / step));
                }

                short[] prim;                 //primitve to hold this current triangle/array in case of needing to be added later
                prim = new short[step];

                bool foundOne = false, foundAll = true;
                //for each vertex of this current primitve
                for (k = 0; k <= step - 1; k++)
                {
                    prim[k] = indices[i + k];
                    if (Misc.ArrayListContainsInt(vertices, indices[i + k]))
                    {
                        foundOne = true;
                    }
                    else
                    {
                        foundAll = false;
                    }
                }

                //see if this primitive should be added or not
                bool add = false;
                if (exclusive == true)
                {
                    if (foundAll == true)
                    {
                        add = true;
                    }
                }
                else
                {
                    if (foundOne == true)
                    {
                        add = true;
                    }
                }

                //add this primitve (which was hold in prim) if case
                if (add == true)
                {
                    primitives.Add(prim);
                }
            }

            this._IB.Unlock();

            //Alert - GettingGeneratedDivisionIB
            if (this.GettingGeneratedDivisionIB != null)
            {
                this.GettingGeneratedDivisionIB(new GettingGeneratedDivisionIBInfo(GettingGeneratedDivisionIBStage.WriteNewIB, -1));
            }

            //an IB that will hold our division - must transfer the primitves found to it
            IndexBuffer genIB;

            genIB = new IndexBuffer(typeof(short), primitives.Count * step, this.Device, Usage.SoftwareProcessing, Pool.Default);

            indices = (short[])genIB.Lock(0, 0);

            i = 0;
            foreach (short[] prim in primitives)
            {
                for (k = 0; k <= step - 1; k++)
                {
                    indices[i + k] = prim[k];
                }

                i += step;
            }

            genIB.Unlock();

            return(genIB);
        }