Getting 2d into 1d, making GLM::Mat4

Started by
2 comments, last by BitMaster 10 years, 7 months ago
Hi Guys, I've just hacked this together so I can have a 1D float array of a matrix to pass to
return glm::make_mat4( float* );
I'm getting c2109 - Subscript requires array or pointer, and it's pointing to the kernel of this loop.. please help! by the way, if the logic is wrong, let me know. Thanks! glm::mat4 make1dfrom2d( float *m_in, bool rowMajor ) { float* m_out; m_out = new float[16]; for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) if( rowMajor ) m_out[ (i*4) + j ] = m_in[j]; // c2109 else m_out[ i + (j*4) ] = m_in[j]; // c2109 glm::mat4 gm_out = glm::make_mat4( m_out ); delete m_out; return gm_out; }
Advertisement

maybe change like so?

float *m_in

=>

float **m_in

or float[4]* or some junk - not sure

I think you are making things too complicated. Maybe this could help:


// make from one dimensional array
float mymat[16];
glm::mat4 m glm::make_mat4(mymat);

// make from two dimensional array
float mymat[4][4];
glm::mat4 m = glm::make_mat4(&mymat[0][0]);

// if you need to change the ordering
m = glm::transpose(m);

Derp

A few side notes:
1) Allocating m_out on the heap is completely unnecessary. new/delete are rather expensive operations you should not use if allocating on the stack works perfectly fine.
2) Setting aside you should not new m_out in the first place, if you do, at least delete[] it. While mixing new[] and delete usually appears to work on MSVC, the standard forbids it and other compilers, like gcc, will blow up in your face.

This topic is closed to new replies.

Advertisement