Created a framebuffer with a renderbuffer depth, depth tests did not work correctly. Changed to a depth texture and it worked fine.