7
\$\begingroup\$

I am making RPG in OpenGl and I need to make some portals. How should I render it if I want to see through the portal on the other side?

\$\endgroup\$
  • \$\begingroup\$ Is your game 2D or 3D? What exactly do you mean by a "portal"? \$\endgroup\$ – Anko Sep 14 '15 at 16:02
  • \$\begingroup\$ It is 3D. By portal I mean something like this link \$\endgroup\$ – Vlastimil Blažek Sep 14 '15 at 19:22
6
\$\begingroup\$

You have two scenes rendered from two camera perspectives: one in local space and one in portal space. You need to superimpose part of the latter onto the former. You need a working knowledge of G-buffers (deferred shading) to achieve this, so I suggest you begin your research there.

First shader pass: Construct a G-buffer for your local scene. In your specific case, you'll have a colour buffer dedicated to masking out the silhouette of the portal from a local perspective; it will contain a colour (say white) denoting where your portal surface is, with everything else in local space being rendered black. This mask is used so that you know which pixels of your rendered local scene should be considered as a window into "portal space".

Second shader pass: Render your portal space scene with same camera orientation, but camera position shifted by the same vector that describes the camera's offset from the portal in local space.

Third/final shader pass: Use mask to cut out the section of the portal scene that you need to super-impose over the portal surface in the local scene, and super-impose in the fragment shader.

DISCLAIMER This is all easier said than done - getting into multi-pass rendering and g-buffers is a considerable amount of work if you've never done it before - expect several long days to several weeks depending on your level of skill, available time and general luck during implementation. Also, understand that deferred shading has a high cost and may be unsuitable for certain mobiles.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Also, see this answer for why it might not be worth your time to implement! You may be better off going with an opaque, swirly portal surface. \$\endgroup\$ – Engineer Sep 13 '15 at 11:44
  • 2
    \$\begingroup\$ You don't need deferred shading and g-buffers. Portal-based rendering has existed since before video cards even had shaders. You can do it by tracking portal surfaces in your CPU-side culling phase, or do it with stencil buffers, or even do it with environment maps, or several other ways. :) \$\endgroup\$ – Sean Middleditch Sep 13 '15 at 18:36
  • \$\begingroup\$ Hi @SeanMiddleditch, you're right of course; stencil buffers are indeed one way I considered mentioning, but thought of sticking with the most modern approach. One reason for this is performant recursive portalling; another is that it slots into modern pipelines, i.e. if the OP already has g-buffers in place, and if they don't yet, they likely will at a later stage. I did not know about the CPU-side culling, thanks for adding. \$\endgroup\$ – Engineer Sep 13 '15 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.