Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="lastVertexId"></param>
        /// <param name="picker"></param>
        /// <returns></returns>
        internal override uint Search(PickingEventArgs arg, uint lastVertexId, ZeroIndexPicker picker)
        {
            OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 4, DrawMode.Points, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = lastVertexId - 0;
                array[1] = lastVertexId - 1;
                array[2] = lastVertexId - 2;
                array[3] = lastVertexId - 3;
                buffer.UnmapBuffer();
            }
            picker.Renderer.Render4InnerPicking(arg, buffer);
            uint id = ColorCodedPicking.ReadStageVertexId(arg.X, arg.Y);

            buffer.Dispose();
            if (lastVertexId - 3 <= 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)
        {
            int indexesLength = indexes.Length;

            if (indexesLength > this.maxElementCount)
            {
                IndexBuffer original = this.indexBuffer;
                this.indexBuffer     = GLBuffer.Create(IndexBufferElementType.UInt, indexesLength, mode, BufferUsage.StaticDraw);
                this.maxElementCount = indexesLength;
                original.Dispose();
            }

            var    indexBuffer = this.indexBuffer as OneIndexBuffer;
            IntPtr pointer     = indexBuffer.MapBuffer(MapBufferAccess.WriteOnly);

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

            indexBuffer.Mode         = mode;
            indexBuffer.ElementCount = indexesLength;
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        protected override void DoInitialize()
        {
            // init shader program.
            ShaderProgram program = this.shaderCodes.CreateProgram();

            // init property buffer objects.
            VertexBuffer positionBuffer = null;
            IBufferable  model          = this.DataSource;

            VertexBuffer[] vertexAttributeBuffers;
            {
                var list = new List <VertexBuffer>();
                foreach (AttributeMap.NamePair item in this.attributeMap)
                {
                    VertexBuffer buffer = model.GetVertexAttributeBuffer(
                        item.NameInIBufferable, item.VarNameInShader);
                    if (buffer == null)
                    {
                        throw new Exception(string.Format("[{0}] returns null buffer pointer!", model));
                    }
                    if (item.NameInIBufferable == positionNameInIBufferable)
                    {
                        if (positionBuffer != null)
                        {
                            throw new Exception(string.Format("Duplicate position buffer is not allowed!"));
                        }

                        positionBuffer = buffer.Clone() as VertexBuffer;
                        positionBuffer.VarNameInVertexShader = "in_Position";// in_Postion same with in the PickingShader.vert shader
                    }
                    list.Add(buffer);
                }
                vertexAttributeBuffers = list.ToArray();
            }

            // init index buffer
            OneIndexBuffer indexBuffer;
            {
                var mode = DrawMode.Points;//any mode is OK as we'll update it later in other place.
                indexBuffer              = GLBuffer.Create(IndexBufferElementType.UInt, positionBuffer.ByteLength / (positionBuffer.Config.GetDataSize() * positionBuffer.Config.GetDataTypeByteLength()), mode, BufferUsage.StaticDraw);
                this.maxElementCount     = indexBuffer.ElementCount;
                indexBuffer.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.
                GLState glState = new PrimitiveRestartState(indexBuffer.ElementType);
                this.stateList.Add(glState);
            }

            // init VAO.
            var vertexArrayObject = new VertexArrayObject(indexBuffer, vertexAttributeBuffers);

            vertexArrayObject.Initialize(program);

            // sets fields.
            this.Program = program;
            this.vertexAttributeBuffers = vertexAttributeBuffers;
            this.indexBuffer            = indexBuffer;
            this.vertexArrayObject      = vertexArrayObject;

            this.positionBuffer = positionBuffer;
        }
Exemplo n.º 4
0
        /// <summary>
        /// 在三角形图元中拾取指定位置的Point
        /// </summary>
        /// <param name="arg">渲染参数</param>
        /// <param name="x">mouse position(Left Down is (0, 0)).</param>
        /// <param name="y">mouse position(Left Down is (0, 0)).</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)
        {
            // 创建临时索引
            OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 3, DrawMode.Points, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = lastVertexId - 0;
                array[1] = lastVertexId - 1;
                array[2] = lastVertexId - 2;
                buffer.UnmapBuffer();
            }
            // 用临时索引渲染此三角形图元(仅渲染此三角形图元)
            modernRenderer.Render4InnerPicking(arg, buffer);
            // id是拾取到的Line的Last Vertex Id
            uint id = ColorCodedPicking.ReadStageVertexId(x, y);

            buffer.Dispose();

            // 对比临时索引,找到那个Line
            if (lastVertexId - 2 <= id && id <= lastVertexId - 0)
            {
                return(id);
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="flatColorVertexId"></param>
        /// <param name="picker"></param>
        /// <returns></returns>
        internal override uint Search(PickingEventArgs arg,
                                      uint flatColorVertexId, DrawArraysPicker picker)
        {
            IndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 4, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = flatColorVertexId - 0;
                array[1] = flatColorVertexId - 1;
                array[2] = flatColorVertexId - 2;
                array[3] = flatColorVertexId - 3;
                buffer.UnmapBuffer();
            }
            var cmd = new DrawElementsCmd(buffer, DrawMode.Points);

            picker.Node.Render4InnerPicking(arg, IndexAccessMode.ByFrame, cmd);
            uint id = ColorCodedPicking.ReadStageVertexId(arg.X, arg.Y);

            buffer.Dispose();

            if (flatColorVertexId - 3 <= id && id <= flatColorVertexId - 0)
            {
                return(id);
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 在三角形图元中拾取指定位置的Point
        /// </summary>
        /// <param name="arg">渲染参数</param>
        /// <param name="lastVertexId">三角形图元的最后一个顶点</param>
        /// <param name="picker">目标Renderer</param>
        /// <returns></returns>
        internal override uint Search(PickingEventArgs arg, uint lastVertexId, DrawArraysPicker picker)
        {
            // 创建临时索引
            IndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 3, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = lastVertexId - 0;
                array[1] = lastVertexId - 1;
                array[2] = lastVertexId - 2;
                buffer.UnmapBuffer();
            }
            var cmd = new DrawElementsCmd(buffer, DrawMode.Points);

            // 用临时索引渲染此三角形图元(仅渲染此三角形图元)
            picker.Node.Render4InnerPicking(arg, IndexAccessMode.ByFrame, cmd);
            // id是拾取到的Line的Last Vertex Id
            uint id = ColorCodedPicking.ReadStageVertexId(arg.X, arg.Y);

            buffer.Dispose();

            // 对比临时索引,找到那个Line
            if (lastVertexId - 2 <= id && id <= lastVertexId - 0)
            {
                return(id);
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="x">mouse position(Left Down is (0, 0)).</param>
        /// <param name="y">mouse position(Left Down is (0, 0)).</param>
        /// <param name="lastVertexId"></param>
        /// <param name="modernRenderer"></param>
        /// <returns></returns>
        internal override uint Search(RenderEventArgs arg,
                                      int x, int y,
                                      uint lastVertexId, ZeroIndexRenderer modernRenderer)
        {
            OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 3, DrawMode.Points, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = 0;
                array[1] = lastVertexId - 1;
                array[2] = lastVertexId - 0;
                buffer.UnmapBuffer();
            }
            modernRenderer.Render4InnerPicking(arg, buffer);
            uint id = ColorCodedPicking.ReadStageVertexId(x, y);

            buffer.Dispose();

            if (0 == id || lastVertexId - 1 == id || lastVertexId - 0 == id)
            {
                return(id);
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="lastVertexId"></param>
        /// <param name="picker"></param>
        /// <returns></returns>
        internal override uint[] Search(PickingEventArgs arg,
                                        uint lastVertexId, ZeroIndexPicker picker)
        {
            OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 6, DrawMode.Lines, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                array[0] = 0; array[1] = lastVertexId - 1;
                array[2] = lastVertexId - 1; array[3] = lastVertexId - 0;
                array[4] = lastVertexId - 0; array[5] = 0;
                buffer.UnmapBuffer();
            }
            picker.Renderer.Render4InnerPicking(arg, buffer);
            uint id = ColorCodedPicking.ReadStageVertexId(arg.X, arg.Y);

            buffer.Dispose();

            if (id + 1 == lastVertexId)
            {
                return(new uint[] { 0, lastVertexId - 1, });
            }
            else if (id == lastVertexId)
            {
                return(new uint[] { lastVertexId - 1, lastVertexId - 0, });
            }
            else if (id == 0)
            {
                return(new uint[] { lastVertexId - 0, 0, });
            }
            else
            {
                throw new Exception("This should not happen!");
            }
        }
Exemplo n.º 9
0
        private IndexBuffer GetBufferInUInt(int length)
        {
            IndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, length, BufferUsage.StaticDraw);

            unsafe {
                IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                var    array   = (uint *)pointer;
                for (int i = 0; i < model.indexes.Length; i++)
                {
                    array[i] = model.indexes[i];
                }
                buffer.UnmapBuffer();
            }
            return(buffer);
        }
Exemplo n.º 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="lastVertexId"></param>
        /// <param name="picker"></param>
        /// <returns></returns>
        internal override uint[] Search(PickingEventArgs arg,
                                        uint lastVertexId, DrawArraysPicker picker)
        {
            IndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, 8, BufferUsage.StaticDraw);

            unsafe
            {
                var array = (uint *)buffer.MapBuffer(MapBufferAccess.WriteOnly);
                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;
                buffer.UnmapBuffer();
            }
            var cmd = new DrawElementsCmd(buffer, DrawMode.Lines);

            picker.Node.Render4InnerPicking(arg, ControlMode.ByFrame, cmd);
            uint id = ColorCodedPicking.ReadStageVertexId(arg.X, arg.Y);

            buffer.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.º 11
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBuffer GetIndexBuffer()
        {
            if (this.indexBuffer == null)
            {
                int            length = TetrahedronModel.index.Length;
                OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UByte, length, DrawMode.Triangles, BufferUsage.StaticDraw);
                unsafe
                {
                    IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                    var    array   = (byte *)pointer;
                    for (int i = 0; i < TetrahedronModel.index.Length; i++)
                    {
                        array[i] = TetrahedronModel.index[i];
                    }
                    buffer.UnmapBuffer();
                }
                this.indexBuffer = buffer;
            }

            return(this.indexBuffer);
        }
Exemplo n.º 12
0
        private IndexBuffer GetBufferInBytes(int length)
        {
            IndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UByte, length, BufferUsage.StaticDraw);

            unsafe {
                IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                var    array   = (byte *)pointer;
                for (int i = 0; i < model.indexes.Length; i++)
                {
                    if (model.indexes[i] == uint.MaxValue)
                    {
                        array[i] = byte.MaxValue;
                    }
                    else
                    {
                        array[i] = (byte)model.indexes[i];
                    }
                }
                buffer.UnmapBuffer();
            }
            return(buffer);
        }
Exemplo n.º 13
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBuffer GetIndexBuffer()
        {
            if (indexBuffer == null)
            {
                ushort[]       faces  = model.GetFaces();
                int            length = faces.Length;
                OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UShort, length, DrawMode.Triangles, BufferUsage.StaticDraw);
                unsafe
                {
                    IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                    var    array   = (ushort *)pointer;
                    for (int i = 0; i < faces.Length; i++)
                    {
                        array[i] = (ushort)(faces[i] - 1);
                    }
                    buffer.UnmapBuffer();
                }
                this.indexBuffer = buffer;
            }

            return(indexBuffer);
        }
