internal override uint Search(RenderEventArgs arg,
                                      int x, int y,
                                      uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer <uint>(DrawMode.Points, BufferUsage.StaticDraw))
            {
                buffer.Create(3);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    array[0] = lastVertexId - 0;
                    array[1] = lastVertexId - 1;
                    array[2] = lastVertexId - 2;
                }
                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (lastVertexId - 2 <= id && id <= lastVertexId - 0)
            {
                return(id);
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 设置要高亮显示的图元。
        /// </summary>
        /// <param name="mode">要高亮显示的图元类型</param>
        /// <param name="indexes">要高亮显示的图元的索引。</param>
        public void SetHighlightIndexes(DrawMode mode, params uint[] indexes)
        {
            var indexBufferPtr = this.indexBufferPtr as OneIndexBufferPtr;
            int indexesLength  = indexes.Length;

            if (indexesLength > this.maxElementCount)
            {
                using (var buffer = new OneIndexBuffer <uint>(
                           indexBufferPtr.Mode, BufferUsage.DynamicDraw))
                {
                    buffer.Alloc(indexesLength);
                    indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
                }
                this.maxElementCount = indexesLength;
            }

            OpenGL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferPtr.BufferId);
            IntPtr pointer = OpenGL.MapBuffer(BufferTarget.ElementArrayBuffer, MapBufferAccess.WriteOnly);

            unsafe
            {
                var array = (uint *)pointer.ToPointer();
                for (int i = 0; i < indexesLength; i++)
                {
                    array[i] = indexes[i];
                }
            }
            OpenGL.UnmapBuffer(BufferTarget.ElementArrayBuffer);
            OpenGL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

            indexBufferPtr.Mode         = mode;
            indexBufferPtr.ElementCount = indexesLength;
        }
        /// <summary>
        /// 设置要高亮显示的图元。
        /// </summary>
        /// <param name="mode">要高亮显示的图元类型</param>
        /// <param name="indexes">要高亮显示的图元的索引。</param>
        public void SetHighlightIndexes(DrawMode mode, params uint[] indexes)
        {
            int indexesLength = indexes.Length;

            if (indexesLength > this.maxElementCount)
            {
                IndexBufferPtr original = this.indexBufferPtr;
                using (var buffer = new OneIndexBuffer(IndexElementType.UInt,
                                                       mode, BufferUsage.DynamicDraw))
                {
                    buffer.Create(indexesLength);
                    this.indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
                }
                this.maxElementCount = indexesLength;
                original.Dispose();
            }

            var    indexBufferPtr = this.indexBufferPtr as OneIndexBufferPtr;
            IntPtr pointer        = indexBufferPtr.MapBuffer(MapBufferAccess.WriteOnly);

            unsafe
            {
                var array = (uint *)pointer.ToPointer();
                for (int i = 0; i < indexesLength; i++)
                {
                    array[i] = indexes[i];
                }
            }
            indexBufferPtr.UnmapBuffer();

            indexBufferPtr.Mode         = mode;
            indexBufferPtr.ElementCount = indexesLength;
        }
Exemplo n.º 4
0
        internal override uint[] Search(RenderEventArgs arg,
                                        int x, int y,
                                        uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer <uint>(DrawMode.Lines, BufferUsage.StaticDraw))
            {
                buffer.Create(8);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    array[0] = lastVertexId - 1; array[1] = lastVertexId - 0;
                    array[2] = lastVertexId - 2; array[3] = lastVertexId - 1;
                    array[4] = lastVertexId - 3; array[5] = lastVertexId - 2;
                    array[6] = lastVertexId - 0; array[7] = lastVertexId - 3;
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (id + 3 == lastVertexId)
            {
                return(new uint[] { id + 3, id, });
            }
            else
            {
                return(new uint[] { id - 1, id, });
            }
        }
