From fa94700e5cc9df33a0e9f2944b558b685a5350bc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 9 Sep 2010 10:09:31 +0000 Subject: [PATCH] From Tassilo Glander, "I want to submit a fix for the plugin to load .x model files (Direct X). The current version crashes when encountering global materials, as also reported in the forum by the author of the plugin. The problem in mesh.cpp (app. ln 247) is, that references to global materials that are given in curly brackets {} are not supported by the reader. However, curly brackets seem to be common, according to Bourke. Unfortunately, I found no specification. However, also the DirectX model viewer that comes with the DirectX-SDK (August 2009) expects curly brackets and refuses models without them. My fix checks 2 more cases ("{ aMaterial }" -> 3 tokens and "{aMaterial}" -> 1 token), and extracts the material name for the lookup. I don't know if this is the most elegant solution, but the tokenizer seems to split based on white spaces. You can reproduce the bug with the attached model (box.x), which loads fine in other tools, such as 3DSmax, DeepExploration or the DirectX model viewer. When I remove the curly brackets at the reference of "myGlobalMaterial", it loads in osgviewer, but is not standard conform. " --- src/osgPlugins/x/mesh.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/osgPlugins/x/mesh.cpp b/src/osgPlugins/x/mesh.cpp index 6eae7d3e2..f1c8128b6 100644 --- a/src/osgPlugins/x/mesh.cpp +++ b/src/osgPlugins/x/mesh.cpp @@ -244,7 +244,20 @@ void Mesh::parseMeshMaterialList(std::istream& fin) // check for "{ }" for a // material which was declared globally - Material * material = _obj->findMaterial(token[0]); + string materialName = token[0]; + // could be given as "{ someName }" which more than 1 tokens + if (materialName == "{" && token.size()>1) + { + materialName = token[1]; + } + // or could be given as "{someName}" which would be in token[0] + else if (materialName.size() > 2 && materialName[0] == '{' && materialName[materialName.size()-1] == '}') + { + // remove curly brackets + materialName = materialName.substr(1, materialName.size()-2); + } + Material * material = _obj->findMaterial(materialName); + if (material) { _materialList->material.push_back(*material);