Ogre tutorial (1): scenenode, entity, scenemanager

Source: Internet
Author: User
Tags translate function

Ogre tutorial (1): scenenode, entity, scenemanager

 
 

Original Author: Clay Culver
Chinese translator: antking

Ogre tutorial (1)
Ogre tutorial (2)

Note: This article is for ogre 1.0.0. If you are using another version, please go to the forum to discuss any issues.

Directory

1 target audience
2 Introduction
3. How to start
4. How does ogre work?
4.1 scenemanager Basics
4.2 entity Basics
4.3 scenenode Basics
5. Your first ogre Program
6 coordinate and vector
7. Add another object
8 entities
More in depth
9 scenenodes more in
Depth
10 try
10.1 Scale)
10.2 Rotation
11 Summary
12 what do you think?

1 target audience

This article assumes that you have C ++ programming knowledge and set ogre in the compiler. (If you do not know how to set it, see ogre beginner guide.) if you have no knowledge about ogre.

2 Introduction

In this tutorial, I will introduce some basic ogre structures: scenemanager, scenenode, and entity.
. In this article, I will not use too much code, but explain some basic theories.
Through this article, you will slowly add code to your program and observe its running results. There is no fixed code for these theories. You can also use these theories to write other codes.

3. How to start

For this tutorial, we use a fixed piece of code (maybe you have seen it in ogre beginner guide ). In this code, you can ignore other code, but createscene
Pay attention to the Code in. In the next tutorial, we will explain in depth how ogre works, so the basic knowledge here is very important. Add the following code to your Compiler:

# Include "exampleapplication. H"

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

~ Tutorialapplication ()



{



}
Protected:



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 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


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 ogresdk in windows, please be sure to add the "[ogresdk_directory]/samples/include" directory
In this project. If you are using the source code of ogre, add "[ogresource_directory]/samples/common/include"
Directory. Then, you can compile and run the program. If you still encounter any problems, please refer to the Wiki's page for such information. If you still cannot, please go to the forum to discuss it.

Program Control: Move wasd with the mouse to determine the direction. Run the ESC key to exit.

4. How does ogre work?

A wide topic. We will explain it from the basics of the scenenode, entity, and scenemanager.

4.1 scenemanager Basics

Scenemanager manages all objects that appear on the screen. When you place an object in a scene, scenemanager will track the coordinates of the object. When you create
When a camera is running, scenemanager will track all of them. When you build blocks, billboards, lights..., scenemanager
They will still be tracked.

Scenemanager also has many types, such as terrain rendering and BSP Tree rendering. In this article, you will learn many types of scenemanager.

4.2 entity Basics

Entity is the shape of the object you render in the scene. You can think of him as a 3D mesh. A robot has a grid, a fish has a grid, and your role's terrain has a large grid. There are no entity for lighting, billboards, particles, and video cameras.

One thing worth noting is that ogre is rendered based on the coordinate and directionality of the object. This means that you cannot directly render a grid in the scenario. You must give scenenode the mesh of the object to be rendered.
, Scenenode contains information such as coordinates and directions.

4.3 scenenode Basics

As mentioned above, scenenode
It is used to keep track of the coordinates and directions of all objects associated with it. When you create a grid, it will not be rendered in the scene, unless you assign this grid to scenenode
. Similarly, scenenode is not the object you want to display on the screen. Only when you create a scenenode and assign it to a grid will scenenode display an object on the screen.

Scenenode
Can assign many objects to him. For example, you have a walking object on the screen, and you want to generate a light to surround it. First, you need to create a scenenode
Create a role mesh and assign it to scenenode. Next, create the light and assign it to scenenode.
. Scenenode also allows you to assign it to other scenenode, so that a hierarchical node system is established. In the next article, we will explain the functions of scenenode in more detail.

An important concept is that the position of scenenode is always related to its parent scenenode, and scenemanager contains the root nodes of all assigned scenenodes.

5. Your first ogre Program

Now, go back to the code we created and find tutorialapplication: createscene.
Member functions. In this tutorial, we will only operate on this function. The first thing we need to do is create a grid. We can call scenemanager's
Createentity function. Add the following row to createscene:

Entity * ent1 = mscenemgr-> createentity ("robot", "robot. mesh ");

Here, a few questions will pop up. First, where does mscenemgr come from? What values do we use to call this function?
The mscenemgr constant contains the current scenemanager object (implemented through the exampleapplication class ). The first parameter is
The name of the Grid created by createentity. All grids must have a unique name. If you try to create two grids with the same name, you will get an error. "Robot. mesh"
It indicates the name of the grid to be used. Here, the mesh we use is imported through the exampleapplication class.

Now we have built a grid, and we need to associate it with scenenode. Because each scenemanager has a root
Scenenode. We will use the following code to create a subnode:

Scenenode * node1 = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode ");

This long statement first calls the getrootscenenode method of the current scenemanager. Then, he calls
Createchildscenenode method. The parameters in the createchildscenenode method are the names of the created scenenode. As mentioned above
The name of scenenode cannot be the same.

Finally, we need to assign the grid to scenenode so that the robot has the rendering coordinates:

Node1-> attachobject (ent1 );

Everything is OK! Compile and run the program. You will see a robot on the screen.

6 coordinate and vector

Before we begin, we need to discuss screen coordinates and ogre vectors. Like other image engines, ogre uses X and Z axes to represent horizontal surfaces and Y to represent vertical axes. As you can see
The screen is the same. The X axis indicates the left, right, and right sides of your screen. The Y axis indicates the bottom-up of your screen, and the top is the positive direction of the Y axis. The Z axis indicates that your screen is from the inside out, and the outside is in the positive direction of the Z axis.

Note: How can our robots face the positive direction of the X axis? This is an attribute of the Grid. How is it designed? Ogre does not specify the direction of your initial model, so the direction of each object mesh you import is not certain.

Ogre uses vector classes to represent coordinates and directions. These vectors can be defined as 2 (vector2), 3 (vector3), and 4 (vector4) dimensions. vector3 is most commonly used. If you are not familiar with vectors, I suggest you refer to the following website:


Http://en.wikipedia.org/wiki/Vector_%28spatial%29

Note: before learning 3D programming, I suggest you take a look at linear algebra and space analytic ry.

Mathematical knowledge about vectors will play an important role in future studies.

7. Add another object

Previously, we explained the role of the coordinate system. Next we will return to our code. In the code we added earlier, we did not explicitly indicate the coordinates of our robots. However, in ogre, there are many functions with initial coordinates. For example:
Scenenode: createchildscenenode
The member function has three parameters: the name of scenenode, the coordinates of scenenode, and the basic rotation of scenenode. For coordinates, as you can see, we start with (0, 0, 0 ). Now we create another scenenode, but this time we show that his coordinates are another value:

Entity * ent2 = mscenemgr-> createentity ("robot2", "robot. mesh ");
Scenenode * node2 = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode2", vector3 (50, 0, 0 ));
Node2-> attachobject (ent2 );

This seems to be the same as we have previously defined, but there are slight changes. First, we name the grid a little different from scenenode. The second difference is the starting position of the initial mesh.
Deviated from the root scenenode50 units (remember that all coordinates of scenenode are related to their parent node ). Compile and run. Now, you have two robots.

8 entities more in
Depth (in-depth entities)

The entities class is very powerful. I don't want to overwrite it here. I just want to talk about its basics. First, we have to mention some powerful member functions in entities.

The first one is entity: setvisible and entity: isvisible. You can take any entity
Set to visible. If you need to hide the entity first and then display it again, you can delete the entity first instead of calling this function.
. The object mesh and arts and sciences are automatically copied to the memory, and you do not need to save them yourself. You need to save the creation and deletion of Entity objects.

The getname function is used to return the entity name. The getparentscenenode function is used to return the entity-related scenenode.

9 scenenodes more in depth

The scenenode class is very complex. There are many things that can be done on scenenodes. Therefore, we only cover some common functions here.

You can use the getposition and setposition functions of scenenode to obtain the points set for scenenode (usually related to the parent node of scenenode ). You can use the translate function to move objects.

Scenenode can not only set the position, but also manage the size and rotation of the object. You can use the scale function to set the conversion size. You can use yaw, roll, and pitch to rotate objects.
Body. You can also use the resetorientation function to set the rotation parameters of objects. You can also use
Setorientation, getorientation, and rotate functions achieve high-quality rotation.

