Click here to Skip to main content
15,917,062 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I use the keyboard to set Z value
Now,I have new problem
How to use fwrite output vertex value into file ?
normal
float vertex[][]={};
float edge[][]={};
then use for and glVertex3dv() to draw box
if output file then read file->set vertex,edge value?
thanks to good friends
Posted

So what's the problem?
You have your vertices stored, right?

Just define the way you want the file to look and write it, stepping through the vertices in your list.

E.g

struct vert{
 double x,y,z;
}

FILE *fp;
vert *vertList = _whateverIsAppropriate_;
int numberfVertices = _whateverIsAppropriate_
fp = fopen(outputFilename, "w");
fwrite(fp, (int*)&numberOfVertices, sizeof(int));
for (curVertex=0; i<numberofvertices;>{ 
 fwrite(fp, vertList[curVertex].x, sizeof(double));
 fwrite(fp, vertList[curVertex].y, sizeof(double));
 fwrite(fp, vertList[curVertex].z, sizeof(double));
}
fclose(fp);


You've stored your vertices differently, and will of course have to access them as required. Same with the edges - you'll have to determine a way of storing them in the file and then add code to do that too. - Perhaps you'll need to store verts, edges and polys?

I've been playing with the (lightwave)obj file format yesterday and today. Perhaps having a look at (*)some example files or (*) the format specification would be helpful reading. As an example, consider the .OBJ file for a simple cube:

MSIL
# Blender3D v249 OBJ File:
# www.blender3d.org
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 0.999999 -1.000001 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8
 
Share this answer
 
Well, the process of loading the saved data is (more-or-less) the reverse of the saving process. If you save it as an MD2 or 3DS compatible format, you may load with and MD2 or 3DS loader. The code I presented is nothing of the sort. I made up the format as I typed my last answer. :)

(It only went into the IDE for 5 seconds so that I could properly indent it - I'm sure I've missed an & operator or two somewhere)

In order to load the data that was saved in the way I specified earlier, the following would be effective:

C#
struct vert
{
    double x,y,z;
}

FILE *fp;
vert *vertList;
int numberfVertices;
fp = fopen(inputFilename, "r");
fread(fp, sizeof(int), 1, &numberOfVertices)
vertList = (vert*)malloc(numberOfVertices * sizeof(vert));

for (curVertex=0; curVertex<numberOfVertices; curVertex++)
{
    fread(fp, sizeof(double), 1, vertList[curVertex].x;
    fread(fp, sizeof(double), 1, vertList[curVertex].y;
    fread(fp, sizeof(double), 1, vertList[curVertex].z;
}

fclose(fp);



Naturally, you may observe that there is no need to perform 3 writes/reads per vertex - one may simply read/write the entire struct from/to disk in a single statement:

C#
fwrite(fp, &vertList[curVertex], sizeof(vert));
fread(fp, sizeof(vert), 1, &vertList[curVertex]);


I can't smell homework can i?
 
Share this answer
 
thanks,good friends
sorry,I late
it's wonderful,simple.
So,when read file,I can use code md2,3ds load ?
or simple is change fwrite by fread then set vertList[curVertex] ?
Problem is how to nice load ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900