Beispiel #1
0
 protected int SetIndexBuffer(IndexBufferDef indexbufferid)
 {
     KeyValuePair<Buffer, int> indexbuffer;
     if (Indexbuffers.ContainsKey(indexbufferid.Id))
         indexbuffer = Indexbuffers[indexbufferid.Id];
     else
         indexbuffer = AllocateIndexBuffer(indexbufferid);
     Renderer.Context.InputAssembler.SetIndexBuffer(indexbuffer.Key, Format.R16_UInt, 0);
     return indexbuffer.Value;
 }
Beispiel #2
0
        protected KeyValuePair<Buffer, int> AllocateIndexBuffer(IndexBufferDef indexbufferid)
        {
            var lod = indexbufferid.Lod;
            var highword = indexbufferid.Highword;
            var indicesStream = new DataStream(sizeof(uint) * QuadEdgeLength * (indexbufferid.Border == 0 ? 2 : 6), true, true);
            var indicesCount = 0;
            var borderwidth = 1 + lod;
            switch (indexbufferid.Id & 0xF)
            {
                case 0x0://Quad Indices
                    for (int i = borderwidth; i < QuadEdgeLength - borderwidth; i++)
                    {
                        indicesStream.Write((short)(QuadEdgeLength * highword + i));
                        indicesStream.Write((short)i);
                        indicesCount += 2;
                        for (int j = 0; j < lod; j++)
                            if (i + 2 < QuadEdgeLength - borderwidth)//So last Index is always part
                                i++;
                            else
                                break;
                    }
                    break;
                case 0x1://Border top
                    if ((indexbufferid.Id & 0x10) == 0x10)
                    {//stitch
                        #region BuildIndicesLists
                        var IndicesA = new List<int>();
                        for (int i = 0; i < QuadEdgeLength - borderwidth; i++)
                        {
                            IndicesA.Add(i * QuadEdgeLength);// * QuadEdgeLength for horizontal iteration (top & bot border)
                            for (uint j = 0; j < lod; j++)
                                if (i + 2 < QuadEdgeLength - borderwidth)//So last Index is always part
                                    i++;
                                else
                                    break;
                        }
                        IndicesA.Add(QuadEdgeLength-1);

                        var IndicesB = new List<int>();
                        var borderwidthB = (highword + 1);
                        for (int i = 0; i < QuadEdgeLength - borderwidthB; i++)
                        {
                            IndicesB.Add(i * QuadEdgeLength);// * QuadEdgeLength for horizontal iteration (top & bot border)
                            for (int j = 0; j < highword; j++)
                                if (i + 2 < QuadEdgeLength - borderwidthB)
                                    i++;
                                else
                                    break;
                        }
                        IndicesB.Add(QuadEdgeLength - 1);
                        #endregion
                        #region DrawStitchedBorder
                        int edgeanchor = IndicesA[1];
                        for (int i = 0; IndicesB[i] < edgeanchor; i++)
                        {//Draw left edge
                            indicesStream.Write((short)(edgeanchor + borderwidth));
                            indicesStream.Write((short)(IndicesB[i+1]));
                            indicesStream.Write((short)(IndicesB[i]));
                            indicesCount += 3;
                        }

                        for(int i = 1; i < IndicesA.Count - 2; i++)//-1 because right edge needs to be drawn seperately and -1 because each iteration uses i and i+1 as anchors
                        {
                            var anchorA = IndicesA[i];
                            var anchorB = IndicesA[i+1];
                            var partnerpoints = IndicesB.Where(index => index >= anchorA && index < anchorB).ToList();
                            partnerpoints.Add(IndicesB.First(index=> index>= anchorB));
                            var firstsmall = partnerpoints.Count/2;
                            for (int j = 0; j < firstsmall; j++)
                            {
                                indicesStream.Write((short)(anchorA + borderwidth));
                                indicesStream.Write((short)(partnerpoints[j + 1]));
                                indicesStream.Write((short)(partnerpoints[j]));
                                indicesCount += 3;
                            }
                            indicesStream.Write((short)(anchorA + borderwidth));
                            indicesStream.Write((short)(anchorB + borderwidth));
                            indicesStream.Write((short)(partnerpoints[firstsmall]));
                            indicesCount += 3;
                            for (int j = firstsmall; j < partnerpoints.Count-1; j++)
                            {
                                indicesStream.Write((short)(anchorB + borderwidth));
                                indicesStream.Write((short)(partnerpoints[j + 1]));
                                indicesStream.Write((short)(partnerpoints[j]));
                                indicesCount += 3;
                            }
                        }

                        edgeanchor = IndicesA[IndicesA.Count-2];
                        for (int i = IndicesB.Count-2; IndicesB[i] >= edgeanchor; i--)
                        {//Draw left edge
                            indicesStream.Write((short)(edgeanchor + borderwidth));
                            indicesStream.Write((short)(IndicesB[i+1]));
                            indicesStream.Write((short)(IndicesB[i]));
                            indicesCount += 3;
                        }
                        #endregion
                    }
                    else
                    {//nostitch
                        #region DrawNonStitchedBorder
                        indicesStream.Write((short)(0));
                        indicesStream.Write((short)((lod + 1)*QuadEdgeLength + borderwidth));
                        indicesStream.Write((short)((lod + 1) * QuadEdgeLength));
                        indicesCount += 3;

                        int i = borderwidth;
                        int nexti = i;
                        while(i < QuadEdgeLength - 1 - borderwidth)
                        {
                            for (int j = 0; j < lod + 1; j++)
                                if (nexti + 1 < QuadEdgeLength - borderwidth)//So last Index is always part
                                    nexti++;
                                else
                                    break; //j-loop
                            indicesStream.Write((short)(i*QuadEdgeLength));
                            indicesStream.Write((short)(i*QuadEdgeLength + borderwidth));
                            indicesStream.Write((short)(nexti * QuadEdgeLength));

                            indicesStream.Write((short)(nexti * QuadEdgeLength));
                            indicesStream.Write((short)(i*QuadEdgeLength + borderwidth));
                            indicesStream.Write((short)(nexti * QuadEdgeLength + borderwidth));

                            indicesCount += 6;
                            i = nexti;
                        }
                        int k = QuadEdgeLength - borderwidth - 1;
                        indicesStream.Write((short)(k*QuadEdgeLength));
                        indicesStream.Write((short)(k*QuadEdgeLength + borderwidth));
                        indicesStream.Write((short)((QuadEdgeLength - 1) * QuadEdgeLength));
                        indicesCount += 3;

                        indicesCount += 3;
                        #endregion
                    }
                    break;
                case 0x2://Border bot

                    break;
                case 0x4://Border left

                    break;
                case 0x8://Border right

                    break;
            }
            indicesStream.Position = 0;
            var buffer = new Buffer(Renderer.D3DDevice, indicesStream, sizeof(uint) * indicesCount, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
            var indexbuffer = new KeyValuePair<Buffer, int>(buffer, indicesCount);
            Indexbuffers.Add(indexbufferid.Id, indexbuffer);
            indicesStream.Dispose();
            return indexbuffer;
        }