PDA

View Full Version : Compute cornners of pixels in world space?



__bob__
11-16-2015, 04:32 AM
Hi, i need for to calculate the size and the world coordinates of the 4 cornners of a pixel using the normal of this pixel?
In my fragment (where i ned this cornners) i still have the world position of each pixel, normal, camera position...

GClements
11-16-2015, 08:19 AM
Given the position (Xw,Yw) of a pixel corner in window space, convert to NDC (Xn,Yn) via the inverse of the viewport tranformation:


Xn = 2*(Xw-Xl)/(Xr-Xl)-1
Yn = 2*(Yw-Yb)/(Yt-Yb)-1

where Xl, Xr, Yb, and Yt are the left, right, bottom and top edges of the viewport.

If M is the inverse of the model-view-projection matrix, (Xp,Yp,Zp) is the primitive's normal, and (Xr,Yr,Zr) is any point on the primitive, calculate


vec3 normal = vec3(Xp,Yp,Zp);
vec3 ref = vec3(Xr,Yr,Zr);
float d = -dot(normal, ref);
vec4 plane = vec4(normal,d)
vec4 p0 = M * vec4(Xn,Yn,0,1);
vec4 p1 = M * vec4(0 0 1 0);
float q = -dot(plane, p0) / dot(plane, p1);
vec4 p = p0 + q * p1;
vec3 result = p.xyz / p.w;