Example #1
0
        /// <summary></summary>
        /// <param name="state"></param>
        public void Draw(DrawState state)
        {
            DrawItems(state);

            //items need to be added to the partition
            if (addList.Count > 0)
            {
                processingActive = true;

                state.PushPreCuller(preCuller);
                Vector3 min, max;

                //add as many items as possible in a sortof-random order
                //most tree structures work best when the data that is added
                //is in as random order as possible.
                //for example, if all the items are in a straight line,
                //then some tree structures will be very badly unbalanced

                //randomize the array
                for (int i = 0; i < addList.Count; i++)
                {
                    if (random == null)
                    {
                        random = new Random();
                    }

                    //swap two random items
                    int indexA = random.Next(addList.Count);
                    int indexB = random.Next(addList.Count);

                    IDraw itemA = addList[indexA];
                    IDraw itemB = addList[indexB];

                    addList[indexB] = itemA;
                    addList[indexA] = itemB;
                }

                preCuller.BeginPreCullItem(state);

                foreach (IDraw item in addList)
                {
                    preCuller.ResetPreCullItem();

                    if (item.CullTest(state))
                    {
                        item.Draw(state);
                    }

                    if (preCuller.TryGetBounds(out min, out max))
                    {
                        AddItem(item, ref min, ref max);
                    }
                    else
                    {
                        drawList.Add(item);
                    }
                }
                addList.Clear();

                //reset the capacity on the first draw,
                //as there are usually a lot of objects added in the first draw
                if (drawList.Count == 0)
                {
                    if (firstDraw)
                    {
                        addList.Capacity  = 4;
                        drawList.Capacity = 4;
                    }
                    if (runOptimize)
                    {
                        this.OptimizeContents();
                    }
                    firstDraw   = false;
                    runOptimize = false;
                }

                if (drawList.Count > 0)
                {
                    addList.AddRange(drawList);
                }
                drawList.Clear();

                state.PopPreCuller();

                processingActive = false;
            }
        }