Environmental constraints and genetic basis for the evolution of viviparity