Hello there.
I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models.
When I store mesh data, I have to store data for each submodel and their respective triangle data, wich are the vertex and neighbor(triang les sharing a common edge) indexes. Now, I'm storing this data in these struct's:
And this is how I register the structs in the Studio rendering code:
Now, the problem comes when I try to save all this data to a single .dat file of mine. I manage to save the data properly for the amount of submodels, the amount of triangles, but when it comes to saving the Face struct array, it either reads it or writes it improperly. This is because when I read it back into a new array, every index has it's variables filled correctly, but the problem comes after the vertex indexes of the 25th Face, after wich all the variables are filled with 0's.
This is how the functions that I use to save and read data looks like:
Please, don't laugh at it, It's mostly a debug code right now.
I've been working on it for a long time, but I still couldn't figure out what's causing this problem. The code seems correct, to me that is, with my limited programming knowledge.
Thanks for any help possible.
I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models.
When I store mesh data, I have to store data for each submodel and their respective triangle data, wich are the vertex and neighbor(triang les sharing a common edge) indexes. Now, I'm storing this data in these struct's:
Code:
struct Face
{
Face() {}
Face(GLushort v0, GLushort v1, GLushort v2)
{
vertexIndexes[0] = v0;
vertexIndexes[1] = v1;
vertexIndexes[2] = v2;
}
Face(GLushort v0, GLushort v1, GLushort v2,
GLushort v3, GLushort v4, GLushort v5)
{
vertexIndexes[0] = v0;
vertexIndexes[1] = v1;
vertexIndexes[2] = v2;
neighborIndexes[0] = v3;
neighborIndexes[1] = v4;
neighborIndexes[2] = v5;
}
int vertexIndexes[3];
int neighborIndexes[3];
};
struct SubModelData
{
std::vector<Face> faces;
};
struct ModelExtraData
{
std::vector<SubModelData> submodels;
};
typedef std::map<std::string, ModelExtraData> ExtraDataMap;
Code:
// Data for shadow volume rendering. ExtraDataMap m_ExtraData; ModelExtraData *m_pCurretExtraData;
This is how the functions that I use to save and read data looks like:
Code:
/*
====================
StudioLoadData
Load data from the .dat file.
====================
*/
bool CStudioModelRenderer::StudioLoadData( /*SubModelData &dst, int bPartIndex*/ )
{
int i;
FILE *pFile;
char str1[256], szFile[256];
std::string filename(m_pRenderModel->name);
sprintf(str1, "/%s.dat", filename.substr(0, filename.rfind('.')).c_str() );
strcpy( szFile, gEngfuncs.pfnGetGameDirectory() );
strcat( szFile, str1 );
pFile = fopen (szFile, "r");
if ( pFile )
{
int iSubmodelCount;
fread( &iSubmodelCount, 4, 1, pFile );
m_pCurretExtraData->submodels.resize(iSubmodelCount);
for ( i = 0; i < iSubmodelCount; i++ )
{
int iTriangleCount;
Face faces[MAXSTUDIOTRIANGLES];
SubModelData &dst = m_pCurretExtraData->submodels[i];
fread( &iTriangleCount, 4, 1, pFile );
dst.faces.reserve(iTriangleCount);
fread( &faces[0], sizeof(Face), iTriangleCount, pFile );
if ( faces )
return true;
}
fclose( pFile );
return true;
}
return false;
}
/*
====================
StudioWriteData
Writes Shadow volume data into a file.
====================
*/
void CStudioModelRenderer::StudioWriteData( void )
{
int i;
FILE *pFile;
char str1[256], szFile[256];
std::string filename(m_pRenderModel->name);
sprintf(str1, "/%s.dat", filename.substr(0, filename.rfind('.')).c_str() );
strcpy( szFile, gEngfuncs.pfnGetGameDirectory() );
strcat( szFile, str1 );
if ( (pFile = fopen (szFile, "w")) != NULL )
{
int iSubmodelCount = m_pCurretExtraData->submodels.size();
fwrite( &iSubmodelCount, 4, 1, pFile );
for ( i = 0; i < iSubmodelCount; i++ )
{
SubModelData &dst = m_pCurretExtraData->submodels[i];
int iTriangleCount = dst.faces.size();
fwrite( &iTriangleCount, 4, 1, pFile );
fwrite( &dst.faces[0], sizeof(Face), dst.faces.size(), pFile );
}
fclose( pFile );
}
}
I've been working on it for a long time, but I still couldn't figure out what's causing this problem. The code seems correct, to me that is, with my limited programming knowledge.
Thanks for any help possible.
Comment