Original Author: Clay Culver Chinese translator: Antking
OGRE Tutorial (1) OGRE Tutorial (2)
Note: This article is for OGRE 1.0.0, if you use other versions, if you encounter problems, please go to the forum.
table of Contents
1 Reader Object 2 Introduction 3 How to start 4 OGRE How to Work 4.1 SceneManager Foundation 4.2 Entity Foundation 4.3 SceneNode Foundation 5 Your First Ogre Program 6 Coordinates and Vector 7 Add another object 8 Entities more in Depth9 Scenenodes more in Depth10 Try 10.1 Scale (Transform) 10.2 Rotate 11 Summary 12 Your idea?
1 reader object
This article is assumed that you have C programming knowledge, and set OGRE in the compiler, (if you don't know how to set up, please see "OGRE beginners") and do not know nothing 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 tell some basic theories. Through this article, you will slowly add code into your program and observe his running results. For these theories, there is no fixed code, you can also write other code through these theories.
3 How to start
For this tutorial, we used a fixed code (maybe you have seen it in "OGRE beginners). In this code, you can ignore other code, but the code in Createscene should pay attention. In the next tutorial, we will explain how to work in an in-depth OGRE, so the basic knowledge here is important. Add the following code to your compiler:
#include "exampleapplication.h"
Class TutorialApplication: Public ExampleApplication {Protected: PUBLIC: TUTORAALAPPLICATION () {}
~ TutorialApplication () {} protected: void createScene (void) {}};
#ifangre_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 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.GETfulDescription (). c_str ()); # Endif} Return 0;
If you are OGRESDK under Windows, make sure to add "[OGRESDK_DIRECTORY] / Samples / Include" directory to this project. If you use the source code of the OGRE, add the "[OgResource_directory] / Samples / Common / Include" directory. Then, you can compile and run. If you have encountered a problem, please see the page about these information about this information. If you can't, please go to the Forum.
Program Control: Move with WASD, the mouse is determined. The ESC button exits.
4 ogre work
A wide theme. We will explain from his foundation The Scenenode, Entity, And SceneManager.
4.1 SceneManager foundation
SceneManager management appears all objects on the screen. When you put an object into the scene, SceneManager will track the coordinates of this object. When you create an imager, SceneManager will track all of them. When you build wooden blocks, billboards, lights ..., SceneManager will still track them.
SceneManager has many types, with rendering terrain, with rendering BSP trees, and so on. In this article, you will learn a lot of SceneManager.
4.2 Entity Foundation
Entity is the shape of an object you rendered in the scene. You can imagine him into a 3D grid. A robot has a grid, a fish has a grid, and your role walks the terrain with a large grid. For example, light, billboards (Billboards), particles, camera, etc. There are no Entity.
One thing that is worth noting is that OGRE is rendered separately according to the rendering of the coordinates and directions of the object. This means that you can't render it directly to a grid in the scene. You must give the grid of the object to be rendered to SceneNode, SceneNode, contain information such as coordinates and directions.
4.3 SceneNode Foundation
It has been mentioned above, SceneNode is used to keep track of all the coordinates and directions of the objects associated therewith. When you build a grid, he will not rendering in the scene unless you give this grid to SceneNode. Similarly, SceneNode is not an object you want to display on the screen, only when you build a scenenode, and give him a grid, he will display an object on the screen.
SceneNode can give him many objects. For example, you have a walking object on the screen and you want a light to surround him. First, you need to build a scerenode and then build a grid of the role and give him SceneNode. The next step is established, and he gives him SceneNode. SceneNode also allows you to give him another SceneNode so that a grade node system is established. In the next article, we will explain the features of SceneNode more detailed. An important concept is that the position of the scenenode is always related to his parent SceneNode, and the SceneManager contains all the root nodes of the assigned scenenodes.
5 Your first OGRE program
Now return to our code that we started, find the TutorialApplication :: Createscene member function. In this tutorial, we will only operate this function. The first thing we have to do is establish a grid. We can implement it by calling the SceneManager's CreateEntity function. Add below to createScene:
Entity * ent1 = mscenemgr-> createentity ("robot", "robot.mesh");
Here, there are several problems will pop up. First, where is Mscenemgr from, what value we use to call this function? The MsceneMGR constant contains the current SceneManager object (this is implemented by the ExampleApplication class). The first parameter is the name of the grid built through CreateEntity. All grids must have a unique name. If you try to build two grids with the same name, you will get an error. "Robot.Mesh" is the only name of the grid we want to use. Here, the grid we use is imported through the ExampleApplication class.
To now, we have established a grid, we also need to use SceneNode to associate him. Also because each SceneManager has a root SceneNode, we will build a child node with the code below:
Scenenode * node1 = mscenemgr-> getrootscenenode () -> CreateChildScenenode ("robotnode");
This long statement first calls the GetrootScenenode method of the current SceneManager. Then he calls the root of the CreateChildscenenode method. The parameters in the CreateChildScenenode method are the name of the SceneNode we build. As mentioned earlier, SceneNode does not allow names.
Finally, we need to give the grid to SceneNode so that the Robot has rendered coordinates:
Node1-> attachObject (ent1);
Everything is OK! Compile and run, you will see a robot on the screen.
6 coordinates and vector
We need to discuss the screen coordinates and OGRE vector before we begin to explain. Ogre is like other image engines, with X, Z axis represents horizontal surface, y represents a vertical axis. As you look at the screen, the X axis represents the left and right of your screen, right, right is the X-axis positive direction. The Y axis represents the downstream of your screen, the above is the Y-axis positive direction. The Z axis represents the origin of your screen, the outside is the z-axis positive direction.
Note, how do our robots face the positive direction of the X-axis? This is an attribute of the grid. How did he design? OGRE does not specify the direction of your initial model, so the direction of each object grid you import is not necessarily. OGRE uses a vector class to represent coordinates and directions. These vectors can be defined as 2 (Vector2), 4 (Vector4) dimension, where vector3 is most common, if you are not familiar with the vector, I suggest you look at the following website:
http://en.wikipedia.org/wiki/vector_(spatial)
IN: I suggest that you look at the "Line-shaped Algebra" before learning 3D programming knowledge.
Mathematical knowledge about the vector will play an important role in future learning.
7 Add another object
In front, we explain the role of the coordinate system, let's go back to our code. In front, in the code we add, we did not explicitly indicate the coordinates of our robots. But in OGRE, there are many functions that can initially coordinate. For example: SceneNode :: CreateChildscenenode member function, there are three parameters: SceneNode's name, SceneNode coordinates, and basic rotation of SceneNode. For coordinates, as you can see, we are initially (0, 0, 0). Now, we build another SceneNode, but this time we show his coordinates for another value:
Entity * ent2 = mscenemgr-> CreateEntity ("robot2", "robot.mesh"); SceneNode * node2 = mscenemgr-> getrootscenenode () -> CreateChildscenenode ("robotnode2", vector3 (50, 0, 0)); Node2- > attachobject (ent2);
This looks like the previous we define, just slight changes. First, we have a small difference when we name the grid and SceneNode. The second difference is that the position of our initial grid begins from the root Scenenode 50 unit (remember all coordinates of Scenenode and their parent node). Compile and run, now you have two robots.
8 Entities more in Depth (in-depth enttive)
Entities class feature is very powerful, I don't want to overwide here, just talk about his basic. First, we have to mention some of the powerful member functions in Entities.
The first is Entity :: setvisible and entity :: isvisible. You can set any Entity to visual. If you need to hide Entity, then display it, you can do this function, and use first to delete Entity, and then create him from the new. The mesh and appendix of the object are automatically copied to memory, and you don't need you to save them yourself. What you need to save is the establishment and deletion of the Entity object.
The GetName function is used to return the name of the Entity, and the getParentScenenode function is used to return to SceneNode associated with Entity.
9 Scenenodes more in depth
The SceneNode class is very complicated. There are many things on Scenenodes to do, so we are only overwhelming to some common functions. You can get and set the point of the SceneNode's point (usually related to the parent node of SceneNode) with SceneNode's getPosition, SetPosition function. You can move objects with Translate functions.
SceneNode can not only set the location, but also manage the transformation size and rotation of the object. You can set the transformation size with the scale function. You can rotate the object with YAW, ROLL, PITCH. You can also use the ReseTorientation function to set the rotation parameters of the object. You can also use the setOrientation, getorient, and the rotate function to achieve high quality rotation.
Let's take a look at the attachobject function. If you want to put the object to the Scenenode point, these related functions will be very useful: NumattachedObjects, getattachedObject (this function has multiple versions), DetatchObject (also multiple versions), DetatchallObjects. Group functions Handle the parenta SceneNode.
All coordinates are related to the parent Scenenode node, we can do two SceneNode nodes that move each other. There is a code below:
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 change is as follows:
SceneNode * node2 = node1-> CreateChildScenenode ("robotnode2", vector3 (50, 0, 0));
Then then the bon node of RobotNode2 as 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 can move RobotNode because robotnode2 is the child node of RobotNode, and RobotNode2 will also follow: Node1-> Translate (Vector3 (25, 0, 0));
If you have trouble here, we can understand it from the root SceneNode from the top. Here, we start Node1 from (0, 0, 0), then transferred to (25, 0, 0), so the coordinates of Node1 are (25, 0, 0). Node2 begins (50, 0, 0), plus (10, 0, 0). Therefore, the new coordinate is (60, 0, 10). Now let us calculate the true coordinates of these objects. Starting from the root Scenenode node, his location is always (0,0,0). Now, Node1's coordinates are (root node1): (0, 0, 0) (25, 0) = (25, 0, 0). The Node2 is a node1 child node, so his coordinate is 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 get the name of Scenenodes and entities through getScenenode, getentity function. These two functions are methods in sceneManager, so you don't need to keep a pointer in each scerenode you created. You only need to hang the one you often use.
10 trial
Through this chapter, we learned the basic knowledge of Entities, Scenenodes, and the SceneManager. Below, I will give them some routines. In this example, we build a set of robots in the scene.
Scale
The following code is to rotate the grid through the Scale function in Scenenode.
Entity * ent = mscenemgr-> createentity ("robot", "robot.mesh"); scenenode * node = mscenemgr-> getrootscenenode () -> CreateChildscenenode ("robotnode"); node-> attachobject (ent);
Node-> scale (.5, 1, 2);
ENT = mscenemgr-> createentity, "robot.mesh"); node = mscenemgr-> getrootscenenode () -> CreateChildscenenode ("robotnode2", vector3 (50, 0, 0)); node-> attachobject (entne " );
Node-> Scale (1, 2, 1);
Rotate
The following code is through angle and radius, rotating objects with the YAW, PITCH, AND ROLL function.
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, "robot.mesh"); Node = mscenemgr-> getrootscenenode () -> CreateChildScenenode ("robotnode2"); node-> attachobject (ent);
Node-> Pitch (Degree (-90));
Ent = mscenemgr-> createentity, "robot.mesh"); node = mscenemgr-> getrootscenenode () -> CreateChildscenenode ("robotnode3", vector3 (100, 0, 0)); node-> attachobject (entne " );
Node-> roll (degree (-90));
11 summary
In a chapter, we have learned some basic knowledge about SceneManager, SceneNode, And Entity class. But the functions mentioned in the text are not very well understood. In the next chapter, we will explain these functions in detail.
12 Your idea?
If you are not very clear about something on the text, please go to the Forum. Regarding this translation, please send me an onking@gmail.cn My blog is: http://akinggame.gameres.com