• Advertisement
Sign in to follow this  

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

This topic is 1599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
Advertisement

maybe change like so?

 

 float *m_in

 

=>

 

 float **m_in

 

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

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement