Basic tutorial 3 (sky, ground, and atomization)

Source: Internet
Author: User
Introduction

In this tutorial, we will explore the sky, ground, and atomization processes in ogre. Through this tutorial, you should understand the usage and differences between skybox, skydome, and skyplane. You will also learn about different types of atomization effects and how to use them.

Start from here

As before, we will use the code we have written as a template. Create a new project in your compiler and add the following code:

#include "ExampleApplication.h"class TutorialApplication : public ExampleApplication{protected:public:    TutorialApplication()    {    }    ~TutorialApplication()     {    }protected:    void chooseSceneManager(void)    {    }    void createScene(void)    {    }};#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32#define WIN32_LEAN_AND_MEAN#include "windows.h"INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )#elseint main(int argc, char **argv)#endif{    // Create application object    TutorialApplication app;    try {        app.go();    } catch( Exception& e ) {#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);#else        fprintf(stderr, "An exception has occured: %s/n",                e.getFullDescription().c_str());#endif    }    return 0;}

If you can successfully compile this code, you can use the WASD key to move it, move the mouse to the camera, and run the ESC key to exit the program.

Create root object and scenario Manager

Root object

First, we need ogre to create a ground. We must set the scenario manager as the scenario manager on the ground, instead of the default one in exampleapplication. Add the following code to the choosescenemanager function:

        mSceneMgr = mRoot->createSceneManager(ST_EXTERIOR_CLOSE);

The root object is the core object of ogre. You can see the UML diagram of the Ogre object here. So far, you have seen most objects except rendersystem. In the above Code, we tell the root node that we need a scenario manager of the st_exterior_close type. Then, the root object requires the scenario enumerator to search for the scenario manager and then return it.

After your program is set up, you will almost no longer be exposed to the root object, and you will not directly call the scenario enumerator.

Scenario manager Creation

I 'd like to explain how to create and manage the scenario manager first to avoid misunderstanding in the future. The scenario manager is not a singleton. Unlike scene nodes and lights, you can directly use the new scenemanager () statement to create it directly (instead of using the root createscenemanager method, but you should use it ). You can have multiple scenario managers to load multiple independent ry and ry at the same time. You can swap these scenario managers at any time by recreating the viewport (for details, see intermediate tutorial 4) you can also use the multi-view method to display multiple scenario managers at the same time (this will be covered in the advanced tutorial ).

Why do we use the createscenemanager function instead of manually creating our scenario manager object? When we use a single scenario manager, the ogre plug-in system gives us great flexibility. Only the scenario types are defined in the scenetype Enumeration type. Until now, the exampleapplication routine still uses st_generic as our scenario manager. You may think that the scenemanager class is used as the basic, but as long as you have not modified the Plugins. config file, you are not using it. If you use the octreescenemanager plug-in, octreescenemanager registers itself as the st_generic type and overwrites it as the basic scenemanager class. Octreescenemanager uses a system to pick invisible objects, so it is faster than the standard scenemanager ). If you remove the octreescenemanager plug-in from the Plugins. cfg file, you only needPossibleIt will use the basic scenemanager and may use a better scenario Manager based on your plug-in settings. This is the beauty of the Ogre system.

So far, we have used only the most basic methods of the root object to create the scenario manager. In fact, we can use a string instead of scenetype enumeration to request a scenario manager, the flexible scenemanager factory of ogre allows you to define any number of scenario manager types and create and destroy them as needed. For example, we use a plug-in that allows you to create a scenario manager named "fooscenemanager". We will create a scenario manager as follows:

       // do not add this to the project      mSceneMgr = mRoot->createSceneManager("FooSceneManager");

This will create such a scenario manager and give it a default name. Although it is not used in this tutorial, you should always use the second parameter of createscenemanager to name the scenario manager. For example, you can create two scenario managers with different names:

      // do not add this to the project      mSceneMgr1 = mRoot->createSceneManager("FooSceneManager", "foo");      mSceneMgr2 = mRoot->createSceneManager("FooSceneManager", "bar");

By naming the scenario manager, we no longer need to keep pointers to track them. The root object will be done for us. Then, when you want to use the "foo" and "bar" scenario managers, you can do this:

      // do not add this to the project      SceneManager *foo = mRoot->getSceneManager("foo");      SceneManager *bar = mRoot->getSceneManager("bar");

When you stop using a scenario manager, use the root destroyscenemanager to delete it and release the memory.

Although we will not explain it through the tutorial, you can also define your own scenemanager factory by inheriting the scenemanagerfactory class. If you have created your own scenario manager, or want to make some changes to it before your program uses a standard scenario Manager (such as creating a camera, creating lighting, loading ry, etc ), this will be very useful.

Terrain

Add a ground in the scenario

Now we have figured out the root object and scenario manager. It is time to create a ground (of course in Ogre ). The base class of the scenario manager defines a method called setworldgeometry, so that the derived class is easy to create a scenario. When the ground scenario Manager (terrainscenemanager) is used, it requires a file name to load the ground attributes. Add this line of code to the createscene function:

        mSceneMgr->setWorldGeometry( "terrain.cfg" );

Compile and run your program, and move the camera a little bit to see the ground generated by ogre. Is it easy?

"Terrain. cfg" File

The terrain. cfg file contains many ground generation options. As in the past, I will only talk about basic things. For more details, see/* here. It should be noted that the ground scenario manager design contains the paging function, but it has not yet been implemented. However, there is a plug-in ogre that can currently implement this function: Paging Scene Manager.

The ground scenario Manager uses a heightmap to generate a ground. You can change the height chart by changing the heightmap. Image parameter. You can change the post graph on the ground by changing worldtexture. You can also change detailtexture to make the ground more realistic. You can find the default images of terrain. cfg in/Media/materials/textures.

Illuminating the ground

In the previous tutorial, we just learned how to use lights and shadows. Unfortunately, they are difficult to use on the ground. Using a post image that has already processed the illumination effect is much easier than using the actual illumination. In the atomization Processing Section, we will also discuss how to use "pseudo darkness ". If you want to use the actual lighting effect, you should consider paging Scene Manager, which has good lighting support.

Sky

Ogre provides three types of sky: sky box, Sky sky, and sky. We will learn them one by one. Now you must add the following code in the choosescenemanager function:

        ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

Sky box

The sky box is actually a giant cube that contains all objects in the scenario. Add the following code to createscene to understand what the sky box is:

        mSceneMgr->setSkyBox( true, "Examples/SpaceSkyBox" );

Compile and run your program. How about it? (You can change the High-pixel texture to make it look more realistic .) When we call setskybox, we can set several useful parameters. The first one indicates whether to enable the sky box. If you want to cancel the sky box, you only need to call mscenemgr-> setskybox (false, ""); the second parameter is the material script used by the sky box.

The third and fourth parameters of setskybox are not very important. The third parameter sets the distance between the sky box and the camera. The fourth parameter determines whether the sky box is rendered before other objects or after other objects. Let's try to change the third parameter from the default 5000 to a small value and then see what the effect is:

        mSceneMgr->setSkyBox( true, "Examples/SpaceSkyBox", 10 );

Nothing changed! This is because the default value of the fourth parameter is true, that is to say, the sky box will be rendered first, and other things will be rendered above the sky box, so that the sky box will look behind the scenes. (Note that you should not make the third parameter smaller than the distance of the camera's near cut surface, otherwise it will be invisible !) In fact, the sky box should not be rendered first, because you will render all of it. If you finally render it, ogre will only render the visible part, thus improving the running speed. So let's try the final rendering of the sky box:

        mSceneMgr->setSkyBox( true, "Examples/SpaceSkyBox", 5000, false );

Now it looks like it hasn't changed, but the invisible part of the sky box won't be rendered. One thing to note is that when you set the sky box very close, some of the ry will be cropped. Try this:

        mSceneMgr->setSkyBox( true, "Examples/SpaceSkyBox", 100, false );

Have you seen it? The ground looks like it goes through the sky. This is definitely not the result you want (if you are still normal ). If you want to use the sky box in your program, you must decide how to use it. When you finally render the sky box, the program runs faster, but you must be careful not to hide your ry. In general, setting everything after the second parameter to the default is the safest.

Skysky

The sky is very similar to the sky box. You need to use setskydome to create the sky. He will create a giant cube that contains all objects in the scenario. The biggest difference is that the texture is projected onto the cube using the sphere method. What you see is actually a cube, but its texture looks like it is stuck on a sphere. This kind of Sky has a major defect, that is, there is no texture under the cube, and you must have something similar to the ground to cover it.

The ogre sample texture will clearly show you this defect. Delete setskybox and add the following code in createscene:

        mSceneMgr->setSkyDome( true, "Examples/CloudySky", 5, 8 );

When you are running, move the lens to the middle of the ground, and then the lens is almost parallel to the ground. Press the r key to switch to the grid state, and you will find that you still see a cube (with no bottom), but those clouds look like they are covered on the sphere. (Note that the cloud transformation is defined in the "examples/cloudysky" script, not all the sky clouds have this effect .)

The first two parameters of the sky are the same as those of the sky box. The third parameter is the curvature of the sky. We recommend that you use a value between 2 and 65 for the Ogre API. Lowering the third parameter will give you a better sense of distance. Increasing the third parameter will reduce the curvature and achieve a smoother effect. Try to set it to 2 and then set it to 65 to observe their differences. The following shows the bending effect of the sky, which is the effect when the third parameter is 2:

Http://www.idleengineer.net/images/beginner03_2.png

This is the result when the third parameter is 64:

Http://www.idleengineer.net/images/beginner03_64.png

The fourth parameter is the number of times the texture is repeated. You need to set it based on the size of your texture. But note that this parameter is of the real type (floating point number) rather than integer. The fifth and sixth parameters are respectively distance and rendering order, which are the same as those in the sky box.

Sky

The sky is quite different from the first two kinds of sky. We use a plane to replace the cube. (Note that all the following attributes of the sky are in the middle of the ground as much as possible) delete all the codes of the Sky Dome in createscene. We need to first create a plane, then it is down. The difference between setskyplane and the first two functions is that it has no distance parameter. Correspondingly, we set the distance in the D variable of plane.

        Plane plane;       plane.d = 1000;       plane.normal = Vector3::NEGATIVE_UNIT_Y;

Now we have defined a plane. Now we can create a plane. Note that the fourth parameter is the size of the Sky (here it is X units). The fifth parameter is the number of repeated times:

       mSceneMgr->setSkyPlane( true, plane, "Examples/SpaceSkyPlane", 1500, 75 );

Compile and run your program. The sky we created has two defects. The first reason is that the Paster pixels are low, which is hard to see during repetition. You just need to draw a texture with a high pixel edge. The main drawback is that when you look at the horizontal line, you can see the end of the sky. Even if you have a good texture, it will not look very realistic if you can see the end of the sky. This usage of the sky can only be used in scenarios where the basin or the surrounding area is surrounded by walls. However, his requirements for graphics cards are lower than those in the sky box and sky.

Fortunately, the sky is more than just that. The sixth parameter is similar to the Rendering sequence of the sky box and the sky. The seventh parameter allows you to set the curvature of the sky, so that the plane turns to an arc shape. At the same time, we also need to set the number of lines X and Y (the sky is a huge square, but if we want to bend it, it will become many small squares ). The eighth and ninth parameters are the number of line segments of X and Y:

       mSceneMgr->setSkyPlane( true, plane, "Examples/SpaceSkyPlane", 1500, 50, true, 1.5f, 150, 150 );

Compile and run your program. Now the sky looks much better. You can also use the material of the cloud:

       mSceneMgr->setSkyPlane( true, plane, "Examples/CloudySky", 1500, 40, true, 1.5f, 150, 150  );

Compile and run your program. The movement of the cloud and its repeated methods are worse than those of the sky, especially when you look near the edge of the ground to the horizontal line.

Alternatively, you can use mscenemgr-> setskyplane (false, plane (), ""); to cancel the sky.

Which one is better?

What kind of sky to use depends entirely on your program. If you need to see all the things around you, including the negative Y direction, you may only have to choose the sky box. If you have a ground or something similar to the floor that can cover the negative Y direction, then using the sky canopy will have a more real effect. If you don't see the flat line (for example, a canyon, a prison, or a courtyard in the middle of the castle), the sky will have a good effect and only have a low GPU usage. In the next section, we will explain the most important reason for using the sky because it can be well integrated with the cloud.

These are just suggestions. When you write your own program, you should try these types and then check which one is the best option to decide which one to use.

Atomization effect

Getting started

The atomization effect is very simple in ogre. Before using it, you must note that when you use the terrainscenemanager, you must call the setfog function before calling the setworldgeometry function. (It does not matter in other scenario managers ). Based on the order in which you call these two functions, different vertex programs will be called. (Note that there is a bug in ogre 1.0.0. When you call these functions in the correct order, exponential atomization will not be rendered. This bug was fixed at 1.0.1 ).

Before we start, clear all content except setworldgeometry in the createscene function.

You need to know that the most important thing about setting the atomization effect is that it does not create a "fog" entity in an empty place as you think. In fact, fog is just a filter when you watch an object. When there are no objects in front of you, you cannot see the fog. In fact, you only see the background color of the viewport. To make the fog more vivid, we must set the background color of the view to the fog color.

Fog is divided into two types: Linear and exponential. Linear fog increases linearly, while exponential fog increases exponentially (the concentration of fog at each distance increases more than the previous unit ). It is easier for you to understand the differences in their results. Let's look at several examples.

Atomization type

The first fog type we will see is linear, which is also the easiest to understand. After setworldgeometry is called, you must first set the background color of the view. We can use the createviewport method (as in the previous Guide), but sometimes we do not need to call the view. The Code is as follows:

        ColourValue fadeColour(0.9, 0.9, 0.9);       mWindow->getViewport(0)->setBackgroundColour(fadeColour);

If the view is not unique, you can use the getnumviewports meta function to get the number of views and then constantly operate on her, but this is usually rare (so far we know we only use one view), we can directly get the view. After setting the background color, we can create fog. Remember, this code must appear before setworldgeometry is called.

        mSceneMgr->setFog(FOG_LINEAR, fadeColour, 0.0, 50, 500);

The first parameter sets the fog type (Here we set linear ). The second parameter is the fog color we use. The third parameter does not need to be set in linear fog. The fourth and fifth parameters are used to set the intensity of smog. Here we end from 50 to 500. This means that there is no fog in the camera's 0 to 50 units. Fog gradually increases linearly from 50 to 500 units. It is full of fog except 500 units. Compile and run this application.

Another type of fog is exponential fog. Unlike setting the fog start point and end point, we need to set the fog density (the fourth and fifth parameters do not need to be set ). The Code is as follows:

      mSceneMgr->setFog(FOG_EXP, fadeColour, 0.005);

Compile and run this application. If you are using the DirectX Renderer, you will find it completely out of the fog. Call setfog to correct it before setworldgeometry. The OpenGL Renderer will do as described in the document, which can generate another effect of fog. There is also a more powerful exponential fog function (compared with the previous one, the farther away from the camera, the more dense the fog ). Note that if you use fog_exp2, it will produce more dense fog. Replace the above Code with the following:

     mSceneMgr->setFog(FOG_EXP2, fadeColour, 0.003);

Compile and run the program again. Basically, the three fog functions provided by ogre are interchangeable. You should experiment with these functions to see which one works better in your application.

Fog and sky

When you try to use fog with the sky box/sky dome, you may encounter some interesting problems. Because the sky box/Sky Dome is square, and the fog effect works in a sphere. Clear the content in the createscene method. If you cleverly adjust the parameters of the sky and fog, we can clearly find this problem.

       ColourValue fadeColour(0.9, 0.9, 0.9);       mSceneMgr->setFog(FOG_LINEAR, fadeColour, 0.0, 50, 515);       mWindow->getViewport(0)->setBackgroundColour(fadeColour);       mSceneMgr->setWorldGeometry("terrain.cfg");       mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8, 500);

Compile and run the program. If you turn the camera, you will find some parts of the Sky (the blue part is located from the edge, not the center ):

Http://www.idleengineer.net/images/beginner03_fogbox.png

This is obviously not what we want. Another option is to use the sky. Replace the following code with createscene:

       ColourValue fadeColour(0.9, 0.9, 0.9);       mSceneMgr->setFog(FOG_LINEAR, fadeColour, 0.0, 0, 130);       mWindow->getViewport(0)->setBackgroundColour(fadeColour);       Plane plane;       plane.d = 100;       plane.normal = Vector3::NEGATIVE_UNIT_Y;       mSceneMgr->setWorldGeometry("terrain.cfg");       mSceneMgr->setSkyPlane(true, plane, "Examples/CloudySky", 500, 20, true, 0.5, 150, 150);

This looks right. If we look up, we will be able to see the sky (the real situation is the same), rather than looking out from somewhere else. Whether or not you use bending, this can solve the abnormal phenomenon when you look at the horizon.

There is also a way to make the fog effect do not involve the whole sky, but the material script needs to be modified. This is no longer the content of this lesson. For future reference, it is the parameter for disabling fog effect in materials.

Fog in the dark

When you set the fog, you may not want to use the sky because the fog is so thick that you cannot see the sky. The above-mentioned fog effect techniques are very useful in some scenarios. Instead of setting the fog to a bright color, it is set to a very dark color to see what the effect is. (Note: we set the sky to only 10 units away from the camera before setting the fog ):

       ColourValue fadeColour(0.1, 0.1, 0.1);       mWindow->getViewport(0)->setBackgroundColour(fadeColour);       mSceneMgr->setFog(FOG_LINEAR, fadeColour, 0.0, 10, 150);       mSceneMgr->setWorldGeometry("terrain.cfg");       Plane plane;       plane.d = 10;       plane.normal = Vector3::NEGATIVE_UNIT_Y;       mSceneMgr->setSkyPlane(true, plane, "Examples/SpaceSkyPlane", 100, 45, true, 0.5, 150, 150);

Compile and run the program. This is what we get:

Http://www.idleengineer.net/images/beginner03_darkness.png

Not too bad. Of course, once you use light, you don't have to use this technique. But it does show the flexibility of the fog effect and the interesting things you can do with this engine. If you are writing a first-person game, black fog can make special effects like "blind" or "Spell.

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.