Render order exampleΒΆ

/**
* This example demonstrates how to use AnchorPoint to track
* Ramses scene content in 2D.
*/

struct SceneAndNodes
{
    ramses::Scene* scene;
    ramses::RenderGroup* renderGroup;
    ramses::MeshNode* triangle1;
    ramses::MeshNode* triangle2;
};

/**
* Helper method which creates a simple ramses scene. For more ramses
* examples, check the ramses docs at https://bmwcarit.github.io/ramses
*/
SceneAndNodes CreateSceneWithTriangles(ramses::RamsesClient& client);

int main()
{
    /**
    * Use simple class to create ramses framework objects which are not essential for this example.
    * For more info on those, please refer to the ramses docs: https://bmwcarit.github.io/ramses
    */
    SimpleRenderer renderer;

    /**
     * Create a simple Ramses scene with two alpha blended triangles.
     * We will control the render order of the triangles using the render group they are in.
     */
    auto [scene, renderGroup, triMesh1, triMesh2] = CreateSceneWithTriangles(*renderer.getClient());

    /**
    * Render group binding requires feature level 03 or higher
    */
    rlogic::LogicEngine logicEngine{ rlogic::EFeatureLevel_03 };

    /**
    * In order to create a render group binding we need to first specify what elements we want to expose for render order control,
    * in our case it is the two triangles.
    */
    rlogic::RamsesRenderGroupBindingElements renderGroupElements;
    renderGroupElements.addElement(*triMesh1, "tri1");
    renderGroupElements.addElement(*triMesh2, "tri2");
    rlogic::RamsesRenderGroupBinding* renderGroupBinding = logicEngine.createRamsesRenderGroupBinding(*renderGroup, renderGroupElements);

    /**
    * Show the scene on the renderer
    */
    renderer.showScene(scene->getSceneId());

    /**
     * Simulate an application loop.
     */
    uint32_t frameCounter = 0u;
    while (!renderer.isWindowClosed())
    {
        /**
        * Update the LogicEngine. This will apply changes to Ramses scene from any running animation.
        */
        logicEngine.update();

        /**
        * In this example we change the render order directly using input property API, we swap the render order of the two triangles
        * every few frames to show the difference. In real world application this would be managed by a more complex logic
        * (e.g. sorting based on distance to camera) in another logic node (e.g. LuaScript) that would link to these property inputs.
        */
        if (++frameCounter % 100 < 50)
        {
            renderGroupBinding->getInputs()->getChild("renderOrders")->getChild("tri1")->set(-1);
            renderGroupBinding->getInputs()->getChild("renderOrders")->getChild("tri2")->set(1);
        }
        else
        {
            renderGroupBinding->getInputs()->getChild("renderOrders")->getChild("tri1")->set(1);
            renderGroupBinding->getInputs()->getChild("renderOrders")->getChild("tri2")->set(-1);
        }

        /**
        * In order to commit the changes to Ramses scene caused by animations logic we need to "flush" them.
        */
        scene->flush();

        /**
        * Process window events, check if window was closed
        */
        renderer.processEvents();

        /**
        * Throttle the simulation loop by sleeping for a bit.
        */
        std::this_thread::sleep_for(std::chrono::milliseconds(20));
    }