How to write own GluUnProject()

good day,

ive been trying to implement my own version of gluUnproject()

the window size is 250*250;

for the sake of my test im trying to find the obj position of window cords 125,125,0.

so i pass those values to gluUnproject and it returns:
X: 0 Y: 0 Z : 3.9999999433755855

passing the same values to my version i get these values:

X: 0 Y: 0 Z: 1.8999999320507073

ive checked my inversion method and its returning the correct values.

is there nething obvious that ive missed out ??

and does any one know of any source code examples of other people doing their own implementation ??

Thanks for your time.

vec4* Matrix::UnProjection(double x,double y,double z,Matrix model,Matrix proj,Matrix view)
vec4* newPos = new vec4();
vec4* cords = new vec4();

Matrix m;

Matrix inverseProjection = m.Inverse(m.multiplyTwoMatrix(model,proj));

cords->x = 2*(x-view[0])/view[2] - 1;

cords->y = 2*(y-view[1])/view[3] - 1;

cords->z = 2*(z)-1;

cords->w = 1;

newPos = m.MultiplyVectorMatrix(inverseProjection,cords);

return newPos;

Check the source in Mesa3D.

And a description of the algo: