Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Reading object from file (face-vertex format)

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2006
    Posts
    17

    Reading object from file (face-vertex format)

    I'm trying to figure out what the best way to display an object from a file such as:

    Code :
    CUBE
    8
       1  1 1 1
       2  1 1 -1
       3  1 -1 1
       4  1, -1 -1
       5  -1 1 1
       6  -1 1 -1
       7  -1 -1 1
       8  -1 -1 -1
    6
       4  2 1 3 4
       4  5 6 8 7
       4  1 2 6 5
       4  4 3 7 8
       4  3 1 5 7
       4  2 4 8 6
    As you can see, each vertex is listed first and then each face shows which vertexes are used to construct it.I'm not very familiar with I/O in C++ and have no idea how to start.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    658

    Re: Reading object from file (face-vertex format)

    if not me, somebody else will say it anyway: this is neither opengl nor advanced.

    but since we're here- try something like

    Code :
    char line[128], c[10][10];
    FILE *fp = fopen("file", "r");
    int  num_vertices;
    int  id;
    float x, y, z;
     
    while(fgets(line, 128, fp)) {
     
      if(strncasecmp(line, "CUBE", 4) == 0) {
        //
        // read vertices
        //
        fgets(line, 128, fp);
        num_vertices = atoi(line);
     
        for(int i = 0; i < num_vertices; i ++) {
          fgets(line, 128, fp);
          sscanf(line, "%s %s %s %s", c[0], c[1], c[2], c[3]);
          id = atoi(c[0];
          x = atof(c[1]);
          y = atof(c[2]);
          z = atof(c[3]);
          }
        //
        // read faces the same way like above
        //
        }
      }
     
    fclose(fp);
    that's just a snippet. you have to finish it by yourself by reading the faces and storing vertex and face data in appropriate structures.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2006
    Posts
    17

    Re: Reading object from file (face-vertex format)

    Ok, thanks for you reply. I've been reading the OpenGl Read Book, and it discusses vertex arrays. I've been trying to use these to draw the above cube using explicit values.

    Code :
      glMatrixMode(GL_MODELVIEW);
     
      GLint vertices[] = {1, 1, 1,
    	                  1, 1, -1,
    					  1, -1, 1,
    					  1, -1, -1,
    					  -1, 1, 1,
    					  -1, 1, -1,
    					  -1, -1, 1,
    					  -1, -1, -1};
     
      glVertexPointer(3,GL_INT,0,vertices);
     
      GLubyte allIndices[] = {2, 1, 3, 4, 5, 6, 8, 7, 1, 2, 6, 5, 4, 3, 7, 8, 3, 1, 5, 7, 2, 4, 8, 6};
     
      glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, allIndices);
    As far as I understand that should draw a cube. But as usual it doesn't. What am I doing wrong?

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    658

    Re: Reading object from file (face-vertex format)

    you've probably set up the matrices in the wrong way. try

    Code :
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2., 2., -2., 2., -100., 100.);
     
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(10., 10., 10., 0., 0., 0., 0., 0., 1.);

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2006
    Posts
    17

    Re: Reading object from file (face-vertex format)

    Right, I've sorted it out and that's working now.

    Another problem I've run into is what if the object I'm wanting to draw is made up of quads and triangles, how can I deal with this.

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    658

    Re: Reading object from file (face-vertex format)

    you have several options:

    1. use a display list. good performance, and you can specify anything you want between glNewList and glEndList.

    2. use vertex array or vbo.

    2a) if you use glDrawArrays, you'll have to call it for triangles and quads separately, with different vertex arrays.

    2b) only one vertex array with all vertices and glDrawElements twice: first time with indices for the triangles, second time with indices which specify the quads.

  7. #7
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,119

    Re: Reading object from file (face-vertex format)

    Or simply split every quad into two triangles. GPU will do that anyway when rendering quads. Each call to glDrawArrays, glDrawElements or glBegin consumes some srious time. The only disadvantages would be increased object load time needed to split quads and increased size of index array.

    For triangles made out of quads, vertex array would be the same - only the index array will change. Instead of one quad made of vertices 1, 2, 3 and 4 you would have two triangles made of vertices 1, 2, 3 and 3, 4, 1.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •