ForEachVisibleEntityDo

This command calls the specified function for all entities in the world that are visible to the specified camera. The optional extra parameter will be passed to the callback function each time it is called.

Syntax

Parameters

Remarks

The callback function will only be called for the top-level entities who's recursive bounding boxes intersect the specified camera's frustum volume. Your callback function should be written so that it is called recursively with the top-level entity's children.

Example

#include "Leadwerks.h"

using namespace Leadwerks;

void Callback(Camera* camera, Entity* entity, Object* extra)
{
//We only want to execute this on models. If we don't check the class we will change the color of the light, too.
if (entity->GetClass() == Object::ModelClass)
{
Vec3* v = (Vec3*)extra;
entity->SetColor(v->r, v->g, v->b);
}
}

int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();

Context* context = Context::Create(window);

World* world = World::Create();

Camera* camera = Camera::Create();
camera->Move(0, 0, -6);

Light* light = DirectionalLight::Create();
light->SetRotation(35, 35, 0);

//Create some entities
Model* model = Model::Box();
model->SetColor(0.0, 0.0, 1.0);
model->SetPosition(-5, 0, 0);

model = Model::Box();
model->SetColor(0.0, 0.0, 1.0);
model->SetPosition(-2, 1, 0);

model = Model::Box();
model->SetColor(0.0, 0.0, 1.0);
model->SetPosition(5, 0, 0);

while (true)
{
if (window->Closed() || window->KeyDown(Key::Escape)) return false;

//We're going to pass a Vec3 to the callback in the extra parameter
Vec3* v = new Vec3(1, 0, 0);

//Execute the callback for all entities that intersect the bounding box. The intersected entities will turn red.
world->ForEachVisibleEntityDo(camera, Callback, v);

//Delete the Vec3 object
delete v;

Leadwerks::Time::Update();
world->Update();
world->Render();
context->Sync(false);
}
return 0;
}