webgl – See space from gl_FragCoord

I try to get the sample code so that CalcEyeFromWindow () (first on the page) works in WebGL, but has no luck.

Combine the code with the following with some explicit values ​​in case something is wrong with the passing of parameters. Visualizing the output in a sphere of radio unit, centered in the view and 3 units of the camera. Using step () in the output to provide clear color transitions in the sphere.

The current output is black. If I set the step to 0 for X and Y, the transitions for X and Y are centered on the sphere, so it seems that the problem is at least partly due to the calculation of clipPos.w.

Vertex Shader:

precision highp float;

attribute vec3 a_position;

uniform mat4 u_modelMatrix;
uniform mat4 u_viewMatrix;
uniform mat4 u_projectionMatrix;


main vacuum ()
{
gl_Position = u_projectionMatrix * u_viewMatrix * u_modelMatrix * vec4 (a_position, 1.0);
}

Fragment shader:

precision highp float;

uniform mat4 u_inverseProjectionMatrix;
uniform mat4 u_projectionMatrix;

vec3 CalcEyeFromWindow (void)
{
vec3 ndcPos;
// Hard code window space position in gl_FragCoord
// Dimensions of the hard code view
ndcPos.xy = (2.0 * gl_FragCoord.xy) / vec2 (1000, 500) - 1;
// Depths of hard code clipping
ndcPos.z = (2.0 * gl_FragCoord.z ​​- 1. - 4.) / (4. - 1.);

vec4 clipPos;
clipPos.w = u_projectionMatrix[3][2]    /
(ndcPos.z - (u_projectionMatrix[2][2]    / u_projectionMatrix[2][3]));
clipPos.xyz = ndcPos * clipPos.w;

return (u_inverseProjectionMatrix * clipPos) .xyz;
}

main vacuum () {
// Step to clarify the position
gl_FragColor = vec4 (step (vec3 (.5, .5, 2.2), CalcEyeFromWindow () *
vec3 (1,1, -1)), 1.);
}