View Full Version : Transforming back to obkect coordinates

mehmet sirin
09-06-2010, 05:27 PM
since there is no gluUnproject in webgl, i tried to make such a function on my own.

The normal order of viewing pipeline is as follows:
(ModelView*Projection*Viewport)*Vertex = Pixelcoordinates
When i want to have object coordinates I have to reverse that order and take the inverse:

By this far everything is still correct, right?

Now the practice with some code:


var pixel=vec3.create([pixelX,pixelY,0]);

var view=getViewportMatrix(canvas.clientWidth,canvas.c lientHeigh);
var pM=pMatrix;
var mvM=mvMatrix;
var vp=mat4.multiply(viewport,pM);
var mvp=mat4.multiply(pM,mvM)

var mvpInv=mat4.inverse(mvp);
var objCoord=mat4.multiplyVec3(mvpInv,pixel)
alert(objCoord[0]/5+objCoord[1]/5+objCoord[2]/5+); //5= w component

and that is the viewport matrix:

w/2,0, 0, w/2,
0, h/2,0, h/2,
0, 0, 1, 0,
0, 0, 0, 1

The output of alert is: 349251.15 534146.2 122.0
(At this moment i dont know why the output is like that, but that's not the problem.).

Then the strange thing comes. If i click around in canvas, almost nothing happens to the output of alert. That means the coordinates are almost not changing. The only values that are changed are the one after the point of the transformed object coordinates, e.g. 349251.75, or 349251.05 etc.
It seems to be an viewport-matrix problem. But i don't know exactly..

Maybe someone of got an idea what is going wrong?

I look forward for your answer :)