x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1968

# 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);
``````
translation.gif
0
Jamsb
• 6
• 5
1 Solution

Commented:
No
0

Author Commented:
OK I will be more specific. If this does not work. What SHOULD I be doing?
0

Author Commented:
I would increase points on the question if i could...
0

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

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

Commented:
for(int i=0;i<16;i++)
ResultMatrix[i/4]+=Matrix[i]*Vertex[i%4];
0

Author Commented:
ozo:

0

Commented:
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.
0

Author Commented:
Im going to assume that that should be a modulo. So what was broken? and how did your code fix it?
0

Commented:
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)
0

Commented:
0

Author Commented:
OK. Figured it out. I'm still a newbie :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.