4x3 Matrix for 3D

We are using Opengl for Computer Vision work. Doing this we encountered the following problem.

We don’t want to set view[point, camera etc. explicitely.
In image processing and computer vision we use 3x4 projection matrices §
to map from 3D (X,Y,Z,1) to image coordinates (x,y,1) as follows:

[ P11 P12 P13 P14 ] [ X ] [ x ]
[ P21 P22 P23 P24 ] [ Y ] = [ y ] ( ‘=’ is up to a scale factor)
[ P31 P32 P33 P34 ] [ Z ] [ 1 ] [ 1 ]

Where x = ((P11X+P12Y+P13Z+P14) / (P31X+P32Y+P33Z+P34)) is the image
x position and y is computed in a similar fashion.


Now what I would like to do is find the projection, viewport and modelview
parameters in opengl that correspond to an arbitrary projection matrix


NOTE: We already tried the following: First set the modelview matrix to identity. Then set the projection matrix:

[ P11 P12 P13 P14 ] [ X ] [ x ]
[ P21 P22 P23 P24 ] [ Y ] = [ y ] ( ‘=’ is up to a scale factor)
[ 0 0 0 1 ] [ Z ] [ z ]
[ P31 P32 P33 P34 ] [ 1 ] [ w ]

BUT: We forgot that we loose depth information this way in opengl.
Unfortunately it’s not quite that easy, as the projection matrix, P, that
I’ve got maps directly into image coordinates(!). This means that

(P11X+P12Y+P13Z+P14)/(P31X+P32Y+P33Z+P34)

is in image coordinates already and shouldn’t need a viewport
transformation to be applied to it.


Actually we can’t work out how to compute the ‘correct’ z-value.
And how to use it in opengl. What happens now is, that everything
is “projected” to a plane, so opengl has no depth information and
thus draws elements that should be hidden (in the back of the object).


Any help is highly appreciated!

Actually, why doesn’t the matrix with the third row set to 0 0 0 1 work? If all your w-cordinates are 1, that should set z to one for all your vetice when multiplied by this matrix. Then, after perspective division, they should be propotional to 1/w, and the depth buffer should work just fine. Not the correct values maybe, but still in a correct z-order, isn’t that enough for you?

Perhaps the z-values are put to the depth buffer without the perspective division, that would explain why that matrix doesn’t work. In that case, instead of 0 0 0 1, replace the third row with p31 p32 p33 p34. Now the depth values equal to w, and depth buffering should work again, only with inverted depthFunc.

In both cases you might have to scale the third row to somehow get all the values between 0 and 1. I don’t know how, but I hope this helps you a little.

-Ilkka