Expiring Todayâ€”Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Am I Doing this Matrix Multiplication Correctly?

Posted on 2008-06-16
Medium Priority
1,959 Views
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
Question by:Jamsb
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 5

LVL 84

Expert Comment

ID: 21798598
No
0

LVL 2

Author Comment

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

LVL 2

Author Comment

ID: 21798667
I would increase points on the question if i could...
0

LVL 2

Author Comment

ID: 21798714
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

LVL 84

Accepted Solution

ozo earned 300 total points
ID: 21798721
for(int i=0;i<16;i++)
ResultMatrix[i/4]+=Matrix[i]*Vertex[i%4];
0

LVL 2

Author Comment

ID: 21800636
ozo:

0

LVL 84

Expert Comment

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

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

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

ID: 21800808
0

LVL 2

Author Closing Comment

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  Iâ€™ll start with the algorâ€¦
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I wasâ€¦
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediafâ€¦
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrasâ€¦
###### Suggested Courses
Course of the Month11 days, 9 hours left to enroll