Rendering NavData

You can render all of the game NavData from a database in the 3D view of your editor or game. This helps in checking whether your NavData is loading correctly, whether it matches the terrain correctly, and so on.

Accessing NavData in a Database

  1. Create a class that derives from the IVisualGeometry class.

    class NavDataVisualGeometryBuilder : public Kaim::IVisualGeometry
    {
    
    public:
            virtual void DoBegin(const Kaim::VisualGeometryPrimitiveCounts& /*counts*/) { m_triangles.Clear();            }
            virtual void DoPushTriangle(const Kaim::VisualTriangle& triangle)            { m_triangles.PushBack(triangle); }
            virtual void DoPushLine(const Kaim::VisualLine& /*visualLine*/) {}
            virtual void DoPushText(const Kaim::VisualText& /*visualText*/) {}
            virtual void DoEnd() {}
    
            KyUInt32 GetTriangleCount() const { return m_triangles.GetCount(); }
    
    private:
            Kaim::KyArray<Kaim::VisualTriangle> m_triangles;
    };
    

    Each time you request for the NavData triangles in the database, they are passed to an instance of your NavDataVisualGeometryBuilder class.

  2. Create an instance of your NavDataVisualGeometryBuilder) class, and pass it in a call to Database::SetVisualGeometry().

    // Create a world and a database.
    Kaim::Ptr<Kaim::World> world = *KY_NEW Kaim::World;
    Kaim::Database* database = world->GetDatabase(0);
    Kaim::Ptr<Kaim::NavData> navData = *KY_NEW Kaim::NavData(database);
    
    Kaim::Ptr<NavDataVisualGeometryBuilder> geometry = *KY_NEW NavDataVisualGeometryBuilder;
    database->SetVisualGeometry(geometry);
  3. Initialize the database, and add your NavData to the database. You can use the immediate method or asynchronous method to add NavData to the database. See Using NavData at Runtime.

  4. Call Database::BuildVisualGeometry() to keep the NavData triangles in the IVisualGeometry instance up-to-date. You do not have to track which NavData is added or removed from the database. Instead, you can call BuildVisualGeometry() each time Database::HasChangedLastFrame() returns true as shown in the following example.

    while (gameLoopRunning)
    {
            world->Update();
            if (database->HasChangedLastFrame())
                database->BuildVisualGeometry(); // Call BuildVisualGeometry() only if database has changed.
    }
    

    You can use the triangles in your game or editor based on your implementation of IVisualGeometry.

Example

For the code sample, see Tutorial_VisualGeometry.cpp. This tutorial shows how to access the NavData triangles loaded in a database.