# Rendering 3D models. data and math?

What is the the proper data arrangement for rendering arbitrary shaped 3D models by means of triangular polygons and whats the algorithm to render it?
I understand for example rendering a surface when you have MxN rectangular array and in Y axis we do height values, with this we get regular mesh grid. This can be applied for a cylider with equal top and base radii, if we unfurl it we get the same rectangular MxN grid. But what if we have a cone or asteroid data that cant be represented by rectangular grid.

Algo :

``````glBegin(GL_TRIANGLES);
for (i , each triangle) {
glVertex3f(xi,yi,zi);
}
glEnd();
``````

I don’t really see your problem though.

The problem is how to know from only vertex array[size][3] (3 coords x,y,z) which ones designate a single triangle. And question how data stored: if just vertex array[size][3], or it already supplied with faces information, or something additional when digitized and in which cases?

You need to store and supply the array index information seperately. Look up the GL array commands (DrawArrays et al) in the Red Book, which you should should be able to find on google. There are also extensions which are supported by some hardware on some operating systems, which support index array directly.

It depends on the type of surface you are trying to represent and render.

The solution you are most likely looking for was suggested at in the previous post. You define a not only a set of vertices but also how they are connected up.
So we might have our vertices V = { v0, v1, v2, v3 } and then our faces F = { <0, 1, 2>, <1, 2, 3> }
The meaning of the F numbers is that vertices of indices 0, 1 and 2 form one triangle, while vertices 1, 2, 3 form another triangle. To render these you can simply do this:

``````glBegin(GL_TRIANGLES);
for(int i = 0; i < numFaces; i++)
{
int a = faces[i].a;
int b = faces[i].b;
int c = faces[i].c;
glVertex3f(verts[a].x, verts[a].y, verts[a].z);
glVertex3f(verts[b].x, verts[b].y, verts[b].z);
glVertex3f(verts[c].x, verts[c].y, verts[c].z);
}
glEnd();
``````

When you’re comfortable with this, you should know that it’s much more efficient to use a draw-indexed primitive command, i.e. glDrawElements. This allows the hardware to cache vertices that are accessed many times.