Using Shapes with Terrains

What’s better than chocolate mixing with peanut butter? How about terrains mixing with shapes? Maybe that’s not quite as good, but combining a terrain with a shape is how many 3D games create their environments. Typically, the terrain will make up the ground, and the shapes will make up the sky and other objects within the game. In the following example, we’ll create a sky using a shape and apply it to a standard. Start by opening the file called demo09-06.bb or entering the following code. Save the file with a new name into a new folder and make sure that the files called greenery.jpg and sky2.jpg are copied to that folder.

;demo09-06.bb – Shapes and Terrain
; ––––––––––––––––,
Graphics3D 640,480
SetBuffer BackBuffer()

Const ESC_KEY = 1
Const LEFT_KEY = 203
Const RIGHT_KEY = 205
Const UP_KEY = 200
Const DOWN_KEY = 208

; Create camera
camera=CreateCamera()
PositionEntity camera,0,1,0
; Create a light
light=CreateLight()

;Create our terrain
ground=CreateTerrain(512)
PositionEntity ground, -500,0,-500
ScaleEntity ground, 10,15,10
tex=LoadTexture( "greenery.jpg" )
EntityTexture ground, tex




; Create the terrain
ground=CreateTerrain(512)


; This following code deals with cameras and terrain
While Not KeyDown(ESC_KEY)

    If KeyDown(RIGHT_KEY)=True Then TurnEntity camera,0,-1,0
    If KeyDown(LEFT_KEY)=True Then TurnEntity camera,0,1,0
    If KeyDown(DOWN_KEY)=True Then MoveEntity camera,0,0,-0.5
    If KeyDown(UP_KEY)=True Then MoveEntity camera,0,0,0.5

    RenderWorld
    Flip
Wend
End

Run the program now and you should be able to see a green terrain and nothing (black) as the sky (see Figure 9.12). We are going to change all that by creating a sphere to act as the sky. We will make the sphere extremely large, apply a texture to it, and then use the FlipMesh function we learned earlier to put the texture on the inside of the sphere. Enter this code after the “Creating the terrain” section. (The file on the CD already has this code inserted. If you want to see the terrain without the blue sky, delete the following code from the file on the CD.)

Figure 9.12. The program at this point has terrain and a black sky.


;Creating the sky
sky = CreateSphere (40)
FlipMesh sky
ScaleEntity sky, 100,100,100
PositionEntity sky, 0,50,0
sky_tex = LoadTexture ( "sky2.jpg" )
EntityTexture sky,sky_tex
EntityFX sky,1

Run the program now to see the sky as in Figure 9.13.

Figure 9.13. We’ve now added a bright, beautiful sky to our terrain. This is the final demo09-06.bb program.


I have described in detail all of the previous code in earlier sections of this chapter. Here we are just adding it to the code of an existing terrain. If you run the program now, you’ll see both the terrain and the sky. We’ve actually done something pretty cool here, and I want you to see how it works. We haven’t really created a “sky”—we’ve just created a sphere that takes up most of the screen so that it looks like a sky. Want to see what I mean? Run the program and hold down the down arrow. You’ll move backwards until you can see that your sky was simply a sphere. You wouldn’t actually want this to happen in a real game, so there are measures that we can take to avoid having our “fake” sky from being discovered. First of all, in a real game we would make the sphere much bigger. Rather than having it scaled at 100 times (as we did with this line of code: ScaleEntity sky, 100,100,100), we would scale it much larger and we would create a “parent” so that as our player moved, the sphere that made up the sky would also move. You’ll learn more about parents later in the book. You’ll also notice that if you hold down the up arrow, you’ll move toward the sky and eventually be able to go through it. In an actual game, setting up a “collision” would prevent this. You’ll learn more about collisions in Chapter 11.

..................Content has been hidden....................

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