This is a very simple question, but after a brief search and reading through the "similar questions" I could not yet find an answer.

I'm creating a 2D game using OpenGL and came across a problem: Let's say I wanted to create portals. An Entity can move into a portal A and come out of the corresponding portal B. Transforming the coordinates and velocity of the entity is easy, but this would create "hard" texture-teleportation. Which means: while half of the entity is in the A-portal, it won't be rendered on the corresponding B side. How would I create a smooth transition?

My first Idea was: I define "exit"- and "entry"-zones containing the transformation function (Matrix). Now when an Entity enteres the exit-zone the entity is also drawn at the entry zone using the transformation-matrix relative the the entry/exit-zones. But drawing it twice would still give me the difficulty, that I need to clip the entity on the portals "backside". (The entity shall vanish behind portal A when entering).

So my second thought was, that there should be a solution using (vertex?) shader? But how would such a shader look like? (Example code would be nice, but not neccessary. I'd rather want to have a "what the shader would do" answer). Would I define Uniforms (exit- and entry zones) which the shader would clip/transform ("move") the vertexes from?

How does a typical solution for this clipping look like? Or where could I read some information about these kind of problems? I only found some to 3D versions, where the view of portal B should be rendered in portal A. But this does not solve the object-moving-through-it problem...I think In my opinion changing the draw-order, or rendering something above the backside of the portals is not the right way to go, since this could create strange behavior in certain situations. (Portal B is just behind portal A for example. One of both would be overdrawn I think.)

  • 1
    \$\begingroup\$ Your idea of drawing the object twice and clipping the part that's "behind" the portal is pretty much how it's done. Have you had any difficulty implementing that strategy, that we can help you overcome? \$\endgroup\$
    – DMGregory
    Commented Oct 5, 2019 at 20:52
  • \$\begingroup\$ Well I did not yet come to implement it, since I'm still looking for the "correct" solution. My difficulty in that solution is: How would I clip the texture? There might be something drawn behind the portal, but changing draw-order seems to not solve the problem, if for instance portal b would be just behind portal a (which should create a total normal walk animation). I hope you understand what I mean? So basically I always want to have only the corresponding part of the texture drawn at the corresponding position. Not more and not less \$\endgroup\$
    – Apahdos
    Commented Oct 5, 2019 at 20:59
  • \$\begingroup\$ Want to edit your question to focus on this clipping issue specifically? eg. "How can I clip my sprite as it passes through a portal?" A tightly focused question often helps encourage deep, useful answers. :) \$\endgroup\$
    – DMGregory
    Commented Oct 5, 2019 at 21:06
  • \$\begingroup\$ @DMGregory I thought that was what I was asking for ^^. I changed the title and edited the last paragraph \$\endgroup\$
    – Apahdos
    Commented Oct 5, 2019 at 21:39
  • \$\begingroup\$ It's a bit unclear what you mean with "Portal B might be behind Portal A". In a 3D game, I can see one portal being placed behind another portal (for example being farther away into the sreen, so a higher Z value) but in a 2D game, why would two portals being placed onto each other? \$\endgroup\$ Commented Oct 6, 2019 at 9:10

1 Answer 1


The answer for me myself is "simply" using the so called stencil-buffer, which is a feature in OpenGL. By simplifying the question to "how to clip something behind a wall in 2D" I was able to find the feature and use it. By adding the region in front of the wall (or portal) into the stencil buffer and then only allowing rendering of the entity, if the value within the stencil buffer is 1 OpenGL clips away the rest of the entity, which moves into the wall/portal.

Solving the coming out of the other side of the portal part will simply be adding a clone of the entity at portal B and have it move in correlation to it's clones movement at portal A.

  • \$\begingroup\$ Don't forget to mark this post as the corrected answer, if it helped you solving the problem. \$\endgroup\$ Commented Oct 10, 2019 at 19:12
  • \$\begingroup\$ Thanks for reminding. I wasn't able to do it when I posted the answer, and then forgot marking it as correct \$\endgroup\$
    – Apahdos
    Commented Oct 11, 2019 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .