World
Wide Guide | Knowledge Bank |
Kukushkin's Notebook | Basics
of Simulation
Unlike normal 2-D objects, which are normally drawn
in the same order as drawing commands appear in BGL files, 3-D objects are
drawn in an order that is internally determined by FS5 and is supposed to
solve visibility problems. The code that defines the visual scenery contains
parts (subroutines) that are used to display 3-D objects. These parts are
never executed directly. Instead, FS5 is notified of their presence and later
sorts them according to the distance to the viewer and executes them in the
appropriate order.
The subroutines for displaying 3-D objects are normally contained in the visual scenery. Internally, FS5 uses similar subroutines to display the aircraft from external views, dynamic scenery objects, synth scenery mountains and buildings, etc. This normally ensures that these objects 'fit' well into the visual scenery. The only graphic primitives directly supported by FS5 are dots, lines and polygons. Therefore, most 3-D objects are constructed from multiple graphic primitives. These 3-D primitives are projected into the View window, thus resulting in 2-D primitives that are actually displayed by the graphic driver. All this leads to the problem of hidden surfaces (or, more general, parts) within objects.
For example, the far side of a building should not show through the near side. FS5 only supports a very primitive hidden surface removal within 3D objects, which works only on polygons. It is based on the fact that most objects are, or are built of, convex polyhedra, and that the viewpoint normally cannot be inside these polyhedrons (otherwise there would be a crash condition). Each polygon in FS5 is visible only from one side. Normally, outer sides of the polyhedron are programmed to be visible. Because of the nature of convex polyhedrons, only sides turned to the spectator are visible, and these sides never cover each other. This simple method alone successfully solves the hidden surface problem for many simple objects, like default buildings or synth scenery mountains. It is very well illustrated by the fact that from inside a building, its walls are not visible. Similarly, a mountain is normally not visible from "inside". However one can often see sides of adjacent mountains hanging in the air -- because they are viewed from their visible side. The only way to display more complex objects is to draw their parts in an order that ensures that covered parts of the object get over-painted by covering parts.
Unlike relations between 3-D objects, where this problem is normally resolved by FS5, the problem within objects has to be resolved by the SDL code itself. SDL has instructions that are sufficient for resolving most hidden surface problems. These instructions allow checking whether the viewpoint is located from a specific side of an imaginary plane. The basic idea here is to draw an imaginary plane that cuts the object into 2 simpler pieces that are either simple enough to be drawn without internal visibility problems, or can be divided using the same process into even simpler pieces. Obviously, the piece located from the same side of the plane as the viewpoint can never be covered by the piece located from the opposite side. So the piece from the opposite side is drawn first. Theoretically, every 3D configuration can be successfully rendered using this algorithm. In practice, very big 3-D objects (like mountain landscapes) often cause problems because of different internal limitations of FS5. However, this method is sufficient for displaying small objects, like buildings.
Most 3-D objects have shadows. A shadow is normally generated by executing the SDL subroutine used to display an object in a special mode that causes all primitives to be projected onto the ground. Drawing the shadow requires the same number of primitives to be displayed as drawing the object itself. Thus, having shadows enabled can result in a significant impact on the frame rate.
Also, most 3-D objects support crash detection. 'Support' means that the code that draws them also attempts to determine whether the airplane has actually hit the object, and sets a crash flag if so. A correct crash detection algorithm should normally check aircraft coordinates to be inside the object. However, SDL offers a very poor set of commands for checking aircraft coordinates. Without using complicated and difficult-to-program tricks, it is only possible to see if the aircraft is within a cuboid area. This is not enough for many non-rectangular objects, like mountains. For this reason, the viewpoint coordinates are often used instead of aircraft coordinates.
SDL has a much richer set of instructions for monitoring viewpoint coordinates. The viewpoint-based crash detection is not always reliable. It works correctly only when the viewpoint is inside the aircraft. However, this is not always the case. The viewpoint can also be the tower, the spot plane or a spy satellite (map view). If the spot "plane" hits an object, it could trigger a false crash event. In order to avoid such cases, viewpoint-based crash detection is only activated in windows that display the cockpit view. If no such windows are open, it does not work at all. For example, it is possible to 'fly' inside many otherwise crashable mountains with only a spot view active. As soon as the 'S' key is hit, a crash occurs -- assuming the general crash detection is enabled. After a sudden plane location change, the plane can often crash onto the ground because of a changed aircraft altitude and some delays with the adjustment of the synth floor altitude.
Such situations often happen after loading a situation file, using the World|Airports menu or leaving the slew mode -- the plane "jumps". In order to avoid such crashes, FS5 temporarily disables the crash detection for a few seconds after a sudden location change. After this time period, the correct ground altitude is normally already established and the aircraft has already fallen onto the ground, if it had to be this way. After this time period, the crash detection is re-enabled. Unlike this, the crash detection remains enabled after a new aircraft has been chosen. If it is bigger than the old aircraft, its gear sometimes happens to be below the ground. Then, the aircraft "jumps" and often crashes when it falls back onto the ground.