Jump to content
  • Advertisement
Sign in to follow this  
pachesantiago

Errors while loading a custom file format

This topic is 2465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hy guys.
I have a problem with this function:




status X3D::loadObject(const char * location,const char * texturesLocation,bool importId,int ID,bool aem,bool bypass_materials)
{
#ifdef debug
if(importId == false AND ID < 0)
{
::MessageBox(nullptr,L"Error cargando el modelo(ID incorrecto)/Error loading the model(Incorrect ID)",L"Error",MB_OK | MB_ICONERROR);
return Failed;
}
#endif

// TODO : finish aem loading
if(aem == true)
{
Model temp;

ToRenderCounter tempC(ID,OT_Model,objects_count + 1);
ID3DXMesh* Mesh = NULL;

temp.Position = D3DXVECTOR3(0,0,0);
temp.Scale = D3DXVECTOR3(1,1,1);
temp.Rotation = D3DXVECTOR3(0,0,0);

char temp_data[128];
#ifdef debug
HRESULT hr;
stringstream errorMensaje;
#endif
ifstream inFile;

inFile.open(location,ifstream::in);

if(importId == true)
{
inFile.getline(temp_data,128);
strtok(temp_data," ");

ID = atoi(strtok(NULL," "));
}
else
{
inFile.getline(temp_data,128);
}

inFile.getline(temp_data,128);
strtok(temp_data," ");
DWORD vertexCount = strtoul(strtok(NULL," "),NULL,0);

inFile.getline(temp_data,128);
strtok(temp_data," ");
DWORD facesCount = strtoul(strtok(NULL," "),NULL,0);

inFile.getline(temp_data,128);
strtok(temp_data," ");
DWORD FVF = strtoul(strtok(NULL," "),NULL,0);

#ifdef debug
hr = CheckHR(D3DXCreateMeshFVF(facesCount,vertexCount,D3DXMESH_MANAGED,FVF,DXUTGetD3D9Device(),&Mesh));
if(FAILED(hr))
{
errorMensaje.str("");
errorMensaje << "loadObject failed while creating mesh. Error: " << DXGetErrorStringA(hr) << endl;
errorMensaje >> internal_Log;
}
#endif
#ifndef debug
D3DXCreateMeshFVF(facesCount,vertexCount,D3DXMESH_MANAGED,FVF,DXUTGetD3D9Device(),&Mesh);
#endif
Vertex* vertex = nullptr;
// TODO : add reult check in the lock/unlock of the buffers
Mesh->LockVertexBuffer(0,(void**)&vertex);

for(int x = 0;x < vertexCount;x++)
{
inFile.getline(temp_data,128);
strtok(temp_data," ");
vertex[x].x = atof(strtok(NULL,","));
vertex[x].y = atof(strtok(NULL,","));
vertex[x].z = atof(strtok(NULL,","));
vertex[x].u = atof(strtok(NULL,","));
vertex[x].v = atof(strtok(NULL,","));
vertex[x].nx = atof(strtok(NULL,","));
vertex[x].ny = atof(strtok(NULL,","));
vertex[x].nz = atof(strtok(NULL,","));
}

Mesh->UnlockVertexBuffer();

DWORD* index = nullptr;

Mesh->LockIndexBuffer(0,(void**)&index);

for(int y = 0;y < (facesCount*3);y++)
{
inFile.getline(temp_data,128);
strtok(temp_data," ");
index[y] = strtoul(strtok(NULL," "),NULL,10);
}

Mesh->UnlockIndexBuffer();

DWORD* atributte = nullptr;

Mesh->LockAttributeBuffer(0,&atributte);
for(int y = 0;y < facesCount;y++)
{
inFile.getline(temp_data,128);
strtok(temp_data," ");
atributte[y] = strtoul(strtok(NULL," "),NULL,10);
}

Mesh->UnlockAttributeBuffer();

if(bypass_materials == true)
{
inFile.close();

MakeObjectFromMemoryIndirect(ID,Mesh,&temp,Use_Default_Material,FVF);
MeshBufer.push_back(temp);
objects_to_render_count_X3D.push_back(tempC);

return Completed;
}
else
{
DWORD material_count;

inFile.getline(temp_data,128);
strtok(temp_data," ");
material_count = strtoul(strtok(NULL," "),NULL,10);

char material_location[256];
if(material_count != 0)
{
for(DWORD sentinel = 0;sentinel <= material_count AND material_count != 0;sentinel++)
{
inFile.getline(material_location,256);
strtok(temp_data," ");

LoadMaterial(strtok(NULL," "));
temp.MaterialsIDs.push_back(GetLastMaterialID());
}
}
inFile.close();

MakeObjectFromMemoryIndirect(ID,Mesh,&temp,No_Flag,FVF);
MeshBufer.push_back(temp);
objects_to_render_count_X3D.push_back(tempC);
return Completed;
}
}
else
{
... This isn't important for this case ...
}
return Failed;
}



