Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

3D Game Programming All In One (2004)

.pdf
Скачиваний:
140
Добавлен:
17.08.2013
Размер:
17.91 Mб
Скачать

518 Chapter 18 Making the Game World Environment

20. Open the sky_book-.dml file with UltraEdit. Change the first six lines to read as follows:

skyfront skyright skyback skyleft skytop skybottom

21.Save the file.

22.Open C:\aEmaga6\control\data\maps\book_ch6.mis and locate the line that looks like this:

materialList = "./sky_day.dml";

and replace it with this:

materialList = "./sky_book.dml";

23.Save the file.

24.Launch the Emaga6 sample program and enter into the game. Take a look around. Notice the corners? See how your clouds become distorted?

You already know how to fix up the textures so that they join seamlessly, so I'll leave you to do that. Note that you probably don't have to worry about the top edges, because the top image and the top edges of the side images all have the same RGB value—0,0,192.

Also, the bottom doesn't need to be blended either, because it's not going to be visible beneath our terrain. So that just leaves the perspective distortion to fix.

Adjusting for Perspective

Although we are going to be adjusting for perspective distortion, we aren't going to use the built-in perspective tools in Paint Shop Pro. Instead, we will use the Warp tool.

1.Open up one of your side images, like the front one, for example.

2.Choose Effects, Distortion Effects, Warp. Your image will be distorted as shown in Figure 18.11.

3.Repeat the warping for all three of the other image files so that you've corrected all of the lateral view images, left, right, front, and back.

4.Run Emaga6 and check your work.

Now you might find that after you've done the distortion you now have seams again in your skybox. If so, go back and fix the edges.

There you have it! Your very own do-it-yourself skybox!

Team LRN

The Sky Mission Object

519

The Sky Mission Object

 

You probably noticed when you were editing the

 

Emaga6 MIS file that there was an object defined

 

in there called "Sky". There are lots of goodies in

 

that object for us sky worshipers.

 

Here it is:

 

new Sky(Sky) {

 

position = "-1088 -928 0";

 

rotation = "1 0 0 0";

 

scale = "1 1 1";

 

materialList = "./sky_book.dml";

 

cloudHeightPer[0] = "0.349971";

 

cloudHeightPer[1] = "0.25";

Figure 18.11 Applying perspective-

 

cloudHeightPer[2] = "0.199973";

correcting warp.

cloudSpeed1 = "0.0002";

 

cloudSpeed2

=

"0.0004";

 

cloudSpeed3

=

"0.0006";

 

visibleDistance = "1100";

fogDistance = "1000";

fogColor = "0.820000 0.828000 0.844000 1.000000"; fogStorm1 = "0";

fogStorm2 = "0"; fogStorm3 = "0"; fogVolume1 = "500 0 100"; fogVolume2 = "0 0 0"; fogVolume3 = "0 0 0";

windVelocity = "0.1 0.1 0"; windEffectPrecipitation = "1";

SkySolidColor = "0.547000 0.641000 0.789000 0.000000"; useSkyTextures = "1";

renderBottomTexture = "0"; noRenderBans = "0";

locked = "true";

};

We have already encountered the MaterialList property and have seen that it is used to point to a file that contains the names of the images that will be displayed on the interior faces of our skybox.

Not all of the properties in the skybox are particularly interesting; they owe their presence to Torque's beginnings as the code that drives the Tribes 2 game. The position, scale, and

Team LRN

520Chapter 18 Making the Game World Environment

rotation properties don't accomplish much when you use them—they are there because all objects have those properties whether or not they are meaningful.

The cloudHeight properties are useful, and we will cover them in the next section. The same applies to the properties for fog.

One of the most useful properties is visibleDistance. This property specifies the distance, in world units, beyond which the terrain and all game objects will not be rendered. This is a useful, though rather ham-handed, method for increasing frame rates in game worlds that have many objects present. In conjunction with the fogDistance property, this sort of simulates the concept that all landscape artists are familiar with that objects become hazier and harder to see at a distance. This is because there is simply more atmosphere between you and the objects you are viewing in the distance, and the greater the distance, the more the air obscures your view. This effect is a well-known one called atmospheric perspective. The great Leonardo Da Vinci studied this effect quite a bit back in the 15th and 16th centuries—he called it aerial perspective.

By exaggerating this effect we have a useful mechanism to reduce the number of objects that the video card needs to render, and this improves your frame rate.

