You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Qt Creator导入OBJ文件失败求助(v4.10.0基于Qt5.13.1)

Fixing OBJ Model Loading Issues in Qt Creator's customitemgraph Example

Hey there, let's break down what's going on with your OBJ model loading problem and walk through the solutions!

Why Some OBJs Work and Others Don't

The root issue here is that the customitemgraph example's OBJ parser expects every face entry to follow the full f v/vt/vn format (vertex position index / texture coordinate index / normal index).

Looking at your code snippets:

  • Working OBJ: Uses f 1/1/1 2/2/2 — every vertex has all three components.
  • Broken OBJ: Uses f 225536//133387 — the middle part (texture coordinate index) is missing.

When the parser tries to read that missing texture coordinate index, it's trying to access a position in a QVector that doesn't exist, hence the "index out of range" assertion error. Your attempt to replace // with / didn't work because you still didn't provide a valid texture coordinate index between those slashes.

Solutions to Fix This

1. Fix the OBJ File (Add Texture Coordinates)

The easiest way if you don't want to touch code is to补全模型的纹理坐标 using a 3D tool like Blender:

  • Import your broken OBJ into Blender.
  • Select the model, go to the UV Editing workspace, and unwrap the model (even a simple default unwrap works).
  • Export the model back to OBJ, making sure to check the option to include texture coordinates (usually labeled "UVs" or "Texture Coordinates" in export settings).

This will generate an OBJ with the full f v/vt/vn format that the example expects.

If you want to try a quick manual fix for small models:

  • Add a default texture coordinate at the top of your OBJ file:
    vt 0.0 0.0
    
  • Then replace every // in face entries with /1/ (since the new vt entry is index 1 in OBJ's 1-based numbering):
    // Before
    f 225536//133387 225537//133388
    // After
    f 225536/1/133387 225537/1/133388
    

Note: This is only practical for small models—large ones will take too long to edit manually.

2. Modify the Example's OBJ Parser Code

If you want to make the example support OBJs without texture coordinates, you can adjust the parsing logic to handle missing texture indices:

Find the part of the code where face entries (f lines) are parsed. It might look something like this:

QStringList faceParts = line.split(' ', Qt::SkipEmptyParts);
for (int i = 1; i < faceParts.size(); ++i) {
    QStringList vertexParts = faceParts[i].split('/');
    int posIndex = vertexParts[0].toInt() - 1; // Convert to 0-based index
    int texIndex = vertexParts[1].toInt() - 1;
    int normIndex = vertexParts[2].toInt() - 1;
    // ... use these indices to fetch data from vectors
}

Modify it to handle missing texture coordinates:

QStringList faceParts = line.split(' ', Qt::SkipEmptyParts);
// Add a default texture coordinate if none exist
if (texCoords.isEmpty()) {
    texCoords.append(QVector2D(0.0f, 0.0f));
}
for (int i = 1; i < faceParts.size(); ++i) {
    QStringList vertexParts = faceParts[i].split('/');
    int posIndex = vertexParts[0].toInt() - 1;
    int texIndex = 0; // Default to the first texture coordinate
    int normIndex = vertexParts.size() > 2 ? vertexParts[2].toInt() - 1 : 0;
    
    // Only use the texture index if it's not empty
    if (vertexParts.size() > 1 && !vertexParts[1].isEmpty()) {
        texIndex = vertexParts[1].toInt() - 1;
        // Ensure the index is valid
        if (texIndex >= texCoords.size()) {
            texIndex = 0;
        }
    }
    // ... proceed with using posIndex, texIndex, normIndex
}

This code falls back to a default texture coordinate when none is provided, preventing the QVector index out-of-range error.

3. Troubleshooting "Other Simple OBJs"

If your other simple OBJs also don't work, check their face entries—they're likely missing texture coordinates too. Use either of the above methods to fix them, and they should load correctly.

内容的提问来源于stack exchange,提问作者Sandra

火山引擎 最新活动