Solved

Multiplying by Modelview Matrix Gives Unexpected result

Posted on 2008-06-17
5
1,038 Views
Last Modified: 2013-12-26
In a previous question (http://www.experts-exchange.com/Programming/Game/3D_Prog./Q_23489987.html)
I asked how to find the coordinates of an object after undergoing a transform.

To do this I had to multiply the point I wanted to translate by the modelview matrix. The issue is that the values I get back are the opposite to what's occuring on the screen. ie. if the objects are rotated clockwise on the screen the results I get back are moving anticlockwise.

Do I need to Invert the modelview matrix or somthing like that? Could someone provide code to aid me in this?

//function to do multiplication
 
#include <gl/gl.h>
 
void MultiplyVMatrix(GLfloat* Vertex, GLfloat* Matrix, GLfloat* ResultMatrix)
{
     
      for(int i=0;i<16;i++)
                ResultMatrix[i/4]+=Matrix[i]*Vertex[i%4];
 
}        
 
**************************************************************
//modelview matrix and multiply co-ordinate of cube
 
GLfloat matrix[16];
GLfloat vertex[4]={1,2,-5,1}; //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

0
Comment
Question by:Jamsb
  • 3
  • 2
5 Comments
 
LVL 2

Author Comment

by:Jamsb
ID: 21803397
That's twice I've posted wrongly.

*Correction*
GLfloat vertex[4]={1,2,-5,1};
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 150 total points
ID: 21813276
I think your problem is that you think the matrix is in row major order, but it is actually in column major order.  

see http://www.opengl.org/sdk/docs/man/xhtml/glLoadMatrix.xml to see this illustrated.

That would mean that tou want ResultMatrix[i%4] += Matrix[i]*Vertex[i/4]
0
 
LVL 2

Author Comment

by:Jamsb
ID: 21816770
I feel so stupid now! Its hard trying to imagine what's going wrong with all this Matrix math!  Thanks very much :)
0
 
LVL 2

Author Closing Comment

by:Jamsb
ID: 31467925
Exactly what I wanted.
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 21816875
The only reason I know to look for this is that I did something very similar a few weeks ago, so don't feel too bad.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maths question - easy points 12 70
Homework Help 5 95
Calculating Probability 18 96
Currency Conversion? 1 98
Introduction On a scale of 1 to 10, how would you rate our Product? Many of us have answered that question time and time again. But only a few of us have had the pleasure of receiving a stack of the filled out surveys and being asked to do somethi…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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