McBob22

11-07-2014, 02:40 AM

Im building my frustum using the Mark Morley method for OpenGL Culling it work fine.

What Im having issue to figure out is using the near plane calculated in this method how can I find the intersection point of a line. Basically what Im trying to do is to clip the line at the near plane if it intersect. Using the code below the result is obviously wrong but Im not sure what Im missing:

bool linePlaneIntersection( vec4 plane, vec3 p1, vec3 p2, vec3 *intersect )

{

const float u = ( plane.x * p1.x ) +

( plane.y * p1.y ) +

( plane.z * p1.z ) +

plane.w / ( plane.x * ( p1.x - p2.x ) ) +

( plane.y * ( p1.y - p2.y ) ) +

( plane.z * ( p1.z - p2.y ) );

if( u <= 0.0 || u > 1.0 )

{ return false; }

else

{

intersect->x = u * p1.x + ( 1.0f - u ) * p2.x;

intersect->y = u * p1.y + ( 1.0f - u ) * p2.y;

intersect->z = u * p1.z + ( 1.0f - u ) * p2.z;

return true;

}

}

...

...

...

vec4 plane = ( vec4 *)&frustum[5][0];

vec3 p1 = line_array[ 0 ],

p2 = line_array[ 1 ],

intersect;

if( linePlaneIntersection( plane, p1, p2, &intersect ) )

{

bool is_in = ClipPoint(frustum,p1);

if( is_in )

{ p1 = intersect; }

else

{ p2 = intersect; }

}

What Im having issue to figure out is using the near plane calculated in this method how can I find the intersection point of a line. Basically what Im trying to do is to clip the line at the near plane if it intersect. Using the code below the result is obviously wrong but Im not sure what Im missing:

bool linePlaneIntersection( vec4 plane, vec3 p1, vec3 p2, vec3 *intersect )

{

const float u = ( plane.x * p1.x ) +

( plane.y * p1.y ) +

( plane.z * p1.z ) +

plane.w / ( plane.x * ( p1.x - p2.x ) ) +

( plane.y * ( p1.y - p2.y ) ) +

( plane.z * ( p1.z - p2.y ) );

if( u <= 0.0 || u > 1.0 )

{ return false; }

else

{

intersect->x = u * p1.x + ( 1.0f - u ) * p2.x;

intersect->y = u * p1.y + ( 1.0f - u ) * p2.y;

intersect->z = u * p1.z + ( 1.0f - u ) * p2.z;

return true;

}

}

...

...

...

vec4 plane = ( vec4 *)&frustum[5][0];

vec3 p1 = line_array[ 0 ],

p2 = line_array[ 1 ],

intersect;

if( linePlaneIntersection( plane, p1, p2, &intersect ) )

{

bool is_in = ClipPoint(frustum,p1);

if( is_in )

{ p1 = intersect; }

else

{ p2 = intersect; }

}