Exemplo n.º 5
0
        internal override uint[] Search(RenderEventArgs arg,
                                        int x, int y,
                                        RecognizedPrimitiveIndex lastIndexId,
                                        OneIndexRenderer modernRenderer)
        {
            List <uint> indexList = lastIndexId.IndexIdList;

            if (indexList.Count < 3)
            {
                throw new ArgumentException();
            }

            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer <uint>(DrawMode.LineLoop, BufferUsage.StaticDraw))
            {
                buffer.Create(indexList.Count);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    for (int i = 0; i < indexList.Count; i++)
                    {
                        array[i] = indexList[i];
                    }
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (id == indexList[0])
            {
                return(new uint[] { indexList[indexList.Count - 1], id, });
            }
            else
            {
                uint[] result = null;
                for (int i = 1; i < indexList.Count; i++)
                {
                    if (id == indexList[i])
                    {
                        result = new uint[] { indexList[i - 1], indexList[i], };
                        break;
                    }
                }

                if (result != null)
                {
                    return(result);
                }
                else
                {
                    throw new Exception("This should not happen!");
                }
            }
        }
Exemplo n.º 6
0
        internal override uint[] Search(RenderEventArgs arg,
                                        int x, int y,
                                        RecognizedPrimitiveIndex lastIndexId,
                                        OneIndexRenderer modernRenderer)
        {
            List <uint> indexList = lastIndexId.IndexIdList;

            if (indexList.Count != 4)
            {
                throw new ArgumentException();
            }

            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer <uint>(DrawMode.Lines, BufferUsage.StaticDraw))
            {
                buffer.Create(8);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();

                    array[0] = indexList[0]; array[1] = indexList[1];
                    array[2] = indexList[1]; array[3] = indexList[2];
                    array[4] = indexList[2]; array[5] = indexList[3];
                    array[6] = indexList[3]; array[7] = indexList[0];
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (id == indexList[1])
            {
                return(new uint[] { indexList[0], indexList[1], });
            }
            else if (id == indexList[2])
            {
                return(new uint[] { indexList[1], indexList[2], });
            }
            else if (id == indexList[3])
            {
                return(new uint[] { indexList[2], indexList[3], });
            }
            else if (id == indexList[0])
            {
                return(new uint[] { indexList[2], indexList[0], });
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
Exemplo n.º 7
0
        internal override uint[] Search(RenderEventArgs arg,
                                        int x, int y,
                                        RecognizedPrimitiveInfo primitiveInfo,
                                        OneIndexRenderer modernRenderer)
        {
            uint[] indexList = primitiveInfo.VertexIds;
            if (indexList.Length != 3)
            {
                throw new ArgumentException();
            }

            //if (indexList[0] == indexList[1]) { return new uint[] { indexList[0], indexList[2], }; }
            //else if (indexList[0] == indexList[2]) { return new uint[] { indexList[0], indexList[1], }; }
            //else if (indexList[1] == indexList[2]) { return new uint[] { indexList[1], indexList[0], }; }

            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer(IndexElementType.UInt, DrawMode.Lines, BufferUsage.StaticDraw))
            {
                buffer.Create(6);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    array[0] = indexList[0]; array[1] = indexList[1];
                    array[2] = indexList[1]; array[3] = indexList[2];
                    array[4] = indexList[2]; array[5] = indexList[0];
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, arg.CanvasRect.Height - y - 1);

            indexBufferPtr.Dispose();

            if (id == indexList[1])
            {
                return(new uint[] { indexList[0], indexList[1], });
            }
            else if (id == indexList[2])
            {
                return(new uint[] { indexList[1], indexList[2], });
            }
            else if (id == indexList[0])
            {
                return(new uint[] { indexList[2], indexList[0], });
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        protected override void DoInitialize()
        {
            base.DoInitialize();

            foreach (var item in propertyNameMap)
            {
                PropertyBufferPtr bufferPtr = this.bufferable.GetProperty(
                    item.NameInIBufferable, item.VarNameInShader);
                if (bufferPtr == null)
                {
                    throw new Exception();
                }

                if (item.NameInIBufferable == positionNameInIBufferable)
                {
                    this.positionBufferPtr = new PropertyBufferPtr(
                        "in_Position",// in_Postion same with in the PickingShader.vert shader
                        bufferPtr.BufferId,
                        bufferPtr.DataSize,
                        bufferPtr.DataType,
                        bufferPtr.Length,
                        bufferPtr.ByteLength);
                    break;
                }
            }

            // init index buffer
            {
                //IndexBufferPtr indexBufferPtr = this.bufferable.GetIndex();

                using (var buffer = new OneIndexBuffer <uint>(
                           this.indexBufferPtr.Mode, BufferUsage.DynamicDraw))
                {
                    buffer.Create(this.positionBufferPtr.ByteLength / (this.positionBufferPtr.DataSize * this.positionBufferPtr.DataTypeByteLength));
                    this.indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                }
            }
            {
                var oneIndexBufferPtr = this.indexBufferPtr as OneIndexBufferPtr;
                this.maxElementCount           = oneIndexBufferPtr.ElementCount;
                oneIndexBufferPtr.ElementCount = 0;// 高亮0个图元
            }

            //this.bufferable = null;
            //this.shaderCodes = null;
            //this.propertyNameMap = null;
        }
Exemplo n.º 9
0
        internal override uint[] Search(RenderEventArgs arg,
                                        int x, int y,
                                        uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer(IndexElementType.UInt, DrawMode.Lines, BufferUsage.StaticDraw))
            {
                buffer.Create(8);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    array[0] = lastVertexId - 0; array[1] = lastVertexId - 2;
                    array[2] = lastVertexId - 2; array[3] = lastVertexId - 3;
                    array[4] = lastVertexId - 3; array[5] = lastVertexId - 1;
                    array[6] = lastVertexId - 1; array[7] = lastVertexId - 0;
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, arg.CanvasRect.Height - y - 1);

            indexBufferPtr.Dispose();
            if (id + 2 == lastVertexId)
            {
                return(new uint[] { lastVertexId - 0, lastVertexId - 2, });
            }
            else if (id + 3 == lastVertexId)
            {
                return(new uint[] { lastVertexId - 2, lastVertexId - 3 });
            }
            else if (id + 1 == lastVertexId)
            {
                return(new uint[] { lastVertexId - 3, lastVertexId - 1, });
            }
            else if (id + 0 == lastVertexId)
            {
                return(new uint[] { lastVertexId - 1, lastVertexId - 0, });
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
Exemplo n.º 10
0
        internal override uint Search(RenderEventArgs arg,
                                      int x, int y,
                                      RecognizedPrimitiveIndex lastIndexId,
                                      OneIndexRenderer modernRenderer)
        {
            List <uint> indexList = lastIndexId.IndexIdList;

            if (indexList.Count < 3)
            {
                throw new ArgumentException();
            }

            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer <uint>(DrawMode.Points, BufferUsage.StaticDraw))
            {
                buffer.Alloc(indexList.Count);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    for (int i = 0; i < indexList.Count; i++)
                    {
                        array[i] = indexList[i];
                    }
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            if (id != uint.MaxValue)
            {
                return(id);
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
        internal override uint Search(RenderEventArgs arg,
                                      int x, int y,
                                      RecognizedPrimitiveInfo primitiveInfo,
                                      OneIndexRenderer modernRenderer)
        {
            uint[] indexList = primitiveInfo.VertexIds;
            if (indexList.Length < 3)
            {
                throw new ArgumentException();
            }

            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer(IndexElementType.UInt, DrawMode.Points, BufferUsage.StaticDraw))
            {
                buffer.Create(indexList.Length);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    for (int i = 0; i < indexList.Length; i++)
                    {
                        array[i] = indexList[i];
                    }
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            uint id = ColorCodedPicking.ReadPixel(x, arg.CanvasRect.Height - y - 1);

            indexBufferPtr.Dispose();

            if (id != uint.MaxValue)
            {
                return(id);
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
Exemplo n.º 12
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBufferPtr GetIndexBufferPtr()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new OneIndexBuffer(IndexElementType.UByte, DrawMode.Quads, BufferUsage.StaticDraw))
                {
                    buffer.Create(indexes.Length);
                    unsafe
                    {
                        var array = (byte *)buffer.Header.ToPointer();
                        for (int i = 0; i < indexes.Length; i++)
                        {
                            array[i] = indexes[i];
                        }
                    }
                    indexBufferPtr = buffer.GetBufferPtr();
                }
            }

            return(indexBufferPtr);
        }
Exemplo n.º 13
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new OneIndexBuffer <uint>(
                           this.model.mode, BufferUsage.StaticDraw))
                {
                    buffer.Create(this.model.indexes.Length);
                    unsafe
                    {
                        var array = (uint *)buffer.Header.ToPointer();
                        for (int i = 0; i < this.model.indexes.Length; i++)
                        {
                            array[i] = this.model.indexes[i];
                        }
                    }
                    indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                }
            }

            return(indexBufferPtr);
        }
Exemplo n.º 14
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new OneIndexBuffer <byte>(DrawMode.Triangles, BufferUsage.StaticDraw))
                {
                    buffer.Create(TetrahedronModel.index.Length);
                    unsafe
                    {
                        var array = (byte *)buffer.Header.ToPointer();
                        for (int i = 0; i < TetrahedronModel.index.Length; i++)
                        {
                            array[i] = TetrahedronModel.index[i];
                        }
                    }

                    indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                }
            }

            return(indexBufferPtr);
        }
        /// <summary>
        /// 在三角形图元中拾取指定位置的Point
        /// </summary>
        /// <param name="arg">渲染参数</param>
        /// <param name="x">指定位置</param>
        /// <param name="y">指定位置</param>
        /// <param name="lastVertexId">三角形图元的最后一个顶点</param>
        /// <param name="modernRenderer">目标Renderer</param>
        /// <returns></returns>
        internal override uint Search(RenderEventArgs arg,
                                      int x, int y,
                                      uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            // 创建临时索引
            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer(IndexElementType.UInt, DrawMode.Points, BufferUsage.StaticDraw))
            {
                buffer.Create(3);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    array[0] = lastVertexId - 0;
                    array[1] = lastVertexId - 1;
                    array[2] = lastVertexId - 2;
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            // 用临时索引渲染此三角形图元(仅渲染此三角形图元)
            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            // id是拾取到的Line的Last Vertex Id
            uint id = ColorCodedPicking.ReadPixel(x, arg.CanvasRect.Height - y - 1);

            indexBufferPtr.Dispose();

            // 对比临时索引,找到那个Line
            if (lastVertexId - 2 <= id && id <= lastVertexId - 0)
            {
                return(id);
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
        /// <summary>
        /// 在三角形图元中拾取指定位置的Line
        /// </summary>
        /// <param name="arg">渲染参数</param>
        /// <param name="x">指定位置</param>
        /// <param name="y">指定位置</param>
        /// <param name="lastVertexId">三角形图元的最后一个顶点</param>
        /// <param name="modernRenderer">目标Renderer</param>
        /// <returns></returns>
        internal override uint[] Search(RenderEventArgs arg,
                                        int x, int y,
                                        uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            // 创建临时索引
            OneIndexBufferPtr indexBufferPtr = null;

            using (var buffer = new OneIndexBuffer <uint>(DrawMode.Lines, BufferUsage.StaticDraw))
            {
                buffer.Create(6);
                unsafe
                {
                    var array = (uint *)buffer.Header.ToPointer();
                    array[0] = lastVertexId - 1; array[1] = lastVertexId - 0;
                    array[2] = lastVertexId - 2; array[3] = lastVertexId - 1;
                    array[4] = lastVertexId - 0; array[5] = lastVertexId - 2;
                }

                indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
            }

            // 用临时索引渲染此三角形图元(仅渲染此三角形图元)
            modernRenderer.Render4InnerPicking(arg, indexBufferPtr);
            // id是拾取到的Line的Last Vertex Id
            uint id = ColorCodedPicking.ReadPixel(x, y, arg.CanvasRect.Height);

            indexBufferPtr.Dispose();

            // 对比临时索引,找到那个Line
            if (id + 2 == lastVertexId)
            {
                return(new uint[] { id + 2, id, });
            }
            else
            {
                return(new uint[] { id - 1, id, });
            }
        }
Exemplo n.º 17
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new OneIndexBuffer <ushort>(DrawMode.Triangles, BufferUsage.StaticDraw))
                {
                    ushort[] faces = model.GetFaces();
                    buffer.Create(faces.Length);
                    unsafe
                    {
                        var array = (ushort *)buffer.Header.ToPointer();
                        for (int i = 0; i < faces.Length; i++)
                        {
                            array[i] = (ushort)(faces[i] - 1);
                        }
                    }

                    indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                }
            }

            return(indexBufferPtr);
        }
