In this example, we'll be reading a text file and using the contents to create some geometry in our scene.
In order to do any kind of file input/output, the first thing you'll need to do is to understand the file format that you're wanting to read (or create). In both this example and the one involving writing to text files, we'll be using an example file format—the "foo" file. "Foo" files are text-based files, and each line represents a geometric primitive of a given type, at a given location. The type of geometric primitive is represented by a three-letter string, with "spr" meaning a sphere and "cub" meaning a cube. The type string is then followed by three numbers representing the X, Y, and Z position of the item. So, an example .foo
file might look something like the following:
spr 0 0 0 cub -2 0 -2
Although this is certainly not a particularly useful format, it shares similarities with many common text-based formats. The OBJ format, for...