Problems when using fwrite or fread.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Highlander2nd
    New Member
    • Oct 2008
    • 3

    Problems when using fwrite or fread.

    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:

    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;
    And this is how I register the structs in the Studio rendering code:

    Code:
    	// Data for shadow volume rendering.
    	ExtraDataMap	m_ExtraData;
    	ModelExtraData	*m_pCurretExtraData;
    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:
    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 );
    	}
    
    }
    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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What's more important it defining your file layout. You need to know what you wrote in order to be able to read it back.

    I know your data is in structs and what I see there are two arrays of int that are 3 elements each. Therefore, one format might be Face per record in the file.
    Let's say vertexIndexes followed by neighborIndexes .

    So, you write 3 ints for vertexIndexes followed by 3 ints for neighborIndexes followed by a \n.

    When you read, you read 3 int for vertexIndexes and 3 int neighborIndexes .

    Then move the ints that were read into a Face variable and off you go.

    You should be able to use fprintf to write and fscanf to read.

    Comment

    • Highlander2nd
      New Member
      • Oct 2008
      • 3

      #3
      I actually used that method before, I wrote the numbers out for each integer in one line. But no, I want to write out all of it in one for each submodel. The previous one was too... sloppy for me, and I would have to save for each submodel, wich would create a folder full of files for me.

      I want to stay away from that method, it isn't what I want to achieve. I'll also have to write vertex and bone vertex index data, wich are stored in different types of variables. It's for optimization purposes, as the code right now limits me to low-poly models if I want to keep performance.

      Thanks though, I'll keep trying and studying.

      Comment

      • Highlander2nd
        New Member
        • Oct 2008
        • 3

        #4
        Alright, I think I've solved the problem. It seems as if I should've used iostream in the first place. It works right now.

        My last question would be about the dst.faces array. If you were me, how would you load the data directly into that array? I hate using dst.faces.push_ back, because it messes up values that have -1, it could be done easier than having to do it for each Face.

        Thanks.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You can create a vector and iniialize it with an array:
          Code:
          int arr[9] = {1,2,3,4,5,6,7,8,9};
          vector<int> v(arr, arr+1);
          No need for inividual push_back calls. You can use an int* as a vector<int>::it erator so this is really an intialization from begin() to end(), where end() is one beyond end of the array.

          You should be able to load this array in one read based a read that uses 9* sizeof(int) and a pointer to an int array. This will require a binary file rather than a text file. Here you write out 9*sizeof(int) and you read back 9*sizeof(int).

          If your arrays have holes, the holes will be written and read back. That is, the disc file will be a mirror image of the memory array.

          Comment

          Working...