The problem is in fact in MakeObjectFromMemoryIndirect, but only when is called from load object, otherwhise it works.
the code for MakeObjectFromMemoryIndirect is :



status X3D::MakeObjectFromMemoryIndirect(int Id,ID3DXMesh * Mesh,Model *outModel,DWORD Flags,DWORD FVF,ID3DXPMesh * ProgresiveMesh,TexFilter Filter)
{
outModel->ID = Id;
if(((Animated_Mesh & Flags) == Animated_Mesh))
{
if(((Ignore_Textures & Flags) == Ignore_Textures))
{
outModel->TexturesIDs.clear();
outModel->TexturesIDs.push_back(-1);
}
if(((Use_Default_Material & Flags) == Use_Default_Material))
{
outModel->MaterialsIDs.clear();
outModel->MaterialsIDs.push_back(0);
}
outModel->StandarModel.cleanS();
return Completed;
}
if(((Ignore_Textures & Flags) == Ignore_Textures))
{
outModel->TexturesIDs.clear();
outModel->TexturesIDs.push_back(-1);
}
if(((Use_Default_Material & Flags) == Use_Default_Material))
{
outModel->MaterialsIDs.clear();
outModel->MaterialsIDs.push_back(0);
}
if(((Import_FVF_From_Mesh & Flags) == Import_FVF_From_Mesh))
{
outModel->StandarModel.FVF = Mesh->GetFVF();
}
if(((Import_FVF_From_Progresive_Mesh & Flags) == Import_FVF_From_Progresive_Mesh))
{
outModel->StandarModel.FVF = ProgresiveMesh->GetFVF();
}
else
{
outModel->StandarModel.FVF = FVF;
}
outModel->AnimatedModel.cleanA();
outModel->StandarModel.isProgresive = false;
outModel->StandarModel.ProgresiveMesh = ProgresiveMesh;

if(Mesh != nullptr)
{
DWORD* tempAdyacencia = new DWORD[Mesh->GetNumFaces() * 3];
Mesh->GenerateAdjacency(0.001,tempAdyacencia);
Mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT,tempAdyacencia,outModel->StandarModel.Adyacencia,0,0);

CheckHR(Mesh->CloneMeshFVF(D3DPOOL_MANAGED,outModel->StandarModel.FVF,DXUTGetD3D9Device(),&outModel->StandarModel.Mesh));

//Check integrity
if(outModel->StandarModel.Mesh == nullptr)
{
::MessageBox(nullptr,L"Error clonando el mesh/Error cloning the mesh",L"Error",MB_OK | MB_ICONERROR);

//Release all
delete[] tempAdyacencia;
Mesh->Release();
return Failed;
}

//Release all
delete[] tempAdyacencia;
Mesh->Release();
if(ProgresiveMesh != nullptr)
{
ProgresiveMesh->Release();
}

return Completed;
}
else
{
::MessageBox(nullptr,L"Advertencia, mesh de entrada vacio/Warning, empty input mesh",L"Advertencia/Warning",MB_OK | MB_ICONWARNING);
outModel->StandarModel.Mesh = Mesh;

//Release all
//Mesh->Release();
//ProgresiveMesh->Release();

return With_Errors;
}
}

Here is a simple file( a cube):


