Shrek is not just a sullen ogre who settled in a swamp. Such eternal loneliness was a consequence of the hero's conflict with his own people.
The fact is that ogres in Shrek's world are cruel and warlike creatures for whom attacks on people are considered the norm. But he refused to follow these rules, did not support aggression and even spoke out against cruelty. As a result, Shrek was expelled as a traitor.
The character would not have been able to make friends with people, because his frightening appearance caused only fear and hostility. Therefore, he resigned himself to the role of an outcast and even got used to the image of an unfriendly ogre. Shrek found an abandoned swamp, which remained deserted and was far enough from other ogres to avoid conflicts.

For a long time, the pond and the area around it belonged only to Shrek, but then Lord Farquaad evicted some fairy-tale creatures to the swamp. Having lost his loneliness, the hero in the cartoon Shrek got into adventures and in the end acquired a real family and friends.