public void DrawPrimitives(PrimitiveType primitiveType, int vertexStart, int primitiveCount) { // Flush the GL state before moving on! ApplyState(); // 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 ) ); } // Enable the appropriate vertex attributes. GLDevice.FlushGLVertexAttributes(); // Draw! GLDevice.glDrawArrays( PrimitiveTypeGL(primitiveType), vertexStart, GetElementCountArray(primitiveType, primitiveCount) ); }
public void DrawUserPrimitives <T>( PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int primitiveCount, VertexDeclaration vertexDeclaration ) where T : struct { // Flush the GL state before moving on! ApplyState(); // Unbind current VBOs. GLDevice.BindVertexBuffer(OpenGLDevice.OpenGLVertexBuffer.NullBuffer); // Pin the buffers. GCHandle vbHandle = GCHandle.Alloc(vertexData, GCHandleType.Pinned); // Setup the vertex declaration to point at the VB data. vertexDeclaration.GraphicsDevice = this; vertexDeclaration.Apply(VertexShader, vbHandle.AddrOfPinnedObject()); // Enable the appropriate vertex attributes. GLDevice.FlushGLVertexAttributes(); // Draw! GLDevice.glDrawArrays( PrimitiveTypeGL(primitiveType), vertexOffset, GetElementCountArray(primitiveType, primitiveCount) ); // Release the handles. vbHandle.Free(); }
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 ); }
/// <summary> /// Draw geometry by indexing into the vertex buffer. /// </summary> /// <param name="primitiveType">The type of primitives in the index buffer.</param> /// <param name="baseVertex"> /// Used to offset the vertex range indexed from the vertex buffer. /// </param> /// <param name="minVertexIndex"> /// A hint of the lowest vertex indexed relative to baseVertex. /// </param> /// <param name="numVertices">An hint of the maximum vertex indexed.</param> /// <param name="startIndex"> /// The index within the index buffer to start drawing from. /// </param> /// <param name="primitiveCount"> /// The number of primitives to render from the index buffer. /// </param> /// <remarks> /// Note that minVertexIndex and numVertices are unused in MonoGame and will be ignored. /// </remarks> public void DrawIndexedPrimitives( PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, int startIndex, int primitiveCount ) { // 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) ) ); } // Enable the appropriate vertex attributes. GLDevice.FlushGLVertexAttributes(); // Bind the index buffer GLDevice.BindIndexBuffer(Indices.Handle); // Draw! GLDevice.glDrawRangeElements( PrimitiveTypeGL(primitiveType), minVertexIndex, minVertexIndex + numVertices - 1, GetElementCountArray(primitiveType, primitiveCount), shortIndices ? OpenGLDevice.GLenum.GL_UNSIGNED_SHORT : OpenGLDevice.GLenum.GL_UNSIGNED_INT, (IntPtr)(startIndex * (shortIndices ? 2 : 4)) ); }
public void DrawUserIndexedPrimitives <T>( PrimitiveType primitiveType, T[] vertexData, int vertexOffset, int numVertices, int[] indexData, int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration ) where T : struct, IVertexType { // Flush the GL state before moving on! ApplyState(); // Unbind current buffer objects. GLDevice.BindVertexBuffer(OpenGLDevice.OpenGLVertexBuffer.NullBuffer); GLDevice.BindIndexBuffer(OpenGLDevice.OpenGLIndexBuffer.NullBuffer); // Pin the buffers. GCHandle vbHandle = GCHandle.Alloc(vertexData, GCHandleType.Pinned); GCHandle ibHandle = GCHandle.Alloc(indexData, GCHandleType.Pinned); // Setup the vertex declaration to point at the VB data. vertexDeclaration.GraphicsDevice = this; vertexDeclaration.Apply( VertexShader, (IntPtr)(vbHandle.AddrOfPinnedObject().ToInt64() + vertexDeclaration.VertexStride * vertexOffset) ); // Enable the appropriate vertex attributes. GLDevice.FlushGLVertexAttributes(); // Draw! GLDevice.glDrawRangeElements( PrimitiveTypeGL(primitiveType), 0, numVertices - 1, GetElementCountArray(primitiveType, primitiveCount), OpenGLDevice.GLenum.GL_UNSIGNED_INT, (IntPtr)(ibHandle.AddrOfPinnedObject().ToInt64() + (indexOffset * sizeof(int))) ); // Release the handles. ibHandle.Free(); vbHandle.Free(); }