# Portal Projection

Ok ok I admit, I'm bored and toying with a portal script like the one on steam.

I've figured out the following:

• relative positioning of the mirrored cameras
• render to texture for the portal (the texture is the view from the opposite camera)
• poly culling (since the camera is behind the other portal, I cull all polys behind the front face of the portal

I can't figure out how to scale the projection so I ignore everything outside of the back of the portal. Here's a picture to explain.

The back of portal 2, which is viewed from camera 2, is being displayed on the front of portal 1. My problem is the scale. I want to ignore everything around the dotted line and scale up the view from cam 2 so it fits the front of portal 1 perfectly. One big problem with that scaling is the perspective changes as you move around. I'm not good at matrix math to figure this out. Anyone have an idea?

I'm looking for some matrix I can set as the projection matrix for cam2 to make this work.

-
I have no idea what's going on in the right half of your diagram, and what it has to do with the left half or your problem. – doppelgreener May 5 '11 at 1:05
Sorry, the left is an overhead view of the cameras. The right is the view from cam1. The blue area is the rendertexture for portal1 which displays the view for cam2 – brandon May 5 '11 at 1:49
I'll make a demo of this tomorrow so I can show more precisely what my issue is. It's hard to get it across while it's all in the theory stage :) – brandon May 5 '11 at 20:20
Taking a look at pseudoforms source code might help. (Pseudoform is an open source game/engine with portals - pseudoform.org/index.php?id=media) – sarahm Mar 18 '13 at 8:54
I've since taken linear algebra and all became clear. I just needed to apply a scale to the projection matrix for camera 2 (the dummy cam). In hindsight, it would have been easier to just work directly on the screen buffer and do a replacement for the pixels within the boundary of the portal entrance – brandon Mar 19 '13 at 18:00

Consider this as a pure 2D problem, you have the two renderings as 2D images and all you got to do is overlay one on the other, no scaling, moving around or funny angles of projection. If you just do the simplest copy pasting job you can imagine, it's going to fit perfectly.

I guess you might want to do this in a more seamless manner, using the cam 2 rendering as a texture for the cam 1 rendering. For that to work properly the cam 2 rendering must be the texture of a plane that is perpendicular to the camera angle in rendering 1.

I hope this answers your question, it sure will take a bit of trickery to get everything ordered just right.

-
Check out the image on the right. That's exactly what the view from cam1 looks like. The dotted blue bit needs to be scaled to fit the entire blue region for portal1. So it's already technically copy and pasted. It needs to scale now to ignore the rest of the view which comes from cam2 – brandon May 5 '11 at 1:54
Well, cam2 needs to be exactly the same distance, angle and position from the portal plane as cam1, and you'd render at the same resolution, I thought you had that fixed. In any case, for the sizing to work the cam2 rendering should be applied to a rectangle of width 2*[distance to camera]*[camera zoom], that should give it the correct size. – aaaaaaaaaaaa May 5 '11 at 2:12

This system is unnecessary. Just hardware instance the room (and it's contents), then translate it and rotate it if necessary to match up and let the GPU do the culling. This has the large advantage of making the world seamless- that is, you don't have to specially make the AI work with it or anything, as you're extending the world, effectively, rather than just rendering it twice.

-
Can't do that. That causes all sorts of limitations of how I have to setup the portals and their positions. Like I said, I have everything implemented except the projection matrix. It's a very simple solution that causes no limitations like a second instance – brandon May 5 '11 at 1:47
Portal uses render textures by the way. So does narbacular drop which is where the idea for portal came from – brandon May 5 '11 at 1:50
As someone who literally yesterday played through Portal 1 with developer commentary, it doesn't use render textures :) They had performance issues with doing so. It will re-render part of the last framebuffer as a fallback solution for infinite portals, but in general, it's rendering nested geometry. Check the dev commentary for test chamber levels 3 and 14, and play around with the behavior at 14 a bit - it's quite visually clear when it's deferring to the old rendered version. – ZorbaTHut May 7 '11 at 22:52