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: - 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: - // 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: - /*
-
====================
-
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.
4 3758
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.
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.
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.
You can create a vector and iniialize it with an array: -
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Axel |
last post by:
Hiho,
here my Newbie question (Win32 GUI):
I'm reading a file in binary mode to a char* named buffer. I used
malloc(filesize) to make the needed space avaiable. The filedata in the
buffer seems to be ok..I can write the variable buffer back to a file
and the contents is ok. So I think until here all goes fine...
But now I want write...
|
by: pieter.breed |
last post by:
Hi,
background:
----------
I am the maintainer of a COM C++ dll that is used to read
binary data from a specific file format. I was recently
tasked to speed up the execution speed of this dll. In
analysis I saw that you would very often get fseek()'s
followed by either fwrite()'s or fread()'s. So my
|
by: bill |
last post by:
I am confused by the behavior of the following code. The function
copy() takes two FILE *'s and copies the data from one to the other.
In the main routine, I create a pipe and copy stdin to the write end
while copying the read end to stdout. If the child handles copying the
read end of the pipe to stdout, the main program returns. If the...
|
by: Richard Hsu |
last post by:
// code
#include "stdio.h"
int status(FILE * f) {
printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" :
"false");
}
int case1() {
FILE * f = fopen("c:\\blah", "wb+");
int i = 5;
|
by: afrin216 |
last post by:
Hi,
I'm a beginner in programming with C++. Well I've built a program which requires extensive usage of files and I've been using binary file formats and fread and fwrite commands to perfection.
The project was going well till the curtain was dropped.ie of lately I've been getting junk output whenever I used fread. I noticed that I got the...
| |
by: Lars Erik Bryld |
last post by:
Hello group
I am trying to convert a (supposedly) working python script into php.
The script reads a colour gif-picture and produces a greyscale copy of
it.
The method is to first copy the gif header, then establish the size of
the colour palette, then do a simple averaging of each colour byte
triplet into a grayscale equivalent, and...
|
by: empriser |
last post by:
How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.
|
by: David Mathog |
last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix
write, but no fwrite. That is, no portable C method for writing binary
data, only system calls which were OS specific. At C89 fwrite/fread
were added to the C standard to allow portable binary IO to files. I
wonder though why the choice was made to extend the unix...
|
by: Medvedev |
last post by:
FILE *a,*b;
int z;
char str1;
char str2;
a= fopen(argv,"rb"); //Both files are EXE's
b= fopen(argv,"rb+");
z= fread(str1,1,1,a);
if (z !=1)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |