Link to home
Start Free TrialLog in
Avatar of Jamsb
Jamsb

asked on

Am I Doing this Matrix Multiplication Correctly?

Basically I have written a class to draw a cube centered on a point (XYZ). I perform various tranlations on my scene using glTranslate/glRotate. Now I want to calculate the new position of the cube ("world co-ordinates" if that is the right term.)

It has been suggested that I should multiple the Point by the Modelviewmatrix I have achieved this by doing the following (see code).

Is this code correct and will it do what I want it to?


//function to do multiplication
 
#include <gl/gl.h>
 
void MultiplyVMatrix(GLfloat* Vertex, GLfloat* Matrix, GLfloat* ResultMatrix)
{
	for(int i=0;i<16;i++)
		for(int j=0;j<4;j++)
		ResultMatrix[j]+=Matrix[i]*Vertex[j];
 
}        
 
**************************************************************
//modelview matrix and multiply co-ordinate of cube
 
GLfloat matrix[16];
GLfloat vertex[4]={1,2,-5,0}; //any co-ordinate for testing
GLfloat resultmatrix[4]={0,0,0,0};
 
glGetFloatv(GL_MODELVIEW_MATRIX, matrix); //grab the modelview matrix
 
MultiplyVMatrix(vertex,matrix,resultmatrix);

Open in new window

translation.gif
Avatar of ozo
ozo
Flag of United States of America image

No
Avatar of Jamsb
Jamsb

ASKER

OK I will be more specific. If this does not work. What SHOULD I be doing?
Avatar of Jamsb

ASKER

I would increase points on the question if i could...
Avatar of Jamsb

ASKER

GLfloat vertex[4]={1,2,-5,0}; //any co-ordinate for testing

should read:
GLfloat vertex[4]={1,2,-5,1}; //any co-ordinate for testing
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jamsb

ASKER

ozo:

Can you explain your code please? in particular the following  "ResultMatrix[i/4]".
The the first four results, (i=0,1,2,3) go into ResultMatrix[0]
the next four (i=4,5,6,7) go into ResultMatrix[1]
etc.
Avatar of Jamsb

ASKER

Im going to assume that that should be a modulo. So what was broken? and how did your code fix it?
one is divide, one is moduo
I presumed that
ResultMatrix = Matrix *Vertex
and that Matrix was laid out as
m0  m4  m8  m12
m1  m5  m9  m13
m2  m6  m10 m14
m3  m7 m11  m15

the original code was equivalent to multiplying each element by
(m0+m1+m2+m3+m4+m5+...+m15)
Avatar of Jamsb

ASKER

OK. Figured it out. I'm still a newbie :)