Ogre tutorial (II): cameras, lights, and shadows

Source: Internet
Author: User
 

Original Author: Clay Culver
Chinese translator: antking


Ogre tutorial (1)
Ogre tutorial (2)



1) Prerequisites
2) Introduction
3) Start
4) camera
4.1 ogre camera
4.2 create a camera
5) viewports)
5.1 ogre
5.2 create a view
6) lights and shadows
6.1 shadow types supported by ogre
6.2 use shadow in ogre
6.3 lighting types
6.4 create a light
7) Try
7.1 different shadow types
7.2 light fades
7.3 scenemanager: setambientlight
7.4 background color
7.5 camera: setfarclipdistance
7.6 planes (degree)
8) What do you think?

1) Prerequisites

This article assumes that you have C ++ programming knowledge and installed and set ogre in the compiler. This article is based on the first tutorial.

2) Introduction

In this tutorial, we will explain more about the Ogre structure. This tutorial focuses on lighting objects and explains how they build shadows in ogre. We will also review the camera knowledge.

Through this tutorial, you will add more code to your program and see their results.

3) Start

Like the first tutorial, we will use some prepared code. In our tutorialapplication
Class to add two methods: createviewport and
Createcamera. These two functions are also defined in the basic exampleapplication. However, in this tutorial, we will take a look at how the camera and the viewport actually work.

Create a project in the compiler and add the following code to it:

# Include "exampleapplication. H"

Class tutorialapplication: Public exampleapplication
{
Protected:
Public:
Tutorialapplication ()



{



}




~ Tutorialapplication ()



{



}
Protected:



Virtual void createcamera (void)



{



}




Virtual void createviewports (void)



{



}




Void createscene (void)



{


Entity * ent;


Light * light;



}
};

# 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 str1_line, INT)
# Else
Int 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 are using the Ogre SDK in windows, add "[ogresdk_directory]/samples/include"
Directory to this project. If you use the Ogre source code, replace it with the following path: [ogresource_directory]/samples/common/include"
. When you continue the next section, make sure that your program can run. A black window is displayed during running. If you have any questions, refer to ogre beginner guide.

Program Control: Use the WASD key to move the mouse around. The Escape key exits.

4) camera

4.1 ogre camera

Next, we will create a camera to view the scenario. The camera is a special object, a bit like scenenode. The camera has setposition, yaw, and roll.
, Pit44. you can give it to any scenenode. And scenenode
Similarly, the camera's location is related to its parent node. For mobile and rotating cameras, you should consider them like scenenode.

It is worth noting that you may want to create only one camera in the scenario. That is to say, we cannot create one camera for one part of the scenario, and the other for another part of the scenario, then, you can determine which camera to use based on the scenario display. On the contrary, Ogre can build many camera machines to establish these camera machines to scenenode
Then, the scene display is determined based on the camera in the scenenode point. We will discuss it in detail in the framelistener tutorial.

4.2 create a camera

We will change the camera method in exampleapplication.

Find the tutorialapplication: createcamera function, because the camera is scenemanager
So we use scenemanager to build them. Add the following code to the program:

// Create the camera
Mcamera = mscenemgr-> createcamera ("playercam ");

We create a video camera named playercam. You can use scenemanager by using the camera name
Getcamera function in to get the camera, if you do not want to give the camera pointer to scenemanager.

Next, we will set the camera location and its direction. Because the object is initially at the origin, our camera should be transformed on the + z axis and face the camera to the origin. Add the following code to the program:

// Set its position, direction
Mcamera-> setposition (vector3 (0, 10, 500 ));
Mcamera-> lookat (vector3 (0, 0 ));

You can enable the camera to direct to any location after you call the yaw, rotate, and pitch functions.
Lookat () is important for precise positioning in the game.

It makes it easy for scenenodes to adjust the camera direction in the game. Finally, we set the cut distance to 5. The cut distance of the camera refers to the near or distant object that you can see in front of you.
Body. If you set the cut distance value very small, you are near the object, you cannot see all, you can only see a small part of the object, on the contrary, you will see all the objects. This is useful when you render a large number of objects.
. The following code sets the nearest cut distance:

Mcamera-> setnearclipdistance (5 );

Setfarclipdistance can be used to set the farthest cut distance.

5) viewports)

5.1 ogre

When we start to process multiple cameras, the concept of the view is very important. I think it is important to understand how ogre uses a camera to render a scenario. Multiple scenemanagers in ogre
It is possible to run together. They may also divide the screen into multiple blocks and use the camera to render them separately on the screen (for example, we can imagine two people playing games ). We will explain how these are done in the following tutorial.