The fogDistance property specifies the distance from you that the haziness we just talked about begins. The distant fogginess starts at this point and gets thicker as the distance increases, until the visibleDistance is reached, after which nothing is rendered. By using these two properties, you can prepare a game world where there is a natural-appearing haziness that slowly obscures distant objects until they disappear completely.

n o t e

You should always make sure that visibleDistance is a bigger number than fogDistance, otherwise you risk crashing the game engine in clients in certain situations. In fact, for the sake of safety, always make sure that visibleDistance is at least 50 units larger than fogDistance. Less than that is not really useful anyway.

If you don't want to use a skybox, there is the SkySolidColor property, which you can set. Then you will get a uniformly colored sky all around with a band of changing color near the horizon to simulate the lightening effect we see—something like the gradient we made for our skybox. In this case, to disable the skybox, set useSkyTextures to 0 or False. Set noRenderBans to 0 or False to enable the simulated horizon coloring, and set it to True to disable the coloring.

You can also just prevent the bottom image in the skybox from being rendered, or considered for rendering, by setting renderBottomTexture to 0 or False. This might eke out a frame or two of frame rate for you.

Team LRN

Cloud Layers

521

The windVelocity and windEffectPrecipitation precipitation properties have no effect on their own. They are used in conjunction with the storm effects we will cover later.

Cloud Layers

Your game's sky doesn't start and end with the skybox. A beautiful background sky is nice, and important in some settings, but it's static. If you go outside on a nice day and look around, you will often see a sky with clouds that presents itself much like the one you can make with the skybox.

But more often, you will see that and you will have clouds moving across the sky above you, blowing in the wind. In fact, you will probably notice layers of clouds—often two layers and sometimes even three layers. The lower layers whip across the view above you, while the upper layers move at a more sedate pace, sometimes even in a different direction.

In Torque we can define up to three layers of moving clouds with the Sky mission object in the MIS file that the server uses to define the game world.

Cloud Specifications

For each layer, we define its altitude as a percentage of a pseudo-altitude. Now this is tricky and might be a bit difficult to understand. The first thing to get is that your player can never go up—either in camera fly mode or in a flying vehicle—high enough to reach the lowest cloud layer. In this sense, cloud layers operate somewhat like a skybox. However, you can position the three layers relative to each other. The reason for this is so that the motions of each cloud layer can be calculated in correct proportion to each other. If you have a steady wind that is the same at all altitudes, then the lowest cloud layer will seem to move faster than the others, and the highest will seem to move slower than the others. How much faster or slower depends on the distance between the cloud layers and their distance from your player, as the observer.

And that's what the cloudHeightPer properties do—they inform the visual appearance of the clouds but not their physical location in the game.

Now another consideration is that wind speeds are not the same at all altitudes in real life. Usually, the winds aloft (winds at altitudes of 1,000 feet or greater above the ground) are higher the higher you go, up to about 30,000 or 40,000 feet or so. Then it starts to get really weird.

You can plug in the movement speeds for the clouds at different altitudes using the cloudSpeedn for each specified cloudHeightPer[n] and have the game engine figure out the relative motion based on pseudo-altitude and the speed at that altitude. Unfortunately, Torque doesn't handle wind direction for clouds as well—that would be the final link needed to provide really neat cloud motion. Wind direction is specified by a single windVelocity property that applies to all layers. In real life, wind directions back and veer according to altitude, but we can't do that here.

Team LRN

522Chapter 18 Making the Game World Environment

Using the windVelocity property requires a little thought. The value is expressed as an XYZ coordinate. The third value, the Z, is irrelevant, but the X and Y values are used to calculate the vector on the horizontal plane in two dimensions. The vector then points to the world origin (or center). If we look up at the sky and imagine the X- and Y-axes pasted up there, somewhat like in Figure 18.12, we can figure out the direction.

The value "1 1 0" would describe a wind from the southeast, as illustrated in Figure 18.12, and "1 0 0" would be a wind from the east.

Cloud Textures

Now, you need to tell the engine what all those clouds you have zipping around up there actually look like. You do this by specifying an image file in the same material file that you used to specify the skybox image files. After the first six lines in that file that indicate the skybox images, the next lines indicate the cloud image files. One line equals one cloud layer, with the first line after the skybox image lines indicating layer one, the next being layer two, and the last being layer three, like this:

skyfront

skyright

skyback

skyleft

skytop

skybottom