ID. 44
VC. 24
FC. 12
FVF. 274
V. -0.5,-0.5,-0.5,0,0,-1,0,0
V. -0.5,-0.5,0.5,0,0,-1,0,0
V. -0.5,0.5,0.5,0,0,-1,0,0
V. -0.5,0.5,-0.5,0,0,-1,0,0
V. -0.5,0.5,-0.5,0,0,0,1,0
V. -0.5,0.5,0.5,0,0,0,1,0
V. 0.5,0.5,0.5,0,0,0,1,0
V. 0.5,0.5,-0.5,0,0,0,1,0
V. 0.5,0.5,-0.5,0,0,1,0,0
V. 0.5,0.5,0.5,0,0,1,0,0
V. 0.5,-0.5,0.5,0,0,1,0,0
V. 0.5,-0.5,-0.5,0,0,1,0,0
V. -0.5,-0.5,0.5,0,0,0,-1,0
V. -0.5,-0.5,-0.5,0,0,0,-1,0
V. 0.5,-0.5,-0.5,0,0,0,-1,0
V. 0.5,-0.5,0.5,0,0,0,-1,0
V. -0.5,-0.5,0.5,0,0,0,0,1
V. 0.5,-0.5,0.5,0,0,0,0,1
V. 0.5,0.5,0.5,0,0,0,0,1
V. -0.5,0.5,0.5,0,0,0,0,1
V. -0.5,-0.5,-0.5,0,0,0,0,-1
V. -0.5,0.5,-0.5,0,0,0,0,-1
V. 0.5,0.5,-0.5,0,0,0,0,-1
V. 0.5,-0.5,-0.5,0,0,0,0,-1
I. 0
I. 1
I. 2
I. 2
I. 3
I. 0
I. 4
I. 5
I. 6
I. 6
I. 7
I. 4
I. 8
I. 9
I. 10
I. 10
I. 11
I. 8
I. 12
I. 13
I. 14
I. 14
I. 15
I. 12
I. 16
I. 17
I. 18
I. 18
I. 19
I. 16
I. 20
I. 21
I. 22
I. 22
I. 23
I. 20
A. 0
A. 0
A. 0
A. 0
A. 0
A. 0
A. 0
A. 0
A. 0
A. 0
A. 0
A. 0
P. 0
MC. 1
MT. E:\Documentos de Santiago Pacheco\Visual Studio 2010\Projects\Nanoatack\juego para nacho - borrar\map\material0_ob44.mtl
M. 2.24208e-044
T. Rigid_body
RS. 152357944
R. 1.4013e-045
BBD. 1.79149e-033,4.39968e-039,-1.07102e-030



Is a simple plain text format, but im getting a access violation inside MakeObjectFromMemoryIndirect, in the function CloneMeshFVF.

This is how i called LoadObject:



stringstream location;
location.str("");
location << ExePath() << "\\juego para nacho - borrar\\map\\" << "object_1.aem";
helper.loadObject(location.str().c_str(),"\\",true,1,true,true);


I dont know why, because MakeObjectFromMemoryIndirect works in other cases.
If someone has an idea, please share it.
Ok, thats all.

Share this post


Link to post
Share on other sites
Advertisement

if(Mesh != nullptr)
{
DWORD* tempAdyacencia = new DWORD[Mesh->GetNumFaces() * 3];
Mesh->GenerateAdjacency(0.001,tempAdyacencia);
Mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT,tempAdyacencia,outModel->StandarModel.Adyacencia,0,0);

CheckHR(Mesh->CloneMeshFVF(D3DPOOL_MANAGED,outModel->StandarModel.FVF,DXUTGetD3D9Device(),&outModel->StandarModel.Mesh));


