We present a nondestructive, high resolution optical technique to obtain the surface topography of ancient paintings. The method is based on the projection of a grid of parallel lines, using white light, onto the surface to be analyzed. When viewed at an angle different from the projection angle, the grid pattern appears deformed by the surface shape. This pattern is digitized and then processed using a spatial-carrier phase-shifting algorithm that allows recovering the surface height map with high precision. The method relies on very simple equipment, gives immediate quantitative results, and is suitable for in situ measurements. It permits the recording of topographic maps of ancient paintings as well as the measurement of local detachments of the paint layer. A theoretical analysis of the method is discussed, together with a description of the data analysis algorithm. Finally, we provide some experimental results concerning the diagnostic of a twelfth-century masterpiece.