03 December, 2018

Interior Mapping, The Theory

Ok so this is a little writeup on how interior mapping works. no images for now cause its late and im tired.

For those that have never heard of it, this is a technique to make rendering of many/endless rooms possible without having to actually generate the geometry for it.

I will word this write up for use with normal GPU shaders, but the math behind it can also be translated to any raytracer. This will also only cover the mapping of simple rooms of the same size with no objects inside, but the techniques described here can be extended to also achieve that

Interior mapping (short IM) utilizes ray tracing to procedurally generate the illusion of rooms that are made of geometry. For this to work we need the following:
  • world position of our fragment/pixel that we are rendering. (WP)
  • view vector pointing from camera to the pixel (VV)
Now comes the magic, using WP and VV we can now cast a ray into our virtual interior space. We only have to figure out which wall is hit by our ray, and where it hits, then translate that to UV coordinates that we can use to determine the color of our fragment/pixel.

Because we are limited to simpler programming constructs in a shader i seperated the components of the raycast. Ill describe the process for the X axis, but it is the same for the other axes. Using the axis value i calculate which walls are enclosing it, so e.g. 0.5 is enclosed by a wall at 0 and 1 (or any other division that you wish). Using this information i think up two planes that i can intersect my ray with. e.g. one at 0 with the normal (1,0,0) and one at 1 with the normal (-1,0,0). we now have 2 endless planes(walls) that encase my one dimensional coordinate 0.5. I now use the same component from my ray and calculate the t in 0 = 0.5 + t * ray.x .this will tell me a factor that i have to multiply the ray with till it hits that wall. to accelerate this, i examine the sign of the rays component, to figure out which wall it will hit (forward or backward). this gives me t_x, which tells me the factor for hitting the wall in X direction. now i do the same for y and z to get t_y and t_z. the lowest of those will tell me which wall pair i am going to hit first, x, y or z. again using the respective component from the ray i can figure out if it will be the left or right (respectively up,down,front,back) wall. i then calculate the point of the hit using WP+VV*t using the respective t. i then use the other two components left in the vector to generate my UV coordinates for the texture. i can then return the color, and optionally the normal of the wall, because i know which axis im on, and which wall i hit, of which i know the normal.

So thats basically it. you trace a ray to figure out which wall you hit. the magic is in how its done mathematically. in my actual implementation i did the tracing using a 3dimensional form, which can be useful for tracing objects inside a room that are not at axis aligned. i used the general equation of a plane and a line, and plugged them into each other. then i used wolfram alpha to solve for t to get the t needed to intersect the plane with the line

keep in mind that this is only one of the ways to achieve interior mapping. there are others, its up to you which one you want to use

No comments:

Post a Comment