c++ camera node?

How do you do the following in cxx?

base.cam.node().setLens(lens)

I’ve tried casting camera.node() to a pointer to camera object but it crashes when I access the object.

My guess:

wfx->get_camera(0)->set_lens(lens);

This should be the same:

DCAST(Camera, wfx->get_camera_group().node()->get_child(0))->set_lens(lens);

As for the crash, my guess is that you’ve done something like this:

((Camera*) wfx->get_camera_group().node())->set_lens(lens);

That doesn’t work because the camera group NodePath doesn’t point to a PandaNode of type Camera. The camera group is just a PandaNode that holds every individual Camera as child.

Yeah, that’s what I was trying. Thanks, it works.

Sounds like a good time to advocate for Panda’s DCAST macros. If you had tried to use:

DCAST(Camera, wfx->get_camera_group().node())->set_lens(lens);

it still would have been incorrect, but you would have gotten an assertion failure telling you that you were trying to cast a PandaNode to a Camera. (Assuming that the DCAST macros aren’t disabled for the standard Panda distribution, which is possible, since they add a little overhead and not much value to a Python developer.)

David

Thanks, that’s useful. What kind of overhead are we talking about, though? dll size or performance? Just curious. And by performance I mean whether it has an impact on performance just because the support is there, even when you are not using it, is this the case?

There’s a small runtime cost to perform the actual inheritance test at invocation time.

In normal C++ development mode, with DO_DCAST defined, the DCAST() macro performs this test at each invocation. In production mode, with DO_DCAST not defined, the DCAST() macro is redefined to a simple no-cost static cast.

So, depending on the build rules used for the version of Panda3D distributed on this website, using DCAST might incur a small runtime cost, but will provide useful information if you attempt to cast a pointer to an inappropriate type. Or, it might do nothing at all.

The overhead for a single invocation of DCAST is vanishingly small, but it does add up throughout Panda to merely small. So, for a Panda build that is intended primarily for Python developers, it does make sense to disable the DCAST macros. But if Python developers and C++ developers will be sharing the same Panda build, it makes more sense to leave it in.

I’m not sure what the current setting is.

David