another reading an infile question

i have to prompt for and then read a file of integers. the first integer, n, is the number of vertices. then there are n pairs of integers to represent the 2-d coordinates of the vertices. each line to be drawn is not necessarily on a seperate line in the file.

#include <GL/glut.h>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int point [2];
int number_in = 0;
ifstream infile;

void init( )
{
glClear (GL_COLOR_BUFFER_BIT);
}

void display ()
{
glClearColor( 0.0, 0.0, 1.0, 0.0 );
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D( -10.0, 10.0, -10.0, 10.0);
glColor3f( 1.0, 1.0, 1.0 );
glBegin (GL_LINE_STRIP);
infile >> number_in;
while (!infile.eof())
{
for (int i = 0; i < number_in; i++)
{
infile >> number_in;
point [0] = number_in;
infile >> number_in;
point [1] = number_in;
glVertex2iv (point);
}
infile >> number_in;
}
glEnd ();
}

void main (int argc, char **argv)
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( 700, 500 );
glutInitWindowPosition( 0, 0 );
glutCreateWindow( “Floorplan Design” );
string open_this;
cout << "Please enter the name of the floorplan file to be opened: ";
cin >> open_this;
cout << endl;
infile.open (open_this.c_str());
if (infile.fail())
{
cout << “File failed to open. " << " Please ensure file exists and try again.”
<< endl << endl << endl;
exit (0);
}
else
{
init( );
glutDisplayFunc (display);
glutMainLoop();
}
}

[This message has been edited by strawberrylemonade (edited 02-08-2004).]

Is the file ASCII (TXT) or binary?

In a binary file, would not be a problem I don’t think, but in ASCII need a space or something as a seperator.

Also to improve the routine you would need also some type of error checking and format.
Then some seperator would be good to indicate next vertex coordinets

[This message has been edited by nexusone (edited 02-08-2004).]

its a text file.

what kind of a seporator?

Seperators can be a space ,comma, end line, etc

You can do sometime like this:

example of a file format

N 2 0.1 0.2 0.3 0.4
// where N is number of X/Y pairs, ’
’ is new line

// here is a STL model loader I wrote for ASCII saved ones:

int Load_stl(char* input)
{
FILE *input_file;
char rs[80], c_dex[8][80];
char *ts;
int i, j, c_idex;
float x,y,z;

v_dex = 0;
n_dex = 0;
//printf("Open file
");
if ((input_file = fopen( input, “r”)) == NULL) return 0;
//printf("File open
");
while ( !feof( input_file ) )
{
fgets(rs , 80, input_file);
//printf("Read line: %s
", rs);
//printf("Process line
“);
c_idex = 0;
for(i = 0; i < strlen(rs); i++ )
{
// printf(“processing char %d of %d
“, i,strlen(rs) );
if ( rs[i] != ’ ’ )
{
j = 0;
c_dex[c_idex][j] = rs[i];
// printf(“Found %c”, rs[i]);
do{
i++;
j++;
// printf(”%c”,rs[i]);
c_dex[c_idex][j] = rs[i];
}while ( (rs[i] != ’ ') && (rs[i] != ’
') && (i < strlen(rs)));
// printf(”
");
c_dex[c_idex][j] = 0;
c_idex++;
}
}
// printf("Next process commands %d
", c_idex);
for(i = 0; i < c_idex; i++)
{
// printf("Command: %s
", c_dex[i]);
if (strncmp(c_dex[i], “normal”, 6) == 0)
{
// printf("Process Normal
");
i++;
normal_array[n_dex][0] = atof( c_dex[i]);
//printf("ux =%s, x = %f, ", c_dex[i], normal_array[n_dex][0]);
i++;
normal_array[n_dex][1] = atof( c_dex[i]);
// printf("y = %f, ", normal_array[n_dex][1]);
i++;
normal_array[n_dex][2] = atof( c_dex[i]);
// printf("z = %f
", normal_array[n_dex][2]);
n_dex++;
}
if (strncmp(c_dex[i], “vertex”, 6) == 0)
{
// printf("Process vertex
");
i++;
vertex_array[v_dex][0] = atof( c_dex[i]);
// printf("ux =%s, x = %f, ", c_dex[i], vertex_array[v_dex][0]);
i++;
vertex_array[v_dex][1] = atof( c_dex[i]);
// printf("y = %f, ", vertex_array[v_dex][1]);
i++;
vertex_array[v_dex][2] = atof( c_dex[i]);
// printf("z = %f
", vertex_array[v_dex][2]);
v_dex++;
}
}
// printf("Process command end
");
}

fclose( input_file );
return 1;
}

Example of STL data file txt format.

solid ascii
facet normal 0.000000e+000 1.000000e+000 0.000000e+000
outer loop
vertex 8.343365e-001 -1.968504e-001 1.858222e-001
vertex 8.027033e-001 -1.968504e-001 1.973358e-001
vertex 8.175048e-001 -1.968504e-001 2.812795e-001
endloop
endfacet

[This message has been edited by nexusone (edited 02-08-2004).]

does glVertex not like multidimensional arrays?

i’ve changed my code from what i first posted and everything works great except this part:

for (int i = 0; i < no_walls; i++)
{
glBegin (GL_LINE_STRIP);
for (int j = 0; j < verts; j++)
{
glVertex2fv (vertex [i] [j]);
}
glEnd();
}

is there something i am overlooking?

what is the definition of vertex?