public VertexBuffer(GraphicsDevice graphicsDevice, Type vertexType, int vertexCount, BufferUsageHint usage = BufferUsageHint.StaticDraw) : this(graphicsDevice, vertexCount, usage) { IVertexType tp = Activator.CreateInstance(vertexType) as IVertexType; if (tp == null) { throw new ArgumentException("must be a vertexType"); } this.VertexDeclaration = tp.VertexDeclaration; ThreadingHelper.BlockOnUIThread(() => { vbo = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertexCount * VertexDeclaration.VertexStride), IntPtr.Zero, (OpenTK.Graphics.OpenGL4.BufferUsageHint)BufferUsage); }); ThreadingHelper.BlockOnUIThread(() => { vao = new VertexAttributes(); vao.vbo = vbo; vao.Bind(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); VertexAttributes.ApplyAttributes(vao, VertexDeclaration); GL.BindVertexArray(0); }, true); GraphicsDevice.CheckError(); }
public void Resize(int vertexCount, bool keepData = false) { int tempVBO = 0; ThreadingHelper.BlockOnUIThread(() => { tempVBO = GL.GenBuffer(); vao.Bind(); GL.BindBuffer(BufferTarget.ArrayBuffer, tempVBO); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertexCount * VertexDeclaration.VertexStride), IntPtr.Zero, (OpenTK.Graphics.OpenGL4.BufferUsageHint)BufferUsage); }); ThreadingHelper.BlockOnUIThread(() => { VertexAttributes.ApplyAttributes(vao, VertexDeclaration); GL.BindVertexArray(0); this.VertexCount = vertexCount; GL.DeleteBuffer(vbo); vbo = tempVBO; }, true, this); /* * ThreadingHelper.BlockOnUIThread(() => * { * * }, true);*/ if (keepData) { //TODO: throw new NotImplementedException(); } }
public static void ApplyAttributes(VertexAttributes attribs, VertexDeclaration declaration) { attribs.Bind(); foreach (var el in declaration.VertexElements) { attribs.Add(el, declaration.VertexStride, declaration.InstanceDivisor); } GL.BindVertexArray(0); }
internal void EnsureVAO() { if (vao != null && vao.vbo != vbo) { vao.vbo = vbo; vao.Bind(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); VertexAttributes.ApplyAttributes(vao, VertexDeclaration); GL.BindVertexArray(0); } }
internal void EnsureVAO() { if (vao != null && vao.vbo != vbo) { vao.vbo = vbo; vao.Bind(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); VertexAttributes.ApplyAttributes(vao, VertexDeclaration); GL.BindVertexArray(0); if (tempVBO == -1) { return; } GL.DeleteBuffer(tempVBO); tempVBO = -1; } }
public VertexBuffer(GraphicsDevice graphicsDevice, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsageHint usage = BufferUsageHint.StaticDraw) : this(graphicsDevice, vertexCount, usage) { this.VertexDeclaration = vertexDeclaration; ThreadingHelper.BlockOnUIThread(() => { vbo = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertexCount * VertexDeclaration.VertexStride), IntPtr.Zero, (OpenTK.Graphics.OpenGL4.BufferUsageHint)BufferUsage); }); ThreadingHelper.BlockOnUIThread(() => { vao = new VertexAttributes(); vao.vbo = vbo; vao.Bind(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); VertexAttributes.ApplyAttributes(vao, VertexDeclaration); GL.BindVertexArray(0); }, true); GraphicsDevice.CheckError(); }