Exemplo n.º 18
0
        /// <summary>
        /// 将共享点前移,然后重新渲染、拾取
        /// </summary>
        /// <param name="recognizedPrimitiveIndex0"></param>
        /// <param name="recognizedPrimitiveIndex1"></param>
        /// <param name="drawMode"></param>
        /// <param name="oneIndexBufferPtr"></param>
        /// <param name="lastIndex0"></param>
        /// <param name="lastIndex1"></param>
        private void AssembleIndexBuffer(
            RecognizedPrimitiveIndex recognizedPrimitiveIndex0,
            RecognizedPrimitiveIndex recognizedPrimitiveIndex1,
            DrawMode drawMode,
            out OneIndexBufferPtr oneIndexBufferPtr,
            out uint lastIndex0, out uint lastIndex1)
        {
            List <uint> indexArray = ArrangeIndexes(
                recognizedPrimitiveIndex0, recognizedPrimitiveIndex1, drawMode,
                out lastIndex0, out lastIndex1);

            if (indexArray.Count !=
                recognizedPrimitiveIndex0.IndexIdList.Count
                + 1
                + recognizedPrimitiveIndex1.IndexIdList.Count)
            {
                throw new Exception();
            }

            using (var indexBuffer = new OneIndexBuffer <uint>(drawMode, BufferUsage.StaticDraw))
            {
                indexBuffer.Create(
                    recognizedPrimitiveIndex0.IndexIdList.Count
                    + 1
                    + recognizedPrimitiveIndex1.IndexIdList.Count);
                unsafe
                {
                    var array = (uint *)indexBuffer.Header.ToPointer();
                    for (int i = 0; i < indexArray.Count; i++)
                    {
                        array[i] = indexArray[i];
                    }
                }

                oneIndexBufferPtr = indexBuffer.GetBufferPtr() as OneIndexBufferPtr;
            }
        }
