Does OpenGL performs Visibilty algorithms based on z-index? Or we have to write our own algorithms. Mainly I'm referring to z-buffer algorithm. Is it in-built?
Yes, OpenGL (or really, the GPU hardware that OpenGL is used to access) has built-in support for depth testing via z-buffers. To enable it, you just have to ensure your OpenGL context includes a depth buffer when it's created (the details of this depend on what OS or toolkit you're using). Then, insert these calls in your render function prior to drawing any geometry:
glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glClearDepth(1.0f);
GL_DEPTH_BUFFER_BIT to your
glClear call. That should be all you need to do to enable z-buffering. For more information, see this tutorial.