SmokingRope
04-21-2008, 06:17 PM
I am writing a program to visualize the diving planes of a BSP tree. I associate each node in the tree with an AABB. Every non-leaf node in the tree has a dividing plane. I find the four vertex on the dividing plane that intersect the AABB. I am having trouble with the winding of these four vertex. Planes with certain normals do not actually result in a quad, but rather a quad with an isosceles triangle cut-out (the quad is being flipped.) I have included a screenshot in case that wasn't clear.
http://img134.imageshack.us/img134/5003/quadcutoutyt4.th.jpg (http://img134.imageshack.us/my.php?image=quadcutoutyt4.jpg)
As a breakdown of what i'm doing:
I Construct An Array Consisting Of All 12 Lines Defining The AABB (l_aabbPts)
I find the intersection between each line and the plane
I render the first four intersection points found as a quad
GLfloat l_pts[4][3];
GLuint l_ptsIndex = 0;
GLfloat l_aabbPts[12][2][3] =
{
l_max[0],l_max[1],l_min[2], // Top Right Of The Quad (Top)
l_min[0],l_max[1],l_min[2], // Top Left Of The Quad (Top)
l_min[0],l_max[1],l_max[2], // Bottom Left Of The Quad (Top)
l_max[0],l_max[1],l_max[2], // Bottom Right Of The Quad (Top)
l_max[0],l_max[1],l_min[2], // Top Right Of The Quad (Top)
l_max[0],l_max[1],l_max[2], // Bottom Right Of The Quad (Top)
l_min[0],l_max[1],l_min[2], // Top Left Of The Quad (Top)
l_min[0],l_max[1],l_max[2], // Bottom Left Of The Quad (Top)
l_max[0],l_min[1],l_max[2], // Top Right Of The Quad (Bottom)
l_min[0],l_min[1],l_max[2], // Top Left Of The Quad (Bottom)
l_min[0],l_min[1],l_min[2], // Bottom Left Of The Quad (Bottom)
l_max[0],l_min[1],l_min[2], // Bottom Right Of The Quad (Bottom)
l_max[0],l_min[1],l_max[2], // Top Right Of The Quad (Bottom)
l_max[0],l_min[1],l_min[2], // Bottom Right Of The Quad (Bottom)
l_min[0],l_min[1],l_max[2], // Top Left Of The Quad (Bottom)
l_min[0],l_min[1],l_min[2], // Bottom Left Of The Quad (Bottom)
l_max[0],l_max[1],l_max[2], // Top Right Of The Quad (Front)
l_max[0],l_min[1],l_max[2], // Bottom Right Of The Quad (Front)
l_min[0],l_max[1],l_max[2], // Top Left Of The Quad (Front)
l_min[0],l_min[1],l_max[2], // Bottom Left Of The Quad (Front)
l_min[0],l_max[1],l_min[2], // Top Right Of The Quad (Back)
l_min[0],l_min[1],l_min[2], // Bottom Right Of The Quad (Back)
l_max[0],l_max[1],l_min[2], // Top Left Of The Quad (Back)
l_max[0],l_min[1],l_min[2] // Bottom Left Of The Quad (Back)
};
const GLfloat *l_normal = pBSP->Plane()->m_Normal;
for( int l_i = 0 ; l_i < 12 && l_ptsIndex < 4 ; l_i++ )
{
GLfloat l_a[3], l_b[3];
memcpy(l_a, l_aabbPts[l_i][0], 3*sizeof(GLfloat));
memcpy(l_b, l_aabbPts[l_i][1], 3*sizeof(GLfloat));
GLfloat l_ab[3];
GLfloat l_t;
// Vector Describing Line On AABB
l_ab[0] = l_b[0] - l_a[0];
l_ab[1] = l_b[1] - l_a[1];
l_ab[2] = l_b[2] - l_a[2];
// Distance along line that plane intersects
l_t = pBSP->Plane()->d() - l_normal[0]*l_a[0] -
l_normal[1]*l_a[1] -
l_normal[2]*l_a[2];
l_t /= l_normal[0]*l_ab[0] +
l_normal[1]*l_ab[1] +
l_normal[2]*l_ab[2];
if( l_t < 0.0f || l_t > 1.0f ) continue;
// Plug Distance Back Into Line Equation
l_pts[l_ptsIndex][0] = l_a[0] + l_t*l_ab[0];
l_pts[l_ptsIndex][1] = l_a[1] + l_t*l_ab[1];
l_pts[l_ptsIndex][2] = l_a[2] + l_t*l_ab[2];
++l_ptsIndex;
}
glBegin(GL_QUADS);
glVertex3fv(l_pts[0]);
glVertex3fv(l_pts[1]);
glVertex3fv(l_pts[2]);
glVertex3fv(l_pts[3]);
glEnd();
How can i get the quad vertex into the correct order?
Additionally, it seems this will break if the plane exactly intersects end-points, is there anything i should be doing to account for this?
http://img134.imageshack.us/img134/5003/quadcutoutyt4.th.jpg (http://img134.imageshack.us/my.php?image=quadcutoutyt4.jpg)
As a breakdown of what i'm doing:
I Construct An Array Consisting Of All 12 Lines Defining The AABB (l_aabbPts)
I find the intersection between each line and the plane
I render the first four intersection points found as a quad
GLfloat l_pts[4][3];
GLuint l_ptsIndex = 0;
GLfloat l_aabbPts[12][2][3] =
{
l_max[0],l_max[1],l_min[2], // Top Right Of The Quad (Top)
l_min[0],l_max[1],l_min[2], // Top Left Of The Quad (Top)
l_min[0],l_max[1],l_max[2], // Bottom Left Of The Quad (Top)
l_max[0],l_max[1],l_max[2], // Bottom Right Of The Quad (Top)
l_max[0],l_max[1],l_min[2], // Top Right Of The Quad (Top)
l_max[0],l_max[1],l_max[2], // Bottom Right Of The Quad (Top)
l_min[0],l_max[1],l_min[2], // Top Left Of The Quad (Top)
l_min[0],l_max[1],l_max[2], // Bottom Left Of The Quad (Top)
l_max[0],l_min[1],l_max[2], // Top Right Of The Quad (Bottom)
l_min[0],l_min[1],l_max[2], // Top Left Of The Quad (Bottom)
l_min[0],l_min[1],l_min[2], // Bottom Left Of The Quad (Bottom)
l_max[0],l_min[1],l_min[2], // Bottom Right Of The Quad (Bottom)
l_max[0],l_min[1],l_max[2], // Top Right Of The Quad (Bottom)
l_max[0],l_min[1],l_min[2], // Bottom Right Of The Quad (Bottom)
l_min[0],l_min[1],l_max[2], // Top Left Of The Quad (Bottom)
l_min[0],l_min[1],l_min[2], // Bottom Left Of The Quad (Bottom)
l_max[0],l_max[1],l_max[2], // Top Right Of The Quad (Front)
l_max[0],l_min[1],l_max[2], // Bottom Right Of The Quad (Front)
l_min[0],l_max[1],l_max[2], // Top Left Of The Quad (Front)
l_min[0],l_min[1],l_max[2], // Bottom Left Of The Quad (Front)
l_min[0],l_max[1],l_min[2], // Top Right Of The Quad (Back)
l_min[0],l_min[1],l_min[2], // Bottom Right Of The Quad (Back)
l_max[0],l_max[1],l_min[2], // Top Left Of The Quad (Back)
l_max[0],l_min[1],l_min[2] // Bottom Left Of The Quad (Back)
};
const GLfloat *l_normal = pBSP->Plane()->m_Normal;
for( int l_i = 0 ; l_i < 12 && l_ptsIndex < 4 ; l_i++ )
{
GLfloat l_a[3], l_b[3];
memcpy(l_a, l_aabbPts[l_i][0], 3*sizeof(GLfloat));
memcpy(l_b, l_aabbPts[l_i][1], 3*sizeof(GLfloat));
GLfloat l_ab[3];
GLfloat l_t;
// Vector Describing Line On AABB
l_ab[0] = l_b[0] - l_a[0];
l_ab[1] = l_b[1] - l_a[1];
l_ab[2] = l_b[2] - l_a[2];
// Distance along line that plane intersects
l_t = pBSP->Plane()->d() - l_normal[0]*l_a[0] -
l_normal[1]*l_a[1] -
l_normal[2]*l_a[2];
l_t /= l_normal[0]*l_ab[0] +
l_normal[1]*l_ab[1] +
l_normal[2]*l_ab[2];
if( l_t < 0.0f || l_t > 1.0f ) continue;
// Plug Distance Back Into Line Equation
l_pts[l_ptsIndex][0] = l_a[0] + l_t*l_ab[0];
l_pts[l_ptsIndex][1] = l_a[1] + l_t*l_ab[1];
l_pts[l_ptsIndex][2] = l_a[2] + l_t*l_ab[2];
++l_ptsIndex;
}
glBegin(GL_QUADS);
glVertex3fv(l_pts[0]);
glVertex3fv(l_pts[1]);
glVertex3fv(l_pts[2]);
glVertex3fv(l_pts[3]);
glEnd();
How can i get the quad vertex into the correct order?
Additionally, it seems this will break if the plane exactly intersects end-points, is there anything i should be doing to account for this?