Adding an additional camera


#1

Hi all,
the following simple python code toggles every 5 seconds between 2 cameras:

# imports
from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor
from direct.interval.FunctionInterval import Func

# definitions
cameras = []
activeCam = 0;

# toggleCam task
def toggleCam(task):
    global cameras,activeCam
    cameras[activeCam].node().getDisplayRegion(0).setActive(0)
    activeCam = not activeCam
    cameras[activeCam].node().getDisplayRegion(0).setActive(1)
    return task.again


# Set framework up
framework = ShowBase()
pandaActor = Actor("panda", {"walk": "panda-walk"})
pandaActor.reparentTo(render)
pandaActor.loop("walk")

# set cameras up (first camera = default)
cameras = [framework.cam, framework.makeCamera(framework.win)]

# initially first camera is on and second one is off
cameras[1].node().getDisplayRegion(0).setActive(0)
activeCam = 0

# locate cameras
cameras[0].setPos(0, -30, 6)
cameras[1].setPos(30, -30, 20)
cameras[1].lookAt(0, 0, 6)

# set toggleCamera task = toggle every 5 secs
framework.taskMgr.doMethodLater(5, toggleCam, "toggle camera")

# Do the main loop
framework.run()

I would like to port this in C++, and after browsing this forum, I wrote this code

// imports
#include <pandaFramework.h>
#include <pandaSystem.h>
#include <auto_bind.h>
#include <camera.h>
#include <displayRegion.h>

// definitions
PandaFramework framework;
NodePath cameraNP[2];
PT(DisplayRegion) displayRegion[2];
int activeCam;

// toggleCam task
AsyncTask::DoneStatus toggleCam(GenericAsyncTask* task,
		void * data)
{
	displayRegion[activeCam]->set_active(false);
	activeCam = (activeCam == 0 ? 1 : 0);
	displayRegion[activeCam]->set_active(true);
	return AsyncTask::DS_again;
}

int main(int argc, char* argv[])
{
	// Set framework up
	framework.open_framework(argc, argv);
	framework.set_window_title("Hello World!");
	WindowFramework * window = framework.open_window();
	window->enable_keyboard(); // Enable keyboard detection
	NodePath panda;
	AnimControlCollection pandaAnims;
	panda = window->load_model(framework.get_models(), "panda");
	panda.reparent_to(window->get_render());
	panda.set_pos(5, 0, 0);
	window->load_model(panda, "panda-walk");
	auto_bind(panda.node(), pandaAnims);
	pandaAnims.loop("panda_soft", false);

	// DOESN'T WORK
	// set cameras up (first camera = default)
	Camera* camera;
	cameraNP[0] = window->get_camera(0);
	camera = dynamic_cast<Camera*>(cameraNP[0].get_node(0));
	displayRegion[0] = dynamic_cast<DisplayRegion*>(camera->get_display_region(
			0));
	cameraNP[1] = window->make_camera();
	camera = dynamic_cast<Camera*>(cameraNP[1].get_node(0));
	displayRegion[1] = window->get_graphics_output()->make_display_region();
	displayRegion[1]->set_camera(camera);

	// initially first camera is on and second one is off
	displayRegion[1]->set_active(false);
	activeCam = 0;

	// locate cameras
	cameraNP[0].set_pos(0, -30, 6);
	cameraNP[1].set_pos(30, -30, 20);
	cameraNP[1].look_at(0, 0, 6);

	// set toggleCamera task = toggle every 5 secs
	PT(AsyncTask) task = new GenericAsyncTask("toggle camera task", &toggleCam, NULL);
	task->set_delay(5);
	framework.get_task_mgr().add(task);

	// Do the main loop
	framework.main_loop();
	// Close the framework
	framework.close_framework();
	return 0;
}

This code doesn’t work properly and so I would like some help to correctly do it.

Thanks


#2

instead of

camera = dynamic_cast<Camera*>(cameraNP[0].get_node(0));

I think you should use

PT(Camera) pCamera = DCAST(Camera, cameraNP[0].get_node(0));

#3

i tried but this didn’t solve the problem

thanks anyway


#4

I don’t see anything obviously wrong. In what way does it fail?

David


#5

here is a video of output

mediafire.com/?m3jr325kcm6p27f

(toggling is every 2 secs)