Ogre1.7 terrain, sky and fog

Source: Internet
Author: User

Source: http://blog.csdn.net/leonwei/archive/2010/07/26/5767350.aspx

Terrain

In the past, ogre was rarely used to support terrain. In the past, it only allowed you to import a height chart, and then put a basic texture and a detailed texture on it. After ogre 1.7, the terrain module is added separately, which is very powerful in terms of terrain support. There is little information about the terrain of the new version of ogre, I spent several days experimenting with the root system, and summarized the terrain usage of the new version of ogre.

1. Set the camera's remote clipping surface to infinitely far

If (root-> getrendersystem ()-> getcapabilities ()-> hascapability (Ogre: rsc_infinite_far_plane ))
{
Cam-> setfarclipdistance (0); // enable infinite far clip distance if we can
}

2. Set terrainglobaloptions. This is a class that represents the overall characteristics of the terrain. It can set some features of the terrain. First, you need to create a new one.

Mterrainglobals = new terrainglobaloptions ();

Then set some necessary features

Mterrainglobals-> setmaxpixelerror (8 );

The smaller the value, the more accurate the terrain, the lower the efficiency.

In the rendering of the terrain. Ogre provides a combined Paster rendering, that is, to replace the actual texture with a simple Paster in a distant place, which has many applications:

1. In this way, fine texture generation is not used in distant areas to improve efficiency.

2. You can use this feature to make the game light and dark.

The following describes how to set this parameter. First, you can use composite textures in multiple distances.

Mterrainglobals-> setcompositemapdistance (100 );

Then set the light direction for generating the composite texture.
Mterrainglobals-> setlightmapdirection (Ogre: vector3 (0,-1, 0 ));

Set the light and shade color of the combined Paster
Mterrainglobals-> setcompositemapambient (smgr-> getambientlight ());
Mterrainglobals-> setcompositemapdiffuse (colourvalue: Black );

For example, if you set the parameters above, you will see the terrain within 100 of your camera's surroundings, and the gradient is black. In this way, only the effect around you is illuminated in the game.

3. Set the terrain Group

In ogre, the terrain appears in groups. A Terrain group has multiple terrain instances, and the terrain in A group has the same texture and height, it is convenient to smoothly connect between them. In ogre, the class representing the terrain group is terraingroup, and the class representing a terrain instance is terrain.

First, we need to generate a new terrain group.

Mterraingroup = new terraingroup (Scene Manager till, terrain orientation, the size of the number of terrain vertices, and the size of the terrain area );

The last two parameters in the Ogre manual are not very popular. After testing, the first one is the number of vertices (2 ^ n + 1 ), the last is the space size (side length) of the terrain)

Set the origin location of the terrain

Mterraingroup-> setorigin (Ogre: vector3: zero );

Set some specific attributes of the terrain, get a terrain: importultimportsettings () through ogre: terrain: importdata & defaulibd = mterraingroup-> getdefaultimportsettings (), and modify it.
Defaulibd. inputscale = 600; // This is the scaling of the height value of the imported data (height chart). This attribute is very important, we know that the height on the height graph ranges from 0 to 1 on the image Scale (0 is black), so by default, the height of the terrain will be within the range of 0-1 (this is basically more plain than plain), so we need to use this value to multiply the pixel value on the original image, that is, the larger the value, steep terrain

The texture attachment of the new orge terrain is like this. It allows the terrain to have several texture layers (the number depends on your hardware), marking bit 0 layer, 1 layer .. N layers. By default, you can only see layer 0th. We can set the mixing method of N + 1 layers for the preceding N layers texture mixing results. If n + 1 layers are not mixed, only 0 layers can be seen, first, you need to define the texture and size of these layers.
Defaulibd. layerlist. Resize (3); // you can specify the number of layers of texture.

For example, set Layer 3
Defaulibd. layerlist [0]. worldsize = 2500; // you can specify the texture size for the layer.
Defaulibd. layerlist [0]. texturenames. push_back ("2.jpg"); // you can specify the texture of the layer.
Defaulibd. layerlist [0]. texturenames. push_back ("dirt_grayrocky_normalheight.dds"); // you can specify the texture orientation for the layer.

4. After the texture of each layer is defined, we will use the terrain group to generate the terrain instance.

First import the height chart

Ogre: Image IMG;
IMG. Load ("terrain.png", ogre: resourcegroupmanager: default_resource_group_name );

Use the following function to create a terrain instance.

Mterraingroup-> defineterrain (X, Y, & IMG );

A Terrain group refers to a two-dimensional grid terrain group. The above function refers to generating a terrain instance using the height map at the (x y) of the grid in the terrain group, in ogre, each terrain instance is square. Therefore, if you want to generate a rectangular terrain, multiple terrain instances are required. For example, two terrain instances are generated at (0, 0) and (0, 1 ).

5. After a terrain instance is generated, We need to mix the texture layer of the terrain. This is not equal to the way you lay things on the terrain.

Access each terrain instance with an iterator of the terrain Group

The principle of blending is as follows. For each texture layer, there is a value that stores the Alpha mixed value of each pixel. You can use the Ogre :: terrainlayerblendmap * blendmap1 = terrain-> getlayerblendmap, in this way, you can only see layer 0. You can modify the data to directly modify the hybrid mode. This is like mixing two images, each mixed image represents the surface image of the entire terrain instance. To get the edge length of the image, you can use terrain-> getlayerblendmapsize (); you can also use blendmap1-> convertimagetoterrainspace (X, Y, & Tx, & ty) to obtain the hybrid image space (x, y) the coordinates of the terrain instance space corresponding to that pixel.

You need to use

Blendmap1-> dirty ();
Blendmap1-> Update ();

To update

6. Finally, we call mterraingroup-> freetemporaryresources ();

Of course, after the program is executed, we need to clear the mterraingroup and mterrainglobals;

This is the terrain of the new version of ogre. Although it is much more complicated than the previous operations, I have been pondering for several days, but it can achieve very good terrain effects, the texture layer can be mixed to create terrain effects attached to various textures. Let's try it.

 

  1. Sky

Speaking of the ground, speaking of the day, ogre has three ways to express the sky: skybox skydome skyplane

Skybox

Is a type of cube mapping.

Usage:

Mscenemgr-> setskybox (true, "examples/spaceskybox", 5000, false );

Parameter 1 indicates whether to plot

Parameter 2 indicates the material of the cube mapping.

Parameter 3 represents how far the day is from you

Parameter 4 indicates whether to draw the sky box first. If it is drawn later, it will be on the top of the terrain.

Skydome

This is to bend a texture image from several directions into a day, and the top center may have a spell mark.

Use

Mscenemgr-> setskydome (true, "examples/cloudysky", 5, 8 );

The material is a normal image.

Skyplane

Simply put a picture on the top of the head to represent the day, applicable to the surrounding mountains and so on to block the horizon

These three methods are getting simpler and more efficient.

Fog

The usage of fog in ogre is very simple. You need to call the painting of fog before drawing the objects affected by fog:

Mscenemgr-> setfog (Ogre: fog_exp, fadecolour, 0.005 );

The first parameter here is the mathematical model of fog computing, with linear and exponential

The second is the fog color. Note that you need to set the background color of the view to this color.

The following are parameters in a specific mathematical model.

Note that if you need to fully see the sky, you need to set the closest distance to the distance of the sky to the nearest distance, otherwise the fog will block the sky, although the truth is, but sometimes it is not.

In addition, fog can be not only fog, but also smoke, dark, or scary ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.