no_cloud

cloud2

cloud3

That is the contents of sky_book.dml. Notice the use of the name "no_cloud" for the first cloud layer. In this example I didn't want to have any clouds at that first layer, so for this layer I created an image file that has no clouds in it.

So, you are asking, how do we make a cloud texture that does have clouds? Glad you asked! Let's make one.

Figure 18.12 Wind velocity conversion correcting warp.

1.Launch Paint Shop Pro and create a new file that is 256 pixels by 256 pixels in size, has a transparent background, and includes 16 million (24-bit) colors.

2.Save this empty image file as C:\aEmaga6\control\data\maps\no_cloud.png.

3.Select the Air Brush tool, set it to spray white, and then spray a little bit around your new image, avoiding the edges, like in Figure 18.13. Or spray the edges, but make sure you adjust the edges so that the image is suitable for tiling.

Team LRN

4.Save your image as C:\aEmaga6\control\data\maps\mycloud.png. (Or you can save it as a JPG, if you like.)

5.Edit C:\aEmaga6\control\data\maps\sky_book.dml so that the last three lines look like this:

mycloud

no_cloud

no_cloud

Now run your game, and check out the clouds! Of course, you can add more cloud images for the other layers, or you can use the same image for all three.

Fog 523

Figure 18.13 A simple cloud texture.

Fog

Fog is, of course, another nifty weather feature. We've already encountered one kind of fog that is used to obscure distant objects and terrains. There is another kind of fog that operates in layers, just like the clouds—except that with fog, these are real layers in the game world that you can actually get into with your player, depending on where the layers are placed.

This layered fog is a limited form of volumetric fog. It is limited in the sense that although you can specify the upper and lower bounds of the fog, it will appear at those levels throughout the entire map.

You can use this layered fog to complement the moving cloud textures to create clouds (except that the fog will never be at the same altitude as the cloud textures). You can also deposit fog in low-lying river valleys.

A good use for this fog is underwater, helping to reduce visibility there. This reduced visibility results because of silt and other materials that often exist underwater, cutting down your ability to see far.

This layered volume fog is specified in the Sky mission object that we looked at earlier. An entry looks like this:

fogVolume1 = "500 0 100";

The three parameter numbers are, in order, distance, bottom, and top. Their meanings are shown in Table 18.1.

You already know how to edit the mission file and change the properties of the various mission objects, so go ahead and putz around with the fog values and see how they work.

Team LRN

524 Chapter 18 Making the Game World Environment

 

Table 18.1

Volume Fog Settings

 

Parameter

Description

 

 

distance

View distance when in the layer. This works like the distance fog, except

 

 

that a value of 0 here means there is no fog at all. If you want a really,

 

 

really close view distance, use 1, not 0.

 

bottom

Bottom of fog layer.

 

top

Top of fog layer.

 

 

 

 

Storms

Torque has built-in capabilities to generate storms, using lightning, rain, and thunder. It's pretty cool how this is done. You can manually instigate a storm using script code, and there are some functions provided that will automate portions for you.

n o t e

The lightning storm features require the use of sound effects files, but we don't cover those until the next chapter. So you will have to add the appropriate code to make the sound effects work. This will be done with minimal commentary here—just enough to get the thunder sounds working. See Chapters 19 and 20 for a detailed look at sounds.

Setting Up Sound

There's some preparation we need to do at this point before proceeding with the rest of the weather features. We need to get some sound files, images, and supporting code files and put them in the right places for our game, as follows:

1.In the directory C:\3DGPAi1\RESOURCES\CH18 locate the file SettingsScreen.cs and copy it to the directory C:\aEmaga6\control\client\misc\. Then copy SettingsScreen.gui from the same place to C:\aEmaga6\control\client\interfaces\.

2.Copy the following files to the directory C:\aEmaga6\control\data\sound\:

C:\aEmaga6\control\data\sound\thunder1.wav

C:\aEmaga6\control\data\sound\thunder2.wav

C:\aEmaga6\control\data\sound\thunder3.wav

C:\aEmaga6\control\data\sound\thunder4.wav

C:\aEmaga6\control\data\sound\buttonOver.wav

C:\aEmaga6\control\data\sound\rain.wav

3. Copy the following files from the same place:

C:\aEmaga6\control\data\sound\lightning.dml

C:\aEmaga6\control\data\sound\lightning1frame1.png

Team LRN

Storms 525

