How do i know which vertices are connected to form a triangle from OpenGL

Hi all,

Let say if i have passed in pointer of an array containing 1000s of vertices into glDrawArray(GL_TRIANGLES,…), how can i know which vertices are OpenGL using to produce a triangle??
Are the vertices processed sequentially, for example, {v1, v2, v3} from the 1000s vertices are connected to form a first triangle??

Thanks a lot.

The sequence of vertices is processed in accord with the primitive parameter you provide. GL_TRIANGLES, as a primitive type, means that every individual set of 3 vertices, in order, is an independent triangle. Vertices (0, 1, 2) become the first triangle, with (3, 4, 5) being the second, and so on.

Other primitive types operate on the sequence of vertices differently.

[QUOTE=Alfonse Reinheart;1265570]The sequence of vertices is processed in accord with the primitive parameter you provide. GL_TRIANGLES, as a primitive type, means that every individual set of 3 vertices, in order, is an independent triangle. Vertices (0, 1, 2) become the first triangle, with (3, 4, 5) being the second, and so on.

Other primitive types operate on the sequence of vertices differently.[/QUOTE]

Thanks Alfonse, if i have a set of vertex positions and its respective normals as a result from marching tetrahedra, is there anyway for me to get the indices or face value that is required in .obj format from OpenGL??

[QUOTE=Alfonse Reinheart;1265570]The sequence of vertices is processed in accord with the primitive parameter you provide. GL_TRIANGLES, as a primitive type, means that every individual set of 3 vertices, in order, is an independent triangle. Vertices (0, 1, 2) become the first triangle, with (3, 4, 5) being the second, and so on.

Other primitive types operate on the sequence of vertices differently.[/QUOTE]
Im afraid I disagree to you a little bit. Which will be the second or third… ? depend on the argument of the command glDrawArrayS(GL_TRIANGLES, i, i+3); where variable i can be 0,1,…,n; for your example,
(0,1,2,3,4,5,6,…), 0,1,2can be the first one, and 1,2,3can be the second, but 2,3,4 can also be the second ,this depends on the value of variable i. where i can also be regrard as index.
for (int i=0; i<1002; i++) {
glDrawArrays(GL_TRIANGLES,0+i,3+i);
}; //draw 1000 triangles.
if you want three sequency points as a group to form a triangle, you can,
for (int i=0; i<630; i++) {
glDrawArrays(GL_TRIANGLES,3i,3(i+1)); //wehre i=0,1,2…
};
//draw two hundreds and ten triangles., which every three points in order as a group.

depend on the argument of the command glDrawArrayS(GL_TRIANGLES, i, i+3); where variable i can be 0,1,…,n; for your example,

… That would mean issuing a draw call for every individual triangle. Which is pathological use of the API, and is throwing performance away for no reason. If you have a sequence of triangles in an array, you’re supposed to use a single glDrawArrays call to draw them. That’s what the API function is for; that’s why it takes a vertex count.

… if i have a set of vertex positions and its respective normals as a result from marching tetrahedra, is there anyway for me to get the indices or face value that is required in .obj format from OpenGL??
What do you mean by ‘have’? The output of a Marching Tet utility is an iso-surface made up of triangles. Can you display this iso-surface? If so - how? With code you’ve written? With some other code such as Mathematica? Or, do you simply have triangle definitions in a text file which you have no way to display. If you could post a bit of that text file, we could help out more specifically. I have written a Marching Tet utility which output results into OBJ format. If you’ve such a utility written yourself, with a little more internal organization, writing an OBJ file should be very straightforward.

If you have a sequence of triangles in an array, you’re supposed to use a single glDrawArrays call to draw them. That’s what the API function is for; that’s why it takes a vertex count.

quote an example?

Thanks for your response, actually, i have an existing source based on marching tetrahedra by OpenGL Development Cookbook and is trying to experiment with it to produce an obj file, the output of the marching tetrahedra class are the vertex count as well as a pointer to the data of vertex position as well as its normals. Thus, basically, i have got two essential data, vertex positions and vertex normals to be output to an obj file. However, one thing i am not really sure is about the face value of obj (f 1//2 - for vertex position and normals) as from what i have understood, it is the indices that are used for vertex sharing.
However, let say if i have 100000s of vertices, i would have to compare it (at least what i am thinking right now) to know which vertices are similar and then use the former vertex index.

Thanks a lot.

Below is some vertex that are produced by the source from the book:
v 0.754572 0.061889 0.009851
v 0.754451 0.092834 0.009851
v 0.754451 0.092834 0.000000
v 0.896628 0.061889 0.000000
v 0.896507 0.092834 0.000000
v 0.896507 0.061889 0.009851
v 0.896507 0.061889 0.009851
v 0.896507 0.092834 0.000000

Thanks a lot…

You can get a series of sequence triangles which has a common side each adjacent one. They form a mesh, can edit each primitive inside.
they can also have a common vertex of adjacent each one if you modify parameter i.
a mesh command may come from this.

However, one thing i am not really sure is about the face value of obj (f 1//2 - for vertex position and normals) as from what i have understood, it is the indices that are used for vertex sharing.
However, let say if i have 100000s of vertices, i would have to compare it (at least what i am thinking right now) to know which vertices are similar and then use the former vertex index.

The marching cubes/tetrahedra algorithm has built-in knowledge of when vertices will be shared and with which ones. However, if your generation algorithm doesn’t take advantage of this fact, then the information is lost. Which means that the only way to generate it again is to do the vertex comparison stuff you’re talking about.

If you want to avoid that, you’ll have to change your algorithm to make note of when positions and normals will be shared by two adjacent tetrahedra.

Also remember that, while the .obj format does indeed allow positions and normals to have separate indices, OpenGL does not. You can share two vertices only if the positions and normals are both the same. So really, there’s no point in taking advantage of the .obj format’s ability to have separate indices.

[QUOTE=Alfonse Reinheart;1265606]The marching cubes/tetrahedra algorithm has built-in knowledge of when vertices will be shared and with which ones. However, if your generation algorithm doesn’t take advantage of this fact, then the information is lost. Which means that the only way to generate it again is to do the vertex comparison stuff you’re talking about.

If you want to avoid that, you’ll have to change your algorithm to make note of when positions and normals will be shared by two adjacent tetrahedra.

Also remember that, while the .obj format does indeed allow positions and normals to have separate indices, OpenGL does not. You can share two vertices only if the positions and normals are both the same. So really, there’s no point in taking advantage of the .obj format’s ability to have separate indices.[/QUOTE]

Thanks a lot Alfonse, i will take note of that and try to modify the algorithm to accommodate this feature, as comparison of lot of vertices are going to take a very very long time. Again, thanks a lot for your response. Cheers.