PDA

View Full Version : Reading object from file (face-vertex format)



Phoresis
11-14-2006, 04:22 AM
I'm trying to figure out what the best way to display an object from a file such as:


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

RigidBody
11-14-2006, 04:43 AM
if not me, somebody else will say it anyway: this is neither opengl nor advanced.

but since we're here- try something like


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.

Phoresis
11-14-2006, 05:22 AM
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.


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?

RigidBody
11-14-2006, 05:34 AM
you've probably set up the matrices in the wrong way. try


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

Phoresis
11-14-2006, 06:27 AM
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.

RigidBody
11-14-2006, 09:36 AM
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.

k_szczech
11-14-2006, 11:51 AM
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.