Results 1 to 8 of 8

Thread: re-read from the file

  1. #1
    Member Newbie
    Join Date
    Sep 2002
    Posts
    40

    re-read from the file

    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

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: re-read from the file

    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

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: re-read from the file

    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.

  4. #4
    Member Newbie
    Join Date
    Sep 2002
    Posts
    40

    Re: re-read from the file

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

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: re-read from the file

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

  6. #6
    Member Newbie
    Join Date
    Sep 2002
    Posts
    40

    Re: re-read from the file

    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


  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: re-read from the file

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

  8. #8
    Member Newbie
    Join Date
    Nov 2002
    Posts
    39

    Re: re-read from the file

    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

Similar Threads

  1. How to read another file again?
    By LovingYou in forum OpenGL: Basic Coding
    Replies: 12
    Last Post: 11-27-2007, 06:47 PM
  2. How can I read the 3D Max file??? Thanks
    By arjie in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 10-09-2002, 11:37 AM
  3. how to read MP3 file?
    By in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 05-13-2002, 08:48 PM
  4. Please help ! read .ase file
    By McZ in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-02-2001, 01:13 PM
  5. how to read a file ?
    By McZ in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-05-2001, 12:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean