Ellipsoid Rendering Distortion and Origin Offset

I’m new to Cesium-Native and currently working on an OpenGL + Cesium-Native project to render 3DTiles and Ellipsoid. While the 3DTiles render correctly, the Ellipsoid’s meshes are distorted and the origin is not centered on the sphere. I’m rendering a unit sphere, not in the WGS84 Ellipsoid, so I don’t think I should encounter numerical issues. I’ve borrowed a lot of code from Cesium-Unity, but I still can’t figure out where the bug is. Below are the relevant code snippets I think might be related. I’d really appreciate it if someone with experience could take a look. Thank you!

void *PrepareRendererResources::prepareInMainThread(Cesium3DTilesSelection::Tile &tile, void *pLoadThreadResult_) {
    // SPDLOG_INFO("---------prepare in main---------");
    const Cesium3DTilesSelection::TileContent &content = tile.getContent();
    const Cesium3DTilesSelection::TileRenderContent *pRenderContent = content.getRenderContent();
    if (!pRenderContent) {
        return nullptr;
    }

    std::unique_ptr<LoadThreadResult> pLoadThreadResult(static_cast<LoadThreadResult *>(pLoadThreadResult_));
    std::vector<std::shared_ptr<Mesh>> meshes = pLoadThreadResult->meshes;
    const std::vector<CesiumPrimitiveInfo> &primitiveInfos = pLoadThreadResult->primitiveInfos;

    const CesiumGltf::Model &model = pRenderContent->getModel();
    glm::dmat4 tileTransform = tile.getTransform();
    tileTransform = CesiumGltfContent::GltfUtilities::applyRtcCenter(model, tileTransform);
    CesiumGltfContent::GltfUtilities::applyGltfUpAxisTransform(model, tileTransform); 

    int32_t meshSize = meshes.size();
    if (!meshSize) {
        return nullptr;
    }

    std::string name = "glTF";
    auto urlIt = model.extras.find("Cesium3DTiles_TileUrl");
    if (urlIt != model.extras.end()) {
        name = urlIt->second.getStringOrDefault("glTF");
    }

    size_t meshIndex = 0;

    GameComponent* tilesetComponentPtr = pTileset;
    CesiumGeoreference* georeferencePtr = pTileset->getGeoreference();
    model.forEachPrimitiveInScene(
        -1,
        [tilesetComponentPtr, georeferencePtr, &meshes, &primitiveInfos, &meshIndex, &tile, &tileTransform](const CesiumGltf::Model &gltf,
                                                      const CesiumGltf::Node &node,
                                                      const CesiumGltf::Mesh &mesh,
                                                      const CesiumGltf::MeshPrimitive &primitive,
                                                      const glm::dmat4 &transform) {
        auto positionAccessorIt = primitive.attributes.find("POSITION");
        if (positionAccessorIt == primitive.attributes.end()) {
            return;
        }

        int32_t positionAccessorID = positionAccessorIt->second;
        CesiumGltf::AccessorView<glm::vec3> positionView(gltf, positionAccessorID);
        if (positionView.status() != CesiumGltf::AccessorViewStatus::Valid) {
            return;
        }

        const CesiumPrimitiveInfo &primitiveInfo = primitiveInfos[meshIndex];
        glm::dmat4 modelToEcef = tileTransform * transform;

        auto meshInstancePtr = meshes[meshIndex];
        meshInstancePtr->setParent(tilesetComponentPtr);

        CesiumGlobeAnchor* anchor= new CesiumGlobeAnchor(georeferencePtr);
        meshInstancePtr->addGlobeAnchor(anchor);
        anchor->setDetectTransformChanges(false);
        anchor->setAdjustOrientationForGlobeWhenMoving(false);
        anchor->updateLocalToGlobeFixedMatrixAndEcef(modelToEcef);
        anchor->setParent(meshInstancePtr.get());

        const CesiumGltf::Material *pMaterial = CesiumGltf::Model::getSafe(&gltf.materials, primitive.material);
        if (pMaterial) {
            setGltfMaterialParameterValues(gltf, primitiveInfo, pMaterial, meshes[meshIndex++]);
        }
    });

    return new CesiumGltfObject{std::move(meshes), std::move(pLoadThreadResult->primitiveInfos)};
}



CesiumGeospatial::LocalHorizontalCoordinateSystem CesiumGeoreference::createCoordinateSystem() {
    double scaleToMeters = 1.0 / scale;
    if (originPlacement == CesiumGeoreferenceOriginPlacement::TrueOrigin) {
        glm::dmat4 localToEcef(
                               glm::dvec4(scaleToMeters, 0.0, 0.0, 0.0),
                               glm::dvec4(0.0, scaleToMeters, 0.0, 0.0),
                               glm::dvec4(0.0, 0.0, scaleToMeters, 0.0),
                               glm::dvec4(0.0, 0.0, 0.0, 1.0));
        return CesiumGeospatial::LocalHorizontalCoordinateSystem(localToEcef);
    }
    if (originAuthority == CesiumGeoreferenceOriginAuthority::EarthCenteredEarthFixed) {
        return CesiumGeospatial::LocalHorizontalCoordinateSystem(glm::dvec3(ecefX, ecefY, ecefZ),
                                                                 CesiumGeospatial::LocalDirection::East,
                                                                 CesiumGeospatial::LocalDirection::Up,
                                                                 CesiumGeospatial::LocalDirection::South,
                                                                 scaleToMeters,
                                                                 ellipsoid->getNativeEllipsoid());
    }
    return CesiumGeospatial::LocalHorizontalCoordinateSystem(glm::dvec3(longitude, latitude, height),
                                                             CesiumGeospatial::LocalDirection::East,
                                                             CesiumGeospatial::LocalDirection::Up,
                                                             CesiumGeospatial::LocalDirection::South,
                                                             scaleToMeters,
                                                             ellipsoid->getNativeEllipsoid());
}


void Mesh::draw(Shader& shader, glm::dmat4& modelMatrix) {
   ...
    glm::dmat4 mtx = transform * modelMatrix; // transform equals modelToEcef of  `anchor-
    shader.setMat4("model", glm::mat4(mtx)); >updateLocalToGlobeFixedMatrixAndEcef(modelToEcef);`
   ...
}

Hi @DavidXu2025, welcome to the community!

Based on only a very quick look, the first thing that jumps out at me is that this line:

CesiumGltfContent::GltfUtilities::applyGltfUpAxisTransform(model, tileTransform);

Should be:

tileTransform = GltfUtilities::applyGltfUpAxisTransform(model, tileTransform);

As it is in Cesium for Unity. Without that assignment, the line does nothing, and the glTF up axis is not being accounted for.