rapidjson::Value SerializeCamera()

in GLTFSDK/Source/Serialize.cpp [629:683]


    rapidjson::Value SerializeCamera(const Camera& camera, const Document& gltfDocument, rapidjson::Document& document, const ExtensionSerializer& extensionSerializer)
    {
        rapidjson::Document::AllocatorType& a = document.GetAllocator();
        rapidjson::Value cameraValue(rapidjson::kObjectType);
        rapidjson::Value projectionValue(rapidjson::kObjectType);

        const ProjectionType projectionType = camera.projection->GetProjectionType();

        if (projectionType == PROJECTION_PERSPECTIVE)
        {
            const auto& perspective = camera.GetPerspective();

            projectionValue.AddMember("znear", RapidJsonUtils::ToFloatValue(perspective.znear), a);
            projectionValue.AddMember("yfov", RapidJsonUtils::ToFloatValue(perspective.yfov), a);

            if (perspective.zfar)
            {
                projectionValue.AddMember("zfar", RapidJsonUtils::ToFloatValue(perspective.zfar.Get()), a);
            }

            if (perspective.aspectRatio)
            {
                projectionValue.AddMember("aspectRatio", RapidJsonUtils::ToFloatValue(perspective.aspectRatio.Get()), a);
            }

            SerializeProperty(gltfDocument, perspective, projectionValue, a, extensionSerializer);

            cameraValue.AddMember("perspective", projectionValue, a);
            cameraValue.AddMember("type", RapidJsonUtils::ToStringValue("perspective", a), a);
        }
        else if (projectionType == PROJECTION_ORTHOGRAPHIC)
        {
            const auto& orthographic = camera.GetOrthographic();

            projectionValue.AddMember("xmag", RapidJsonUtils::ToFloatValue(orthographic.xmag), a);
            projectionValue.AddMember("ymag", RapidJsonUtils::ToFloatValue(orthographic.ymag), a);
            projectionValue.AddMember("znear", RapidJsonUtils::ToFloatValue(orthographic.znear), a);
            projectionValue.AddMember("zfar", RapidJsonUtils::ToFloatValue(orthographic.zfar), a);

            SerializeProperty(gltfDocument, orthographic, projectionValue, a, extensionSerializer);

            cameraValue.AddMember("orthographic", projectionValue, a);
            cameraValue.AddMember("type", RapidJsonUtils::ToStringValue("orthographic", a), a);
        }
        else
        {
            throw DocumentException("Camera " + camera.id + " doesn't have a valid projection type");
        }

        SerializeProperty(gltfDocument, camera, cameraValue, a, extensionSerializer);

        RapidJsonUtils::AddOptionalMember("name", cameraValue, camera.name, a);

        return cameraValue;
    }