Skip to main content
1 of 4
Tom
  • 213
  • 2
  • 9

Rendering portals using stencil buffer method

Due to this question I have changed the method of rendering portals. Before I was using FBO and rendering to a texture, but somehow I failed. Now I switched to a stencil buffer method, and due to this method there is more problems than before.

First I have to take care with light and shadows. They should not pass to the virtual scene where the portal is, rather the virtual scene should have their own light and shadows. For now I don't know how to solve this problem.

Second I don't know how to set the stencil parameters for multiple portals (here two). I think each portal geometry should save other numbers to the stencil buffer. For example portal one should save numbers 1, and portal two should save numbers 2. Next how to render the main scene, and what should I do when one portal imposes (in stencil buffer) other portal?

For now I am doing it that way:

  1. Render the portal one geometry to the stencil buffer
  2. Render virtual scene in portal one geometry
  3. Render the portal two geometry to the stencil buffer
  4. Render virtual scene in portal two geometry
  5. Render the main scene

Here's some pseudo-code:

glClearStencil(0); // clear stencil to 0 // glStencilFunc(GL_NEVER, 1, 1); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // render portal one geometry // glStencilFunc(GL_EQUAL, 1, 1); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // render virtual scene to portal one // glStencilFunc(GL_NEVER, 2, 2); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // render portal two geometry // glStencilFunc(GL_EQUAL, 2, 2); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // render virtual scene to portal two // // here I don't know which properties should I use // it should be something like (GL_NOTEQUAL, 1, 1) and (GL_NOTEQUAL, 2, 2) // but how to do that? glStencilFunc(GL_NOTEQUAL, 1, 1); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // render main scene // 
Tom
  • 213
  • 2
  • 9