A demonstration of physically impossible spaces using portals to join two spaces together.
This project makes use of several graphics technologies and techniques in order to make portal flows as smooth as possible.
In order to draw these worlds-in-worlds, this program makes use of the stencil buffer. Each pixel has an associated stencil value that can be used to decide whether or not to write to the framebuffer. (Just as the depth buffer 'tests' before it writes, so too does the stencil buffer). This way, we can draw the world from the portal's view space, but only in a specific, confined area on the screen. This our 'window' into another room.
The portal view must be relative to the portal's orientation, but also the player's view direction as well. There are a lot of coordinate space transformations to put the camera where it needs to to correctly render. Additionally, this same transformation code is used on the CPU for when the player (or objects) walk through the portal plane and are teleported to the other side.
An oblique view frustum is a view whose near clipping plane aligns with any arbitrary plane within the frustum. Because we don't want anything behind portals drawing (giving strange results), we can create a matrix whose near clipping plane aligns with the portal's plane. I found an incredibly useful resource linked below.
Although barebones, I implemented a few optimizations in hopes to get it running as fast as I could.