The Building module

The pi3d.Building module allows you to define a whole level or floor of a building using map files. Like the terrain maps used in the preceding example, the color of the pixels will be converted into different parts of the level. In our case, black is for the walls and white is used for the passages and halls, complete with ceilings:

The building layout is defined by the pixels in the image

The sections built by the Building object are defined by the Scheme used. The Scheme is defined by two sections, by the number of models, and then by the definitions for various aspects of the model, as seen in the following code:

mazeScheme = {"#models": 3, 
  (1,None): [["C",2]],     #white cell : Ceiling 
  (0,1,"edge"): [["W",1]], #white cell on edge by black cell : Wall 
  (1,0,"edge"): [["W",1]], #black cell on edge by white cell : Wall 
  (0,1):[["W",0]]}         #white cell next to black cell : Wall  

The first tuple defines the type of cell/square that the selected model should be applied to. Since there are two pixel colors in the map, the squares will either be black (0) or white (1). By determining the position and type of a particular cell/square, we can define which models (wall, ceiling, or roof) we want to apply.

We can define three main types of cell/square location:

  • A whole square (1,None): This is a white cell representing open space in
    the building.
  • One cell bordering another, on the edge (0,1,"edge"): This is a black cell next to a white one on the map edge. This also includes (1,0,"edge"). This will represent the outer wall of the building.
  • Any black cell that is next to a white cell (0,1): This will represent all of the internal walls of the building.

Next, we allocate a type of object(s) to be applied for that type (W or C):

  • Wall (W): This is a vertical wall that is placed between the specified cells (such as between black and white cells).
  • Ceiling (C): This is a horizontal section of the ceiling to cover the current cell.
  • Roof (R): This is an additional horizontal section that is placed slightly above the ceiling to provide a roofing effect. It is typically used for buildings that may need to be viewed from the outside (this is not used in our example).
  • Ceiling Edge (CE): This is used to join the ceiling sections to the roof around the edges of the building (it is not used in our example since ours is an indoor model).

Finally, we specify the model that will be used for each object. We are using three models in this example (normal walls, walls on an edge, and the ceiling), so we can define the model used by specifying 0, 1, or 2.

Each of the models are defined in the details array, which allows us to set the required textures and shaders for each one (this contains the same information that would normally be set by the .set_draw_details() function), as shown in the following code:

details = [[shader, [wallimg], 1.0, 0.0, 4.0, 16.0], 
           [shader, [wallimg], 1.0, 0.0, 4.0, 8.0], 
           [shader, [ceilingimg], 1.0, 0.0, 4.0, 4.0]] 

In our example, the inside walls are allocated to the wallimg texture (textures/squareblocksred.png) and the ceilings are allocated to the ceilingimg texture (textures/squareblocks4.png). You may be able to note from the following screenshot that we can apply different texture models (in our case, a slightly different scaling) to the different types of blocks. The walls that border the outside of the maze (with the edge identifier) will use the wallimg model texture scaled by 4 x 8 (details[1]) while the same model texture will be scaled 4 x 16 for the internal walls (details[0]):

Walls with different scaling

Both scheme and draw_details are set when the pi3d.Building object is created, as shown in the following code:

building = pi3d.Building(levelList[next_level], 0, 0, mymap,
width=MAP_BLOCK, depth=MAP_BLOCK, height=30.0, name="",
draw_details=details, yoff=-15, scheme=mazeScheme)

Using the map file (levelList[next_level]), the scheme (mazeScheme), and draw details (details), the entire building is created within the environment:

An overhead view of the 3D maze we created
Although we use just black and white in this example, other colored pixels can also be used to define additional block types (and therefore different textures, if required). If another color (such as gray) is added, the indexing of the color mapping is shifted so that black blocks are referenced as 0, the new colored blocks as 1, and the white blocks as 2. See the Silo example in the pi3d demos for details.

We also need to define an ElevationMap object: mymap. The pi3d.Building module makes use of the ElevationMap object's calcHeight() function to correctly place the walls on top of the ElevationMap object's surface. In this example, we will apply a basic ElevationMap object using textures/floor.png, which will generate a flat surface that the Building object will be placed on.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.173.242