I wrote a simple ray marcher (goal is a nice volumetric fire), but I am stuck with this one issue presented on the screenshot below:
when I am close enough, the depth test works correctly, but a tiny jerk backwards, and the box suddenly is considered to be closer than the katana.
Could you tell me what's wrong?
My setup: render fullscreen quad, and then execute this fragment shader code:
void main() {
//aabb of box in world space
vec4 bmin = model * vec4(-1.f.xxx, 1.f);
vec4 bmax = model * vec4(1.f.xxx, 1.f);
Ray r;
RayHitInfo info;
// vpos is fullscreen rect with coords in NDC [-1, 1]
vec4 rd = inverse(projection) * vec4(vpos, -1., 1.);
rd /= rd.w;
rd = inverse(view) * rd;
r.d = normalize(rd.xyz - cam_pos);
r.o = cam_pos;
ray_box(r, bmin.xyz, bmax.xyz, info); //Tavian Barnes
//branchless ray/bb intersection alg.
if(!info.hit) {discard;}
vec3 a = r.o + info.tmin*r.d; //box enter ray pos
vec3 b = r.o + info.tmax*r.d; //box exit ray pos
vec4 depth = a.xyzz; depth.w=1.;
depth = projection * view * depth;
depth/=depth.w;
gl_FragDepth = depth.z;
FRAG_COL = vec4(.9.xxx, .8);
return;
}