PDA

View Full Version : Multiply Vecor by Matrix??!

TheBlob
01-16-2002, 10:41 AM
Hi!
Well, thatīs exactly my question: How do you multiply a vector with a matrix. The result should be also a vector.
Maybe someone could post a little code please!
I was getting very confused writing it myself, because a tut on NeHe says that OpenGL handles marices 'column-first'?!
My intention is to multiply a vector by the Modelview and ProjecionMatrix.

01-16-2002, 12:09 PM
double v[4]; // the vector
double m[16]; // the matrix
double r[]={0, 0, 0, 0}; // result: matrix x vector

// define v and m here.
for (int i=0; i<=3; i++)
{
for (int j=0; j<=3; j++)
{
r[i] = r[i] + m[i+j*4] * v[j];
}
}

I never did that, but this should work. I remember that i read this column-first thing in the red book.Column-first means the matrix entries are stored in this order:
[ 0 4 8 12]
[ 1 5 9 13]
[ 2 6 10 14]
[ 3 7 11 15]
Am i right? Then the code above should work. Try it out.

[This message has been edited by plastichead (edited 01-16-2002).]