Conditional Branching in BGL

One of the most powerful features in BGL is conditional branching. As BGL programs are executed, they generate points, lines, textures and other graphics elements. At certain points in a BGL program, you'll want to base graphics projection decisions on a condition that you specify. For example, when the user is far away from an object, it's not necessary that they see all the detail. In this instance, you can project a simple design instead to speed up the projection rate. Another good example is nighttime flight, when you want to project runway lights and city lights; in this scenario, you can turn off many daytime elements. The scenarios described are done using conditional branching. The following code sample shows an example IFIN1 conditional branch command.

IFIN1object_end,xv,-261,255
IFIN1object_end,zv,-261,257
setwrdrpfuel,1
object_endlabelword

In the preceding example, the user’s ground position is available to BGL as xv, zv (x=viewer location and z=viewer location in the current scale factor’s design space). In the example, a repair and refueling area is defined and located on the ground between -261 < xv < 255 and -261 < zv < 257. Whenever the user’s location (and, presumably, the aircraft's) is in this area, the repair and fueling variable is reset to 1 to indicate to Flight Simulator's aircraft simulation section to fuel and fix up this plane. The first IFIN1 command branches around the refueling command (setwrd rpfuel,1 or set the rpfuel variable to the value 1). If this condition is met, execution moves to the second IFIN1 command. If the aircraft is within the proper zv range, the setwrd command is executed and signals that the plane is to be refueled.

The example shows another BGL capability—that of system control. Notice that the flight sequence has no graphics impact on the screen, but sets a status word for other Flight Simulator systems to take action on. If a scenery designer wanted to, it would have been easy to include some graphics commands after the setwrd (perhaps a green flag popping up to show the pilot he is in the refueling area).

The IFIN1 command branches if one condition is not met. There are also IFIN2 and IFIN3 commands. The following example shows an IFIN3 command that executes a simple or complex graphics object, depending on whether or not the user (xv,yv, zv again) is within a given 3-D bounding volume.

IFIN3simple,xv,-256,512,yv,0,256,zv,-256,600
JUMPcomplex
simple:

The preceding example is fairly self-explanatory but notice the syntax: the simple object is executed if xv is in the -256–512 range, yv is between 0–256, and zv is between -256–600; otherwise, a branch to simple occurs. The preceding example also shows the use of the unconditional JUMP command.