Skip to content

Commit

Permalink
Fix crash in viewer (assimp#5446)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimkulling authored Jan 30, 2024
1 parent c08e3b4 commit f956351
Showing 1 changed file with 30 additions and 43 deletions.
73 changes: 30 additions & 43 deletions tools/assimp_view/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,20 +518,19 @@ int CDisplay::AddTextureToDisplayList(unsigned int iType,
return 1;
}
//-------------------------------------------------------------------------------
int CDisplay::AddMaterialToDisplayList(HTREEITEM hRoot,
unsigned int iIndex)
{
int CDisplay::AddMaterialToDisplayList(HTREEITEM hRoot, unsigned int iIndex) {
ai_assert(nullptr != hRoot);

aiMaterial* pcMat = g_pcAsset->pcScene->mMaterials[iIndex];

if (g_pcAsset->pcScene->mNumMeshes == 0) {
return -1;
}

// find the first mesh using this material index
unsigned int iMesh = 0;
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
{
if (iIndex == g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex)
{
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i) {
if (iIndex == g_pcAsset->pcScene->mMeshes[i]->mMaterialIndex) {
iMesh = i;
break;
}
Expand All @@ -540,12 +539,9 @@ int CDisplay::AddMaterialToDisplayList(HTREEITEM hRoot,
// use the name of the material, if possible
char chTemp[512];
aiString szOut;
if (AI_SUCCESS != aiGetMaterialString(pcMat,AI_MATKEY_NAME,&szOut))
{
if (AI_SUCCESS != aiGetMaterialString(pcMat,AI_MATKEY_NAME,&szOut)) {
ai_snprintf(chTemp,512,"Material %i",iIndex+1);
}
else
{
} else {
ai_snprintf(chTemp,512,"%s (%i)",szOut.data,iIndex+1);
}
TVITEMEXW tvi;
Expand Down Expand Up @@ -577,26 +573,23 @@ int CDisplay::AddMaterialToDisplayList(HTREEITEM hRoot,
aiTextureOp eOp;
aiString szPath;
bool bNoOpacity = true;
for (unsigned int i = 0; i <= AI_TEXTURE_TYPE_MAX;++i)
{
for (unsigned int i = 0; i <= AI_TEXTURE_TYPE_MAX;++i) {
unsigned int iNum = 0;
while (true)
{
if (AI_SUCCESS != aiGetMaterialTexture(pcMat,(aiTextureType)i,iNum,
&szPath,nullptr, &iUV,&fBlend,&eOp))
{
while (true) {
if (AI_SUCCESS != aiGetMaterialTexture(pcMat,(aiTextureType)i,iNum, &szPath,nullptr, &iUV,&fBlend,&eOp)) {
break;
}
if (aiTextureType_OPACITY == i)bNoOpacity = false;
if (aiTextureType_OPACITY == i) {
bNoOpacity = false;
}
AddTextureToDisplayList(i,iNum,&szPath,hTexture,iUV,fBlend,eOp,iMesh);
++iNum;
}
}

AssetHelper::MeshHelper* pcMesh = g_pcAsset->apcMeshes[iMesh];

if (pcMesh->piDiffuseTexture && pcMesh->piDiffuseTexture == pcMesh->piOpacityTexture && bNoOpacity)
{
if (pcMesh->piDiffuseTexture && pcMesh->piDiffuseTexture == pcMesh->piOpacityTexture && bNoOpacity) {
// check whether the diffuse texture is not a default texture

// {9785DA94-1D96-426b-B3CB-BADC36347F5E}
Expand All @@ -606,9 +599,7 @@ int CDisplay::AddMaterialToDisplayList(HTREEITEM hRoot,

uint32_t iData = 0;
DWORD dwSize = 4;
if(FAILED( pcMesh->piDiffuseTexture->GetPrivateData(guidPrivateData,&iData,&dwSize) ||
0xffffffff == iData))
{
if(FAILED( pcMesh->piDiffuseTexture->GetPrivateData(guidPrivateData,&iData,&dwSize) || 0xffffffff == iData)) {
// seems the diffuse texture contains alpha, therefore it has been
// added to the opacity channel, too. Add a special value ...
AddTextureToDisplayList(aiTextureType_OPACITY | 0x40000000,
Expand All @@ -625,33 +616,26 @@ int CDisplay::AddMaterialToDisplayList(HTREEITEM hRoot,
this->AddMaterial(info);
return 1;
}

//-------------------------------------------------------------------------------
// Expand all elements in the tree-view
int CDisplay::ExpandTree()
{
int CDisplay::ExpandTree() {
// expand all materials
for (std::vector< MaterialInfo >::iterator
i = m_asMaterials.begin();
i != m_asMaterials.end();++i)
{
for (std::vector< MaterialInfo >::iterator i = m_asMaterials.begin(); i != m_asMaterials.end();++i) {
TreeView_Expand(GetDlgItem(g_hDlg,IDC_TREE1),(*i).hTreeItem,TVE_EXPAND);
}
// expand all nodes
for (std::vector< NodeInfo >::iterator
i = m_asNodes.begin();
i != m_asNodes.end();++i)
{
for (std::vector< NodeInfo >::iterator i = m_asNodes.begin(); i != m_asNodes.end();++i) {
TreeView_Expand(GetDlgItem(g_hDlg,IDC_TREE1),(*i).hTreeItem,TVE_EXPAND);
}
TreeView_Expand(GetDlgItem(g_hDlg,IDC_TREE1),m_hRoot,TVE_EXPAND);
return 1;
}

//-------------------------------------------------------------------------------
// Get image list for tree view
int CDisplay::LoadImageList(void)
{
if (!m_hImageList)
{
int CDisplay::LoadImageList() {
if (!m_hImageList) {
// First, create the image list we will need.
// FIX: Need RGB888 color space to display all colors correctly
HIMAGELIST hIml = ImageList_Create( 16,16,ILC_COLOR24, 5, 0 );
Expand Down Expand Up @@ -682,12 +666,13 @@ int CDisplay::LoadImageList(void)

m_hImageList = hIml;
}

return 1;
}

//-------------------------------------------------------------------------------
// Fill tree view
int CDisplay::FillDisplayList(void)
{
int CDisplay::FillDisplayList(void) {
LoadImageList();

// Initialize the tree view window.
Expand All @@ -713,11 +698,11 @@ int CDisplay::FillDisplayList(void)
(LPARAM)(LPTVINSERTSTRUCT)&sNew);

// add each loaded material to the tree
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMaterials;++i)
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMaterials; ++i)
AddMaterialToDisplayList(m_hRoot,i);

// add each mesh to the tree
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i)
for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes; ++i)
AddMeshToDisplayList(i,m_hRoot);

// now add all loaded nodes recursively
Expand All @@ -729,8 +714,10 @@ int CDisplay::FillDisplayList(void)
// everything reacts a little bit slowly if D3D is rendering,
// so give GDI a small hint to leave the couch and work ;-)
UpdateWindow(g_hDlg);

return 1;
}

//-------------------------------------------------------------------------------
// Main render loop
int CDisplay::OnRender()
Expand Down

0 comments on commit f956351

Please sign in to comment.