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


    fMat[0]=cMat[0]*gMat[0]+cMat[0]*gMat[1]+cMat[0]*gMat[2]+cMat[0]*gMat[3];
    fMat[1]=cMat[1]*gMat[0]+cMat[1]*gMat[1]+cMat[1]*gMat[2]+cMat[1]*gMat[3];
    fMat[2]=cMat[2]*gMat[0]+cMat[2]*gMat[1]+cMat[2]*gMat[2]+cMat[2]*gMat[3];
    fMat[3]=cMat[3]*gMat[0]+cMat[3]*gMat[1]+cMat[3]*gMat[2]+cMat[3]*gMat[3];
    fMat[4]=cMat[4]*gMat[4]+cMat[4]*gMat[5]+cMat[4]*gMat[6]+cMat[4]*gMat[7];
    fMat[5]=cMat[5]*gMat[4]+cMat[5]*gMat[5]+cMat[5]*gMat[6]+cMat[5]*gMat[7];
    fMat[6]=cMat[6]*gMat[4]+cMat[6]*gMat[5]+cMat[6]*gMat[6]+cMat[6]*gMat[7];
    fMat[7]=cMat[7]*gMat[4]+cMat[7]*gMat[5]+cMat[7]*gMat[6]+cMat[7]*gMat[7];
    fMat[8]=cMat[8]*gMat[8]+cMat[8]*gMat[9]+cMat[8]*gMat[10]+cMat[8]*gMat[11];
    fMat[9]=cMat[9]*gMat[8]+cMat[9]*gMat[9]+cMat[9]*gMat[10]+cMat[9]*gMat[11];
    fMat[10]=cMat[10]*gMat[8]+cMat[10]*gMat[9]+cMat[10]*gMat[10]+cMat[10]*gMat[11];
    fMat[11]=cMat[11]*gMat[8]+cMat[11]*gMat[9]+cMat[11]*gMat[10]+cMat[11]*gMat[11];
    fMat[12]=cMat[12]*gMat[12]+cMat[12]*gMat[13]+cMat[12]*gMat[14]+cMat[12]*gMat[15];
    fMat[13]=cMat[13]*gMat[12]+cMat[13]*gMat[13]+cMat[13]*gMat[14]+cMat[13]*gMat[15];
    fMat[14]=cMat[14]*gMat[12]+cMat[14]*gMat[13]+cMat[14]*gMat[14]+cMat[14]*gMat[15];
    fMat[15]=cMat[15]*gMat[12]+cMat[15]*gMat[13]+cMat[15]*gMat[14]+cMat[15]*gMat[15];


    glLoadMatrixf(fMat);
}

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.

Recommended Answers

All 5 Replies

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...

hmmm

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.