20,741
edits
m (→Approach) |
m (→Approach) |
||
| Line 33: | Line 33: | ||
{{WIP}} | {{WIP}} | ||
By looking at existing code, we can learn more about the SimGear/FlightGear APIs for loading 3D models from the base package. A commonly-used idiom can be seen in the model-manager[https://gitorious.org/fg/flightgear/source/4038ba3d511f887f0f166d8f60eed4a322e96e66:src/Model/modelmgr.cxx#L66] (slightly modified for clarity): | |||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
try { | |||
const char *model_path = "Models/Geometry/glider.ac"; | |||
std::string fullPath = simgear::SGModelLib::findDataFile(model_path); | |||
osg::Node * object = SGModelLib::loadDeferredModel(fullPath, globals->get_props()); | |||
} catch (const sg_throwable& t) { | |||
SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << model_path << ":\n " | |||
<< t.getFormattedMessage() << t.getOrigin()); | |||
return; | |||
} | |||
</syntaxhighlight> | |||
Next, we'll need to ensure that read/write permissions (as per [[IORules]]) are properly handled by our new code, which is commonly done using <code>fgValidatePath()</code>: | |||
<syntaxhighlight lang="cpp"> | |||
if (!fgValidatePath(file.c_str(), false)) { | |||
SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied " | |||
"(unauthorized access)"); | |||
return false; | |||
} | |||
</syntaxhighlight> | |||
Once those two snippets are integrated, we end up with something along these lines: | |||
<syntaxhighlight lang="cpp"> | |||
const char *model_path = "Models/Geometry/glider.ac"; | |||
if (!fgValidatePath(mode_path, false)) { | |||
SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied " | |||
"(unauthorized access)"); | |||
return false; | |||
} | |||
try { | |||
std::string fullPath = simgear::SGModelLib::findDataFile(model_path); | |||
osg::Node * object = SGModelLib::loadDeferredModel(fullPath, globals->get_props()); | |||
} catch (const sg_throwable& t) { | |||
SG_LOG(SG_AIRCRAFT, SG_ALERT, "Error loading " << model_path << ":\n " | |||
<< t.getFormattedMessage() << t.getOrigin()); | |||
return; | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Next, we only need to coax our new Canvas element to use said <code>osg::Node</code> object | |||