It looks like the argument that you pass to the pAdjacencyOut parameter of ID3DXMesh::OptimizeInplace is not properly initialised (I don't see where outModel->StandarModel.Adyacencia is being allocated with buffer of correct size for OptimizeInplace to fill in for you). SoOptimizeInplace is probably trashing memory.

Share this post


Link to post
Share on other sites


if(Mesh != nullptr)
{
DWORD* tempAdyacencia = new DWORD[Mesh->GetNumFaces() * 3];
Mesh->GenerateAdjacency(0.001,tempAdyacencia);
Mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT,tempAdyacencia,outModel->StandarModel.Adyacencia,0,0);

CheckHR(Mesh->CloneMeshFVF(D3DPOOL_MANAGED,outModel->StandarModel.FVF,DXUTGetD3D9Device(),&outModel->StandarModel.Mesh));


It looks like the argument that you pass to the pAdjacencyOut parameter of ID3DXMesh::OptimizeInplace is not properly initialised (I don't see where outModel->StandarModel.Adyacencia is being allocated with buffer of correct size for OptimizeInplace to fill in for you). SoOptimizeInplace is probably trashing memory.

thanks for that, i will correct it, but that isnt the problem.
i try commenting optimizeInplace and it still doesnt works.
anyway, that function works with others meshes, so i think that the error is in load object.

Share this post


Link to post
Share on other sites

come on guys, any ideas?


Have you stepped through with a debugger, line by line, to verify the results of each line? Also, have you enabled debug information for D3D? Are you checking the results of each call to D3D to make sure that there aren't any errors? It's possible a call to D3D earlier is failing, but you may not be checking for that and proceeding to operate on invalid data.

Share this post


Link to post
Share on other sites
I run it with the debugger (Visual Studio debugger and CDB) and everything looks right.
I have no idea why it dosent works.
The best clue that i have is that when i run CDB and when it trows the error I type kb, it shows that there is some function inside optimize in place or clone mesh fvf that is called optimize faces, and one of the parameters is NULL.
i dont know if that helps

Share this post


Link to post
Share on other sites

I have had problems like this before. I cleared it up by running D3DXCleanMesh and D3DXWeldVertices after loading the file. Try it and see

Thanks, i will try that and see if it works

Share this post


Link to post
Share on other sites
Hidden
Bad news it dosent work :(
Here is the new code


if(bypass_materials == true)
{
inFile.close();

DWORD* Adjacency = new DWORD[Mesh->GetNumFaces() * 3];
DWORD* AdjacencyOut = new DWORD[Mesh->GetNumFaces() * 3];
D3DXWELDEPSILONS Epsilons;
// Set epsilon values
Epsilons.Normal = 0.001;
Epsilons.Position = 0.1;
Mesh->GenerateAdjacency(0.001,Adjacency);
#ifdef debug
CheckHR(D3DXWeldVertices(Mesh,D3DXWELDEPSILONS_DONOTSPLIT,NULL,Adjacency,AdjacencyOut,nullptr,nullptr));
CheckHR(D3DXCleanMesh(D3DXCLEAN_OPTIMIZATION,Mesh,Adjacency,&MeshCorrected,AdjacencyOut,nullptr));
#endif
#ifndef debug
D3DXWeldVertices(MeshCorrected,D3DXWELDEPSILONS_DONOTSPLIT,NULL,AdjacencyOut,nullptr,nullptr,nullptr);
D3DXCleanMesh(D3DXCLEAN_OPTIMIZATION,Mesh,Adjacency,&MeshCorrected,AdjacencyOut,nullptr);
#endif
delete Adjacency;
delete AdjacencyOut;

MakeObjectFromMemoryIndirect(ID,MeshCorrected,&temp,Use_Default_Material,FVF);
MeshBufer.push_back(temp);
objects_to_render_count_X3D.push_back(tempC);

return Completed;
}
else
{
DWORD material_count;

inFile.getline(temp_data,128);
strtok(temp_data," ");
material_count = strtoul(strtok(NULL," "),NULL,10);

char material_location[256];
if(material_count != 0)
{
for(DWORD sentinel = 0;sentinel <= material_count AND material_count != 0;sentinel++)
{
inFile.getline(material_location,256);
strtok(temp_data," ");

LoadMaterial(strtok(NULL," "));
temp.MaterialsIDs.push_back(GetLastMaterialID());
}
}
inFile.close();

DWORD* Adjacency = new DWORD[Mesh->GetNumFaces() * 3];
DWORD* AdjacencyOut = new DWORD[Mesh->GetNumFaces() * 3];
D3DXWELDEPSILONS Epsilons;
// Set epsilon values
Epsilons.Normal = 0.001;
Epsilons.Position = 0.1;
Mesh->GenerateAdjacency(0.001,Adjacency);
#ifdef debug
CheckHR(D3DXWeldVertices(Mesh,D3DXWELDEPSILONS_DONOTSPLIT,NULL,Adjacency,AdjacencyOut,nullptr,nullptr));
CheckHR(D3DXCleanMesh(D3DXCLEAN_OPTIMIZATION,Mesh,Adjacency,&MeshCorrected,AdjacencyOut,nullptr));
#endif
#ifndef debug
D3DXWeldVertices(MeshCorrected,D3DXWELDEPSILONS_DONOTSPLIT,NULL,AdjacencyOut,nullptr,nullptr,nullptr);
D3DXCleanMesh(D3DXCLEAN_OPTIMIZATION,Mesh,Adjacency,&MeshCorrected,AdjacencyOut,nullptr);
#endif
delete Adjacency;
delete AdjacencyOut;

MakeObjectFromMemoryIndirect(ID,MeshCorrected,&temp,Use_Default_Material,FVF);
MeshBufer.push_back(temp);
objects_to_render_count_X3D.push_back(tempC);
return Completed;
}



Any clue anyone.
Maybe i set up incorrectly WeldVertices and CleanMesh.

Thanks anyway smasherprog

[color="#1d3652"]

Share this post


Link to post
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!