How to calculate near and far plane for glOrtho in OpenGL ?

I am using orthographic projection glOrtho for my scene. I implemented a virtual trackball to rotate an object beside that I also implemented a zoom in/out on the view matrix. Say I have a cube of size 100 unit and is located at the position of (0,-40000,0) far from the origin. If the center of rotation is located at the origin once the user rotate the cube and after zoom in or out, it could be position at some where (0,0,2500000) (this position is just an assumption and it is calculated after multiplied by the view matrix). Currently I define a very big range of near(-150000) and far(150000) plane, but some time the object still lie outside either the near or far plane and the object just turn invisible, if I define a larger near and far clipping plane say -1000000 and 1000000, it will produce an ungly z artifacts. So my question is how do I correctly calculate the near and far plane when user rotate the object in real time? Thanks in advance!

I have implemented a bounding sphere for the cube. I use the inverse of view matrix to calculate the camera position and calculate the distance of the camera position from the center of the bounding sphere (the center of the bounding sphere is transformed by the view matrix). But I couldn’t get it to work. can somebody further explain what is the relationship between the camera position and the near plane?

It sounds like you’re on the right track. Using simple bounding primitives for your objects (e.g. spheres or OBBs). Transform these bounding primitives into eye-space. Once in eye-space, determine the min/max Z used. That’ll suggest what depth range your near and far planes should encompass.

Keep in mind that dynamically changing your near and far planes every frame can cause differences in the depth values computed and thus in depth testing. So if visual quality is a big concern, you can take steps to avoid changing your depth range more than necessary.