Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Check for every triangle of a mesh if it intersects triangle of another mesh

  1. #1
    Junior Member Newbie
    Join Date
    May 2014
    Posts
    7

    Check for every triangle of a mesh if it intersects triangle of another mesh

    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:

    Code :
    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;
            }

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    495
    Quote Originally Posted by thebigbo View Post
    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.

    Quote Originally Posted by thebigbo View Post
    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:
    Code :
    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;
     }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •