basically what ive got going is some transformations using my own functions, ive got them working fine still using glMultMatrix but im not allowed to use that for this.

im trying to make my own matrix multiplication function and im getting stuck somewhere.

the matricies are all one dimensional arrays and im going around in loops.

im multiplying 2 4x4 matricies (so a array with 16 elements)

void myMulti(GLfloat (&gMat)[16]) //givenmatrix

    glGetFloatv(GL_MODELVIEW_MATRIX, cMat); //current active matrix (model view in this case currently loaded with identity matrix)
    GLfloat fMat[16]={0}; //finalmatrix



i know theres a better way to do this, thre has to be but i couldnt work out a way to do it with for loops cause of it being a 1d array.

also i think i may have something wrong with my matrix multiplication there also, but if i can get it into some loops, that will most likley streamline the function and solve those problems also. ty for help in advance.

Edited by Slavrix

5 Years
Discussion Span
Last Post by raptr_dflo

fMat indexes equals cMat indexes each line
gMat indexes increment by one 4 times a line
gMat indexes remain same for 4 lines, then increase by 4 for the next four lines

fMat is incremented by the product cMat * gMat 4 times each line

 for(int i = 0; i < 4; ++i)
   for(int j = 0; j < 4; ++j)
     for(int k = 0; k < 4; ++k)
       fMat[(4 * i) + j] += cMat[(4 * i) + j] * gMat[(4 * i) + k].

Anyway, I think this works.


yea thats working for my matrix calculation exactly the same way. so i must have a mistake in my calculation as its not giving me the corect output...


when c++ reads an one dimensional array for a matrix, does it read in in column major or row major ?

row major          column major

0 1 2 3             0 4 8 12
4 5 6 7             1 5 9 13
8 9 10 11           2 6 10 14
12 13 14 15         3 7 11 15

this could be why my maths may be screwing up

Edited by Slavrix


Referring to your title:
You do know that an one dimensional space is only a line, right?

Referring to your second post:
An array is an array, it is a collection of elements, hence not a row nor a column.

Edited by Kashaku


this is why im getting my maths mixed up i think for multiplying my matricies. as im not sure on which way their being read


You need to determine whether the 1D version of the matrix is in row- or column- major order. Also, whether the matrices are designed to be applied in prefix or postfix order. These are more or less the same question. I prefer to think of matrices as I learned them in calculus, so they pre-multiply column vectors (instead of post-multiplying row vectors). In that case, a translation has the Tx, Ty, Tz values down the right-hand edge (as opposed to across the bottom). But regardless, printing out a translation matrix will probably give you all the information you need, regardless of which way you think of your matrices: the translation values will either be all adjacent in positions 12-14, or spread out in positions 3, 7 and 11.

Line 4 of Lerner's code above is incorrect. If they were 2D arrays, in row-major order, the result would be

fMat[i][j] += cMat[i][k] * gMat[k][j];

or in 1D-land (if still in row-major order):

fMat[i*4 + j] += cMat[i*4 + k] * gMat[k*4 + j];

And don't forget to initialize fMat[i*4 + j] = 0.0; before your loop over k!

All this should be enough to get you well on your way.

Edited by raptr_dflo: minor typo

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.