bool importLight()

in libraries/hvvr/samples_shared/model_import_fbx.cpp [397:447]


bool importLight(ImportState& state, FbxNode* pNode, FbxLight* pLight) {
    hvvr::transform cframe = AsTransform(pNode->EvaluateGlobalTransform());

    FbxDouble3 Color = pLight->Color.Get();
    hvvr::vector3 color = hvvr::vector3(float(Color[0]), float(Color[1]), float(Color[2]));
    color = hvvr::sRgbToLinear(color);

    float intensity = float(pLight->Intensity.Get() * 0.01);

    // TODO: is this necessary? It was needed for Oculus Home...
    float falloffStart = float(pLight->FarAttenuationStart.Get()) * 0.01f;
    float falloffEnd = float(pLight->FarAttenuationEnd.Get()) * 0.01f;

    hvvr::LightUnion light;
    if (pLight->LightType.Get() == FbxLight::eDirectional) {
        light.type = hvvr::LightType::directional;
        light.directional.Direction = hvvr::normalize(cframe * hvvr::vector3(0, 0, -1));
        light.directional.Power = color * intensity;
    } else if (pLight->LightType.Get() == FbxLight::ePoint) {
        if (bool(pLight->EnableFarAttenuation) == false) {
            printf("\nIMPORT WARNING: Ignoring point light because FarAttenuation not enabled.\n");
            return true; // we won't consider this a failure case
        }

        light.type = hvvr::LightType::point;
        light.point.Color = color;
        light.point.Position = cframe.translation;
        light.point.FalloffEnd = falloffEnd;
        light.point.FalloffScale = 1.0f / (falloffEnd - falloffStart);
    } else if (pLight->LightType.Get() == FbxLight::eSpot) {
        if (bool(pLight->EnableFarAttenuation) == false) {
            printf("\nIMPORT WARNING: Ignoring spot light because FarAttenuation not enabled.\n");
            return true; // we won't consider this a failure case
        }

        float cosInnerAngle = cosf(float(pLight->InnerAngle.Get()) * hvvr::RadiansPerDegree);
        float cosOuterAngle = cosf(float(pLight->OuterAngle.Get()) * hvvr::RadiansPerDegree);

        light.type = hvvr::LightType::spot;
        light.spot.Direction = hvvr::normalize(cframe * hvvr::vector3(0, 0, -1));
        light.spot.Color = color;
        light.spot.Position = cframe.translation;
        light.spot.FalloffEnd = falloffEnd;
        light.spot.FalloffScale = 1.0f / (falloffEnd - falloffStart);
        light.spot.CosOuterAngle = cosOuterAngle;
        light.spot.CosAngleScale = 1.0f / (cosInnerAngle - cosOuterAngle);
    }

    state.model.lights.emplace_back(std::move(light));
    return true;
}