Object Order

When you have more than one object on the screen, there is a chance that they can overlap each other and you won’t be able to see one or the other. Sometimes you’ll actually want an object to be hidden by others, and you can create this effect by controlling the order of an object. Let’s create a program that contains two spheres that overlap and then change their order using the EntityOrder command. This will be demo05-06.bb on the CD.

First, try on your own to create two overlapping spheres, each with different colors (see Figure 5.21), and then compare it to the following code:

Figure 5.21. When two objects overlap, they’ll blend into one another unless you adjust their order.


; demo05-06.bb - Creating two spheres without setting order
; ------------------
Graphics3D 640,480
SetBuffer BackBuffer()

Const ESC_KEY = 1
; Create camera
camera = CreateCamera()
; Create a light
light = CreateLight()
; Create our spheres
sphere1 = CreateSphere(32)
sphere2 = CreateSphere(32)
PositionEntity sphere1,-1,0,5
EntityColor sphere1, 102,23,231

sphere2=CreateSphere()
PositionEntity sphere2,0,0,5
EntityColor sphere2, 21,78,199


; This following code makes our program run
While Not KeyDown(ESC_KEY)
      RenderWorld
      Flip
Wend
End

Now when you run the program, you’ll see that the two shapes blend together at the point where they overlap. Rather than having the objects blend together, you can have one overlap the other by changing the order in which they are drawn using the EntityOrder command. With the EntityOrder command you can enter a number either below or above 0 to change the order of an object. For example, let’s say we had an object called sphere. If we entered the code EntityOrder sphere, -1, the sphere will be drawn behind other objects; if the code was EntityOrder sphere, 1, the sphere would be drawn in front of the other objects. Let’s try this in our code. Change this code (just add the last line):

sphere1 = CreateSphere()
PositionEntity sphere1,-1,0,5
EntityColor sphere1, 102,23,231

to this:

sphere1 = CreateSphere()
PositionEntity sphere1,-1,0,5
EntityColor sphere1, 102,23,231
EntityOrder sphere1, -1

This is on the CD, at demo05-07.bb. Now run the program and see how it looks. Then go back and change the –1 to a 1 and run the program again to see how it affects how the spheres seem to overlap. Figure 5.22 shows how, now, the images overlap correctly!

Figure 5.22. The demo05-07.bb program.


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

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