flopoloco's picture

Creating a 2d batch renderer

I have started making a 2D renderer in OpenTK with C#.

Something I do wrong and the application crashes.
Problem is in line 104, when I try to run GL.DrawArrays

The first loop works and a line is displayed, but in the second loop the application crashes.

I must find a way to delete the data of the vertex buffer and then upload them again.

https://copy.com/iz3Uk04cWYxs

Any ideas on this?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
flopoloco's picture

This is a minified version of the application, changing the data of the buffer dynamically. It works perfectly, I will try to do the same thing in the batch renderer.

using System;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
 
namespace OpenTKReWriteData
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			var random = new Random(DateTime.Now.Millisecond);
			var vertexCount = 0;
			var vertexCountMax = 1000;
			var vertexData = new float[vertexCountMax];
			var vertexBuffer = 0;
 
			var window = new GameWindow();
 
			window.Load += (object sender, EventArgs e) => {
				vertexBuffer = GL.GenBuffer();
				GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
				GL.BufferData(
					BufferTarget.ArrayBuffer,
					new IntPtr(vertexCountMax * 4),
					IntPtr.Zero,
					BufferUsageHint.DynamicDraw
				);
 
			};
 
			window.RenderFrame += (object sender, FrameEventArgs e) => {
				GL.Clear(ClearBufferMask.ColorBufferBit);
 
				// Get a random vertex count
				vertexCount = random.Next(2, vertexCountMax);
				if (vertexCount % 2 == 1)
				{
					if (vertexCount < vertexCountMax) vertexCount++;
				}
 
				// Update vertex array data
				for (var i = 0; i < vertexCount; i++)
				{
					vertexData[i] = ((float)random.NextDouble()*2f)-1f;
				}
 
				// Upload vertex data to buffer
				GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
				GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)0, (IntPtr)(vertexCount * 4), vertexData);
 
				// Bind vertex attributes
				GL.EnableVertexAttribArray(0);
				GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0);
 
				// Draw data
				GL.DrawArrays(PrimitiveType.Lines, 0, vertexCount);
 
				window.SwapBuffers();
			};
 
			window.Run();
		}
	}
}

flopoloco's picture

Here's is a new version to get the data from a list instead of an array.

using System;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
 
namespace OpenTKReWriteData
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			var random = new Random(DateTime.Now.Millisecond);
 
			var vertexCount = 0;
			var vertexCountMax = 10000;
			var vertexBuffer = 0;
 
			var vertexList = new System.Collections.Generic.List<float>();
			Action<float, float> addVertex = (_x, _y) =>
			{
				vertexList.Add(_x);
				vertexList.Add(_y);
				vertexCount++;
			};
 
			Action resetVertices = () =>
			{
				vertexList.Clear();
				vertexCount = 0;
			};
 
			Func<float> getRandomPosition = () =>
			{
				return ((float)random.NextDouble()*2f)-1f;
			};
 
			var window = new GameWindow();
 
			window.Load += (object sender, EventArgs e) => {
				vertexBuffer = GL.GenBuffer();
				GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
				GL.BufferData(
					BufferTarget.ArrayBuffer,
					new IntPtr(vertexCountMax * 4),
					IntPtr.Zero,
					BufferUsageHint.DynamicDraw
				);
 
			};
 
			window.RenderFrame += (object sender, FrameEventArgs e) => {
				GL.Clear(ClearBufferMask.ColorBufferBit);
 
				for (var i = 0; i < vertexCountMax; i++)
				{
					addVertex(getRandomPosition(), getRandomPosition());
				}
 
				// Upload vertex data to buffer
				GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
				GL.BufferSubData(BufferTarget.ArrayBuffer,
					(IntPtr)0,
					(IntPtr)(vertexCount * 4),
					vertexList.ToArray()
				);
 
				// Bind vertex attributes
				GL.EnableVertexAttribArray(0);
				GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0);
 
				// Draw data
				GL.DrawArrays(PrimitiveType.Lines, 0, vertexCount);
 
				resetVertices();
				window.SwapBuffers();
			};
 
			window.Run();
		}
	}
}

It's very cool to see how fast this is! :D

2014 06 26 03 29 13 Open TK Game Window