Esempio n. 1
0
        private static DrawElementsType GetOpenTKIndexType(IndexDataElementType indexType)
        {
            switch (indexType)
            {
            default:
            case IndexDataElementType.UnsignedByte: return(DrawElementsType.UnsignedByte);

            case IndexDataElementType.UnsignedShort: return(DrawElementsType.UnsignedShort);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Uploads the specified indices into a subsection of this buffer, keeping all other content.
        /// This method changes neither size nor index element type associated with this buffer.
        /// This method is unsafe. Where possible, prefer the overload that accepts a managed array.
        ///
        /// Index data is optional - if not specified, vertices will be rendered in order of definition.
        /// </summary>
        /// <param name="bufferIndex">Offset in this buffer to which the new data will be copied, as number of elements.</param>
        /// <param name="indexType"></param>
        /// <param name="indexData"></param>
        /// <param name="indexCount"></param>
        public void LoadIndexSubData(IndexDataElementType indexType, int bufferIndex, IntPtr indexData, int indexCount)
        {
            this.EnsureNativeIndex();

            int indexSize = GetIndexElementSize(indexType);

            this.nativeIndex.LoadSubData(
                IntPtr.Add(IntPtr.Zero, bufferIndex * indexSize),
                indexData,
                indexCount);
        }
Esempio n. 3
0
        /// <summary>
        /// Uploads the specified indices to the GPU using a specific <see cref="IndexDataElementType"/>
        /// to describe the data type that is used.
        /// This method is unsafe. Where possible, prefer the overload that accepts a managed array.
        ///
        /// Index data is optional - if not specified, vertices will be rendered in order of definition.
        /// </summary>
        /// <param name="indexType"></param>
        /// <param name="indexData"></param>
        /// <param name="indexCount"></param>
        public void LoadIndexData(IndexDataElementType indexType, IntPtr indexData, int indexCount)
        {
            this.EnsureNativeIndex();

            this.indexCount = indexCount;
            this.indexType  = indexType;

            int indexSize = GetIndexElementSize(indexType);

            this.nativeIndex.LoadData(indexData, indexCount * indexSize);
        }
Esempio n. 4
0
        /// <summary>
        /// Uploads the specified indices to the GPU.
        ///
        /// Index data is optional - if not specified, vertices will be rendered in order of definition.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <param name="count"></param>
        public void LoadIndexData <T>(T[] data, int index, int count) where T : struct
        {
            IndexDataElementType elementType = GetIndexElementType <T>();

            this.EnsureNativeIndex();

            this.indexCount = count;
            this.indexType  = elementType;

            this.nativeIndex.LoadData(data, index, count);
        }
Esempio n. 5
0
        /// <summary>
        /// Clears this buffer from all previous index data and re-initializes it
        /// with the specified number of index elements.
        ///
        /// Index data is optional - if not specified, vertices will be rendered in order of definition.
        /// </summary>
        public void SetupIndexData(IndexDataElementType indexType, int count)
        {
            this.EnsureNativeVertex();

            this.indexCount = count;
            this.indexType  = indexType;

            int indexSize = GetIndexElementSize(indexType);

            this.nativeVertex.SetupEmpty(count * indexSize);
        }
Esempio n. 6
0
        /// <summary>
        /// Clears this buffer from all previous index data and re-initializes it
        /// with the specified number of index elements.
        ///
        /// Index data is optional - if not specified, vertices will be rendered in order of definition.
        /// </summary>
        public void SetupIndexData <T>(int count) where T : struct
        {
            IndexDataElementType elementType = GetIndexElementType <T>();

            this.EnsureNativeVertex();

            this.indexCount = count;
            this.indexType  = elementType;

            this.nativeVertex.SetupEmpty <T>(count);
        }
Esempio n. 7
0
        private static int GetIndexElementSize(IndexDataElementType indexType)
        {
            int indexSize = 0;

            if (indexType == IndexDataElementType.UnsignedByte)
            {
                indexSize = sizeof(byte);
            }
            else if (indexType == IndexDataElementType.UnsignedShort)
            {
                indexSize = sizeof(ushort);
            }
            return(indexSize);
        }
Esempio n. 8
0
        /// <summary>
        /// Draws the vertices of a single <see cref="DrawBatch"/>, after all other rendering state
        /// has been set up accordingly outside this method.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="ranges"></param>
        /// <param name="mode"></param>
        private void DrawVertexBatch(VertexBuffer buffer, RawList <VertexDrawRange> ranges, VertexMode mode)
        {
            NativeGraphicsBuffer indexBuffer = (buffer.IndexCount > 0 ? buffer.NativeIndex : null) as NativeGraphicsBuffer;
            IndexDataElementType indexType   = buffer.IndexType;

            // Since the QUADS primitive is deprecated in OpenGL 3.0 and not available in OpenGL ES,
            // we'll emulate this with an ad-hoc index buffer object that we generate here.
            if (mode == VertexMode.Quads)
            {
                if (indexBuffer != null)
                {
                    Logs.Core.WriteWarning(
                        "Rendering {0} instances that use index buffers is not supported for quads geometry. " +
                        "To use index buffers, use any other geometry type listed in {1}. Skipping batch.",
                        typeof(DrawBatch).Name,
                        typeof(VertexMode).Name);
                    return;
                }

                NativeGraphicsBuffer.Bind(GraphicsBufferType.Index, this.sharedBatchIBO);

                this.GenerateQuadIndices(ranges, this.sharedBatchIndices);
                this.sharedBatchIBO.LoadData(
                    this.sharedBatchIndices.Data,
                    0,
                    this.sharedBatchIndices.Count);

                GL.DrawElements(
                    PrimitiveType.Triangles,
                    this.sharedBatchIndices.Count,
                    DrawElementsType.UnsignedShort,
                    IntPtr.Zero);

                this.sharedBatchIndices.Clear();
            }
            // Otherwise, run the regular rendering path
            else
            {
                // Rendering using index buffer
                if (indexBuffer != null)
                {
                    if (ranges != null && ranges.Count > 0)
                    {
                        Logs.Core.WriteWarning(
                            "Rendering {0} instances that use index buffers do not support specifying vertex ranges, " +
                            "since the two features are mutually exclusive.",
                            typeof(DrawBatch).Name,
                            typeof(VertexMode).Name);
                    }

                    NativeGraphicsBuffer.Bind(GraphicsBufferType.Index, indexBuffer);

                    PrimitiveType    openTkMode      = GetOpenTKVertexMode(mode);
                    DrawElementsType openTkIndexType = GetOpenTKIndexType(indexType);
                    GL.DrawElements(
                        openTkMode,
                        buffer.IndexCount,
                        openTkIndexType,
                        IntPtr.Zero);
                }
                // Rendering using an array of vertex ranges
                else
                {
                    NativeGraphicsBuffer.Bind(GraphicsBufferType.Index, null);

                    PrimitiveType     openTkMode = GetOpenTKVertexMode(mode);
                    VertexDrawRange[] rangeData  = ranges.Data;
                    int rangeCount = ranges.Count;
                    for (int r = 0; r < rangeCount; r++)
                    {
                        GL.DrawArrays(
                            openTkMode,
                            rangeData[r].Index,
                            rangeData[r].Count);
                    }
                }
            }
        }