PDA

View Full Version : re-read from the file



lara
11-25-2002, 08:56 AM
Well , I can draw my scene by reading from the file, but while the window is still open, and i do any changes to my file, the scene also changes.
I don't want the scene to change till I press a key.
Hoe can I prevent the scene from changing.I close the file after reading from it.

Lara

nexusone
11-25-2002, 09:01 AM
Would have to see how you have coded your program, not enought information.

Post the code!


Originally posted by lara:
Well , I can draw my scene by reading from the file, but while the window is still open, and i do any changes to my file, the scene also changes.
I don't want the scene to change till I press a key.
Hoe can I prevent the scene from changing.I close the file after reading from it.

Lara

nexusone
11-25-2002, 10:21 AM
Then you need to have a read file routine and read data into a variable.

int x[10], y[10], z[10];


void Read_data(void)
{
// get data and place into variable array.

}

then only call Read_data() when a key is pressed.

lara
11-25-2002, 10:55 AM
I tried that too.
But,
It does not seem to help either.
Every time I save the text file with some changes and the window is open, it automatically re reads...

nexusone
11-25-2002, 12:17 PM
It can not re-read it, if the read is done in another routine in which is called by pressing say the "R" key.
If you have closed the file, then the file stream also would be closed and new data in the file would not be displayed until read is called again.

The problem thus is in the way you are structureing your program.
Re-write it using a varaible to store the data from the file, and post it.


display()
{
// Draw Graphics
}

read()
{
// read data
}



Originally posted by lara:
I tried that too.
But,
It does not seem to help either.
Every time I save the text file with some changes and the window is open, it automatically re reads...

lara
11-26-2002, 10:39 AM
Originally posted by lara:
Probably something esle is affecting it too:
I have this read_data which reads and stores the data in some variables..but still if you change the input file, the cubes move:

Here is the code:

#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream.h>
#include <fstream.h>

ifstream fin;
double x=0.0, y=0.0, size=0.0;
double a[10],b[10],c[10];
int lnum=0;
int line =0;

void read_data(void)
{
lnum=0;
fin.open ("input2.txt");
if (fin.fail())
{
cout << "Could not open file\n";
exit(1);
}

while (!fin.eof())
{
fin >> x >> y >> size ;
a[lnum] = x;
b[lnum] = y;
c[lnum] = size;

lnum++;
}
if (fin.is_open())
fin.close();
return;
}

void display(void)
{

glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); // clear the matrix
gluLookAt (0.0, 0.0, 3, 0.0, 0.0, -2.0, 0.0, 1, 0.0);
read_data();
line=0;
while (line != lnum )
{

glTranslatef (-a[line], -b[line], 0);


glutSolidCube (c[line]);

glTranslatef (a[line], b[line], 0);

line++;
}

glutSwapBuffers(); //only displays after the scene is drawn, so as
// to avoid flickering
}


// a simple reshape function as seen in class
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60,1,1,10);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}

// handles keyboard events
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'r':
case 'R':
read_data();
glutPostRedisplay();
break;
case 27: /* Escape Key */
exit(0); // closes window
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (600, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow("Example");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);

glutMainLoop();
return 0;
}

And my input.txt:
0.0 0.7 .2
1.0 1.0 .3
-0.5 0.7 .3
1.5 1.0 .1

What else is wrong in there?
Lara

nexusone
11-26-2002, 11:13 AM
I have located your problem... look at the code below:

#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream.h>
#include <fstream.h>

ifstream fin;
double x=0.0, y=0.0, size=0.0;
double a[10],b[10],c[10];
int lnum=0;
int line =0;

void read_data(void)
{
lnum=0;
fin.open ("input2.txt");
if (fin.fail())
{
cout << "Could not open file\n";
exit(1);
}

while (!fin.eof())
{
fin >> x >> y >> size ;
a[lnum] = x;
b[lnum] = y;
c[lnum] = size;

lnum++;
}
if (fin.is_open())
fin.close();
return;
}

void display(void)
{

glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); // clear the matrix
gluLookAt (0.0, 0.0, 3, 0.0, 0.0, -2.0, 0.0, 1, 0.0);
//********************************
read_data(); // Remove this, you keep reading the data every time you call the display routine, that is why it updates when you change your text file.... only need it in your keyboard routine!
//********************************
line=0;
while (line != lnum )
{

glTranslatef (-a[line], -b[line], 0);


glutSolidCube (c[line]);

glTranslatef (a[line], b[line], 0);

line++;
}

glutSwapBuffers(); //only displays after the scene is drawn, so as
// to avoid flickering
}


// a simple reshape function as seen in class
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60,1,1,10);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}

// handles keyboard events
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'r':
case 'R':
read_data();
glutPostRedisplay();
break;
case 27: /* Escape Key */
exit(0); // closes window
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (600, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow("Example");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);

glutMainLoop();
return 0;
}



[This message has been edited by nexusone (edited 11-26-2002).]

Claude
11-26-2002, 11:17 AM
void display(void)
{

glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); // clear the matrix
gluLookAt (0.0, 0.0, 3, 0.0, 0.0, -2.0, 0.0, 1, 0.0);
read_data();

You read the data each time you draw.Create
a Init function.Move read_data(); in it.
Call Init in Main.

Hope it helps Claude