There are three main types of rocks: igneous, sedimentary, and metamorphic. Igneous rocks form when magma cools and crystallizes, either underground or on the surface. Sedimentary rocks form through the compaction and cementation of sediments, usually deposited by water. Metamorphic rocks were originally igneous or sedimentary rocks, but were changed by extreme heat and pressure within the Earth's crust, altering their structure. Rocks can be transformed between these types through the rock cycle as they are weathered, eroded, deposited, buried deep within the Earth, and sometimes melted.