3
\$\begingroup\$

Very simple and quick question which surprisingly I couldn't find an answer to over the internet: what is the equivalent of glUniformMatrixfv for opentk? I've browsed all the 7 overloads of GL.UniformMatrix4 and none of them seems correct to me and/or I have found any example usage for.

E.g. if I have a

Matrix4[] matrices;

variable (properly initialized etc..) that I want to map on a

mat4 matrices[2];

in glsl, which GL.UniformMatrix4 overload should I use?

P.S. Using OpenTK.Next NuGet package version 1.1.1616.8959

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

According to GL spec:

void glUniformMatrix4fv(GLint location,
                        GLsizei count,
                        GLboolean transpose,
                        const GLfloat *value);

*location*
Specifies the location of the uniform value to be modified.

*count*
Specifies the number of matrices that are to be modified. 
This should be 1 if the targeted uniform variable is not an array of matrices, 
and 1 or more if it is an array of matrices.

*transpose*
Specifies whether to transpose the matrix 
as the values are loaded into the uniform variable. Must be GL_FALSE.

*value*
Specifies a pointer to an array of count values
that will be used to update the specified uniform variable.

So in my opinion (I've never used OpenTK before) you can use the following function:

static unsafe void OpenTK.Graphics.OpenGL4.GL.UniformMatrix4(Int32 location,
                                                             Int32 count,
                                                             bool transpose,
                                                             Double * value)

Which has the same parameters as the C++ equivalent. Just pass the proper parameters to this function and everything should be ok:

OpenTK.Graphics.OpenGL4.GL.UniformMatrix4(matrices_location,
                                          2        /* count - 2 matrices */,
                                          false    /* transpose */,
                                          matrices /* ref, pointer */);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hey there, thanks for the answer. I was looking for an ad hoc Matrix4[] overload or at least a non unsafe one. I found out that the best way to do this is basically unfolding the matrix in an array and pass by value the float[]. If you added this to your answer I would be pleased to mark your response as correct. Have a nice day. \$\endgroup\$
    – Alakanu
    Jun 29, 2016 at 8:36
0
\$\begingroup\$

This isn't necessarily the best way to do it, but you could just unroll the matrix in a for loop that adds each element to a float[].

private float[] Matrix4ToArray(Matrix4 matrix)
{
    float[] data = new float[16];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            data[i*4+j] = matrix[i, j];

        }
    }
    return data;
}

Copying the data as a block would be cheaper, or something like a member access function to the underlying data on the OpenTK API side, but I couldn't find any, and the above worked well enough for my needs.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .