PDA

View Full Version : another reading an infile question



strawberrylemonade
02-08-2004, 09:29 AM
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).]

nexusone
02-08-2004, 10:12 AM
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).]

strawberrylemonade
02-08-2004, 11:08 AM
its a text file.

what kind of a seporator?

nexusone
02-08-2004, 03:25 PM
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 \n // where N is number of X/Y pairs, '\n' 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 \n");
if ((input_file = fopen( input, "r")) == NULL) return 0;
//printf("File open\n");
while ( !feof( input_file ) )
{
fgets(rs , 80, input_file);
//printf("Read line: %s \n", rs);
//printf("Process line\n");
c_idex = 0;
for(i = 0; i < strlen(rs); i++ )
{
// printf("processing char %d of %d\n", 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] != '\n') && (i < strlen(rs)));
// printf("\n");
c_dex[c_idex][j] = 0;
c_idex++;
}
}
// printf("Next process commands %d \n", c_idex);
for(i = 0; i < c_idex; i++)
{
// printf("Command: %s \n", c_dex[i]);
if (strncmp(c_dex[i], "normal", 6) == 0)
{
// printf("Process Normal \n");
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 \n", normal_array[n_dex][2]);
n_dex++;
}
if (strncmp(c_dex[i], "vertex", 6) == 0)
{
// printf("Process vertex \n");
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 \n", vertex_array[v_dex][2]);
v_dex++;
}
}
// printf("Process command end\n");
}


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).]

strawberrylemonade
02-21-2004, 08:18 PM
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?

chowe6685
02-21-2004, 10:02 PM
what is the definition of vertex?