C:\aEmaga6\control\data\sound\lightning1frame2.png

C:\aEmaga6\control\data\sound\lightning1frame3.png

C:\aEmaga6\control\data\sound\rain.dml

C:\aEmaga6\control\data\sound\rain.png

However, this time they go to the directory C:\aEmaga6\control\data\maps\.

4. Edit the file C:\aEmaga6\control\client\initialize.cs and locate the following line:

Exec("./interfaces/MasterScreen.gui");

and after it, add this line:

Exec("./interfaces/SetupScreen.gui");

5. Next, locate the line:

Exec("./misc/MasterScreen.cs");

and after it, add this line:

Exec("./misc/SetupScreen.cs");

6.Edit the file C:\aEmaga6\control\client\default_profile.cs and add the following line near the top:

GuiButtonProfile.soundButtonOver = "AudioButtonOver";

7.Copy the file C:\3DGPAi1\RESOURCES\CH18\OpenAL32.dll to the directory C:\aEmaga6\.

8.Locate the file C:\aEmaga6\control\client\initialize.cs and add these lines to the top:

$pref::Audio::driver = "OpenAL"; $pref::Audio::forceMaxDistanceUpdate = 0; $pref::Audio::environmentEnabled = 0; $pref::Audio::masterVolume = 1.0; $pref::Audio::channelVolume1 = 1.0; $pref::Audio::channelVolume2 = 1.0; $pref::Audio::channelVolume3 = 1.0; $pref::Audio::channelVolume4 = 1.0; $pref::Audio::channelVolume5 = 1.0; $pref::Audio::channelVolume6 = 1.0; $pref::Audio::channelVolume7 = 1.0; $pref::Audio::channelVolume8 = 1.0;

$GuiAudioType

=

1;

$SimAudioType

=

2;

$MessageAudioType = 3;

Team LRN

526Chapter 18 Making the Game World Environment

new AudioDescription(AudioGui)

{

volume

= 1.0;

isLooping= false;

is3D

=

false;

type

=

$GuiAudioType;

};

new AudioDescription(AudioMessage)

{

volume

= 1.0;

isLooping= false;

is3D

=

false;

type

=

$MessageAudioType;

};

new AudioProfile(AudioButtonOver)

{

filename = "~/data/sound/buttonOver.wav"; description = "AudioGui";

preload = true;

};

Now that we've done that, we can move on to the storm-specific stuff.

9.Type the following into a new file and save it as C:\aEmaga6\control\server\misc\ weather.cs.

datablock AudioProfile(HeavyRainSound)

{

filename = "~/data/sound/rain.wav";

description = AudioLooping2d;

};

datablock AudioProfile(ThunderCrash1Sound)

{

filename = "~/data/sound/thunder1.wav"; description = Audio2d;

};

datablock AudioProfile(ThunderCrash2Sound)

{

filename = "~/data/sound/thunder2.wav"; description = Audio2d;

};

datablock AudioProfile(ThunderCrash3Sound)

Team LRN

Storms 527

{

filename = "~/data/sound/thunder3.wav"; description = Audio2d;

};

datablock AudioProfile(ThunderCrash4Sound)

{

filename = "~/data/sound/thunder4.wav"; description = Audio2d;

};

datablock LightningData(LightningStorm)

{

strikeTextures[0] = "~/data/maps/lightning.dml"; thunderSounds[0] = ThunderCrash1Sound; thunderSounds[1] = ThunderCrash2Sound; thunderSounds[2] = ThunderCrash3Sound; thunderSounds[3] = ThunderCrash4Sound;

};

datablock PrecipitationData(HeavyRain)

{

type = 1;

materialList = "~/data/maps/rain.dml"; soundProfile = "HeavyRainSound"; sizeX = 0.1;

sizeY = 0.1; movingBoxPer = 0.35; divHeightVal = 1.5; sizeBigBox = 1; topBoxSpeed = 20; frontBoxSpeed = 30; topBoxDrawPer = 0.5; bottomDrawHeight = 40; skipIfPer = -0.3; bottomSpeedPer = 1.0; frontSpeedPer = 1.5; frontRadiusPer = 0.5;

};

10.Finally, add some datablocks to the mission file to cause our new storm features to load when the game launches. Locate the mission file again, C:\aEmaga6\control\data\maps\book_ch6.mis, and find the last two lines of code, which should look like this:

};

//--- OBJECT WRITE END ---

Team LRN