Improve company productivity with a Business Account.Sign Up

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

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
0
Jamsb
Asked:
Jamsb
  • 6
  • 5
1 Solution
 
ozoCommented:
No
0
 
JamsbAuthor Commented:
OK I will be more specific. If this does not work. What SHOULD I be doing?
0
 
JamsbAuthor Commented:
I would increase points on the question if i could...
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
JamsbAuthor Commented:
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
0
 
ozoCommented:
      for(int i=0;i<16;i++)
                ResultMatrix[i/4]+=Matrix[i]*Vertex[i%4];
0
 
JamsbAuthor Commented:
ozo:

Can you explain your code please? in particular the following  "ResultMatrix[i/4]".
0
 
ozoCommented:
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
 
JamsbAuthor Commented:
Im going to assume that that should be a modulo. So what was broken? and how did your code fix it?
0
 
ozoCommented:
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
 
JamsbAuthor 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.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now