AFAIK, the history of codpiece is funny. I read that in Middle Ages, men wore tunics and chausses, a sort of stockings, with nothing in the middle. But tunics went shorter and shorter, so codpieces were invented. Some day, someone had the idea of sewing together the chausses and the codpiece in one garment, which gave
breeches.