Skip to content Skip to sidebar Skip to footer

How To Identify Click Inside The 3d Object Or Outside 3d Object Using Near And Far Positions

I am working in 3D object rendering using OpenGLES 2.0 with Android,Java code.How to identify click inside the 3D object or outside 3D object using following code near and far posi

Solution 1:

How to identify click inside the 3D object or outside 3D objec?

You have to verify if you hit any of the primitives of the object.

The point at the near plane and the point at the far plane define a ray through the world:

float[] nearPos = unProject(viewMatrix, projMatrix, screenX, screenY, 0);
float[] farPos  = unProject(viewMatrix, projMatrix, screenX, screenY, 1);

pseudo code:

R0 = nearPos
D  = normalize(farPos - nearPos)

Find the intersection point of a ray with a primitive

To find the surface which is hit by the ray, the distance of the intersection point of each surface (primitive) with the ray and the start point of the ray has to be calculated. The surface which has the lowest distance (in the ray direction), is hit.

To find the distance of the intersection point of a ray with a triangle primitive, the following steps has to be done:

  1. Find the intersection point of the ray and the plane which is defined by the 3 points of the triangle primitive.
  2. Calculate the distance between the interection point and the start point of the ray.
  3. Test if the intersection point is in the direction of the ray (not in the opposite direction)
  4. Test if the intersection point is in or on the triangle contur.

Find the intersection point and the intersection distance:

enter image description here

A plane is defined by a norm vector (NV) and a point on the plane (P0). If a triangle is given by the 3 points PA, PB and PC, the plane can be calculated as follows:

P0 = PA
NV = normalize( cross( PB-PA, PC-PA ) )

The intersection of a ray with a plane is calculated by substituting the equation of the ray P_isect = dist * D + R0 into the equation of the plane dot( P_isect - P0, NV ) == 0. It follows:

dist_isect = dot( P0 - R0, NV ) / dot( D, NV ) 
P_isect    = R0 + D * dist_isect

Test if the intersection point is in the direction of the ray:

The intersection point is in the direction of the ray, if `dist_isect is greater or equal 0.0.

Test if the intersection point is in or on the triangle contur

To find out, if a point is inside a triangle, has to be tested, if the line from a corner point to the intersection point is between the to legs which are connect to the corner point:

boolPointInOrOn( P1, P2, A, B )
{
    CP1 = cross( B - A, P1 - A )
    CP2 = cross( B - A, P2 - A )
    return dot( CP1, CP2 ) >= 0
}

boolPointInOrOnTriangle( P, A, B, C )
{
    return PointInOrOn( P, A, B, C ) &&
           PointInOrOn( P, B, C, A ) &&
           PointInOrOn( P, C, A, B )
} 

See also: Is it possble get which surface of cube will be click in OpenGL?

Post a Comment for "How To Identify Click Inside The 3d Object Or Outside 3d Object Using Near And Far Positions"