To understand how ogre renders a scenario, you must understand the three structures of ogre: the camera, the scenemanager, and
Renderwindow. Renderwindow we have not mentioned before, but it is very simple. It is the window for displaying objects.

Scenemanager is used to create a camera to display scenes. You must tell renderwindow that the camera should be displayed on the screen and what part of the camera should be displayed in the window. While
You tell renderwindow that the camera's window is your own. In the common usage of ogre, you only need to create a camera, which occupies the whole
Renderwindow, so there is only one view.

In this chapter, we will explain how to use a camera to create a view. We can use this viewport to set the background color of our rendered scene.

5.2 create a view

Let's go back to the section using exampleapplication to create a viewport and find tutorialapplication: createviewports
This method. To create a viewport, we only need to simply call the addviewport method in renderwindow and use the camera to support it. The mwindow class is derived from the exampleapplication class. Therefore, we add the following code:

// Create one viewport, entire window
Viewport * Vp = mwindow-> addviewport (mcamera );

Now, we have our own viewport. What can we do with it? The most important thing is to use it to call the setbackgroundcolour method to set the background color. Here, because we want to handle the light, we set the background color to black.

VP-> setbackgroundcolour (colourvalue (0, 0 ));

Note that the color values are expressed in red, green, and blue, and the value ranges from 0 to 1. Finally, the most important thing we need to do is to set the camera aspect ratio.
. If you set a mode other than the standard full-screen viewport, you will get a very strange scene. Here, we set the initial aspect ratio:

// Alter the camera aspect ratio to match the viewport
Mcamera-> setaspectratio (real (VP-> getactualwidth ())/
Real (VP-> getactualheight ()));

6) lights and shadows

6.1 shadow types supported by ogre

Ogre currently supports three Shadow types:

1. Adjust the texture shadowtype_texture_modulative. This is used to create a black-and-white texture shadow.

2. Adjust the template shadowtype_stencil_modulative )-
This technology is used to render all objects that are not transparent in the scene. This method is the same as adding a template shadow. It is not commonly used but accurate.

3. Add template shadowtype_stencil_additive-this technology is used to render each light in a scenario. This is very difficult on the graphics card.

Ogre does not use Soft Shadows as part of the engine.

6.2 use shadow in ogre

It is relatively simple to use shadow in ogre, scenemanager
The setshadowtechnique member is provided to set the shadow type. You can also use the secastshadows member to set the shadow.
Here, we set the environment light to black and then set the shadow. Find the tutorialapplication: createscene member and add the following code:

Mscenemgr-> setambientlight (colourvalue (0, 0, 0 ));
Mscenemgr-> setshadowtechnique (shadowtype_stencil_additive );

Now scenemanager uses the adjusted texture shadow. Next, we create an object and make it cast a shadow:

Ent = mscenemgr-> createentity ("ninja", "ninja. mesh ");
Ent-> setcastshadows (true );
Mscenemgr-> getrootscenenode ()-> createchildscenenode ()-> attachobject (
Ent );

