Chapter 4. Gaia Engine overview
The progress of reading is obvious, it may be that there are too many things these two days. However, this chapter is also too long, and there is a spell with the length of the top three chapters.
Snow said that the best learning engine method is to go to the source code. FT, I am watching the electronic version, where to find a set of CD-ROM? Please ask the friends who have this book to match the CD to pack email, please Top!
I can't see the code is painful, very painful ...
Meet Gaia, THE 3D TERRAIN ENGINE
How good is the name of the people - the mother of the earth, the mother of the earth. One is dry terrain. Snook said that it is still very important to get engine naming, which can be used to distinguish between its own defined stuff and DX original Dongdong. I feel that there is no name without the name.
Moreover, if an engine uses a third-party library to achieve, for example, 3D sound or physical simulation, there will be a conflict on code, so, the best is to use a Namespace to pack the engine, one hundred.
If you don't use NameSpace, you will add a GL in front of each function like OpenGL. Every A ... a lot. So use NameSpace or a very good choice.
The application host
The CGAMEHOST class is a class based on CD3DAPPLIATION. It is a single, which is the core of the engine.
In CGAMEHOST, there is also a part of the control Ticks, that is, the update speed of the engine is fixed. Snook gave a cgamehost :: Framemove code, in addition to implementing Ticks, it is also useful to adjust the priority of the current thread to avoid engines that have always been a CPU, quite considerate.
Creating pools of data
Write yourself for Data Pool? Actually there is a TERRAIN ENGINE and a memory management component, this GAIA is quite formulated.
Why do you want this POOL? Because when alloc is too much, it is very efficient to apply directly to memory directly. The more ALLOC, the lower the efficiency. Then, the best way is to pre-apply for a large memory space to make a pool, you can see this pool as a container, and after alloc can be applied in this pool, no longer apply to memory. This efficiency is much higher.
This Pool has a contradiction in design: To ensure the type of Pool, that is, different types are represented by different template classes. However, there is a need for a unified interface to perform REQUEST and RELEASE operations. The solution is simple, build a Common Interface, all the Template Class inherits and extension from this interface, OK.
Making Pool's easiest way is to use Array, make a large enough Objects type Array. Such efficiency is high, but how it is big enough to know in advance, saying that you know the maximum number of objects Objects. You can use this if you can know the maximum number in advance. Otherwise, Array is not flexible enough.
The method used by GAIA is this: There is a template class called CDataPool, with a list of tables, called cpoolgroup. Each element of this linked list is an Array. This makes a balance in efficiency and flexibility.
There is still a benefit so that a cdataPool can be unique to one array, and the index does not change. In fact, CDATAPOOL can be seen as a 2D number of groups, of course, with ordinary Array. How to quickly find an Available MEMBER (memory area)? Snook adds a linked list in each CDataGroup to store the index of the Available MEMBER. Places often index use array, where you often increase, will use Linked List. In the fixed place, use a Linked List to dynamically use. This truth has learned when you learn DS, but it is really possible to get through people. Look at this Data Pool design, you know that you are still very tender ...
Managing Shared Data Resources
In the engine, some Dongdong can be shared by many Game Objects, such as Testure, Vertex Buffers, INDEX BUFFERS, RENDER METHOD, and more.
In GAIA, use CResourcePoolItem this base class to describe the shared resources. This is of course also to make all resources can have a Common Interface. These interfaces include reading and writing of generating, destroying, allowing, disabling, and disk files. These interfaces are of course Virtual, waiting for other classes to inherit.
CRESOURCEPOOL is a Class of CDataPool for managing CRESOURCEPOOLITEM.
Each shared resource has a text string to record the absolute path of this resource. And GAIA also uses STL Map in CResourcePool to make mappings, corresponding to the Text String and resources. STL's MAP is written by RB Tree, o (logn) efficiency.
Finally, be a monomer of CRESOUCEPOOLMANAGER to manage Resouce Pools. CResourcePool can register in CRESOUCEPOOLMANAGER and label the type of resources, such as sound, animation, and more.
The resource file is first divided into different families, and then there are different types (Types) in each family. The type of resource uses 32BITS, Family and Type each account for 16bits, called CRESOUCECODE. When you register, you will provide the resource CRESOUCECODE to help Manager find the correct interface.
The Resouce Base Class
CResourcePoolItem is an abstract class that only provides an interface (pure virtual function), which requires other classes to inherit and implement specific methods.
Where disableresouce and restoreresouce are when Device Lost, destroy all devices related objects, then wait until Devie Restore recover.
D3DX also provides Resource Manager. When you use D3DPOOL_MANAGED, DX will provide automatic management when you use D3DPOOL_MANAGED.
But this is not applicable to dynamic resources, such as Texture or Geometry, if you need to calculate frequently, it is best managed. So, writing Resource Manager in Gaia is not repeated labor. Conversely, for static data, you can also select DX to manage or manually manage when the base class is overwritten. Well, this is really flexible.
TEXTURE Resources & Surface Materials
The simplest resources are not Ctexture because it is too common. And this class is a simple package for iDirect3dTexture9. For CsurfaceMaterial, it is an improvement to D3DXMATERIAL. Because Material can only specify a Texture once a time. This is still applicable to many years ago, but now it is obvious. So CsurfaceMaterial supports 16 textures. 16? Now the graphics card is not supported ... Yes, Snook uses a method to achieve multi-texture multi-textures multi-passs, which is capable. 16 Snook is too small ...
Render Method Resources
GAIA encapsulates ID3Dxeffect, which is the CeffectFile class. In addition to the basic ID3DXEffect interface, the CeffectFile class also provides a variable operation, of course, is also encapsulated with ID3dxeffect and ID3DXBaseEffect (Snow wrote ID3DXEFFECTBASE, and I checked me for half a day MSDN).
Because for the variables of the Effect Files, the best way is to use Handle. So, you can define some semantics, then make their handle into List, which is very efficient.
In addition, a CEFFECTFILE linked list constitutes a crenderMethod to describe the rendering of a scene. And in CRenderMethod, there is also a CSURFACEMATERIAL to depict the entire rendering process.
Index & Vertex buffers
IB and VB are too common. As the most important data of GAIA, GAIA's full model retains IB and VB. IB and VB are very useful for dynamic data, but be careful. Using IB and VB, you can build geometries, and you can also animate geometry already existing. These two situations in GAIA exist.
The so-called dynamics is actually replaced. To lock IB and VB with LOCK to change the value in most graphics cards is very poor. Once updated, it will be replaced. This gives dynamic data a single line: can only go to the graphics card, can no longer return to the memory (read back to update some data).
The basic class is CvertexBuffer and CindexBuffer. These buffers may be ordinary or dynamic.
How can I achieve dynamics? NV and MS proposed a method. For example, there is N top points, you want to move M Frame, then declare an IB of M * N, so that the first frame uses 0 to N-1, the second frame is used with n to 2n-1, and so on.
Model Resources
CMODELRESOURCE used to represent the model should be the most important resource. In its interior, there is a hierarchy tree of D3DXFrame. Because there is a lot of vertices in the scene, a cmodelresource can even represent multiple objects in the scene. For example, a competitive man model, in addition to your own bones, there are muscles and clothes, and weapons and armor, each mesh is CrenderMethod and CsurFacematerial to describe. Then made them into an entity, which is CmodelResource (a CModelResource represents multiple objects).
Chapter 2 said that D3DXFrame and D3DXMeshcontainer can be extended. Here is expanding them. By expanding with CeffectFile and CsurfaceMaRialial, Snow turns a single texture of Mesh into a Mesh that can be rendered by HLSL and supporting multi-texture. By expanding the X file, CMODELRESOURCE also implements File I / O. D3DX provides the three interfaces of ID3DXAllocateHierarchy, ID3dxLoadUserData, and ID3DXSAVEUSERDATA to implement user extension. What you need is to inherit the base class and then turn the pure virtual function is OK.
Scene Nodes & Objects
CSCENENode defines a specific coordinate system in a 3D environment (?). These nodes are connected together through the inheritance relationship between "Double Friends - Child". All Object in the scene is inherited from cscenenode, including csceneObject.
CsceneObject is based on CSCENENODE, but uses a bounded box around Node to define a volume. CSceneObject can be used to represent a model in a game, in which this box can be rented to represent this object. (This should be used to make collision tests)
The Render Queue
The main significance of this render pipeline is to reduce the change of Render State, because you may render a lot of Effect File in a frame, then you have to set the same render state, and set the same value, then it The same value is written repeatedly.
With Render Queue, we can remove excess State changes, you can manage these stuff in Models, VB, IB, and Texture.
Rendezed Object first enters the render queue, then Queue will be sorted and finally rendered.
Display an Object requires three stuffs: Geometry, Material, and Rendering Method, there are also some parameters, such as the translation matrix. Because each resource has a fixed index, then three indexes can represent the MEMBER in Queue.
If you sort these three index, the most important thing is EFFECT FILE, then Geometry, and finally MATERIAL.
The three index constitute a 48BITS value, which is to use this value to sort the Member in Queue. In this way, the geometry using the same Effect File is ranked together, and then in these, all of them are divided with the same matials. (It is a multi-keyword sort) that is very efficient.
CrenderEntry Code expands this idea, it uses 20bytes. This can not only store those values (12Bytes), but also provide more information. This information includes a callback function (Callback function) and user-defined parameters. This callback function is used to trigger. Render Queue collects all Entry and follows Priority, finally rendered and triggered the callback function.
That 12bytes is used to store information about Geometry, Material, and Rendering Method. In addition to INDEX, each additional parameter is more, for this parameter, different resources have different purposes. After reading this chapter, it has a preliminary understanding of Gaia. To be honest, from these types of design, I completely see Snook, whether it is a deep accumulation of software development or on instant rendering. I have never understood where the business games are complicated. It is now indeed too complicated, it is no wonder that there is such a high efficiency.