示例#1
0
// =====================================================================================
//  GetAlternateOrigin
// =====================================================================================
void GetAlternateOrigin (const vec3_t pos, const vec3_t normal, const patch_t *patch, vec3_t &origin)
{
    const dplane_t *faceplane;
    const vec_t *faceplaneoffset;
    const vec_t *facenormal;
    dplane_t clipplane;
    Winding w;

    faceplane = getPlaneFromFaceNumber (patch->faceNumber);
    faceplaneoffset = g_face_offset[patch->faceNumber];
    facenormal = faceplane->normal;
    VectorCopy (normal, clipplane.normal);
    clipplane.dist = DotProduct (pos, clipplane.normal);

    w = *patch->winding;
    if (w.WindingOnPlaneSide (clipplane.normal, clipplane.dist) != SIDE_CROSS)
    {
        VectorCopy (patch->origin, origin);
    }
    else
    {
        w.Clip (clipplane, false);
        if (w.m_NumPoints == 0)
        {
            VectorCopy (patch->origin, origin);
        }
        else
        {
            vec3_t center;
            bool found;
            vec3_t bestpoint;
            vec_t bestdist = -1.0;
            vec3_t point;
            vec_t dist;
            vec3_t v;

            w.getCenter (center);
            found = false;

            VectorMA (center, PATCH_HUNT_OFFSET, facenormal, point);
            if (HuntForWorld (point, faceplaneoffset, faceplane, 2, 1.0, PATCH_HUNT_OFFSET))
            {
                VectorSubtract (point, center, v);
                dist = VectorLength (v);
                if (!found || dist < bestdist)
                {
                    found = true;
                    VectorCopy (point, bestpoint);
                    bestdist = dist;
                }
            }
            if (!found)
            {
                for (int i = 0; i < w.m_NumPoints; i++)
                {
                    const vec_t *p1;
                    const vec_t *p2;
                    p1 = w.m_Points[i];
                    p2 = w.m_Points[(i + 1) % w.m_NumPoints];
                    VectorAdd (p1, p2, point);
                    VectorAdd (point, center, point);
                    VectorScale (point, 1.0/3.0, point);
                    VectorMA (point, PATCH_HUNT_OFFSET, facenormal, point);
                    if (HuntForWorld (point, faceplaneoffset, faceplane, 1, 0.0, PATCH_HUNT_OFFSET))
                    {
                        VectorSubtract (point, center, v);
                        dist = VectorLength (v);
                        if (!found || dist < bestdist)
                        {
                            found = true;
                            VectorCopy (point, bestpoint);
                            bestdist = dist;
                        }
                    }
                }
            }

            if (found)
            {
                VectorCopy (bestpoint, origin);
            }
            else
            {
                VectorCopy (patch->origin, origin);
            }
        }
    }
}