jingquan's picture

Sending an array of Vector3 to shader

Hello,

Is it possible to send an array of Vector3 to my shader,

uniform vec3 positions[22];

In my code I tried to pass a list of positions using GL.Uniform3(), but there is no overload to send an array of Vector3.

List<Vector3> listPositions = new List<Vector3>();
GL.Uniform3(location, listPositions.Count, listPositions.ToArray());

Should I send an array of floats instead? I'm just looking for a lazy one line method if you can implement. :)

Thanks I really enjoy using opentk!


Comments

Comment viewing options

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

You are right, there is no Vector3[] overload for GL.Uniform3. There is no technical reason for this, it's just that noone has ever asked for it before :)

I would be happy to merge a pull request against opentk/develop that implements this (see Source/OpenTK/Graphics/OpenGL/GLHelper.cs.)

Otherwise, the solution would be to cast the vector array to a float pointer. The downside is that this requires unsafe code:

           var positions = listPositions.ToArray();
           unsafe
            {
                fixed (float *p = &positions[0].X)
                {
                    GL.Uniform3(0, 3 * position.Length, p);
                }
            }
jingquan's picture

Thank you, the Fiddler.

HappyEmu's picture

Hi

I also would love to see support for uploading arrays of the usual struct types (Vec2, Vec3, Vec4) to OpenGL using the generic GL.Uniformi and GL.ProgramUniformi calls.

On a related note: right now, there is no overloaded method for ProgramUniformMatrix4 that accepts an actual Matrix4 struct as parameter. It only accepts arrays of primitive data types. Are there any plans to add this feature?

Thanks!

the Fiddler's picture

Hi HappyEmu,

please file a feature request at https://github.com/opentk/opentk/issues to ensure this does not get forgotten.

HappyEmu's picture

Will do, thanks! :-)

the Fiddler's picture

I have implemented these overloads in https://github.com/opentk/opentk/pull/95. However, this requires testing before I can merge.

Do you have any test code for GL.UniformMatrix* and GL.ProgramUniform / GL.ProgramUniformMatrix* that can verify whether the new overloads are working as expected?