preamble
When the model is larger and lagging occurs, then use LOD (Level of Detail) for hierarchical detail adjustment to make the otherwise laggy model lag-free.
Ben is the LOD introduction.
Demo
LOD
summarize
LOD, also known as hierarchical detail modeling, is a real-time 3D computer graphics technique designed to improve rendering efficiency and performance by dynamically adjusting the level of detail of an object's rendering based on its position and importance in the scene.
When the viewpoint is close to the object, the details of the model can be observed in abundance; when the viewpoint is far away from the model, the details observed are gradually blurred. The system drawing program selects the corresponding details for display according to certain judgment conditions, thus avoiding the waste of time caused by drawing those relatively insignificant details, and at the same time effectively coordinating the relationship between screen continuity and model resolution.
Osg::LOG node
In OSG, the LOD technique is implemented through the osg::LOD node. osg::LOD node is a special scene node that can contain multiple child nodes, each representing a model with a different level of detail. Depending on the distance of the viewpoint from the object or the size of the pixels on the screen, the osg::LOD node selects the appropriate child node for rendering.
Addition of child nodes
Models with different levels of detail can be added as child nodes to the osg::LOD node via the addChild method. Each child node needs to specify a distance range (or pixel size range) within which the child will be rendered.
Distance and pixel size modes
The osg::LOD node supports two switching modes distance mode and pixel size mode.
- Distance mode: selects child nodes based on the distance from the viewpoint to the center of the object's enclosing box;
- Pixel size mode: selects child nodes based on the pixel size of the object on the screen.
Center Mode Setting
For distance mode, the osg::LOD node also supports two center modes: the center of the box and the center of the custom mode. The bounding box center mode uses the center of the object's bounding box as the point for calculating the distance; the custom center mode allows the user to specify a custom center point.
Advantages and applications of LOD technology
- Improved rendering efficiency: By dynamically adjusting the level of detail in the model, LOD technology can significantly reduce the number of polygons that need to be rendered, thus improving rendering speed.
- Optimize memory usage: Although the osg::LOD node in OSG loads all models into memory at once, it only draws them selectively, which still helps optimize memory usage because there is no need to allocate separate memory space for each model. In addition, OSG provides the osg::PagedLOD node, which supports dynamic paged loading, allowing model files to be loaded as needed to further optimize memory usage.
- Enhance visual effects: LOD technology can reduce the rendering burden by simplifying the model under the premise of ensuring visual effects, thus allowing developers to place more objects in the scene or realize more complex visual effects.
- Wide range of application scenarios: LOD technology is suitable for a variety of 3D scenes that require efficient rendering, such as urban planning, terrain rendering, and game development. In these scenes, the number of objects and the level of detail are often very high, using LOD technology can significantly improve the rendering performance and user experience.
Limitations of LOD technology
Despite its many advantages, the LOD technique has some limitations. For example, visual jumps may occur when switching between models with different levels of detail, especially when there is a large difference in the level of detail between two models. In addition, designing and managing models with different levels of detail requires a certain amount of time and resource investment.
(sth. or sb) else
The LOD technique in OSG is an efficient and flexible 3D rendering technique that optimizes rendering performance and memory usage by dynamically adjusting the level of detail in the model. The LOD technique is an indispensable tool when developing large-scale 3D scenes.
LOD implementation
Without going into much detail about the model, the key is the osg::Lod node and how to add it:
// Add mode, use line model in the range 0 to 100
pLod->addChild(pGeode, 0, 100);
// Add model, anything not in the scope of the setup is this one
// pLod->addChild(pGeode); // can't be used, it's expected to be used for anything that isn't set, but in fact it's practically useless, and doesn't show up anyway.
pLod->addChild(pGeode, 100, 1000);
Demo key source code
Drawing section
// Drawing
{
#if 1
// First model
for(int partIndex = 0; partIndex < kMode.listPart.size(); partIndex++)
{
// Create an object where the user keeps geometry information
osg::ref_ptr<osg::Geometry> pGeometry = new osg::Geometry;
// Create an array of four vertices
osg::ref_ptr<osg::Vec3Array> pVec3Array = new osg::Vec3Array;
// Add four vertices
pGeometry->setVertexArray(pVec3Array.get());
// Create four colors of data
osg::ref_ptr<osg::Vec4Array> pVec4Array = new osg::Vec4Array;
// Add four colors
pGeometry->setColorArray(pVec4Array.get());
// Bind the color
pGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
double r, g, b;
r = 1.0f;
g = 1.0f;
b = 0.0f;
for(int elementShellIndex = 0; elementShellIndex < kMode.listPart.at(partIndex).listElementShell.size(); elementShellIndex++)
{
// x y z
pVec3Array->push_back(osg::Vec3(kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n1).x,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n1).y,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n1).z));
pVec3Array->push_back(osg::Vec3(kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n2).x,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n2).y,
kMode.hashNid2Node.value(kMode.listPart.at(partIndex).listElementShell.at(elementShellIndex).n2).z));
pVec3Array->push_back(osg::Vec3