Пример #1
0
 /// <summary>
 /// Actually create this BufferObject resources.
 /// </summary>
 /// <param name="ctx">
 /// A <see cref="GraphicsContext"/> used for allocating resources.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// Exception thrown if <paramref name="ctx"/> is null.
 /// </exception>
 /// <exception cref="ArgumentException">
 /// Exception thrown if <paramref name="ctx"/> is not current on the calling thread.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// Exception thrown if this BufferObject has not client memory allocated and the hint is different from
 /// <see cref="BufferObjectHint.StaticCpuDraw"/> or <see cref="BufferObjectHint.DynamicCpuDraw"/>.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// Exception thrown if this BufferObject is currently mapped.
 /// </exception>
 protected override void CreateObject(GraphicsContext ctx)
 {
     // Base implementation
     base.CreateObject(ctx);
     // Compute necessary information to support primitive restart even if the platform does not implement it
     if (!PrimitiveRestart.IsPrimitiveRestartSupported(ctx) && RestartIndexEnabled)
     {
         PrimitiveRestartOffsets = GetRestartIndices(out PrimitiveRestartCounts);
     }
 }
            /// <summary>
            /// Draw the elements instances
            /// </summary>
            ///  <param name="ctx">
            /// The <see cref="GraphicsContext"/> used for drawing.
            /// </param>
            /// <param name="instances">
            /// A <see cref="UInt32"/> that specify the number of instances to draw.
            /// </param>
            public override void DrawInstanced(GraphicsContext ctx, uint instances)
            {
                CheckCurrentContext(ctx);

                ArrayBufferObjectBase.IArraySection arraySection = ArrayIndices.GetArraySection(0);
                Debug.Assert(arraySection != null);

                // Element array must be (re)bound
                ctx.Bind(ArrayIndices, true);

                // Enable restart primitive?
                if (ArrayIndices.RestartIndexEnabled)
                {
                    if (PrimitiveRestart.IsPrimitiveRestartSupported(ctx))
                    {
                        // Enable primitive restart
                        PrimitiveRestart.EnablePrimitiveRestart(ctx, ArrayIndices.ElementsType);
                        // Draw elements as usual
                        DrawElementsInstanced(ctx, arraySection.Pointer, instances);
                        // Disable primitive restart
                        PrimitiveRestart.DisablePrimitiveRestart(ctx);
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
                else
                {
                    uint count = (ElementCount == 0) ? ArrayIndices.ItemCount : ElementCount;
                    Debug.Assert(count - ElementOffset <= ArrayIndices.ItemCount, "element indices array out of bounds");

                    // Draw vertex arrays by indices
                    DrawElementsInstanced(ctx, arraySection.Pointer, instances);
                }
            }
            /// <summary>
            /// Draw the elements.
            /// </summary>
            /// <param name="ctx">
            /// The <see cref="GraphicsContext"/> used for drawing.
            /// </param>
            public override void Draw(GraphicsContext ctx)
            {
                CheckCurrentContext(ctx);

                ArrayBufferObjectBase.IArraySection arraySection = ArrayIndices.GetArraySection(0);
                Debug.Assert(arraySection != null);

                // Element array must be (re)bound
                ctx.Bind(ArrayIndices, true);

                // Enable restart primitive?
                if (ArrayIndices.RestartIndexEnabled)
                {
                    if (PrimitiveRestart.IsPrimitiveRestartSupported(ctx))
                    {
                        // Draw elements with primitive restart
                        PrimitiveRestart.EnablePrimitiveRestart(ctx, ArrayIndices.ElementsType);
                        DrawElements(ctx, arraySection.Pointer);
                        PrimitiveRestart.DisablePrimitiveRestart(ctx);
                    }
                    else
                    {
                        // Note: uses MultiDrawElements to emulate the primitive restart feature; PrimitiveRestartOffsets and
                        // PrimitiveRestartCounts are computed at element buffer creation time
                        Gl.MultiDrawElements(ElementsMode, ArrayIndices.PrimitiveRestartOffsets, ArrayIndices.ElementsType, ArrayIndices.PrimitiveRestartCounts, ArrayIndices.PrimitiveRestartOffsets.Length);
                    }
                }
                else
                {
                    uint count = (ElementCount == 0) ? ArrayIndices.ItemCount : ElementCount;
                    Debug.Assert(count - ElementOffset <= ArrayIndices.ItemCount, "element indices array out of bounds");

                    // Draw vertex arrays by indices
                    DrawElements(ctx, arraySection.Pointer);
                }
            }