In Uganda, two people who have sex could end up serving life sentences in prison if they happen to be the same gender. Should friends or family fail to tip officials off, they run the risk of joining them in jail under a law passed last week. The measure — which has to be signed off by the President — marks the culmination of a four-year campaign. “This is a victory for Uganda,” said David Bahati, the MP behind the bill. “Parliament has voted against evil.”

Most people in Britain would see bigoted Bahati as the real evil in this case. On the same day, the Nigerian senate passed an anti-gay bill, outlawing even membership of advocacy groups. Just days before, India’s top court rejected a ruling decriminalising homosexuality and Australian judges struck down gay marriage laws, even as Russia’s President sanctions state prejudice, fuelling a climate of fear.

It’s a depressing end to the year on one of the key human rights issues of our age. Yet we should not despair. Yes, these examples — and there have been other grotesque setbacks in the global fight for gay equality, to say nothing of beatings and murders — are disturbing. But we are in the midst of a worldwide revolution in which the tide is turning against homophobia.

In the U.K., today there are 24 openly gay MPs — and, remarkably, more on the Tory benches than those of other parties combined. Same-sex marriage was opposed by two-thirds of voters three decades ago, while nine out of 10 were horrified by gay adoptions; now only one person in five disapproves of gay relationships, and about half are comfortable with gay adoption. Similar shifts can be seen elsewhere in the west and Latin America, driven by more tolerant and globalised generations.

A clear majority of Americans support equal marriage; a decade ago, the figure was less than one-third. Amid the furore, we should not forget that, not long ago four-fifths of Americans disapproved of interracial marriage; it was accepted by a majority there only shortly before the turn of the 21{+s}{+t}century. A decade later, voters chose a black president.

Such transformations show how rapidly societies can alter norms. Global surveys show antipathy to gay men and lesbians falling fast, with just one in three saying homosexuality is never acceptable. There are signs of change in Asia, where a majority in Japan and the Philippines now accept homosexuality, and support for gay rights has more than doubled in South Korea in six years. Even in China and India there is evidence of a significant decline in hostility: this may be aided in India by reaction to judicial recriminalisation.

The most antediluvian attitudes are found in poorer countries with the highest rates of religiosity. Often they are the legacy of corrosive colonial codes: half the remaining sodomy laws are relics of British imperial rule. The journey towards genuine equality for lesbian, gay and transgender people is still long. It will speed up if the Pope follows his stateent — that he cannot judge someone for being gay — up with real action. Regardless, those recent setbacks should not cause despair, for the signs are that humanity is moving in the right direction. — © Guardian Newspapers Limited, 2013