Let's take a look at the attachobject functions. If you want to put the thing system on the scenenode point, these functions will be very
Use: numattachedobjects, getattachedobject (this function has multiple versions), detatchobject (there are also multiple versions)
This), detatchallobjects. There is also a set of functions to process the Parent and Child scenenode.

All coordinates are related to the parent scenenode node. We can create two mutually moving scenenode nodes. The following code is provided:

Entity * ent1 = mscenemgr-> createentity ("robot", "robot. mesh ");
Scenenode * node1 = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode ");
Node1-> attachobject (ent1 );

Entity * ent2 = mscenemgr-> createentity ("robot2", "robot. mesh ");
Scenenode * node2 = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode2", vector3 (50, 0, 0 ));
Node2-> attachobject (ent2 );

If we take the sixth line:
Scenenode * node2 = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode2", vector3 (50, 0, 0 ));

The changes are as follows:

Scenenode * node2 = node1-> createchildscenenode ("robotnode2", vector3 (
50, 0, 0 ));

Then, use robotnode2 as the child node of the robotnode. If you move node1, you will move node2. for example, the following code moves robotnode2:
Node2-> translate (vector3 (10, 0, 10 ));

The following code moves the robotnode because robotnode2 is a child node of the robotnode, and robotnode2 will also move:
Node1-> translate (vector3 (25, 0, 0 ));

If you are in trouble here, we can understand it from the root scenenode
Top-down. Here, we start node1 from (0, 0) and then transfer to (25, 0, 0). Therefore, the coordinates of node1 are (, 0 ). Node2 starts at (50, 0, 0), plus (10, 0, 0 ). Therefore, the new coordinates are (60, 0, 10 ).

Now, let's calculate the real coordinates of these objects. Slave scenenode
When the node starts, its position is always (0, 0, 0 ). Now, the coordinates of node1 are (root + node1) :( 0, 0) + (25, 0, 0) = (25, 0, 0 ).
Node2 is a subnode of node1. Therefore, its coordinates are (root + node1 + node2): (0, 0, 0) + (25, 0, 0) +
(60, 0, 10) = (85, 0, 10). This is just an example of the scenenode coordinate hierarchy.

You can use the getscenenode and getentity functions to obtain scenenodes and entities.
. These two functions are the methods in scenemanager. Therefore, you do not need to maintain a pointer in each scenenode you create. You only need to move the one you often use.

10 try

Through this chapter, we learned about entities, scenenodes, And the scenemanager.
. Below, I will give some of their routines. In this example, we create a set of robots in the scenario.

Scale

The following code uses the scale function in scenenode to rotate the grid.

Entity * ent = mscenemgr-> createentity ("robot", "robot. mesh ");
Scenenode * node = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode ");
Node-> attachobject (ENT );

Node-> Scale (. 5, 1, 2 );

Ent = mscenemgr-> createentity ("robot2", "robot. mesh ");
Node = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode2", vector3 (50, 0, 0 ));
Node-> attachobject (ENT );

Node-> Scale (1, 2, 1 );

Rotate

The following code uses the yaw, pitch, and roll functions to rotate an object by angle and radius.

Entity * ent = mscenemgr-> createentity ("robot", "robot. mesh ");
Scenenode * node = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode", vector3 (-100, 0, 0 ));
Node-> attachobject (ENT );

Node-> yaw (degree (-90 ));

Ent = mscenemgr-> createentity ("robot2", "robot. mesh ");
Node = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode2 ");
Node-> attachobject (ENT );

Node-> pitch (degree (-90 ));

Ent = mscenemgr-> createentity ("robot3", "robot. mesh ");
Node = mscenemgr-> getrootscenenode ()-> createchildscenenode (
"Robotnode3", vector3 (100, 0, 0 ));
Node-> attachobject (ENT );

Node-> Roll (degree (-90 ));

11 Summary

In this chapter, we learn about scenemanager, scenenode, and entity.
Class. However, I am not very familiar with the functions mentioned in this article. In the next chapter, we will explain these functions in detail.

12 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.