I was wondering how it could be that in ancient times public nudity, sexually related topics and even semipublic sex could be a thing, then be outlawed (especially in tage islamic world), be reinvented after the middle ages, again completely banished during industrial revolution until the 50s and 60s and today be handled completely different in the various cultures and religions.
The shift you are referring to is termed 'the invention of privacy.'
It is an incredibly complicated question that touches a lot of issues. Also, your question focuses on too wide of a topic, so I'll limit the discussion here to Western Europe (and by extension, the US) because that is where my expertise is.
The works and research in this field is somewhat obscure and highly complicated. The most important book, La Vie du privee--The History of Private Life--is nearly four volumes long. The other major (recent) work by McKeon is about 1000 pages and very difficult to read as a layman (or even as an expert...)
As you noted, there was not a huge reaction or thought about semipublic sex, nudity or sexual topics. These things were common, from what we know of surviving records in London, Paris, Florence, Venice, Milan, and other surviving city records. For example, there were Cock and Hen bars in London, which would be equivalent to a single's bar today. In these clubs, what we would call nonpenatrative sex was on more or less public display, and indeed, it was common to see it in the streets as well.
Part of this is because there was no police force to enforce public decorum as we have today. And even if they were, they would not necessarily have had the authority to do anything about public lewdness, drunkenness, or other misbehavior as we would see it.
The larger part of it, however, had to do with 'the invention of privacy.' What I mean by that is that in the late middle ages and early renaissance, it was quite common for households to be one room, and in particular, to have one shared bed for the entire family. This meant that children would be in bed and in all probability witness or be aware of their parents making their siblings. Here's Ian Moulton:
Bedchambers-and the beds themselves-slowly shifted from being common living areas (in lower-class homes) or sites for social gatherings (in upper-class ones) to being what they are today-private space for the single person or couple who sleep in them. Reading became dominantly private and silent rather than spoken and communal.
The shift happened because of two major reasons. The first is that building material became much cheaper and architechtural knowledge became much more common, allowing the creation of multiple rooms, better heating, and 'the creation of the upstairs.'
The second reason is a little more complicated, and stems, essentially, from the Protestant Revolution touched off by Martin Luther. In "'traditional' cultures, the difference between public and private modes of experience is conceived as a distinction that is not particulary separate. Sex (or prayer, or work) could happen in public or private and the experience was not necessarily different.. In modernity the public and the private are separated out from each other.
Here's McKeon:
In the eyes of its adversaries, the Roman Catholic Church interposed between God and the believer a hierarchy of ecclesiastical rituals and powers whose officious mediations encouraged irresponsibility, indifference, and hypocrisy while siphoning off power to the secular institution of the Church. Reformation doctrine made the question of salvation explicit and self-conscious by placing it in the hands of Luther's 'priesthood of all believers.' --by making it a matter not of external authority but of internal conscience."
....
for justification [by] external works [as in Roman Catholicism] the reformers substituted justification by internal faith or 'grace,' which the Calvinist doctrine of predestination ensured was utterly beyond the will or actions of the believer." ...through discipline in their private callings, the elect became 'public' saints."
[tl;dr]: Protestant religions believed that salvation came from an own interior faith (grace), and it did not matter the good works you did in the public world.
However, the controversial nature of Protestantism in some places, and the differences between various protestant sects made life difficult for believers of one doctrine or another. FOr example, in England the state Anglican religion did not take well to alternative streaks such as Puritanism or Calvinism
Thomas Sprat believed that "all wise men should have two Religions; the one a Publick, for their Conformity with the People, the other, a private to be kept in their own breasts.
In McKeon's view:
"17th century protestants literalized this location through the institution of the devotional closet... they set the secrecy, solitude and privacy of the devotional closet against more public spaces for prayer. This set up the correlation with domestic spaces in which these activities (and others) ocurred--micro, macro, bodily and architectural privacy.
In other words, the Protestant need for internal and private space lead to an internal or private room or devotional closet, that developed into the architecture of the home. Once a intellectual division existed between private and public faith and persons, the divisions became more public and acknowledged. This had major impacts on Western culture's ideas and led to the separation of many other things (such as men's and women's work, and the division between the 'animal' and 'human' worlds.)
[This] (http://www.reddit.com/r/AskHistorians/comments/1lkszg/askhistorians_ama_thread_history_of_sexuality/) thread might help to fill in the more modern bits that u/vertexoflife wasn't able to.