Solved

Am I Doing this Matrix Multiplication Correctly?

Posted on 2008-06-16
11
1,943 Views
Last Modified: 2013-12-26
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
Comment
Question by:Jamsb
  • 6
  • 5
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21798598
No
0
 
LVL 2

Author Comment

by:Jamsb
ID: 21798607
OK I will be more specific. If this does not work. What SHOULD I be doing?
0
 
LVL 2

Author Comment

by:Jamsb
ID: 21798667
I would increase points on the question if i could...
0
 
LVL 2

Author Comment

by:Jamsb
ID: 21798714
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
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 21798721
      for(int i=0;i<16;i++)
                ResultMatrix[i/4]+=Matrix[i]*Vertex[i%4];
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Author Comment

by:Jamsb
ID: 21800636
ozo:

Can you explain your code please? in particular the following  "ResultMatrix[i/4]".
0
 
LVL 84

Expert Comment

by:ozo
ID: 21800675
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
 
LVL 2

Author Comment

by:Jamsb
ID: 21800681
Im going to assume that that should be a modulo. So what was broken? and how did your code fix it?
0
 
LVL 84

Expert Comment

by:ozo
ID: 21800758
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
 
LVL 84

Expert Comment

by:ozo
ID: 21800808
0
 
LVL 2

Author Closing Comment

by:Jamsb
ID: 31467816
OK. Figured it out. I'm still a newbie :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now