Calculate the ray direction vector from the screen coordinate

I am looking for the best way (or note that this is the best way) to shift the pixel coordinate to the corresponding beam direction from an arbitrary position / direction of the camera.

My current method is as follows. I define "camera" as a position vector, a lookat vector, and a vector up, named as such. (Note that the lookat vector is a single vector in the direction the camera is facing, not where (position-lookat) is the direction, just like the standard in XNA Matrix.CreateLookAt). These three vectors can uniquely determine the position of the camera. Here is the real code (well, actually, not a real, simplified abstracted version) (Language - HLSL)

float xPixelCoordShifted = (xPixelCoord / screenWidth * 2 - 1) * aspectRatio; float yPixelCoordShifted = yPixelCoord / screenHeight * 2 - 1; float3 right = cross(lookat, up); float3 actualUp = cross(right, lookat); float3 rightShift = mul(right, xPixelCoordShifted); float3 upShift = mul(actualUp, yPixelCoordShifted); return normalize(lookat + rightShift + upShift); 

(return value is the direction of the beam)

So here is what I ask: this is the best way to do this, possibly using matrices, etc. The problem with this method is that if you have a too wide viewing angle, the edges of the screen will get a "radially stretched" appearance.

0
source share
1 answer

You can calculate it ( ray ) in a pixel shader, HLSL code:

 float4x4 WorldViewProjMatrix; // World*View*Proj float4x4 WorldViewProjMatrixInv; // (World*View*Proj)^(-1) void VS( float4 vPos : POSITION, out float4 oPos : POSITION, out float4 pos : TEXCOORD0 ) { oPos = mul(vPos, WorldViewProjMatrix); pos = oPos; } float4 PS( float4 pos : TEXCOORD0 ) { float4 posWS = mul(pos, WorldViewProjMatrixInv); float3 ray = posWS.xyz / posWS.w; return float4(0, 0, 0, 1); } 

Information about the location and direction of your camera is in the View matrix (Matrix.CreateLookAt).

0
source

All Articles