Entropy Skyplane Tutorial
From LagoonWiki
A skyplane is an alternative to a skybox. This tutorial explains how to put a skyplane into a scene using the Galaga/Entropy/Ogre3D engine.
Contents |
Creating a Skyplane
Temporarily comment out the call that creates your skybox, if you have not already. Add the following code near the place you previously created the skybox:
Plane plane; plane.d = 1000; plane.normal = Vector3::NEGATIVE_UNIT_Y;
Plane is a data type, so the first line creates an object of this type named plane. The second line defines its distance to be 1000. This is relative to the camera's elevation, not an absolute elevation in the scene. The third line defines the plane's normal vector, i.e. which way to turn the side of the plane that will be textured. In this case we want the plane to face downward, so we use a predefined constant that points the vector in the negative y direction.
We need a material to paint the sky with. Another tutorial will cover materials in more detail, but to get the skyplane started just do the following for now:
- Create a directory galaga/data/materials/
- Create directories galaga/data/materials/scripts/ and galaga/data/materials/textures/.
- In galaga/data/materials/textures, create a 1024x1024 image bluesky.png. For now just paint it a sort of sky blue; we will get fancy later. The size is not particularly critical, so long it is a square with dimensions a power of two. However, it will be stretched somewhat when deployed, so smaller images may not look very good when you start painting details on them.
- In galaga/data/materials/scripts/ create an ASCII file named BlueSky.material, containing the following text:
material BlueSky
{
technique
{
pass
{
lighting off
depth_write off
fog_override true
texture_unit
{
texture bluesky.png
}
}
}
}
- This is about as simple as a texture gets. Notice that in addition to identifying your texture, it invokes a fog_override. This will keep the fog from obscuring it; we will modify this later.
- Ensure that the files are read when you start the engine by including galaga/data/materials/scripts and galaga/data/materials/textures in your resources.cfg.
- Now add this to your code right after the code that created the plane:
sceneManager->setSkyPlane(true, plane, "BlueSky", 1500, 1);
The arguments are as follows:
- enables the skyplane (false would turn it off).
- is the name of the variable that holds the plane you described above.
- is the name of the plane's material. (The engine recognizes the name because it read the material script when you started it.)
- is the horizontal dimension of the sky. Use the same as the horizontal dimension of your terrain, or a little larger.
- is how many times to repeat the bluesky.png texture to tile the sky. If you are using a plain blue image, once is sufficient. In other cases you may want to use a larger number.
Recompile and start your engine to observe the effect.
Bending the Skyplane
You probably see an undesirable artifact at the edge of the sky. One way to fix it is to bend the edges of the skyplane down to a point below the horizon. (Another is to obscure the edges with fog; see below.) A skyplane is bent by using a few more arguments to the call that creates it, as follows:
sceneManager->setSkyPlane(true, plane, "CloudySky", 500, 5, true, 1.5f, 50, 50);
The additional arguments are as follows:
- 6. is a "render first" option, that suppresses undesirable artifacts in certain circumstances. It is not strictly part of the bending logic, though bending will sometimes make it necessary.
- 7. tells how strong a bend you want. Higher numbers make it bend more.
- 8 & 9 tell how many segments to use for the bend. The apparent curve is actually composed of a number of flat segments. The more segments you use, the closer it approaches a true curve, though with some additional processing overhead.
Try making your skyplane wide enough and bent down enough to go behind the edges of your terrain. (Comment out your fog, or make it very thin, so you can see the edge of the world.)
Using Fog with a Skyplane
The instructions above had you disable fog so you can see what you are doing. Turn the fog back on in your code, and modify the fog_override definition in the material.
If you just set fog_override to false (the default), the fog will probably completely obscure your skyplane due to its distance. You can use additional parameters in the material definition in order to make the sky less obscured when you look straight up, and more obscured as you look down toward the horizon.
Modify your material definition to look something like this:
fog_override true linear 0.9 0.9 0.9 0.0 100 1500
The new parameters define the behavior of the fog between the skyplane and the camera. It amounts to an independent definition of the fog, and will require different parameters if you want the sky to be slightly fogged up but still visible. The parameters are just as in the call in the code: which density model to use, what color to use, an ignored parameter, and the start/end distances. Notice that the start/end distances are not elevations per se, unles you are looking straight up. If you look upward at an angle the calculated distance to that point in the sky is longer, so you will see more fog than when looking straight up. This has the effect you would expect if you were looking through a layer of fog at an angle.
You can also use exponential fog:
fog_override true exp 0.9 0.0 0.9 0.005 0 0
The parameters are as before, except that you cannot leave the two ignored parameters off the end like you can in the compiled code.
It takes some work to get your sky fog to come together with your ground fog smoothly. See if you can find the parameters that will make it work. (Notice that you can use fog and bending together; give it a try too.)
Texturizing the Skyplane
You can of course paint onto bluesky.png to keep the sky from being perfectly clear. However, whatever you paint will be distorted in a very unintuitive way. For example, paint a yellow spot to represent the sun, and then see how it looks in your scene.
The material tutorial includes tricks to make clouds move across the sky and change shape as they go.
