PDA

View Full Version : Multiply MVP matrices by a (0,0,0) point=(0,0,0)?



Narann
02-07-2012, 05:38 AM
Hi OpenGL community. :)

I'm fighting with matrices and just discover a point that I don't understand. (As many, I'm a little new to matrix manipulation).

Even if my probkem is not directly related to OpenGL, I'm sure some advanced peoples here should have the answer to this.

I try to project a point to a camera to know his position in screen space.

I have a simple scene:
- A simple point in the center of the world (locator).
- A camera looking at this point.

I create my mvpMatrix this way (pseudo code):


locatorMatrix = locator.getMatrix(worldSpace=True) # because my locator is (0,0,0), this is a simple default matrix here
camMatrix = myCam.getMatrix(worldSpace=True)
camProjMatrix = myCamShape.projectionMatrix()

mvpMatrix = locatorMatrix*camMatrix*camProjMatrix
result = mvpMatrix*locator.getTranslation(worldSpace=True) # I multiply my "in the center of the world" point to my matrix

print result
[0.0, 0.0, 0.0]

This is actually not very surprising no? If if multiply a matrix by any (0,0,0) point, it should always return zero...

But of course my "in the center of the world" point is not at (0,0) in my camera... It is about (0.5, 0.5). This is the point I don't understand. I've done some search on internet to know how do this.

I suppose I've miss something.

I would really be very grateful if someone just point me where my error is... :(

Have a good day all and thanks in advance! ;)

BionicBytes
02-07-2012, 05:50 AM
mvpMatrix = locatorMatrix*camMatrix*camProjMatrix

The answer lies in how you are handling the Matricies. Column ordered or Row ordered? OpenGL, by default, uses the Column ordering convention where as DirectX uses Row ordering. So if you have been using the internet to look at this, you may find you have been doing only 1/2 a job as many article assume DirectX convention.

If you follow OpenGL's column ordering, then matrix muliplication is a post multiply operation. In other words, the order of the multiplication of matrices is the reverse of what you have written:
mvpMatrix = camProjMatrix * camMatrix * locatorMatrix

If you have Row ordered matrices then:
mvpMatrix = locatorMatrix*camMatrix*camProjMatrix

Narann
02-07-2012, 05:58 AM
Thank a lot. Matrices i get are supposed to be "OpenGL compliant" so yes, this is OpenGL. I've reverse the multiplication order.

But the problem doesn't change no?

I want to get point on screen coordinate. But what ever mvpMatrix is, if I multiply it by a (0,0,0) vector, I still have a 0,0,0 position in the end...

That is my problem actually. :(

I suppose I've missed something.

BionicBytes
02-07-2012, 07:46 AM
But what ever mvpMatrix is, if I multiply it by a (0,0,0) vector, I still have a 0,0,0 position in the end
No, not really.
Firsly you can't multiply a matrix4x4 by a vec3. You need to multiply by a vec4 - that's what OpenGL does.
Usually we only specify position as x,y,z and in GLSL ve have gl_Vertex as a built-in attribute. However, it's actually a vec4 whereby the last component(w)=1.0
That's important because the effect of having the vec4 (x,y,z,1.0) when multiplying by the MVP matrix is that the 4th column of the matrix is preserved (the part of the matrix which contains the translate portion). If you omit the w component or specify w=0 during your matrix functions, then the last column of the matrix is not added to the resulting vector and you will get different results.

Narann
02-07-2012, 08:48 AM
Thanks for this precious informations. :)

There was a problem with "Vector". I'm trying with "Point". I'm using pymel and I suppose I have a problem in the way I get values...

I will continue my investigations.

Thanks a lot for this! :)