0

The title says it all.

For some reason this code is working:

float m[3][3] = {
    {1.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f},
    {0.0f, 0.0f, 1.0f}
};
glUniformMatrix3fv(basicShader->uniformAt(5), 1, GL_TRUE, &m[0][0]);

But this one isn't:

float **m = new float*[3];
for(int i = 0; i < 3; i++) {
    m[i] = new float[3];
    for(int j = 0; j < 3; j++) m[i][j] = 0;
}
m[0][0] = 1.0f;
m[1][1] = 1.0f;
m[2][2] = 1.0f;
glUniformMatrix3fv(basicShader->uniformAt(5), 1, GL_TRUE, &m[0][0]);
2
  • 4
    float[3][3] is a 2 dimensional array, in a coherent memory block. float ** is an array of pointers and each pointer points to a new 1 dimensional array. Commented Oct 7, 2018 at 14:00
  • Ohhh, that makes sense. So do I have to manually convert float ** to float[3][3] in order to send it to opengl? Commented Oct 7, 2018 at 14:02

1 Answer 1

1
float* m = new float[9] {1.0f, 0.0f, 0.0f, 
                         0.0f, 1.0f, 0.0f,
                         0.0f, 0.0f, 1.0f };



glUniformMatrix3fv(basicShader->uniformAt(5), 1, GL_TRUE, m);

2D arrays are actually laid out continuously.

EDIT: Just saying, in your first example you don't need &m[0][0]. Just &m is sufficient because the address points automatically to the first element.

Sign up to request clarification or add additional context in comments.

2 Comments

I was hoping I could keep float **m somehow but I guess I can't. But your solution will work. Thanks!
It doesn't work just the way the OpenGL function parses the data. It parses 9 consecutive floats from the dereferenced pointer and doesn't dereference the single rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.