The carbon molecules are taken from carbon dioxide (`CO_2` ), the oxygen molecules are taken from both carbon dioxide and water (`H_2O` ), and the hydrogren molecules are taken from water. The chemical reaction is as follows:
`6H_2O +6CO_2 + "energy" -gt C_6H_(12)O_6 + 6O_2`
In plants this process is called photosynthesis and the energy takes the form of light.
In humans this process is called chemeosynthesis, it takes place in the intestines, and the energy takes the form of carbohydrates.
If there is an insufficient source of carbohydrates to produce the needed glucose, gluconeogensis takes place instead in the liver using protein and fat as an energy source. This explains why low-carb diets are counterproductive. The liver either takes the protein stored in your food or from you muscles in order to produce the glucose. This in turn lowers your metabolism, which is partially a function of the amount of muscle you have.