Exemplo n.º 19
0
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                using (var buffer = new OneIndexBuffer <ushort>(DrawMode.Triangles, BufferUsage.StaticDraw))
                {
                    buffer.Alloc(model.faces.Count * 3);
                    unsafe
                    {
                        var array = (ushort *)buffer.Header.ToPointer();
                        for (int i = 0; i < model.faces.Count; i++)
                        {
                            array[i * 3 + 0] = (ushort)(model.faces[i].Item1 - 1);
                            array[i * 3 + 1] = (ushort)(model.faces[i].Item2 - 1);
                            array[i * 3 + 2] = (ushort)(model.faces[i].Item3 - 1);
                        }
                    }

                    indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                }
            }

            return(indexBufferPtr);
        }
        /// <summary>
        ///
        /// </summary>
        protected override void DoInitialize()
        {
            // init shader program.
            ShaderProgram program = this.shaderCodes.CreateProgram();

            // init property buffer objects.
            VertexAttributeBufferPtr positionBufferPtr = null;
            IBufferable model = this.Model;

            VertexAttributeBufferPtr[] vertexAttributeBufferPtrs;
            {
                var list = new List <VertexAttributeBufferPtr>();
                foreach (var item in this.attributeMap)
                {
                    VertexAttributeBufferPtr bufferPtr = model.GetVertexAttributeBufferPtr(
                        item.NameInIBufferable, item.VarNameInShader);
                    if (bufferPtr == null)
                    {
                        throw new Exception(string.Format("[{0}] returns null buffer pointer!", model));
                    }
                    if (item.NameInIBufferable == positionNameInIBufferable)
                    {
                        positionBufferPtr = new VertexAttributeBufferPtr(
                            "in_Position",// in_Postion same with in the PickingShader.vert shader
                            bufferPtr.BufferId,
                            bufferPtr.Config,
                            bufferPtr.Length,
                            bufferPtr.ByteLength);
                    }
                    list.Add(bufferPtr);
                }
                vertexAttributeBufferPtrs = list.ToArray();
            }

            // init index buffer
            OneIndexBufferPtr indexBufferPtr;
            {
                using (var buffer = new OneIndexBuffer(IndexElementType.UInt,
                                                       DrawMode.Points, // any mode is OK as we'll update it later in other place.
                                                       BufferUsage.DynamicDraw))
                {
                    buffer.Create(positionBufferPtr.ByteLength / (positionBufferPtr.DataSize * positionBufferPtr.DataTypeByteLength));
                    indexBufferPtr = buffer.GetBufferPtr() as OneIndexBufferPtr;
                }
                this.maxElementCount        = indexBufferPtr.ElementCount;
                indexBufferPtr.ElementCount = 0;// 高亮0个图元
                // RULE: Renderer takes uint.MaxValue, ushort.MaxValue or byte.MaxValue as PrimitiveRestartIndex. So take care this rule when designing a model's index buffer.
                GLSwitch glSwitch = new PrimitiveRestartSwitch(indexBufferPtr);
                this.switchList.Add(glSwitch);
            }

            // init VAO.
            var vertexArrayObject = new VertexArrayObject(indexBufferPtr, vertexAttributeBufferPtrs);

            vertexArrayObject.Initialize(program);

            // sets fields.
            this.Program = program;
            this.vertexAttributeBufferPtrs = vertexAttributeBufferPtrs;
            this.indexBufferPtr            = indexBufferPtr;
            this.vertexArrayObject         = vertexArrayObject;

            this.positionBufferPtr = positionBufferPtr;
        }
