Original:beginner Tutorial 1:scenenode, Entity, and Scenemanager structure
PrerequisitesThis tutorial assumes that you have the basics of C + + programming and can configure and compile ogre applications (if you have problems configuring your environment, see
OGRE + MinGW + code::blocks Environment Construction). In addition to configuring the environment, you do not need to have any knowledge about ogre.
IntroductionIn this tutorial, I will introduce you to the most basic structure in Ogre: Scenemanager, Scenenode, and entity object. We will not involve a lot of code, but rather the general concepts of the beginner ogre. When you are reading the process, You will slowly add code to your project and observe the resulting results. There is no better way to get acquainted with these concepts than the actual programming! So don't just read it.
Start
the original CodeIn this tutorial we will use a pre-constructed code structure. In addition to what we are going to add to the Createscene method, you should ignore the other code. In the tutorial that follows, we'll go deep into how ogre works, but now we're going to start at the most basic level. Create a project with the environment of your choice and add the following code: #include "ExampleApplication.h"
Class Tutorialapplication:public Exampleapplication
{
Protected
Public
Tutorialapplication ()
{
}
~tutorialapplication ()
{
}
Protected
void Createscene (void)
{
}
};
#if Ogre_platform = = Ogre_platform_win32
#define Win32_lean_and_mean
#include "Windows.h"
int WINAPI WinMain (hinstance hInst, hinstance, LPSTR strcmdline, int)
#else
int main (int argc, char **argv)
#endif
{
Create Application Object
Tutorialapplication app;
try {
App.go ();
} catch (exception& e) {
#if Ogre_platform = = Ogre_platform_win32
MessageBox (NULL, E.getfulldescription (). C_STR (), "An exception have occurred!", MB_OK | Mb_iconerror | Mb_taskmodal);
#else
fprintf (stderr, "an exception have occurred:%s",
E.getfulldescription (). C_STR ());
#endif
}
return 0;
When this program runs, use WASD to move, the mouse to rotate the angle of view, ESC key exit (like CS AH).
Problem AnalysisIf you're having problems, check to see if you've configured your environment, or view more detailed information in Ogre.log. If you need more help, search for forums or post queries. Before that, you should read the posting rules and give the Ogre.log, exception information, error message, debugging information and so on as detailed as possible. ,
Message Box
problemIf you use a VisualStudio that has Unicode support, you may get an error in the MessageBox (), there are two solutions: one is to change the MessageBox to MessageBoxA, and the other is to remove Unicode support from the project setup. .
missing configuration file or DLLIf you are running a newly compiled program with a missing DLL or configuration file (*.cfg), make sure you set the environment path [Projectfolder]/bin/release, [Projectfolder]/bin/debug.
resource or plug-in issuesMake sure that the plugins.cfg and resources.cfg are in the same directory as the executable file (or in the bin directory). Plugins.cfg tells Ogre which Render library is available (Direct3D9, OpenGL, etc.). Resources.cfg is a exampleapplication resource configuration file with textures, meshes, and scripts paths. Both are text files, so edit them and make sure the path is correct. Otherwise, your Ogre settings dialog will not have any rendering options, or you will see it on the screen or in Ogre.log:
Description:.. /.. /media/packs/ogrecore.zip-error whilst opening archive:unable to read zip file |
If this happens, open the Resources.cfg file and point the path to the Ogre Media folder. Note You cannot use any environment variables, such as $ (somevariable).
The application cannot be started in Visual StudioThe general situation is that the environment is configured so that some files cannot be found. A detailed workaround is not discussed in this article.
Ogre
How to workThis is a major topic. We start from scenemanagers until entities and scenenodes. These three classes are the basis for building a ogre application.
Scenemanager
BasicEverything that appears on the screen is managed by the Scenemanager (scene manager) (imagine). When you place objects in the scene, the Scenemanager records their positions. When you create a camera to observe the scene (which we'll refer to later in the tutorial), Scenemanager records them. When you create plane, billboard, light ... And so on, Scenemanager also recorded them. There are many kinds of scenemanager. There are rendering of the terrain, there are rendering the BSP map, and so on. You can see a variety of different types of scenemanager here. In the course of these tutorials, we will involve other scenemanager.
Entity
BasicAn entity is an object that you can draw in a scene. You can think of the entity as a substitute for mesh. A robot is an entity, a fish is an entity, and the terrain in which your character is walking is also a huge entity. Light (lights), Billboard (bulletin board), particle (particle), camera (camcorder) and so on is not the entity. One of the things you need to be aware of in Ogre is that it separates the objects that can be rendered from their positions and orientations. This means that you cannot put an entity directly into the scene. So you have to have the entity attached to a Scenenode object, and this scenenode contains information about the position and direction.
Scenenode
BasicAs stated above, the Scenenode (Scene node) records the position and direction of all objects attached to it. When you create an entity, it is not drawn until you bind it to a scenenode. Similarly, a single scenenode is not an object that can be displayed on the screen. Only if you create a scenenode and bind an entity (or other object) on it, can it really show up on the screen. Scenenode can bind a number of objects. For example, you have a character walking around the screen, and you want the lights to surround him. The way to do this is to first create a scenenode, then create a role entity and bind to the Scenenode. Next industry you want to create a light object and bind it to Scenenode. Scenenode is also tied to another scenenode, which allows us to create hierarchical nodes. We'll cover the advanced attached approach to Scenenode in the later tutorials. You have an important aspect to note about Scenennode, that is, the location of Sencenode is always relative to its parent scenenode, and each Scenemanager All contain one root node to bind all other scenenode.
your first Ogre applicationNow go back to the code we created earlier. Locate the Tutorialapplication::createscene member function. In this tutorial we will only take advantage of the contents of this function. The first thing to do is set the ambient light for the scene so that we can see what we're doing. We can do this by calling the Setambientlight function and specifying the color. Note that the Colourvalue's constructor expects a range of values ranging from 0 to 1 for each component of the red and green blue. Add this line in Createscene: Mscenemgr->setambientlight (Colourvalue (1, 1, 1)); The next thing we need to do is create an entity. We can call Scenemanager's createentity member function: Entity *ent1 = mscenemgr->createentity ("Robot", "Robot.mesh"); OK, there are a few problems. First of all, where did mscenemgr come from? And what are the parameters of the function we call? The mscenemgr variable is contained in the current Scenemanager object (inherited from the Exampleapplication class). The first parameter of Createentity is the name of the entity we created.
All entity must have a unique name. If you try to create two entity with the same name, you will receive an error. The "Robot.mesh" parameter specifies the mesh (mesh) we use for entity. Similarly, the grid has been preloaded by the Exampleapplication class. Now that we've created one, we need to create a scenenode to bind . because each scenemanager has a root scenenode, We're going to create that node's sub-node: scenenode *node1 = mscenemgr->. Getrootscenenode ()->createchildscenenode ( "Robotnode" ); This long statement first invokes the current Scenemanager's getrootscenenode. The Createchildscenenode method of the Scenenode is then called. The Createchildscenenode parameter is the name of the Scenenode we created. Like the entity class, two scenenodes cannot have the same name. Finally, we need to bind entity to Scenenode so that the robot has a drawing position: node1->attachobject ( ent1 ); All right! Compile and run your program. You should see a standing robot on the screen. Note: Robot.mesh is not in Ogrecore.zip. Follow this tutorial to do this and your program may run without displaying anything. I found that adding the appropriate path in RESOURCES.CFG will make the program work.
Filesystem=. /.. /media/materials/programsfilesystem=. /.. /media/materials/scriptsfilesystem=. /.. /media/materials/texturesfilesystem=. /.. /media/models |
coordinate systems and vectorsBefore we go any further, we need to talk about the screen coordinate system and the ogre vector object. Ogre (like many other graphics engines) use the X and Z axes as the horizontal plane and the Y axis as the vertical axis. When you look at the screen, the x-axis should be left-to-right, and the right side is the square of the x-axis. The y-axis is from bottom to top of the screen, and the upper end is the positive direction. The z-axis is from inside to outside, and the opposite side of the screen is the positive direction. Why is the robot facing the X axis in the right direction? That's the grid's own property, because it's the way it is. Ogre does not set the orientation of your model. Each grid you load will have a different orientation. Ogre uses the vector class to represent the position and direction (no point Class). The vectors are 2 (VECTOR2), 3 (Vector3), and 4 (VECTOR4), of which Vector3 is the most commonly used. If you are unfamiliar with vectors, I suggest that you review them before you study ogre carefully. The mathematical knowledge associated with vectors is very useful when you are doing complex programs.
add to other objectsNow that you understand the coordinate system, we can go back to our code. In the three lines of code we wrote, we didn't set the location where the machine appeared. Most ogre functions have default values for parameters. For example, the Scenenode::createchildscenenode member function in Ogre has 3 parameters: Scenenode's name, Scenenode position, and the initial direction. As you can see, the position has been set to (0, 0, 0). Let's create a scenenode, but this time we specify the location: Entity *ent2 = mscenemgr->createentity ("Robot2", "R Obot.mesh ");
Scenenode *node2 = Mscenemgr->getrootscenenode ()->createchildscenenode ("RobotNode2", Vector3 (50, 0, 0));
Node2->attachobject (ENT2); this looks very familiar. We did the same thing as before, except two. First, we have a slightly different name for entity and Scenenode. The 2nd is that we specify the position in the x-axis distance from the root Scenenode 50 units (remember that all scenenode locations are relative to the parent node). Compile and run the demo. Now there are two side-by robot.
deep understanding of entityEntityThe application of the class is very extensive, here I will not cover all the use method ... Just enough for you to get started. I would like to point out that there are some functions that can be used directly in Entity. The first is entity::setvisible and entity::isvisible. You can set any entity to be visible by calling this function. If you want to temporarily hide an entity, calling this function is much better than destroying it and rebuilding it. Note that you do not need to cache these entity. Each object's network and texture will only load in memory once, so you don't have much to save as you try to keep them. The only savings is the amount of time you spend creating and destroying an entity object. The GetName function returns the name of the entity, and Getparentscenenode returns the Scenenode that the entity is bound to.
Explore ScenenodeScenenodeVery complex. A lot of things can be done by Scenenode, but we only deal with the ones that are most commonly used. You can get and set the Scenenode location (always relative to the parent scenenode) by GetPosition and SetPosition. You can use the Translate method to move it relative to the current position. The Scenenode not only sets the position, but also allows the object to be retracted and rotated. You can set the proportions of an object by using the scale function. You can use yaw, roll, and pitch functions to rotate objects. You can call Resetorientation to reset the object's orientation. You can also use SetOrientation, getorientation, and rotate functions for more advanced rotations. We will not refer to quaternion (four yuan) before the final part of the tutorial. You've seen the Attachobject function. These related functions are also useful if you want to manipulate objects bound to Scenenode: Numattachedobjects, Getattachedobject (This function has many versions), Detachobject (also multiple), Detachallobjects. There is also a complete set of functions to handle parent-child nodes. Because all the positions and movements are relative to the parent node, we can easily move two scenenode together. We now have this code in the program: Entity *ent1 = mscenemgr->createe Ntity ("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 line 5th from: scenenode *node2 = mscenemgr->getrootscenenode ()- >createchildscenenode ( "RobotNode2", vector3 ( 50, 0, 0 ) ); Change to this: scenenode *node2 = node1->createchildscenenode ( "RobotNode2", vector3 ( 50, 0, 0 ) ), so we turn RobotNode2 into Robotnode's child nodes. Node2 also moves as you move Node1, but moving node2 does not affect Node1. For example, this code can only move robotnode2: node2->translate ( Vector3 10, 0, 10 ) ); The following code will move Robotnode, and RobotNode2 will move because RobotNode2 is a robotnode child node: node1->translate ( vector3 ( 25, 0, 0 ) ); If you're not clear, the simplest way is to start down from the root node. Like this, we move the Node1 from (0, 0, 0) to (25, 0, 0), thus Node1 's position is changed from the parent to (25,0, 0). Node2 starts from (50, 0, 0) and moves (10, 0, 10), so its new position relative to the parent is (60, 0, 10). Now we figure out where these things really are. Start with the root scenenode. It is the position always (0, 0, 0). Now, the location of the Node1 is (root + node1): (0, 0, 0) + (25, 0, 0) = (25, 0, 0). It's not surprising. Node2 is the child node of Node1, so its position is (root + Node1 + node2): (0, 0, 0) + (25, 0, 0) + (60, 0, 10) = (85, 0, 10). This is also a good example of explaining the hierarchical structure of scenenode. You will rarely need to calculate the absolute position of your nodes. Finally, note that you can get scenenode and entity by calling Scenemanager's getscenenode and getEntity methods, So you don't have to save a pointer for each scenenode you create. But you can save the one you use most often.
TryBy now you should have mastered entitiy, Scenenode, and Scenemanager. I recommend adding or removing robots from the scene based on the code above. When you're done, empty all the contents of the Createscene method and play the following code snippet:
ZoomYou can zoom the grid by calling the scale method in Scenenode. Try changing the value of the scale parameter to see what you can get:
RotateYou can use the Yaw,pitch and roll method to rotate the object, and the roll method may use degrees or radian units. Pitch is the rotation around the x-axis, yaw is the rotation around the y-axis, and roll is the rotation around the z-axis. Use your right hand to do a demonstration: Put your thumb, index finger and middle finger each 90 degrees, and then the thumb to the right x axis, do a variety of rotation test. Try changing the angle and combining multiple transformations: 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));
Ogre
EnvironmentMost of the files referenced in this tutorial (. DLL and. CFG) can be found in debug or release under Ogresdk's "Bin". The debug program you create should use the files in the Debug folder, The release program should use the Release folder. Note the terminology under many specific Windows is discussed here. Linux is the same, but the shared library file ends with. So and in other places, something is slightly different. If you have a problem, you can post it to the Ogre Forum help section.
DLL
and Plugins Now that we have been exposed to some ogre environment, I would like to explain in general how the Ogre Library works, and how to apply it more conveniently. Ogre is divided into several groups of shared library files. The first group is the link library itself and the libraries it relies on. The Ogre Library is contained in the OgreMain.dll. This DLL requires some other link libraries, such as Cg.dll. These DLLs must be included in each ogre application, without exception. The second set of link libraries is a plug-in. Ogre the various functions reasonably assigned to the link library, because they can be based on the needs of your program to join. The file name of the basic plug-in in Ogre begins with "Plugin_". If the program needs to, you can create a new plugin, but we will not cover this in the tutorial. Ogre also uses plug-ins for rendering (such as OpenGL, DirectX, etc.). These plugins are prefixed with "rendersystem_". They are already available, and you can add or remove them from your program. This is useful when you write OpenGL shaders or special applications and want to turn off the ability to run under DirectX, and you can simply remove the corresponding Rendersystem plugin so that it becomes unusable. Also, if your target platform is non-standard, you can write your own Rendersystem plugin, but this will not be covered in the tutorial. We'll mention how to remove the plugin in the next section. The third set of link libraries is a third-party library and other helpful libraries. Ogre it itself is just a graphics rendering library. It doesn't contain things like GUI systems, input controls, physics engines, and so on. You have to use other link libraries to do these things. The Ogre Demos and SDK include some of these third-party libraries. The Cegui library is a GUI system that can be easily integrated with Ogre, whose DLLs start with "cegui*" and "OgreGUIRenderer.dll" is part of it. The use of Cegui will be covered in later tutorials. Keyboard and mouse input is done via OIS (an input system). It is included in the OIS.dll. Some libraries (not included in the SDK) can provide more features (such as sound and physics engines), and you can find more information on wikis and forums. The idea is that when you test your program in Ben, you can "open" everything. is not to remove anything). When you are ready to publish your program, you need to generate it in release mode, including the release DLL that your program uses, and remove the DLLs you can't use. If your program doesn't use Cegui but uses OIS, itYou should not include Cegui DLLs, but must contain OIS DLLs, or your program will not run properly
configuration fileOgre has many configuration files. They control which plugins are loaded, where the program resources are loaded, and so on. I seem to look briefly at what each configuration file does. If you have more detailed questions, you should go directly to Ogre's Help forum.
plugins.cfgThis file contains the plugins that your program uses. If you want to add or remove a plugin, you need to edit the file. To remove a plug-in, just delete the corresponding line or add # at the beginning of the comment off. To add a plugin, you need to add a line like this: "Plugin=[pluginname]". Be careful not to forget to add the end of the plugin name. DLL. Your plugin does not force the beginning of "Rendersystem_" or "Plugin_". You can also find the directory for the plugin by changing the "pluginfolder" variable definition ogre. You can use absolute or relative paths, but you cannot use environment variables, such as $ (somevariable).
resources.cfgThis file contains a list of paths to the Ogre lookup resource. Resources include scripts, grids, textures, and so on. You can use absolute or relative paths, but you cannot use environment variables, such as $ (somevariable). Note that ogre does not scan subdirectories, so if you have multi-level catalogs, you must manually add them. For example, you have such a directory tree, "Res/meshes" and "Res/meshes/small", you have to add two lines of entries in the configuration file that contain these paths.
media.cfgThis file tells ogre more detailed resource information. It seems you don't need to edit this file now, so I'll skip the details. More information can be found in the user manual and Ogre forums.
ogre.cfgThis file is generated by the Ogre Configuration dialog box. This file can set your computer and graphics options. You should not distribute this file to other people when sharing your program, because their settings may be different from yours. Note that you should not edit this file directly, but should use the Configuration dialog box.
quake3settings.cfgThis file is used by Bspscenemanager. You don't need this file, unless you use the Scene manager (now you don't), so ignore it. You should not send this file, unless you use Bspscenemanager, even when your program determines that it may be completely different from the present. These are the configuration files that the ogre directly manipulate. Ogre must be able to find "plugins.cfg", "Resources.cfg", and "media.cfg" to function properly. In the following tutorial we will cover how to change the location of these files and manipulate them to complete more advanced features.
a better way to test your program
Note that this section is about Windows and Visual C + + (Xoyojank: So I'll just mention it roughly)It says that you want to add the path to the Bin folder to the project settings, or the program will not find files such as DLLs. It is more convenient to copy the DLL into the program directory each time than it is windows/system32/. Slightly
SummaryHere you should have a preliminary grasp of Scenemanager, Scenenode, and Entity classes. You don't have to be familiar with all the functions I've introduced. Since they are the most basic objects, we will often use them. You'll learn more about them in the next tutorial. You should also be familiar with configuring the ogre environment. Basic Tutorial 2
cameras, lights, and ShadowsFinally, the translation is finished. In fact, Ogre 3D Chinese (http://ogre3d.cn) has been translated, just removed some things. I translate this, also want to have some exercise to oneself. Hope to continue to learn, you can refer to: http://ogre3d.cn/wiki/ INDEX.PHP?TITLE=%E6%96%87%E6%A1%A3:%E6%95%99%E7%A8%8B:%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/HTTP Www.ogre3d.org/wiki/index.php/Ogre_Tutorials
If you are using OGRESDK under Windows, be sure to add this directory "[Ogresdk_directory]/samples/include" to the project's include path (the ExampleApplication.h file is loaded from this location). If you are using the ogre version of the source code, you need to include the "[Ogresource_directory]/samples/common/include" directory. Confirm that you can compile and run the code before proceeding to the next section, although there is nothing outside of a blank screen with frame-speed frames. We will add some objects to display later in this tutorial.
"Reprinted" Ogre:beginner Tutorial 1:scenenode, Entity, and Scenemanager structure