Origin of orbital ordering in YTiO3 and LaTiO3