Zooming

Rather than having to move a camera to get closer or farther away from an object, you can take advantage of Blitz3D’s camera zooming capabilities. In this exercise, we’ll create a sphere that seems far off in the distance and then give the player the ability to zoom in and out using the CameraZoom command.

Start by creating a program that contains only a simple sphere, as designated by the following code. Alternatively, if you want to save a little time typing, open the program called demo07-06.bb.

;demo07-06.bb - Camera zooming
; ––––––––––––––––
Graphics3D 640,480
SetBuffer BackBuffer()

Const ESC_KEY = 1
Const UP_KEY = 200
Const DOWN_KEY = 208

; Create camera
camera=CreateCamera()
zoom# = 1
; Create a light
light=CreateLight()

; This is the code for creating the sphere
sphere=CreateSphere()
PositionEntity sphere,0,0,9
ScaleEntity sphere, .2, .2, .2
EntityColor sphere, 0, 26, 125

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

    If KeyDown(UP_KEY) Then zoom# = zoom# + 1
    If KeyDown(DOWN_KEY) Then zoom# = zoom# -1

    If zoom# < 1 Then zoom# = 1
    CameraZoom camera, zoom#


    RenderWorld
    Flip
Wend
End

The program should look like Figure 7.13 when it is run.

Figure 7.13. The demo07-06.bb program, initial zoom.


Now that we have a camera and a simple sphere far off in the distance, we can add some parameters that will allow us to zoom. The first thing that we do is set the starting zoom values. The zoom value, or zoom level, is represented in the code by zoom#. You can set the zoom value to any number you want. The number 1 represents no zoom at all, any number greater than 1 will zoom you in, and any number less than 1 will zoom you out. For our program, we’ll set the default zoom level at 30 to test the zoom.

The next step is to apply the CameraZoom command to our camera:

; Applying the zoom
CameraZoom camera,zoom#

Go ahead and run the program now. You’ll see that the sphere that was previously so far away is now in your face (see Figure 7.14) because we zoomed in so strongly.

Figure 7.14. The demo07-06.bb program with the zoom level at 30: the sphere looks much, much closer.


Let’s take a look at the main loop.

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

    If KeyDown(UP_KEY) Then zoom# = zoom# + 1
    If KeyDown(DOWN_KEY) Then zoom# = zoom# - 1
    If zoom# < 1 Then zoom# = 1
    CameraZoom camera, zoom#


    RenderWorld
    Flip
Wend

What this code says is that if the down arrow is pressed, then the zoom level (zoom#) decreases by 1, and the opposite if the up arrow is pressed. We make sure the zoom never goes too low by saying that if the zoom is less than 1, it becomes 1 again. Then, using the CameraZoom function, the new zoom is attached each frame to the camera!

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

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