# Projecting a 2D ray to 3D space

This topic is 3474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

(as a coder, I communicate better in code, im not saying "gimmie code", its just literally easier for me to communicate this way) The following code translates from 3D space to 2D, perfectly.
   matrix = projection_matrix.mul(model_view_matrix);

// Transform the vertex
Vec4 result = new Vec4();
Vec4 input  = new Vec4(v.x, v.y, v.z, 1.0f);

result.x = Math3D.dot(matrix.getRow(0),input);
result.y = Math3D.dot(matrix.getRow(1),input);
result.z = Math3D.dot(matrix.getRow(2),input);
result.w = Math3D.dot(matrix.getRow(3),input);

double rhw = 1 / result.w;
double resultx = (1 + result.x * rhw) * viewport[2] / 2 + viewport[0];
double resulty = (1 - result.y * rhw) * viewport[3] / 2 + viewport[1];
double resultz = (result.z * rhw) * (depthrange[1] - depthrange[0]) + depthrange[0];

what I need is the inverse of that function, a converter from 2D space to 3D space, this is what I have currently but it does not do the job...
   matrix = projection_matrix.mul(model_view_matrix);
matrix.invert();

double resultx = ((2*(v.x-viewport[0])/viewport[2])-1)/v.w;
double resulty = ((2*(v.y-viewport[1])/viewport[3])+1)/v.w;
double resultz = ((v.z-depthrange[0])/(depthrange[1] - depthrange[0]))/v.w;

// Transform the vertex
Vec4 result = new Vec4();
Vec4 input  = new Vec4(v.x, v.y, v.z, 1.0f);

result.x = Math3D.dot(matrix.getRow(0),input);
result.y = Math3D.dot(matrix.getRow(1),input);
result.z = Math3D.dot(matrix.getRow(2),input);
result.w = Math3D.dot(matrix.getRow(3),input);

Any help in this regard would be appreciated

##### Share on other sites
1) You can't rely on a projection matrix being invertible.
2) There is no general way to recover the information lost when you reduce the rank of a vector.

For a simple example of why this is a problem, consider the vector <x,y,z> projected on the XY plane. The resulting vector is <x,y,0>.

So, given a vector <x,y,0>, find its three dimensional counterpart. The problem is that there are infinite solutions. The original z could be 5, 5000, or Π-8i. There is no longer any way to find out.

##### Share on other sites
You are technically correct, which is why I preserve the Z transformation from the first equation (I dont use it to display), but I am passing it back to second equation.

##### Share on other sites
Quote:
 Original post by GeddenYou are technically correct

The best kind of correct!

Quote:
 which is why I preserve the Z transformation from the first equation (I dont use it to display), but I am passing it back to second equation.

If you communicate best in code, then maybe you should communicate enough so I don't have to guess what problems you've worked around. The only thing that stands out about your implementation is that resultx, resulty, and resultz seem to do nothing in the second function.

Are they implemented somewhere that isn't shown?
If the function isn't doing what you expect, how is it failing?
Have you tried replacing the matrix and vector with simple things that you can verify by hand to see where they are failing?
If your matrix inversion fails, do you have any way to detect it or recover from the error?
If you are preserving information for restoration already, why not preserve the whole thing?
What problem are you actually trying to solve? Is this the best way to do it?

Johnny 5 needs input.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 96
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009641
• ### Who's Online (See full list)

There are no registered users currently online

×