maybe the code itself will say more than 1000 words ill coment everything.
int index = model->VBO_BE[faceindex].INDEX_START; //first face vertex index in VBO
t3dpoint pop = model->VBO_V[index]; //point on checked plane
t3dpoint A = vectorAB(model->VBO_V[index],model->VBO_V[index+1]); //vector from first vertex to second vertex of the plane (face)
t3dpoint B = vectorAB(model->VBO_V[index],model->VBO_V[index - model->VBO_BE[faceindex].length - 1]); //since its a quad this is a vector from first vertex to last vertex
t3dpoint n = Normalize(A * B); //calculate plane normal and normalize it
t3dpoint Ap = ProjectPointOntoPlane(n,pop, rA); //project ray start onto plane
t3dpoint Bp = ProjectPointOntoPlane(n,pop, rB);//ray end point on plane
t3dpoint AB = vectorAB(Ap,Bp); //make a vector on plane
float dstA = float(n3dPoint_plane_distance(rA, model->MATH_FACE_NORMALS[faceindex], model->MATH_FACE_DISTANCES[faceindex])); //oh yes since plane equations are Ax+By+Cz+D=0 i made helper arrays with these values (A,B,C,D)
float dstAB = n3ddistance(rA, rB);
float perDST = dstA / dstAB; //get perccentage of the distances of ray start from plane / distance between start and end of ray
t3dpoint PointOnPlane = Ap + AB*perDST; //since Ap should be on plane it should return actual intersection point
res = PointOnPlane;
helper functions
long double n3dPoint_plane_distance(t3dpoint p, t3dpoint vnormalnover, float D)
{
long double d = -1.0f;
long double num = p.x * vnormalnover.x + p.y * vnormalnover.y+ p.z * vnormalnover.z + D;
num = abs( num );
long double denom = vnormalnover.x*vnormalnover.x + vnormalnover.y*vnormalnover.y+vnormalnover.z*vnormalnover.z;
denom = sqrtl(denom);
if (denom == 0.0f) return -1;
return num / denom;
}
t3dpoint __fastcall ProjectPointOntoPlane(t3dpoint normal, t3dpoint pointonplane, t3dpoint projected_point)
{
t3dpoint v = vectorAB(pointonplane,projected_point);
float dist = Dot(v,normal);
return projected_point - (normal*dist);
}