Original Post
I have a raymarcher that I'm trying to integrate into a normal polygon render. For test purposes this is just the basic OpenGL pipeline, while the raymarcher uses GLSL.
I'm having trouble writing the correct Z values in the raymarcher. I appreciate most of the problems could be solved by sorting, but I don't want to have to disable the depth check when rendering polygon objects near the raymarched objects.
The projection matrix is:
And the ray calculation is (view space):
The Z for the polygon objects is written by the GPU, and the Z for the raymarcher is written like this:
From what I've Googled and read, that should be the correct value, or the GPU should use it to write the correct value to the Z. But the Z for the raymarcher is clearly following a different curve - polygons get drawn over it when it's near, and it gets drawn over polygons when its far.
Can anyone tell me where I'm going wrong? Or is there just not enough accuracy with a basic D projection matrix?
I'm having trouble writing the correct Z values in the raymarcher. I appreciate most of the problems could be solved by sorting, but I don't want to have to disable the depth check when rendering polygon objects near the raymarched objects.
The projection matrix is:
static float p_fMatProj[16] = { 0 };
float aspect = width / height;
float D = 1.303f;
float zp = D;
p_fMatProj[ 0] = zp;
p_fMatProj[ 5] = zp * aspect;
p_fMatProj[10] = 1.0f;
p_fMatProj[11] = zp / D;
p_fMatProj[14] = -D;And the ray calculation is (view space):
vec2 position = -1.0 + (2.0 * (gl_FragCoord.xy / resolution.xy));
position.y *= resolution.y / resolution.x;
vec3 target = vec3(position, 0.);
vec3 eye = vec3(0., 0., -1.303); // FOV 75 degrees.
vec3 ray = normalize(target - eye);The Z for the polygon objects is written by the GPU, and the Z for the raymarcher is written like this:
float zc = (gl_ProjectionMatrix * vec4(point, 1.)).z;
float wc = (gl_ProjectionMatrix * vec4(point, 1.)).w;
gl_FragDepth = zc / wc;From what I've Googled and read, that should be the correct value, or the GPU should use it to write the correct value to the Z. But the Z for the raymarcher is clearly following a different curve - polygons get drawn over it when it's near, and it gets drawn over polygons when its far.
Can anyone tell me where I'm going wrong? Or is there just not enough accuracy with a basic D projection matrix?