public VertexBuffer(Type type, int length, bool isStream = false) { TotalBuffers++; VBO = GL.GenBuffer(); var usageHint = isStream ? GL.GL_STREAM_DRAW : GL.GL_STATIC_DRAW; streaming = isStream; this.type = type; try { vertextype = (IVertexType)Activator.CreateInstance(type); decl = vertextype.GetVertexDeclaration(); } catch (Exception) { throw new Exception(string.Format("{0} is not a valid IVertexType", type.FullName)); } GL.GenVertexArrays(1, out VAO); GLBind.VertexArray(VAO); GL.BindBuffer(GL.GL_ARRAY_BUFFER, VBO); GL.BufferData(GL.GL_ARRAY_BUFFER, (IntPtr)(length * decl.Stride), IntPtr.Zero, usageHint); size = length; decl.SetPointers(); VertexCount = length; }
public void Dispose() { GL.DeleteBuffer(VBO); GL.DeleteVertexArray(VAO); GLBind.VertexArray(0); GLBind.VertexBuffer(0); }
public void Dispose() { TotalBuffers--; GL.DeleteBuffer(VBO); GL.DeleteVertexArray(VAO); GLBind.VertexArray(0); }
public void UnsetElementBuffer() { GLBind.VertexBuffer(VBO); GLBind.VertexArray(VAO); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0); HasElements = false; _elements = null; }
public void SetElementBuffer(ElementBuffer elems) { GLBind.VertexBuffer(VBO); GLBind.VertexArray(VAO); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, elems.Handle); HasElements = true; _elements = elems; }
public void SetData(short[] data) { GLBind.VertexArray(RenderState.Instance.NullVAO); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, Handle); fixed(short *ptr = data) { GL.BufferData(GL.GL_ELEMENT_ARRAY_BUFFER, new IntPtr(data.Length * 2), (IntPtr)ptr, isDynamic ? GL.GL_DYNAMIC_DRAW : GL.GL_STATIC_DRAW); } }
public ElementBuffer(int count, bool isDynamic = false) { this.isDynamic = isDynamic; IndexCount = count; Handle = GL.GenBuffer(); GLBind.VertexArray(RenderState.Instance.NullVAO); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, Handle); GL.BufferData(GL.GL_ELEMENT_ARRAY_BUFFER, new IntPtr(count * 2), IntPtr.Zero, isDynamic ? GL.GL_DYNAMIC_DRAW : GL.GL_STATIC_DRAW); }
public void SetData(ushort[] data, int count, int start = 0) { GLBind.VertexArray(RenderState.Instance.NullVAO); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, Handle); fixed(ushort *ptr = data) { ; GL.BufferSubData(GL.GL_ELEMENT_ARRAY_BUFFER, new IntPtr(start * 2), new IntPtr(count * 2), (IntPtr)ptr); } }
public void Dispose() { TotalBuffers--; if (streaming) { Marshal.FreeHGlobal(buffer); } GL.DeleteBuffer(VBO); GL.DeleteVertexArray(VAO); GLBind.VertexArray(0); }
public void SetData(ushort[] data, int count) { GLBind.VertexArray(0); GLBind.VertexBuffer(0); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, Handle); fixed(ushort *ptr = data) { ; GL.BufferSubData(GL.GL_ELEMENT_ARRAY_BUFFER, IntPtr.Zero, new IntPtr(count * 2), (IntPtr)ptr); } }
public VertexBuffer(VertexDeclaration decl, int length, bool isStream = false) { this.decl = decl; VBO = GL.GenBuffer(); var usageHint = isStream ? GL.GL_STREAM_DRAW : GL.GL_STATIC_DRAW; GL.GenVertexArrays(1, out VAO); GLBind.VertexArray(VAO); GLBind.VertexBuffer(VBO); GL.BufferData(GL.GL_ARRAY_BUFFER, (IntPtr)(length * decl.Stride), IntPtr.Zero, usageHint); decl.SetPointers(); VertexCount = length; }
public void Draw(PrimitiveTypes primitiveType, int baseVertex, int startIndex, int primitiveCount) { RenderState.Instance.Apply(); int indexElementCount = primitiveType.GetArrayLength(primitiveCount); GLBind.VertexArray(VAO); GL.DrawElementsBaseVertex(primitiveType.GLType(), indexElementCount, GL.GL_UNSIGNED_SHORT, (IntPtr)(startIndex * 2), baseVertex); TotalDrawcalls++; }
public void SetData <T>(T[] data, int?length = null) where T : struct { if (typeof(T) != type && typeof(T) != typeof(byte)) { throw new Exception("Data must be of type " + type.FullName); } int len = length ?? data.Length; GLBind.VertexBuffer(VBO); GLBind.VertexArray(VAO); var handle = GCHandle.Alloc(data, GCHandleType.Pinned); GL.BufferSubData(GL.GL_ARRAY_BUFFER, IntPtr.Zero, (IntPtr)(len * decl.Stride), handle.AddrOfPinnedObject()); handle.Free(); }
public void Draw(PrimitiveTypes primitiveType, int baseVertex, int startIndex, int primitiveCount) { if (primitiveCount == 0) { throw new InvalidOperationException("primitiveCount can't be 0"); } RenderState.Instance.Apply(); int indexElementCount = primitiveType.GetArrayLength(primitiveCount); GLBind.VertexArray(VAO); GL.DrawElementsBaseVertex(primitiveType.GLType(), indexElementCount, GL.GL_UNSIGNED_SHORT, (IntPtr)(startIndex * 2), baseVertex); TotalDrawcalls++; }
internal void DrawInternal(PrimitiveTypes primitiveType, int primitiveCount) { GLBind.VertexArray(VAO); if (HasElements) { GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, _elements.Handle); int indexElementCount = primitiveType.GetArrayLength(primitiveCount); GL.DrawElements(primitiveType.GLType(), indexElementCount, GL.GL_UNSIGNED_SHORT, IntPtr.Zero ); } else { int indexElementCount = primitiveType.GetArrayLength(primitiveCount); GL.DrawArrays(primitiveType.GLType(), 0, indexElementCount ); } TotalDrawcalls++; }
public void Draw(PrimitiveTypes primitiveType, int start, int primitiveCount) { RenderState.Instance.Apply(); GLBind.VertexArray(VAO); if (HasElements) { int indexElementCount = primitiveType.GetArrayLength(primitiveCount); GL.DrawElements(primitiveType.GLType(), indexElementCount, GL.GL_UNSIGNED_SHORT, (IntPtr)(2 * start) ); } else { int indexElementCount = primitiveType.GetArrayLength(primitiveCount); GL.DrawArrays(primitiveType.GLType(), start, indexElementCount ); } TotalDrawcalls++; }
public void UnbindAll() { GLBind.VertexArray(RenderState.Instance.NullVAO); }
public void UnbindAll() { GLBind.VertexArray(0); GLBind.VertexBuffer(0); }
internal void Bind() { GLBind.VertexArray(VAO); }