Archived

This topic is now archived and is closed to further replies.

Opening 3d files into an opengl program

This topic is 4992 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

Im currently attempting to create a simple rendering program (since Im too lazy to reinstall my copy of MAX 4). I have set up the basic program outline, as you will see in the link below, and I am currently attempting to work on the file opening aspect of the program. I am NOT good at file I/O by any means and I absolutly dispise working with it (probably because of my lack of experience). So, I was wondering if anyone knows about a good tutorial where I could possibly learn how to open files easily? Or oculd someone possibly post up some example code? It would be very helpful! I am currently pulling my hair out with the frustration that I am having with this I/O stuff right now... A link to the program exe, scaned for virii, is below. The NeHe tutorials have been very helpful, splitters and such. http://www.orcish-design.net/OrcishRendering.zip

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What do you mean (sorry im not good in english) ? Is it to read all of your vertices, that are contained in a file ?
In that case, i ll give you a part of my project. The following function reads vertices in a file, with the syntax :
!name

VALUES
4631 0 0

VERTICES
-46.66667 46.66673 49.99994
-50.00000 49.99993 -50.00007
-50.00000 -50.00007 -49.99993
-46.66667 -46.66660 50.00006
50.00000 -50.00007 -49.99993 ...

int CMesh::init_vertexdata(char* path)
{
char cbuf[1024];
float n1,n2,n3;
int i;
int num_v,num_e,num_f;
FILE *geofile;

if(!(geofile = fopen(path,"r"))
{
printf("ERROR: can''t read %s\n",path);
return 0;
}

//reads the number of verts,edges,faces

sprintf(cbuf,"";
while(strcmp(cbuf,"VALUES\n"!=0)
fgets( cbuf, 256, geofile );

if(fgets(cbuf,256,geofile ));
else
{
printf("ERROR: bad geometry file: %s\n",cbuf);
return 0;
}
if(cbuf[0] == ''#'') while(getc(geofile) != ''\n'');
else
{
sscanf(cbuf,"%d %d %d",&num_v,&num_e,&num_f);
}

//initializes the array of verts

if(!(vertexes = (GLfloat**)malloc(num_v*sizeof(GLfloat*))))
{
printf("ERROR: malloc failed on vertexes\n";
exit(2);
}
for (i=0;i<num_v;i++)
{
if(!(vertexes = (GLfloat *)malloc(3*sizeof(GLfloat))))
{
printf("ERROR: malloc failed on vertexes[%i]", &i);
exit(2);
}
}

//reads verts coords and stores them in the vertices array

sprintf(cbuf,"";
while(strcmp(cbuf,"VERTICES\n"!=0)
fgets( cbuf, 256, geofile );

for(i=0; i<num_v; i++)
{
if(fgets( cbuf, 256, geofile ));
else
{
printf("ERROR: bad geometry file\n";
return 0;
}
if(cbuf[0] == ''#'') while(getc(geofile) != ''\n'');
else
{
sscanf(cbuf,"%f %f %f",&n1,&n2,&n3);
vertexes[i][0]=n1;
vertexes[i][1]=n2;
vertexes[i][2]=n3;
}
}

fclose(geofile);

VERTEX_NUMBER=num_v;
return 1;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What do you mean (sorry im not good in english) ? Is it to read all of your vertices, that are contained in a file ?
In that case, i ll give you a part of my project. The following function reads vertices in a file, with the syntax :
!name

VALUES
4631 0 0

VERTICES
-46.66667 46.66673 49.99994
-50.00000 49.99993 -50.00007
-50.00000 -50.00007 -49.99993
-46.66667 -46.66660 50.00006
50.00000 -50.00007 -49.99993 ...

int CMesh::init_vertexdata(char* path)
{
char cbuf[1024];
float n1,n2,n3;
int i;
int num_v,num_e,num_f;
FILE *geofile;

if(!(geofile = fopen(path,"r"))
{
printf("ERROR: can''t read %s\n",path);
return 0;
}

//reads the number of verts,edges,faces

sprintf(cbuf,"";
while(strcmp(cbuf,"VALUES\n"!=0)
fgets( cbuf, 256, geofile );

if(fgets(cbuf,256,geofile ));
else
{
printf("ERROR: bad geometry file: %s\n",cbuf);
return 0;
}
if(cbuf[0] == ''#'') while(getc(geofile) != ''\n'');
else
{
sscanf(cbuf,"%d %d %d",&num_v,&num_e,&num_f);
}

//initializes the array of verts

if(!(vertexes = (GLfloat**)malloc(num_v*sizeof(GLfloat*))))
{
printf("ERROR: malloc failed on vertexes\n";
exit(2);
}
for (i=0;i<num_v;i++)
{
if(!(vertexes = (GLfloat *)malloc(3*sizeof(GLfloat))))
{
printf("ERROR: malloc failed on vertexes[%i]", &i);
exit(2);
}
}

//reads verts coords and stores them in the vertices array

sprintf(cbuf,"";
while(strcmp(cbuf,"VERTICES\n"!=0)
fgets( cbuf, 256, geofile );

for(i=0; i<num_v; i++)
{
if(fgets( cbuf, 256, geofile ));
else
{
printf("ERROR: bad geometry file\n";
return 0;
}
if(cbuf[0] == ''#'') while(getc(geofile) != ''\n'');
else
{
sscanf(cbuf,"%f %f %f",&n1,&n2,&n3);
vertexes[i][0]=n1;
vertexes[i][1]=n2;
vertexes[i][2]=n3;
}
}

fclose(geofile);

VERTEX_NUMBER=num_v;
return 1;
}

Share this post


Link to post
Share on other sites