Here, ninja. mesh is
Exampleapplication pre-import. To enable ninja. mesh to display projected shadows, we must create a shadow-supported surface for it.
(Translator's note: The following text may be incorrect. The original text is as follows:
Again, the ninja. mesh has been preloaded for us by
Exampleapplication. We also need something for the ninja to stand on (so
That he has something to cast shadows onto). To do this we will create
Simple plane for him to stand on. This is not meant to be a tutorial on
Using meshmanager, but we will go over the very basics since we have
Use it to create a plane. First we need to define the plane
(Http://www.ogre3d.org/docs/api/html/classOgre_1_1Plane.html) object
Itself, which is done by supplying a normal and the distance from
Origin. We cocould (for example) use planes to make up parts of world
Geometry, in which case we wowould need to specify something other than 0
For our origin distance. For now we just want a plane to have
Positive Y axis as its normal (that means we want it to face up), and no
Distance from the origin:
)
First, we must create a plane object for it, which supports the distance between the normal and the source. We can make the surface a part of the world's geometric objects. In this case, we will indicate the distance from us to the origin. But now, we make the axis of the face's normal point to the positive y axis, without the distance to the origin:

Plane plane (vector3: unit_y, 0 );

Now, we will store this surface and use it in programs at a low cost. While meshmanager
Class is used to maintain the path of all the grids in the import scenario. The createplane method is used to generate plane definitions and generate grids based on these definition values. The Code is as follows:

Meshmanager: getsingleton (). createplane ("Ground ",
Resourcegroupmanager: default_resource_group_name, plane,
, 20, true, 5, vector3: unit_z );

Now, you may want to know How meshmanager is used, but I don't want to explain it now. Here, we store the 1500x1500 grid named "ground". Create the grid and fix it in the scene:

Ent = mscenemgr-> createentity ("groundentity", "Ground ");
Mscenemgr-> getrootscenenode ()-> createchildscenenode ()-> attachobject (ENT );

Before we finish the ground, we need to do two important things. The first thing is to tell scenemanager
We don't want to cast shadows without knowing what shadows are used. The second thing is the texture of the ground. Our robot Ken ninja
The material script has been defined. When we set up our ground, we did not set up a liberal arts program for him. Here we use the material script in "examples/Rockwall" in ogre:

Ent-> setmaterialname ("examples/Rockwall ");
Ent-> setcastshadows (false );

Now we have ninja and ground in the scenario. Let's compile and run it. We didn't see any objects! Why?
That's because we didn't add lights. Let's add lights.

6.3 lighting types

Three types of lights are provided in ogre:

1, point (lt_point)-emits from a point and spreads to four sides.

2. lt_spotlight-the point light is a bit like the hidden light. The light starts from a point and then spreads in one direction. You can also define the angle of the circle of the point light, which is divided into the light source part and the scattering part.

3, direction light (lt_directional )-
The direction light is a bit like the mirror light. For example, if you build a night scene, you want to simulate the moonlight. Here, you can use the direction light of the moon to simulate the moonlight.

There are many parameters, the most important of which is diffuse and specular. Each material script defines how much diffuse can be reflected by a material.
And specular light. In the following sections, we will learn in detail.

6.4 create a light

To create a light in ogre, we need to call the createlight method in scenemanager. After a light is set, we can set its location or associate it with scenenode.
To facilitate the movement. There are only two functions for setting the light: setposition
And setdirection. Therefore, to create a fixed light, you only need to call the setposition method. If you want to establish activity light, you only need to associate it with scenenode.

Next, let's create a point. The first step is to create a light and set the type and position:

Light = mscenemgr-> createlight ("light1 ");
Light-> settype (light: lt_point );
Light-> setposition (vector3 (0,150,250 ));

We have already set the light. Next we need to set the diffuse and specular color. Here we set the red color:

Light-> setdiffusecolour (1.0, 0.0, 0.0 );
Light-> setspecularcolour (1.0, 0.0, 0.0 );

Compile and run now. Now you can see ninja and its shadow. Note that we cannot see the light source, but can only see the result of the light source.

Next, we will establish the direction light, where we have established the yellow direction light:

Light = mscenemgr-> createlight ("light3 ");
Light-> settype (light: lt_directional );
Light-> setdiffusecolour (colourvalue (. 25,. 25, 0 ));
Light-> setspecularcolour (colourvalue (. 25,. 25, 0 ));

We didn't set the direction of the light, but only set its position. Below, we will set its direction: pointing to the positive Z axis and the negative Y axis:

Light-> setdirection (vector3 (0,-1, 1 ));

Compile and run. You will get two shadows in the scene. Finally, we will build the light:

Light = mscenemgr-> createlight ("light2 ");
Light-> settype (light: lt_spotlight );
Light-& gt; setdiffusecolour (0, 0, 1.0 );
Light-& gt; setspecularcolour (0, 0, 1.0 );

We also need to set the position and direction of the light:

Light-> setdirection (-1,-1, 0 );
Light-> setposition (vector3 (300,300, 0 ));

Imagine that the lens can adjust the aperture to control the light range. You can use the setspotlightrange method to adjust the same lighting:

Light-> setspotlightrange (degree (35), degree (50 ));

7) Try

7.1 different shadow types

In this article, we only use the shadowtype_stencil_additive type of shadowtype_stencil_additive. Please try the other two types to see what happened. There are many other shadow functions in scenemanager. Try them to see if you can use them.

7.2 light fades

Setattenuation is also provided in the light to reduce the light. Add this function to your vertex, set different values, and see what the effect is.

7.3 scenemanager: setambientlight

Test the setambientlight method in scenemanager.

7.4 background color

Change the color value in the createviewports function. Learn how to change it for better use.

7.6 planes (degree)

In this chapter, we have not explained planes in detail. We will explain it in the next lesson. Here, you mainly learn how to use the createplane function.

8) What do you think?

If you are not very clear about some of the content in this article, please go to the forum to discuss it. Please send me an email about this translation.
Antking@gmail.cn my blog: http://akinggame.gameres.com

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.