Why is adding mirrors to video games so hard from a programming point of view?

1 15
Avatar for xzero
Written by
2 years ago
Topics: Technology, Articles

Reflection programming, which distinguishes mirrors (or any other reflective surface such as water) is one of the most difficult things in programming 3D graphics in real time (such as games) and the most expensive (it needs a long processing that most graphics card processors until recently could not) This makes them unsuitable for real time graphics, due to the need to process the indirect lighting that gives these surfaces the advantage of reflection.

In the most popular Rendering mode in games today, which is Rasterization, the final color of each pixel within a certain frame is calculated by fetching its Texture (after selecting the polygon and its 3d model) and then calculating the existing Direct Lighting In the scene by calculating if there is a direct ray from the existing lights that reach this polygon without obstacles between them and the accumulation of the effect of these rays in addition to taking the color of these rays into account.

In the event that the player is standing in front of a mirror in a room within the game and it contains a single light, then the color of this player’s face can be known based on the reflection of the room’s light on him, while the final color of the mirror cannot be known based on that light only, but each light ray must be calculated He went to the player's face, and then part of it was reflected in the mirror, then the image of the player's face is drawn on the mirror, and this process, as I said, is very expensive in terms of the necessary calculations and the number of rays required to obtain an acceptable quality reflection image.

Previously, this weak point was circumvented by several optical tricks, one of which is to consider the mirror as a transparent window, and behind it a multiplication of the entire scene, that is, copying all models in the scene and placing them in reverse from the other side of the window, including making a second copy of the character Which stands in front of the mirror and moves it in reverse with the movement of the main player, which gives the impression that it is actually a mirror. The disadvantages of this method are that it is not used in complex scenes and is limited to simple or closed scenes, or for example finding a reflection only for the player and not for other purposes behind him.

To get a true reflection, regardless of the complexity of the scene, you need global illumination, ie calculating the lighting in the scene not only from local and direct lighting, but from all different light sources, including lighting reflected from other objects.

One of the techniques to achieve this is ray tracing, and its idea differs from Rasterization in that instead of going to the object and then knowing the direct lights that hit it, a reverse ray is sent here from the supposed pixel place within the frame, then tracing its path and seeing what the objects are It collides with it, then we follow the path of this ray and see if it collides with lights or other objects, and this ray is followed several times after each reflection according to the accuracy of the desired reflection, and each time the resulting light is cumulatively calculated.

In recent generations of Nvidia and AMD graphics processors, Ray tracing has finally been supported, as these processors have reached the point where they can perform these complex operations within real time constraints (although they are still in their infancy, and their performance is still relatively weak and cannot Apply it in high screen resolution and ultra detail, for example, without sacrificing a large portion of frames per second.

Look at this picture, it shows Fortnite with (and without) Ray tracing

2
$ 0.25
$ 0.25 from @TheRandomRewarder
Avatar for xzero
Written by
2 years ago
Topics: Technology, Articles

Comments

Wow I never knew game programmers do go through a whole lot just to give us a realistic game. Thanks for this post.

$ 0.00
2 years ago