Beispiel #1
0
        public void DrawInstancedPrimitives(
            PrimitiveType primitiveType,
            int baseVertex,
            int minVertexIndex,
            int numVertices,
            int startIndex,
            int primitiveCount,
            int instanceCount
            )
        {
            // Note that minVertexIndex and numVertices are NOT used!

            // If this device doesn't have the support, just explode now before it's too late.
            if (!GLDevice.SupportsHardwareInstancing)
            {
                throw new NoSuitableGraphicsDeviceException("Your hardware does not support hardware instancing!");
            }

            // Flush the GL state before moving on!
            ApplyState();

            // Unsigned short or unsigned int?
            bool shortIndices = Indices.IndexElementSize == IndexElementSize.SixteenBits;

            // Set up the vertex buffers.
            for (int i = 0; i < vertexBufferCount; i += 1)
            {
                GLDevice.BindVertexBuffer(
                    vertexBufferBindings[i].VertexBuffer.Handle
                    );
                vertexBufferBindings[i].VertexBuffer.VertexDeclaration.Apply(
                    VertexShader,
                    (IntPtr)(
                        vertexBufferBindings[i].VertexBuffer.VertexDeclaration.VertexStride *
                        (vertexBufferBindings[i].VertexOffset + baseVertex)
                        ),
                    vertexBufferBindings[i].InstanceFrequency
                    );
            }

            // Enable the appropriate vertex attributes.
            GLDevice.FlushGLVertexAttributes();

            // Bind the index buffer
            GLDevice.BindIndexBuffer(Indices.Handle);

            // Draw!
            GLDevice.glDrawElementsInstanced(
                PrimitiveTypeGL(primitiveType),
                GetElementCountArray(primitiveType, primitiveCount),
                shortIndices ?
                OpenGLDevice.GLenum.GL_UNSIGNED_SHORT :
                OpenGLDevice.GLenum.GL_UNSIGNED_INT,
                (IntPtr)(startIndex * (shortIndices ? 2 : 4)),
                instanceCount
                );
        }