Adding an additional camera

Return to C++ coding using Panda3D

Adding an additional camera

Postby consultit » Thu Dec 01, 2011 4:23 am

Hi all,
the following simple python code toggles every 5 seconds between 2 cameras:
Code: Select all
# 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
Code: Select all
// 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
consultit
 
Posts: 35
Joined: Wed Nov 30, 2011 7:41 am

Postby Chrys » Thu Dec 01, 2011 7:53 am

instead of
Code: Select all
camera = dynamic_cast<Camera*>(cameraNP[0].get_node(0));


I think you should use

Code: Select all
PT(Camera) pCamera = DCAST(Camera, cameraNP[0].get_node(0));
Chrys
 
Posts: 212
Joined: Tue Nov 02, 2010 8:29 am
Location: Montreal - Canada

Postby consultit » Thu Dec 01, 2011 11:00 am

i tried but this didn't solve the problem

thanks anyway
consultit
 
Posts: 35
Joined: Wed Nov 30, 2011 7:41 am

Postby drwr » Thu Dec 01, 2011 2:48 pm

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

David
drwr
 
Posts: 11425
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby consultit » Fri Dec 02, 2011 1:02 pm

here is a video of output

http://www.mediafire.com/?m3jr325kcm6p27f


(toggling is every 2 secs)
consultit
 
Posts: 35
Joined: Wed Nov 30, 2011 7:41 am


Return to C++ coding using Panda3D

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests