1

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 3d 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;
        }

You might wonder, if I'm about to check for collisions between 2 spheres, why I don't use their radii instead. This is because I intend to use more complex shapes in future, where this simple method won't be of any use.

user1118321
  • 25,567
  • 4
  • 55
  • 86
thebigbo
  • 27
  • 7

1 Answers1

1

For the first question you should look at this answer, i think it answers it
procedurally generate a sphere mesh

For the second part of your problem you can always use spatial partitioning to subdivide your space and then iterate over faces in each sup-space, here is a detailed answer i gave earlier
intersection of two triangle meshes

Community
  • 1
  • 1
Ahmed Matar
  • 241
  • 1
  • 6
  • Thank you. I'd also like to know in what order vertices belonging to the same triangle appear, e.g. vertices[0], vertices[1] and vertices[2] make one triangle, then vertices[1], vertices[2] and vertices[3] -- another triangle etc. – thebigbo May 18 '14 at 17:29
  • Generally we use an array of indices to store faces vertices, i.e array r = {0,1,2,1,2,3,........} with each 3 numbers represent indices of vertices that construct one face – Ahmed Matar May 19 '14 at 08:52