View Full Version : Compute ray direction

06-15-2016, 05:00 AM
Hi all,

I need to compute primary ray direction in fragment shader. I do this like that :
- compute the 4 ndc cornners of near plane

vec3 hg = (invV*vec4(-1, 1,-1,1)).xyz;
vec3 bg = (invV*vec4(-1,-1,-1,1)).xyz;
vec3 hd = (invV*vec4( 1, 1,-1,1)).xyz;
vec3 bd = (invV*vec4( 1,-1,-1,1)).xyz;

- put uv between [-1,1]

vec2 uv = 0.5*(vec2(-1)+2*texCoord.xy);

- compute the position of a pixel on near plane

vec3 pt = mix(mix(bg,bd,texCoord.x),mix(hg,hd,texCoord.x),te xCoord.y);

- at last compute the ray direction

vec3 dir = normalize(pt-camPos.xyz);
vec3 orig = camPos.xyz;

Am i right?

06-15-2016, 06:13 AM
You can do it simpler :

vec3 pt = (invV*vec4(uv,-1,1)).xyz;