Clever bug exploitation – backface culling

February 3, 2019

4 min. to read

Since the first release, SilentPatch for San Andreas comes with a fix under a cryptic internal name “lightbeam fix”. Thankfully, changelog sheds some more light on that:

  • Toggling car lights on does not make windows invisible when viewed from inside anymore

While windshields (and all car windows in general) always look the same way when observed from the outside, no matter if it’s day:

or night:

However, things don’t look so good when looking at windows from the other side. That’s how they look at day:

while at night the window vanishes:

An observant viewer will know what is going on right away – when the object is visible from one side but not another, backface culling comes into play.

Thus, before we can proceed, a few words on what backface culling is. It’s not a complex concept, so relax!

Backface culling

Everyone can agree that rendering less is better. Backface culling is one of the easiest techniques to reduce the amount of drawn triangles, making use of the fact that each triangle on the model has sides defined. Side is defined by the order of vertices used to build a triangle:

This translates well to real life concepts – even though two sides of a clean piece of paper may look very similar, they are distinct nonetheless and you are free to call them a “front” and “back” side by whatever arbitrary convention.

Now imagine you get six pieces of paper and build a solid cube out of them. As far as you are concerned, their inner sides could be any color and you wouldn’t see that at all. So if they can be any color, why not make them transparent?

This is a gross oversimplification, because back sides of triangles naturally are not becoming transparent – instead, they are not being rendered at all (but to you, a viewer, both should look the same – hence the analogy).

When applied to a full geometric shape, backface culling results in an effect like this.


Left - no culling, right - back faces culled.

Now it’s obvious how culling backfaces helps tremendously – it results in more or less half of the scene not being rendered! Since determining side of the triangle is a very quick operation (a single dot product, so essentially a sum of 3 multiplications), it can save a lot of rendering time.

In-game application

Back to the subject of car windows, we now can see that windows appear to vanish when observed from the inside due to backface culling being enabled at night, but not at day. Why is it this incosistent?

These are to blame: light beams from headlights, which are drawn only at night!


I'll light up the way, and also break render states while I'm at it.

Indeed, in San Andreas vehicles are supposed to be drawn with backface culling disabled (some pieces, like hood or windshield, are “paper thin” and thus the player is supposed to be able to see both sides). However, light beams change multiple render states and attempt to restore them afterwards – but not doing a good job at it. As a result, they “restore” culling state to “cull backfaces”. Therefore, as long as light beams are drawn, the entire vehicle will also be drawn with backface culling enabled, thus culling windows and potentially other components.

SilentPatch corrects this by making light beams restore correct render states, thus avoiding this bug. Fix done, everyone is happy, right?

Not quite. ¯\_(ツ)_/¯

Using bugs to create visual effects? Hold my cookie

A bug has been reported to me via e-mail, stating:

I have Krystofer’s vehicle mods installed. These vehicles have lightbars that don’t work with the patch installed.

I got my hands on these modded vehicles, and sure enough – the lightbar looks a bit different with and without SP:


Left - no SilentPatch, right - SilentPatch installed.

Exercise: Can you, a reader, guess what is the issue here, knowing that it is related to backface culling? Answer provided below.


Some vehicle modders have noticed this bug and decided to make use of it in a clever way. Since at day triangles are drawn no matter what direction they are set, but at night the only vertices to draw are the ones facing the camera, it can be leveraged to obscure some triangles at day and unhide them at night. Indeed, in this case the lightbar consists of two layers:

Front faces are visible at all times, but back faces will only be visible at day – so at night they vanish, revealing another part of the model with a different textures! This creates the illusion of light bar lighting up at night.

While it’s an interesting way to make use of an issue, it’s a bug nonetheless. How do we solve that?

Coming up with a solution to please everyone

Thankfully, SP already has solutions for similar problems. Recall that some stock models, as well as modded ones, can’t handle properly blurred rotors correctly. SilentPatch solves this by exposing a section in its INI file, allowing people to add specific models to an “exception list” – so models listed there will not get blurred rotors.

We can do same with this fix! An upcoming build of SilentPatch (Build 30) will feature a new INI section, allowing people to exclude models which make use of this bug:

[LightbeamFixExceptions]
596
597
598
599

Success! This approach will not affect users not installing modded vehicles, and those who do will get an option to opt-out of this fix if their favourite car mod relies on this behaviour.


Share