Ejemplo n.º 1
0
bool RayTesselatedSegmentPrimitive::Intersect(const Ray& ray, Intersection* intersection) {
    Vec3f v0, v1;
    _ResolveVertexAttrib(v0,v1,segments->pos);
    float t,u, rayEpsilon;
    bool hit = IntersectHairSegment(v0,v1,segments->radius,ray,&t,&u,&rayEpsilon);

    if(hit) {
        // intesection
        intersection->rayEpsilon = rayEpsilon;
        intersection->t = t;
        intersection->dp.P = ray.Eval(t);
        intersection->dp.uv = Vec2f(u,0);
        if(segments->tangent.empty()) {
            intersection->dp.Ng = segments->segmentTangent[idx];
            intersection->dp.N = segments->segmentTangent[idx];
        } else {
            intersection->dp.Ng = ElementOperations::SegmentTangent(v0,v1);
            intersection->dp.N = _InterpolateVertexAttrib(u,segments->tangent).GetNormalized();
        }
        intersection->dp.GenerateTuTv();
        if(segments->uv.empty()) {
            intersection->dp.st = intersection->dp.uv;
        } else {
            intersection->dp.st = _InterpolateVertexAttrib(u,segments->uv);
        }
        intersection->m = material;
    }

    return hit;
}
Ejemplo n.º 2
0
bool RayTesselatedTrianglePrimitive::Intersect(const Ray& ray, Intersection* intersection) {
    Vec3f v0, v1, v2;
    _ResolveVertexAttrib(v0,v1,v2,triangles->pos);
    float t, b1, b2, rayEpsilon;
    bool hit = IntersectTriangle(v0,v1,v2,ray, &t, &b1, &b2, &rayEpsilon);

    if(hit) {
        // geometry
        intersection->t = t;
        intersection->rayEpsilon = rayEpsilon;
        intersection->dp.P = ray.Eval(t);
        intersection->dp.uv = Vec2f(b1,b2);
        if(triangles->normal.empty()) {
            intersection->dp.Ng = triangles->faceNormal[idx];
            intersection->dp.N = triangles->faceNormal[idx];
        } else {
            intersection->dp.Ng = ElementOperations::TriangleNormal(v0,v1,v2);
            intersection->dp.N = _InterpolateVertexAttrib(b1,b2,triangles->normal).GetNormalized();
        }
        intersection->dp.GenerateTuTv();

        // shading
        if(triangles->uv.empty()) {
            intersection->dp.st = intersection->dp.uv;
        } else {
            intersection->dp.st = _InterpolateVertexAttrib(b1,b2,triangles->uv);
        }

        // material
        intersection->m = material;
    }

    return hit;
}
Ejemplo n.º 3
0
bool RayTesselatedSpherePrimitive::Intersect(const Ray& ray, Intersection* intersection) {
    Vec3f p;
    float r;
    float rayEpsilon;
    Matrix4d m;
    Matrix4d mi;
    _ResolveSphereAttrib(p,r,m,mi);

    // xform the ray
    Ray xformRay = ray;
    xformRay.Transform(mi);

    float t;
    bool hit = IntersectSphere(p, r, xformRay, &t, &rayEpsilon);

    if(hit) {
        intersection->rayEpsilon = rayEpsilon;
        intersection->t = t;
        intersection->dp.P = xformRay.Eval(t);
        intersection->dp.Ng = (intersection->dp.P - p).GetNormalized();
        intersection->dp.N = intersection->dp.Ng;
        intersection->dp.uv[0] = (atan2f(intersection->dp.N[0], intersection->dp.N[1]) + (float)PI) / (float)(2*PI);
        intersection->dp.uv[1] = acosf(intersection->dp.N[2]) / (float)PI;
        intersection->dp.GenerateTuTv();
        intersection->dp.st = intersection->dp.uv;
        intersection->m = material;
    }

    // xform back
    if(hit) {
        intersection->Transform(m);
    }

    return hit;
}