Exemplo n.º 14
0
        public IndexBuffer GetIndexBuffer()
        {
            if (this.indexBuffer == null)
            {
                Face[]         faces  = Teapot.faceData;
                int            length = faces.Length * 3;
                OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UShort, length, DrawMode.Triangles, BufferUsage.StaticDraw);
                unsafe
                {
                    IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                    var    array   = (ushort *)pointer;
                    for (int i = 0; i < faces.Length; i++)
                    {
                        array[i * 3 + 0] = (ushort)(faces[i].vertexId1 - 1);
                        array[i * 3 + 1] = (ushort)(faces[i].vertexId2 - 1);
                        array[i * 3 + 2] = (ushort)(faces[i].vertexId3 - 1);
                    }
                    buffer.UnmapBuffer();
                }
                this.indexBuffer = buffer;
            }

            return(this.indexBuffer);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Creates a <see cref="ShaderStorageBuffer"/> object directly in server side(GPU) without initializing its value.
 /// </summary>
 /// <param name="elementType"></param>
 /// <param name="usage"></param>
 /// <param name="length"></param>
 /// <returns></returns>
 public static ShaderStorageBuffer Create(Type elementType, int length, BufferUsage usage)
 {
     return(GLBuffer.Create(IndependentBufferTarget.ShaderStorageBuffer, elementType, length, usage) as ShaderStorageBuffer);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Creates a <see cref="TextureBuffer"/> object directly in server side(GPU) without initializing its value.
 /// </summary>
 /// <param name="elementType"></param>
 /// <param name="length"></param>
 /// <param name="usage"></param>
 /// <returns></returns>
 public static TextureBuffer Create(Type elementType, int length, BufferUsage usage)
 {
     return(GLBuffer.Create(IndependentBufferTarget.TextureBuffer, elementType, length, usage) as TextureBuffer);
 }
Exemplo n.º 17
0
 /// <summary>
 /// Creates a <see cref="PixelUnpackBuffer"/> object directly in server side(GPU) without initializing its value.
 /// </summary>
 /// <param name="elementType"></param>
 /// <param name="usage"></param>
 /// <param name="length"></param>
 /// <returns></returns>
 public static PixelUnpackBuffer Create(Type elementType, int length, BufferUsage usage)
 {
     return(GLBuffer.Create(IndependentBufferTarget.PixelUnpackBuffer, elementType, length, usage) as PixelUnpackBuffer);
 }
Exemplo n.º 18
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public IndexBuffer GetIndexBuffer()
        {
            if (this.indexBuffer == null)
            {
                int length = model.indexes.Length;
                if (model.positions.Length < byte.MaxValue)
                {
                    OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UByte, length, DrawMode.TriangleStrip, BufferUsage.StaticDraw);
                    unsafe
                    {
                        IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                        var    array   = (byte *)pointer;
                        for (int i = 0; i < model.indexes.Length; i++)
                        {
                            if (model.indexes[i] == uint.MaxValue)
                            {
                                array[i] = byte.MaxValue;
                            }
                            else
                            {
                                array[i] = (byte)model.indexes[i];
                            }
                        }
                        buffer.UnmapBuffer();
                    }
                    this.indexBuffer = buffer;
                }
                else if (model.positions.Length < ushort.MaxValue)
                {
                    OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UShort, length, DrawMode.TriangleStrip, BufferUsage.StaticDraw);
                    unsafe
                    {
                        IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                        var    array   = (ushort *)pointer;
                        for (int i = 0; i < model.indexes.Length; i++)
                        {
                            if (model.indexes[i] == uint.MaxValue)
                            {
                                array[i] = ushort.MaxValue;
                            }
                            else
                            {
                                array[i] = (ushort)model.indexes[i];
                            }
                        }
                        buffer.UnmapBuffer();
                    }
                    this.indexBuffer = buffer;
                }
                else
                {
                    OneIndexBuffer buffer = GLBuffer.Create(IndexBufferElementType.UInt, length, DrawMode.TriangleStrip, BufferUsage.StaticDraw);
                    unsafe
                    {
                        IntPtr pointer = buffer.MapBuffer(MapBufferAccess.WriteOnly);
                        var    array   = (uint *)pointer;
                        for (int i = 0; i < model.indexes.Length; i++)
                        {
                            array[i] = model.indexes[i];
                        }
                        buffer.UnmapBuffer();
                    }
                    this.indexBuffer = buffer;
                }
            }

            return(indexBuffer);
        }
Exemplo n.º 19
0
 /// <summary>
 /// Creates a <see cref="AtomicCounterBuffer"/> object directly in server side(GPU) without initializing its value.
 /// </summary>
 /// <param name="elementType"></param>
 /// <param name="length"></param>
 /// <param name="usage"></param>
 /// <returns></returns>
 public static TransformFeedbackBuffer Create(Type elementType, int length, BufferUsage usage)
 {
     return(GLBuffer.Create(IndependentBufferTarget.TransformFeedbackBuffer, elementType, length, usage) as TransformFeedbackBuffer);
 }
Exemplo n.º 20
0
 /// <summary>
 /// Creates a <see cref="AtomicCounterBuffer"/> object directly in server side(GPU) without initializing its value.
 /// </summary>
 /// <param name="elementType"></param>
 /// <param name="length"></param>
 /// <param name="usage"></param>
 /// <returns></returns>
 public static AtomicCounterBuffer Create(Type elementType, int length, BufferUsage usage)
 {
     return(GLBuffer.Create(IndependentBufferTarget.AtomicCounterBuffer, elementType, length, usage) as AtomicCounterBuffer);
 }