PDA

View Full Version : Check for every triangle of a mesh if it intersects triangle of another mesh

thebigbo
05-18-2014, 05:59 AM
Hi,

I use vector storing vertices data needed to draw a sphere. The question is, how do I know which three vertices build a triangle and how do I iterate through every single triangle of one mesh to check if it intersects with a triangle of another mesh.

Here is how I populate vector vertices with data:

vector<GLfloat> vertices;

float const R = 1.0f / (float)(rings - 1);
float const S = 1.0f / (float)(sectors - 1);
unsigned int r, s;

vertices.resize(rings * sectors * 3);
vector<GLfloat>::iterator v = vertices.begin();
for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++)
{
float const x = sinf(M_PI * r * R) * cosf(2 * M_PI * s * S);
float const y = sinf(-M_PI_2 + M_PI * r * R );
float const z = sinf(2.0f * M_PI * s * S) * sinf(M_PI * r * R );

*v++ = x * radius;
*v++ = y * radius;
*v++ = z * radius;
}

GClements
05-18-2014, 12:34 PM
how do I know which three vertices build a triangle
You use the index array which you generate at the same time as the vertex positions.

Here is how I populate vector vertices with data:
This generates a cylindrical mesh of (rings-1)*sectors quads.

The indices can be generated as:

int indices[(rings-1)*sectors*2*3];
int *p = indices;
for (int r = 0; r < rings-1; r++)
for (int s = 0; s < sectors; s++)
{
// four corners of quad (r,s)
int v00 = r * sectors + s;
int v01 = r * sectors + (s + 1) % sectors;
int v10 = (r + 1) * sectors + s;
int v11 = (r + 1) * sectors + (s + 1) % sectors;
// lower-right triangle
*p++ = v00; *p++ = v10; *p++ = v11;
// upper-left triangle
*p++ = v00; *p++ = v11; *p++ = v01;
}