Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Model class SDKMESH loader #13

Closed
omml opened this issue Jul 17, 2015 · 11 comments
Closed

Bug in Model class SDKMESH loader #13

omml opened this issue Jul 17, 2015 · 11 comments
Assignees
Labels

Comments

@omml
Copy link

omml commented Jul 17, 2015

I think I found a bug when loading and rendering the island.sdkmesh using a Model class object. The island.sdkmesh model has two mesh parts but only one is rendered.

@walbourn
Copy link
Member

I assume you mean the island.sdkmesh from the legacy DirectX SDK. It contains only a single mesh(based off using sdkmeshdump.cpp)

SDKMESH C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Samples\Media\IslandScene\island.sdkmesh
    Version 101, LittleEndian
    1 meshes, 1 VBs, 1 IBs, 2 subsets, 1 frames, 2 materials
    VB #0 - 55530 vertices, 32 stride
        "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0
        "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12
        "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24
    IB #0 - 55530 indices (18510 faces) IT_32BIT
          1:    0, 1, 2
          2:    3, 4, 5
          3:    6, 7, 8
          4:    9, 10, 11
          5:    12, 13, 14
     ...
      18506:    55515, 55516, 55517
      18507:    55518, 55519, 55520
      18508:    55521, 55522, 55523
      18509:    55524, 55525, 55526
      18510:    55527, 55528, 55529
    Meshes
     Mesh #0 ''
        1 VBs [ 0 ], 0 IB, 2 subsets, 0 bones
        Subsets (2):
         Subset 0 ''
             material 0, TriangleList, start 0, count 20640
         Subset 1 ''
             material 1, TriangleList, start 0, count 34890
    Materials
     Material 0 ''
         Diffuse: 'grass_turf_v2_tex.dds'
         A: (0.000000, 0.000000, 0.000000, 1.000000)
         D: (0.223529, 0.682353, 0.000000, 1.000000)
         S: (0.000000, 0.000000, 0.000000, 1.000000) ^3.200000
         E: (0.000000, 0.000000, 0.000000, 1.000000)
     Material 1 ''
         Diffuse: 'island_v1_tex.dds'
         A: (0.000000, 0.000000, 0.000000, 1.000000)
         D: (0.662745, 0.541176, 0.380392, 1.000000)
         S: (0.000000, 0.000000, 0.000000, 1.000000) ^3.200000
         E: (0.000000, 0.000000, 0.000000, 1.000000)
    Frames
     Frame 0 ''
         Mesh 0 ('')

I see it has two mesh parts, and I don't see any specific reason why it wouldn't work. Let me try it out.

@omml
Copy link
Author

omml commented Jul 17, 2015

Hello, sorry, I meant 2 subsets. Regards

@walbourn
Copy link
Member

Model is rendering both parts fine. The issue is that the way the model is modeled, the first model part must be drawn after the second model part because they sit immediately on top of one another. The only sorting built-into the generic Model::Draw is to draw the non-alpha materials first, then draw the alpha material ones.

To render island.sdkmesh, you need to not use the generic Model::Draw and draw the individual parts in the 'correct' order for that particular model. See ModelMeshPart for how to draw the individual pieces.

@omml
Copy link
Author

omml commented Jul 17, 2015

Ok, I'll try that, thanks for your advise.

@walbourn
Copy link
Member

One quick hack for this specific model is to reach into the first ModelMeshPart and set isAlpha to true. This will cause the simple "non-alpha" vs. "alpha" sort to change the draw order.

model->meshes[0]->meshParts[0]->isAlpha = true;

@omml
Copy link
Author

omml commented Jul 21, 2015

I tried checking the values of the meshParts members of the mentioned mesh:
for (int i=0; i<2; i++)
{
auto effect = m_model->meshes[0]->meshParts[i]->effect;
auto indexBuffer = m_model->meshes[0]->meshParts[i]->indexBuffer;
auto indexCount = m_model->meshes[0]->meshParts[i]->indexCount;
auto indexFormat = m_model->meshes[0]->meshParts[i]->indexFormat;
auto inputLayout = m_model->meshes[0]->meshParts[i]->inputLayout;
auto isAlpha = m_model->meshes[0]->meshParts[i]->isAlpha;
auto primitiveType = m_model->meshes[0]->meshParts[i]->primitiveType;
auto startIndex = m_model->meshes[0]->meshParts[i]->startIndex;
auto vbDecl = m_model->meshes[0]->meshParts[i]->vbDecl;
auto vertexBuffer = m_model->meshes[0]->meshParts[i]->vertexBuffer;
auto vertexOffset = m_model->meshes[0]->meshParts[i]->vertexOffset;
auto vertexStride = m_model->meshes[0]->meshParts[i]->vertexStride;
}

For the first meshPart I get:
indexCount = 20640
startIndex = 0
isAlpha = false
vertexOffset = 0
vertexStride = 32

For the second one I get:
indexCount = 34890
startIndex = 0
isAlpha = false
vertexOffset = 0
vertexStride = 32

I wonder if I should be getting different values for startIndex, vertexOffset or vertexStride for the second meshPart, or I am doing something wrong?

Thanks

@walbourn
Copy link
Member

I see the problem. That model uses a non-zero vertexStart value for the second subset which is an offset to each index in the IB.

@walbourn walbourn reopened this Jul 21, 2015
@walbourn
Copy link
Member

Ignore my original comment about isAlpha. I was wrong.

The fix is to add a single line to ModelLoadSDKMESH.cpp:

part->indexCount = static_cast<uint32_t>( subset.IndexCount );
part->startIndex = static_cast<uint32_t>( subset.IndexStart );
part->vertexOffset = static_cast<uint32_t>( subset.VertexStart ); // This was MISSING!
part->vertexStride = static_cast<uint32_t>( vbArray[ mh.VertexBuffers[0] ].StrideBytes );
part->indexFormat = ( ibArray[ mh.IndexBuffer ].IndexType == DXUT::IT_32BIT ) ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT;
part->primitiveType = primType; 
part->inputLayout = il;
part->indexBuffer = ibs[ mh.IndexBuffer ];
part->vertexBuffer = vbs[ mh.VertexBuffers[0] ];
part->effect = mat.effect;
part->vbDecl = vbDecls[ mh.VertexBuffers[0] ];

Thanks for digging into this one. I also need to add this information to my sdkmeshdump.cpp

@walbourn walbourn added bug and removed question labels Jul 21, 2015
@walbourn walbourn self-assigned this Jul 21, 2015
@walbourn
Copy link
Member

Fix submitted. Will be included in the next release. Thanks!

c0dcd42

@walbourn
Copy link
Member

BTW, after updating my dump utility, I can see the offset is in use:

             Subset 0 ''
                     material 0, TriangleList
                    index start 0, index count 20640
                    vertex start 0, vertex count 20640
             Subset 1 ''
                     material 1, TriangleList
                    index start 0, index count 34890
                    vertex start 20640, vertex count 34890

The Sample Content Exporter never uses a non-zero VertexStart. It uses IndexStart instead and doesn't currently merge/coalesce VBs/IBs. Therefore the bug was only reproducible with older sdkmesh files produced by legacy exporter tools like the DirectX SDK's .x file converter meshconvert.

@walbourn walbourn changed the title Bug in Model class? Bug in Model class SDKMESH loader Jul 21, 2015
@omml
Copy link
Author

omml commented Jul 22, 2015

That's great! thanks.

@walbourn walbourn assigned walbourn and unassigned walbourn Jun 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants