iliak's picture

GL.BufferData<T>()

Hi

I make a BufferObject class to handle generic VBO (index buffer using uint, texture buffer using floats, normal buffer using floats...). Here's the code :

	/// <summary>
	/// Stores user datas in graphic card's memory
	/// </summary>
	public class BufferObject<T> : IDisposable
	{
		/// <summary>
		/// Apply updates
		/// </summary>
		public void Apply()
		{
			if (!Display.Capabilities.HasVBO)
				return;
 
			try
			{
				GL.BindBuffer(BufferTarget.ArrayBuffer, Handle);
				GL.BufferData<T>(BufferTarget.ArrayBuffer, (IntPtr)(Count * ElementSize), Buffer.ToArray(), BufferUsageHint.StaticDraw);
 
			}
			catch (Exception e)
			{
				bool er = GL.IsBuffer(Handle);
				Trace.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
			}
 
 
		}

I have the following error on the GL.BufferData line : The type 'T' must be a non-nullable value type in order to use it as parameter 'T2' in the generic type or method 'OpenTK.Graphics.OpenGL.GL.BufferData(OpenTK.Graphics.OpenGL.BufferTarget, System.IntPtr, T2[], OpenTK.Graphics.OpenGL.BufferUsageHint)'.

Any idea ?


Comments

Comment viewing options

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

Add the struct generic constraint:

public class BufferObject<T> : IDisposable
    where T : struct

This is not enough to ensure that T is blittable but it is better than nothing. OpenTK.BlittableValueType provides additional checks to ensure that T is a valid type for OpenGL interop at runtime.

iliak's picture

Ok, thanks !