Three-dimensional free electron laser gain and evolution of optical modes