Search code examples
c#openglsharpgl

Setting values to TexImage3D function in SharpGL C#


C++ Code using OpenGL:

vector<RGB> LUT;  //creating a vector3 array
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, 16, 16, 16, 0, GL_RGB,GL_FLOAT, &LUT[0]);

The above C++ code is working fine. &LUT[0] is accepted as it is of type const GLvoid * data

C# Code using SharpGL:

Vector3[] vec3 = new Vector3[2]; //creating a vector3 array
gl.TexImage3D(GL_TEXTURE_3D, 0, GL_RGB, 16, 16, 16, 0, GL_RGB,GL_FLOAT, &LUT[0]);

My problem is, in SharpGL &LUT[0] is not accepting, stating the message that it accepts only IntPtr types. Is there anyway I can resolve this issue?


Solution

  • You can pack float[] into an IntPtr.

    float[] data = {
        ...
    };
    GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
    IntPtr dataPtr = handle.AddrOfPinnedObject();
    var size = Marshal.SizeOf(typeof(float)) * data.Length;
    

    Remember to free it after having called gl.TexImage3D().

    handle.Free();