Irrlicht is an open source 3D engine that supports multiple graphics libraries across platforms. The Design of irrlicht is a good embodiment of the interface and implementation separation principle. The entire framework design is exquisite and clear, and it is a good teaching material for learning 3D engine design.
Let's take a look at the namespace division. We can see that Irr has a very clear structure:
IRR |
Everything in the irrlicht engine can be found in this Namespace |
IRR: Core |
In this namespace can be found basic classes like vectors, Planes, arrays, lists and so on |
IRR: Gui |
The GUI Namespace contains useful classes Easy creation of a graphical user interface |
IRR: Io |
|
IRR: Scene |
|
IRR: Scene: quake3 |
|
IRR: Video |
The video Namespace contains classes Accessing the video Driver. All 2D and 3D rendering is Done here |
The IRR space provides support for some underlying architectures of the engine, such as the event processing system, operating system abstraction, reference counting, and device abstraction.
IRR: The core space provides basic mathematical and data structure components, such as vectors, matrices, semicolons, lists, arrays, and red/black trees.
IRR: Io space provides components for Io operations and supports object property serialization and XML.
IRR: The video space contains 3D graphic function interfaces and components. For example, image-driven and material rendering abstraction.
IRR: Scene space contains 3D engine rendering and Management Based on Scene Graph, and provides rich support for camera, bone animation, particle system, and so on.
IRR: the GUI space contains the irrlicht GUI module. It is also an interface-based design.
Me
What I want to focus on is IRR: video and IRR: scene. This is the core part of the 3D engine. It aims to analyze how irrlicht abstracts different graphic APIs,
And how to use scene
Graph-material systems run engines and integrate various 3D technologies into these systems. In addition, how IRR achieves cross-platform support is also analyzed.
As an engine overview, first look at the short example in the document:
Irrlichtdevice * Device = createdevice
(Video: edt_direct3d8
,
Core: dimension2d (640,480 ));
Video: ivideodriver * driver = device-> getvideodriver ();
Scene: iscenemanager * scenemgr = device-> getscenemanager ();
The three statements show the three most basic irrlicht objects-devices, graphics drivers, and scenario managers. Simply put, you can use the global function createdevice to create a device based on the specified driver type. Then, you can get the driver object and scenario manager object through the device object.
Device-> setwindowcaption (L "Hello world! "
);
The device object has some device-related capabilities. For example, you can set the window title here.
// Load and show quake2. md2 Model
Scene: iscenenode * node = scenemgr-> addanimatedmeshscenenode (scenemgr-> getmesh ("quake2model. md2"
));
// If everything worked, add a texture and disable Lighting
If
(Node)
{
Node-> setmaterialtexture (0, driver-> gettexture ("texture.bmp"
));
Node-> setmaterialflag (Video: emf_lighting
, False
);
}
// Add a first person shooter style user controlled camera
Scenemgr-> addcamerascenenodefps ();
These lines of code show scene
Iscenenode is the abstraction of the basic components of the irrlicht scenario. An animated mesh node and a camera node are added here. Node
It has material attributes (of course, camera does not need materials, but iscenenode should take into account all kinds of nodes as an abstraction. In order to make the interface level simpler, this is also a practical method.
Method)
// Draw everything
While
(Device-> Run () & driver)
{
Driver-> beginscene (true
, True
, Video: scolor (0,255 ));
Scenemgr-> drawall ();
Driver-> endscene ();
}
These lines are the process of running the engine. The first condition for the engine to run is that the device is still running: Device-> Run (). You can disable the device through closedevice.
Scenemgr-> drawall () executes rendering of all nodes. node rendering is not only draw, node transform calculation, but also animation computing (including skeleton Movement
Operations on special nodes such as camera (Special nodes and light sources) are included in the rendering scope, called in drawall in a unified manner, and various nodes implement their respective rendering, final
Scenemgr is centrally managed (such as sorting) Rendering, which is one of the features of the engine based on the Scene Graph architecture.
Device-> drop ();
This statement reflects the reference counting mechanism of irrlicht, and manages the number of object references through grab and drop. If everything is normal, the reference drop is 0, the device destroys itself, and exits the program.
Slave
In this very short example, we can see the basic components and running processes of irrlicht. Irrlicht provides scene management mechanisms and provides a large number of predefined scene
Nodes implements many functions, so that you can easily use the engine. The advantage of this mechanism is that nodes can be constantly expanded and reused in future projects.
Of course, the irrlicht engine is not perfect. At the end of the source code analysis series, I will analyze some specific problems and shortcomings of irrlicht. Of course, as an open-source engine, irrlicht is already very good, my main goal is to learn irrlicht through analysis to lay a foundation for designing a 3D engine.