Exemplo n.º 21
0
        public IndexBufferPtr GetIndex()
        {
            if (indexBufferPtr == null)
            {
                if (model.positions.Length < byte.MaxValue)
                {
                    using (var buffer = new OneIndexBuffer <byte>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Alloc(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (byte *)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                if (model.indexes[i] == uint.MaxValue)
                                {
                                    indexArray[i] = byte.MaxValue;
                                }
                                else
                                {
                                    indexArray[i] = (byte)model.indexes[i];
                                }
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
                else if (model.positions.Length < ushort.MaxValue)
                {
                    using (var buffer = new OneIndexBuffer <ushort>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Alloc(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (ushort *)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                if (model.indexes[i] == uint.MaxValue)
                                {
                                    indexArray[i] = ushort.MaxValue;
                                }
                                else
                                {
                                    indexArray[i] = (ushort)model.indexes[i];
                                }
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
                else
                {
                    using (var buffer = new OneIndexBuffer <uint>(DrawMode.TriangleStrip, BufferUsage.StaticDraw))
                    {
                        buffer.Alloc(model.indexes.Length);
                        unsafe
                        {
                            var indexArray = (uint *)buffer.Header.ToPointer();
                            for (int i = 0; i < model.indexes.Length; i++)
                            {
                                indexArray[i] = model.indexes[i];
                            }
                        }

                        indexBufferPtr = buffer.GetBufferPtr() as IndexBufferPtr;
                    }
                }
            }

            return(indexBufferPtr);
        }