Trying to get at least the very simple part 1 of the Lighthouse3D Radar Frustum Culling tutorial to work... and am absolutely baffled that I can't even make that part work in my renderer.
So the first step is: you test if a point is in front the near-plane or behind the far-plane, and early-cull if that's the case. (If not, you'd then perform further tests but I'm stuck just with that first part.)
I use the world-space center (x1y2z3) of a 2x2 cube and have a camera that I can move around and rotate freely. All my vector and matrix stuff must be rather solid as the renderer otherwise works just fine. So here's my take (in Go) of this first part, the simple "Z vs near-or-far" testing:
func (cam *Camera) frustumHasPoint(point *Vec3) bool {
var pc Vec3
v := point.Sub(&cam.Controller.Pos) // point minus camPos
ref := cam.Controller.dir // take a copy of camDir
ref.Z = -ref.Z
ref.Normalize() // camDir was already normalized but anyway...
pc.Z = v.Dot(&ref)
if pc.Z > cam.Perspective.ZFar || pc.Z < cam.Perspective.ZNear {
return false
}
return true
}
Now why do I reverse the Z of ref? Because in the tutorial they write: "Notice that the referential in the figure is not a right hand system (as in OpenGL), because the orientation of Z has been reversed to make the tutorial more intuitive" -- well, in a GL tutorial of course this has the opposite effect...
Well if do reverse Z as above, it culls more than it should about 50% of the time; if I don't, then it "over-culls" about 98% of the time..
Wwhat am I missing?
Resolved. Cause was brain malfunction... the tutorial clearly writes about getting the x/y/z axes first for describing the frustum, somehow I missed that..